<s> import simplejson <EOL> import re <EOL> from itertools import cycle <EOL> from django . core . urlresolvers import reverse <EOL> from molly . apps . library . models import LibrarySearchQuery <EOL> def validate_isxn ( s ) : <EOL> def encode ( s ) : <EOL> return '<STR_LIT>' . join ( str ( d ) if d < <NUM_LIT:10> else '<STR_LIT:X>' for d in s ) <EOL> def decode ( s ) : <EOL> return [ <NUM_LIT:10> if d == '<STR_LIT:X>' else int ( d ) for d in s ] <EOL> def isxn_checksum ( s , initial = None ) : <EOL> if not initial : <EOL> initial = len ( s ) <EOL> cs = <NUM_LIT:0> <EOL> for d in s : <EOL> cs , initial = ( cs + ( d * initial ) ) % <NUM_LIT:11> , initial - <NUM_LIT:1> <EOL> return cs <EOL> def ean_checksum ( s ) : <EOL> return sum ( d * m for d , m in zip ( s , cycle ( [ <NUM_LIT:1> , <NUM_LIT:3> ] ) ) ) % <NUM_LIT:10> <EOL> s = re . sub ( '<STR_LIT>' , '<STR_LIT:X>' , s . replace ( '<STR_LIT:->' , '<STR_LIT>' ) . strip ( ) . upper ( ) ) <EOL> if not re . match ( "<STR_LIT>" , s ) : <EOL> return None , None <EOL> s = decode ( s ) <EOL> if len ( s ) == <NUM_LIT> : <EOL> if ean_checksum ( s ) != <NUM_LIT:0> : <EOL> return None , None <EOL> if s [ <NUM_LIT:2> ] == <NUM_LIT:7> : <EOL> s = s [ <NUM_LIT:3> : <NUM_LIT:10> ] + [ ( <NUM_LIT:11> - isxn_checksum ( s [ <NUM_LIT:3> : <NUM_LIT:10> ] , initial = <NUM_LIT:8> ) ) % <NUM_LIT:11> ] <EOL> return encode ( s ) , '<STR_LIT>' <EOL> else : <EOL> return encode ( s ) , '<STR_LIT>' <EOL> else : <EOL> cs , n = <NUM_LIT:0> , len ( s ) <EOL> for d in s : <EOL> cs , n = ( cs + ( d * n ) ) % <NUM_LIT:11> , n - <NUM_LIT:1> <EOL> if cs != <NUM_LIT:0> : <EOL> return None , None <EOL> return encode ( s ) , ( '<STR_LIT>' if len ( s ) == <NUM_LIT:8> else '<STR_LIT>' ) <EOL> class ApplicationSearch ( object ) : <EOL> def __init__ ( self , application ) : <EOL> self . conf = application <EOL> def perform_search ( self , request , query , only_app ) : <EOL> number , number_type = validate_isxn ( query ) <EOL> if not number_type : <EOL> return [ ] <EOL> else : <EOL> query = { <EOL> number_type : number <EOL> } <EOL> query = LibrarySearchQuery ( ** query ) <EOL> results = [ ] <EOL> for item in self . conf . provider . library_search ( query ) : <EOL> results . append ( { <EOL> '<STR_LIT:title>' : item . title , <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' % item . publisher , <EOL> '<STR_LIT:url>' : reverse ( '<STR_LIT>' , args = [ item . control_number ] ) , <EOL> } ) <EOL> return results </s>
