<s> import string , posix , os <EOL> _cddbrc = '<STR_LIT>' <EOL> _DB_ID_NTRACKS = <NUM_LIT:5> <EOL> _dbid_map = '<STR_LIT>' <EOL> def _dbid ( v ) : <EOL> if v >= len ( _dbid_map ) : <EOL> return string . zfill ( v , <NUM_LIT:2> ) <EOL> else : <EOL> return _dbid_map [ v ] <EOL> def tochash ( toc ) : <EOL> if type ( toc ) == type ( '<STR_LIT>' ) : <EOL> tracklist = [ ] <EOL> for i in range ( <NUM_LIT:2> , len ( toc ) , <NUM_LIT:4> ) : <EOL> tracklist . append ( ( None , <EOL> ( int ( toc [ i : i + <NUM_LIT:2> ] ) , <EOL> int ( toc [ i + <NUM_LIT:2> : i + <NUM_LIT:4> ] ) ) ) ) <EOL> else : <EOL> tracklist = toc <EOL> ntracks = len ( tracklist ) <EOL> hash = _dbid ( ( ntracks >> <NUM_LIT:4> ) & <NUM_LIT> ) + _dbid ( ntracks & <NUM_LIT> ) <EOL> if ntracks <= _DB_ID_NTRACKS : <EOL> nidtracks = ntracks <EOL> else : <EOL> nidtracks = _DB_ID_NTRACKS - <NUM_LIT:1> <EOL> min = <NUM_LIT:0> <EOL> sec = <NUM_LIT:0> <EOL> for track in tracklist : <EOL> start , length = track <EOL> min = min + length [ <NUM_LIT:0> ] <EOL> sec = sec + length [ <NUM_LIT:1> ] <EOL> min = min + sec / <NUM_LIT> <EOL> sec = sec % <NUM_LIT> <EOL> hash = hash + _dbid ( min ) + _dbid ( sec ) <EOL> for i in range ( nidtracks ) : <EOL> start , length = tracklist [ i ] <EOL> hash = hash + _dbid ( length [ <NUM_LIT:0> ] ) + _dbid ( length [ <NUM_LIT:1> ] ) <EOL> return hash <EOL> class Cddb : <EOL> def __init__ ( self , tracklist ) : <EOL> if os . environ . has_key ( '<STR_LIT>' ) : <EOL> path = os . environ [ '<STR_LIT>' ] <EOL> cddb_path = path . split ( '<STR_LIT:U+002C>' ) <EOL> else : <EOL> home = os . environ [ '<STR_LIT>' ] <EOL> cddb_path = [ home + '<STR_LIT:/>' + _cddbrc ] <EOL> self . _get_id ( tracklist ) <EOL> for dir in cddb_path : <EOL> file = dir + '<STR_LIT:/>' + self . id + '<STR_LIT>' <EOL> try : <EOL> f = open ( file , '<STR_LIT:r>' ) <EOL> self . file = file <EOL> break <EOL> except IOError : <EOL> pass <EOL> ntracks = int ( self . id [ : <NUM_LIT:2> ] , <NUM_LIT:16> ) <EOL> self . artist = '<STR_LIT>' <EOL> self . title = '<STR_LIT>' <EOL> self . track = [ None ] + [ '<STR_LIT>' ] * ntracks <EOL> self . trackartist = [ None ] + [ '<STR_LIT>' ] * ntracks <EOL> self . notes = [ ] <EOL> if not hasattr ( self , '<STR_LIT:file>' ) : <EOL> return <EOL> import re <EOL> reg = re . compile ( r'<STR_LIT>' ) <EOL> while <NUM_LIT:1> : <EOL> line = f . readline ( ) <EOL> if not line : <EOL> break <EOL> match = reg . match ( line ) <EOL> if not match : <EOL> print '<STR_LIT>' + file <EOL> continue <EOL> name1 , name2 , value = match . group ( <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ) <EOL> if name1 == '<STR_LIT>' : <EOL> if name2 == '<STR_LIT>' : <EOL> self . artist = value <EOL> elif name2 == '<STR_LIT:title>' : <EOL> self . title = value <EOL> elif name2 == '<STR_LIT>' : <EOL> if not self . toc : <EOL> self . toc = value <EOL> if self . toc != value : <EOL> print '<STR_LIT>' <EOL> elif name2 == '<STR_LIT>' : <EOL> self . notes . append ( value ) <EOL> elif name1 [ : <NUM_LIT:5> ] == '<STR_LIT>' : <EOL> try : <EOL> trackno = int ( name1 [ <NUM_LIT:5> : ] ) <EOL> except ValueError : <EOL> print '<STR_LIT>' + file <EOL> continue <EOL> if trackno > ntracks : <EOL> print '<STR_LIT>' % ( trackno , file ) <EOL> continue <EOL> if name2 == '<STR_LIT:title>' : <EOL> self . track [ trackno ] = value <EOL> elif name2 == '<STR_LIT>' : <EOL> self . trackartist [ trackno ] = value <EOL> f . close ( ) <EOL> for i in range ( <NUM_LIT:2> , len ( self . track ) ) : <EOL> track = self . track [ i ] <EOL> if track and track [ <NUM_LIT:0> ] == '<STR_LIT:U+002C>' : <EOL> try : <EOL> off = self . track [ i - <NUM_LIT:1> ] . index ( '<STR_LIT:U+002C>' ) <EOL> except ValueError : <EOL> pass <EOL> else : <EOL> self . track [ i ] = self . track [ i - <NUM_LIT:1> ] [ : off ] + track <EOL> def _get_id ( self , tracklist ) : <EOL> if type ( tracklist ) == type ( '<STR_LIT>' ) : <EOL> if tracklist [ - <NUM_LIT:4> : ] == '<STR_LIT>' : <EOL> self . id = tracklist [ : - <NUM_LIT:4> ] <EOL> self . toc = '<STR_LIT>' <EOL> return <EOL> t = [ ] <EOL> for i in range ( <NUM_LIT:2> , len ( tracklist ) , <NUM_LIT:4> ) : <EOL> t . append ( ( None , ( int ( tracklist [ i : i + <NUM_LIT:2> ] ) , int ( tracklist [ i + <NUM_LIT:2> : i + <NUM_LIT:4> ] ) ) ) ) <EOL> tracklist = t <EOL> ntracks = len ( tracklist ) <EOL> self . id = _dbid ( ( ntracks >> <NUM_LIT:4> ) & <NUM_LIT> ) + _dbid ( ntracks & <NUM_LIT> ) <EOL> if ntracks <= _DB_ID_NTRACKS : <EOL> nidtracks = ntracks <EOL> else : <EOL> nidtracks = _DB_ID_NTRACKS - <NUM_LIT:1> <EOL> min = <NUM_LIT:0> <EOL> sec = <NUM_LIT:0> <EOL> for track in tracklist : <EOL> start , length = track <EOL> min = min + length [ <NUM_LIT:0> ] <EOL> sec = sec + length [ <NUM_LIT:1> ] <EOL> min = min + sec / <NUM_LIT> <EOL> sec = sec % <NUM_LIT> <EOL> self . id = self . id + _dbid ( min ) + _dbid ( sec ) <EOL> for i in range ( nidtracks ) : <EOL> start , length = tracklist [ i ] <EOL> self . id = self . id + _dbid ( length [ <NUM_LIT:0> ] ) + _dbid ( length [ <NUM_LIT:1> ] ) <EOL> self . toc = string . zfill ( ntracks , <NUM_LIT:2> ) <EOL> for track in tracklist : <EOL> start , length = track <EOL> self . toc = self . toc + string . zfill ( length [ <NUM_LIT:0> ] , <NUM_LIT:2> ) + string . zfill ( length [ <NUM_LIT:1> ] , <NUM_LIT:2> ) <EOL> def write ( self ) : <EOL> import posixpath <EOL> if os . environ . has_key ( '<STR_LIT>' ) : <EOL> dir = os . environ [ '<STR_LIT>' ] <EOL> else : <EOL> dir = os . environ [ '<STR_LIT>' ] + '<STR_LIT:/>' + _cddbrc <EOL> file = dir + '<STR_LIT:/>' + self . id + '<STR_LIT>' <EOL> if posixpath . exists ( file ) : <EOL> posix . rename ( file , file + '<STR_LIT>' ) <EOL> f = open ( file , '<STR_LIT:w>' ) <EOL> f . write ( '<STR_LIT>' + self . title + '<STR_LIT:\n>' ) <EOL> f . write ( '<STR_LIT>' + self . artist + '<STR_LIT:\n>' ) <EOL> f . write ( '<STR_LIT>' + self . toc + '<STR_LIT:\n>' ) <EOL> for note in self . notes : <EOL> f . write ( '<STR_LIT>' + note + '<STR_LIT:\n>' ) <EOL> prevpref = None <EOL> for i in range ( <NUM_LIT:1> , len ( self . track ) ) : <EOL> if self . trackartist [ i ] : <EOL> f . write ( '<STR_LIT>' % ( i , self . trackartist [ i ] ) ) <EOL> track = self . track [ i ] <EOL> try : <EOL> off = track . index ( '<STR_LIT:U+002C>' ) <EOL> except ValueError : <EOL> prevpref = None <EOL> else : <EOL> if prevpref and track [ : off ] == prevpref : <EOL> track = track [ off : ] <EOL> else : <EOL> prevpref = track [ : off ] <EOL> f . write ( '<STR_LIT>' % ( i , track ) ) <EOL> f . close ( ) </s>
