1 # This file implements a class which forms an interface to the .cddb
2 # directory that is maintained by SGI's cdman program.
8 # c = Cddb(r.gettrackinfo())
10 # Now you can use c.artist, c.title and c.track[trackno] (where trackno
11 # starts at 1). When the CD is not recognized, all values will be the empty
13 # It is also possible to set the above mentioned variables to new values.
14 # You can then use c.write() to write out the changed values to the
17 import string
, posix
, os
21 _dbid_map
= '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ@_=+abcdefghijklmnopqrstuvwxyz'
23 if v
>= len(_dbid_map
):
24 return string
.zfill(v
, 2)
29 if type(toc
) == type(''):
31 for i
in range(2, len(toc
), 4):
32 tracklist
.append((None,
33 (string
.atoi(toc
[i
:i
+2]),
34 string
.atoi(toc
[i
+2:i
+4]))))
37 ntracks
= len(tracklist
)
38 hash = _dbid((ntracks
>> 4) & 0xF) + _dbid(ntracks
& 0xF)
39 if ntracks
<= _DB_ID_NTRACKS
:
42 nidtracks
= _DB_ID_NTRACKS
- 1
45 for track
in tracklist
:
51 hash = hash + _dbid(min) + _dbid(sec
)
52 for i
in range(nidtracks
):
53 start
, length
= tracklist
[i
]
54 hash = hash + _dbid(length
[0]) + _dbid(length
[1])
58 def __init__(self
, tracklist
):
59 if os
.environ
.has_key('CDDB_PATH'):
60 path
= os
.environ
['CDDB_PATH']
61 cddb_path
= string
.splitfields(path
, ',')
63 home
= os
.environ
['HOME']
64 cddb_path
= [home
+ '/' + _cddbrc
]
66 self
._get
_id
(tracklist
)
69 file = dir + '/' + self
.id + '.rdb'
76 ntracks
= string
.atoi(self
.id[:2], 16)
79 self
.track
= [None] + [''] * ntracks
80 self
.trackartist
= [None] + [''] * ntracks
82 if not hasattr(self
, 'file'):
85 reg
= re
.compile(r
'^([^.]*)\.([^:]*):[\t ]+(.*)')
90 match
= reg
.match(line
)
92 print 'syntax error in ' + file
94 name1
, name2
, value
= match
.group(1, 2, 3)
98 elif name2
== 'title':
103 if self
.toc
!= value
:
104 print 'toc\'s don\'t match'
105 elif name2
== 'notes':
106 self
.notes
.append(value
)
107 elif name1
[:5] == 'track':
109 trackno
= string
.atoi(name1
[5:])
110 except strings
.atoi_error
:
111 print 'syntax error in ' + file
113 if trackno
> ntracks
:
114 print 'track number ' + `trackno`
+ \
115 ' in file ' + file + \
119 self
.track
[trackno
] = value
120 elif name2
== 'artist':
121 self
.trackartist
[trackno
] = value
123 for i
in range(2, len(self
.track
)):
124 track
= self
.track
[i
]
125 # if track title starts with `,', use initial part
126 # of previous track's title
127 if track
and track
[0] == ',':
129 off
= string
.index(self
.track
[i
- 1],
131 except string
.index_error
:
134 self
.track
[i
] = self
.track
[i
-1][:off
] \
137 def _get_id(self
, tracklist
):
138 # fill in self.id and self.toc.
139 # if the argument is a string ending in .rdb, the part
140 # upto the suffix is taken as the id.
141 if type(tracklist
) == type(''):
142 if tracklist
[-4:] == '.rdb':
143 self
.id = tracklist
[:-4]
147 for i
in range(2, len(tracklist
), 4):
149 (string
.atoi(tracklist
[i
:i
+2]), \
150 string
.atoi(tracklist
[i
+2:i
+4]))))
152 ntracks
= len(tracklist
)
153 self
.id = _dbid((ntracks
>> 4) & 0xF) + _dbid(ntracks
& 0xF)
154 if ntracks
<= _DB_ID_NTRACKS
:
157 nidtracks
= _DB_ID_NTRACKS
- 1
160 for track
in tracklist
:
161 start
, length
= track
162 min = min + length
[0]
163 sec
= sec
+ length
[1]
166 self
.id = self
.id + _dbid(min) + _dbid(sec
)
167 for i
in range(nidtracks
):
168 start
, length
= tracklist
[i
]
169 self
.id = self
.id + _dbid(length
[0]) + _dbid(length
[1])
170 self
.toc
= string
.zfill(ntracks
, 2)
171 for track
in tracklist
:
172 start
, length
= track
173 self
.toc
= self
.toc
+ string
.zfill(length
[0], 2) + \
174 string
.zfill(length
[1], 2)
178 if os
.environ
.has_key('CDDB_WRITE_DIR'):
179 dir = os
.environ
['CDDB_WRITE_DIR']
181 dir = os
.environ
['HOME'] + '/' + _cddbrc
182 file = dir + '/' + self
.id + '.rdb'
183 if posixpath
.exists(file):
185 posix
.rename(file, file + '~')
187 f
.write('album.title:\t' + self
.title
+ '\n')
188 f
.write('album.artist:\t' + self
.artist
+ '\n')
189 f
.write('album.toc:\t' + self
.toc
+ '\n')
190 for note
in self
.notes
:
191 f
.write('album.notes:\t' + note
+ '\n')
193 for i
in range(1, len(self
.track
)):
194 if self
.trackartist
[i
]:
195 f
.write('track'+`i`
+'.artist:\t'+self
.trackartist
[i
]+'\n')
196 track
= self
.track
[i
]
198 off
= string
.index(track
, ',')
199 except string
.index_error
:
202 if prevpref
and track
[:off
] == prevpref
:
205 prevpref
= track
[:off
]
206 f
.write('track' + `i`
+ '.title:\t' + track
+ '\n')