Kaydet (Commit) e202c38d authored tarafından Sjoerd Mullender's avatar Sjoerd Mullender

Can now also give a hashed ID to Cddb.

üst 1f057546
...@@ -14,7 +14,7 @@ ...@@ -14,7 +14,7 @@
# You can then use c.write() to write out the changed values to the # You can then use c.write() to write out the changed values to the
# .cdplayerrc file. # .cdplayerrc file.
import string, posix import string, posix, os
_cddbrc = '.cddb' _cddbrc = '.cddb'
_DB_ID_NTRACKS = 5 _DB_ID_NTRACKS = 5
...@@ -56,45 +56,15 @@ def tochash(toc): ...@@ -56,45 +56,15 @@ def tochash(toc):
class Cddb: class Cddb:
def __init__(self, tracklist): def __init__(self, tracklist):
if posix.environ.has_key('CDDB_PATH'): if os.environ.has_key('CDDB_PATH'):
path = posix.environ['CDDB_PATH'] path = os.environ['CDDB_PATH']
cddb_path = string.splitfields(path, ',') cddb_path = string.splitfields(path, ',')
else: else:
home = posix.environ['HOME'] home = os.environ['HOME']
cddb_path = [home + '/' + _cddbrc] cddb_path = [home + '/' + _cddbrc]
self.artist = ''
self.title = '' self._get_id(tracklist)
if type(tracklist) == type(''):
t = []
for i in range(2, len(tracklist), 4):
t.append((None, \
(string.atoi(tracklist[i:i+2]), \
string.atoi(tracklist[i+2:i+4]))))
tracklist = t
ntracks = len(tracklist)
self.track = [None] + [''] * ntracks
self.id = _dbid((ntracks >> 4) & 0xF) + _dbid(ntracks & 0xF)
if ntracks <= _DB_ID_NTRACKS:
nidtracks = ntracks
else:
nidtracks = _DB_ID_NTRACKS - 1
min = 0
sec = 0
for track in tracklist:
start, length = track
min = min + length[0]
sec = sec + length[1]
min = min + sec / 60
sec = sec % 60
self.id = self.id + _dbid(min) + _dbid(sec)
for i in range(nidtracks):
start, length = tracklist[i]
self.id = self.id + _dbid(length[0]) + _dbid(length[1])
self.toc = string.zfill(ntracks, 2)
for track in tracklist:
start, length = track
self.toc = self.toc + string.zfill(length[0], 2) + \
string.zfill(length[1], 2)
for dir in cddb_path: for dir in cddb_path:
file = dir + '/' + self.id + '.rdb' file = dir + '/' + self.id + '.rdb'
try: try:
...@@ -103,6 +73,10 @@ class Cddb: ...@@ -103,6 +73,10 @@ class Cddb:
break break
except IOError: except IOError:
pass pass
ntracks = string.atoi(self.id[:2], 16)
self.artist = ''
self.title = ''
self.track = [None] + [''] * ntracks
if not hasattr(self, 'file'): if not hasattr(self, 'file'):
return return
import regex import regex
...@@ -123,6 +97,8 @@ class Cddb: ...@@ -123,6 +97,8 @@ class Cddb:
elif name2 == 'title': elif name2 == 'title':
self.title = value self.title = value
elif name2 == 'toc': elif name2 == 'toc':
if not self.toc:
self.toc = value
if self.toc != value: if self.toc != value:
print 'toc\'s don\'t match' print 'toc\'s don\'t match'
elif name1[:5] == 'track': elif name1[:5] == 'track':
...@@ -142,7 +118,7 @@ class Cddb: ...@@ -142,7 +118,7 @@ class Cddb:
track = self.track[i] track = self.track[i]
# if track title starts with `,', use initial part # if track title starts with `,', use initial part
# of previous track's title # of previous track's title
if track[0] == ',': if track and track[0] == ',':
try: try:
off = string.index(self.track[i - 1], off = string.index(self.track[i - 1],
',') ',')
...@@ -152,12 +128,51 @@ class Cddb: ...@@ -152,12 +128,51 @@ class Cddb:
self.track[i] = self.track[i-1][:off] \ self.track[i] = self.track[i-1][:off] \
+ track + track
def _get_id(self, tracklist):
# fill in self.id and self.toc.
# if the argument is a string ending in .rdb, the part
# upto the suffix is taken as the id.
if type(tracklist) == type(''):
if tracklist[-4:] == '.rdb':
self.id = tracklist[:-4]
self.toc = ''
return
t = []
for i in range(2, len(tracklist), 4):
t.append((None, \
(string.atoi(tracklist[i:i+2]), \
string.atoi(tracklist[i+2:i+4]))))
tracklist = t
ntracks = len(tracklist)
self.id = _dbid((ntracks >> 4) & 0xF) + _dbid(ntracks & 0xF)
if ntracks <= _DB_ID_NTRACKS:
nidtracks = ntracks
else:
nidtracks = _DB_ID_NTRACKS - 1
min = 0
sec = 0
for track in tracklist:
start, length = track
min = min + length[0]
sec = sec + length[1]
min = min + sec / 60
sec = sec % 60
self.id = self.id + _dbid(min) + _dbid(sec)
for i in range(nidtracks):
start, length = tracklist[i]
self.id = self.id + _dbid(length[0]) + _dbid(length[1])
self.toc = string.zfill(ntracks, 2)
for track in tracklist:
start, length = track
self.toc = self.toc + string.zfill(length[0], 2) + \
string.zfill(length[1], 2)
def write(self): def write(self):
import posixpath import posixpath
if posix.environ.has_key('CDDB_WRITE_DIR'): if os.environ.has_key('CDDB_WRITE_DIR'):
dir = posix.environ['CDDB_WRITE_DIR'] dir = os.environ['CDDB_WRITE_DIR']
else: else:
dir = posix.environ['HOME'] + '/' + _cddbrc dir = os.environ['HOME'] + '/' + _cddbrc
file = dir + '/' + self.id + '.rdb' file = dir + '/' + self.id + '.rdb'
if posixpath.exists(file): if posixpath.exists(file):
# make backup copy # make backup copy
......
...@@ -14,7 +14,7 @@ ...@@ -14,7 +14,7 @@
# You can then use c.write() to write out the changed values to the # You can then use c.write() to write out the changed values to the
# .cdplayerrc file. # .cdplayerrc file.
import string, posix import string, posix, os
_cddbrc = '.cddb' _cddbrc = '.cddb'
_DB_ID_NTRACKS = 5 _DB_ID_NTRACKS = 5
...@@ -56,45 +56,15 @@ def tochash(toc): ...@@ -56,45 +56,15 @@ def tochash(toc):
class Cddb: class Cddb:
def __init__(self, tracklist): def __init__(self, tracklist):
if posix.environ.has_key('CDDB_PATH'): if os.environ.has_key('CDDB_PATH'):
path = posix.environ['CDDB_PATH'] path = os.environ['CDDB_PATH']
cddb_path = string.splitfields(path, ',') cddb_path = string.splitfields(path, ',')
else: else:
home = posix.environ['HOME'] home = os.environ['HOME']
cddb_path = [home + '/' + _cddbrc] cddb_path = [home + '/' + _cddbrc]
self.artist = ''
self.title = '' self._get_id(tracklist)
if type(tracklist) == type(''):
t = []
for i in range(2, len(tracklist), 4):
t.append((None, \
(string.atoi(tracklist[i:i+2]), \
string.atoi(tracklist[i+2:i+4]))))
tracklist = t
ntracks = len(tracklist)
self.track = [None] + [''] * ntracks
self.id = _dbid((ntracks >> 4) & 0xF) + _dbid(ntracks & 0xF)
if ntracks <= _DB_ID_NTRACKS:
nidtracks = ntracks
else:
nidtracks = _DB_ID_NTRACKS - 1
min = 0
sec = 0
for track in tracklist:
start, length = track
min = min + length[0]
sec = sec + length[1]
min = min + sec / 60
sec = sec % 60
self.id = self.id + _dbid(min) + _dbid(sec)
for i in range(nidtracks):
start, length = tracklist[i]
self.id = self.id + _dbid(length[0]) + _dbid(length[1])
self.toc = string.zfill(ntracks, 2)
for track in tracklist:
start, length = track
self.toc = self.toc + string.zfill(length[0], 2) + \
string.zfill(length[1], 2)
for dir in cddb_path: for dir in cddb_path:
file = dir + '/' + self.id + '.rdb' file = dir + '/' + self.id + '.rdb'
try: try:
...@@ -103,6 +73,10 @@ class Cddb: ...@@ -103,6 +73,10 @@ class Cddb:
break break
except IOError: except IOError:
pass pass
ntracks = string.atoi(self.id[:2], 16)
self.artist = ''
self.title = ''
self.track = [None] + [''] * ntracks
if not hasattr(self, 'file'): if not hasattr(self, 'file'):
return return
import regex import regex
...@@ -123,6 +97,8 @@ class Cddb: ...@@ -123,6 +97,8 @@ class Cddb:
elif name2 == 'title': elif name2 == 'title':
self.title = value self.title = value
elif name2 == 'toc': elif name2 == 'toc':
if not self.toc:
self.toc = value
if self.toc != value: if self.toc != value:
print 'toc\'s don\'t match' print 'toc\'s don\'t match'
elif name1[:5] == 'track': elif name1[:5] == 'track':
...@@ -142,7 +118,7 @@ class Cddb: ...@@ -142,7 +118,7 @@ class Cddb:
track = self.track[i] track = self.track[i]
# if track title starts with `,', use initial part # if track title starts with `,', use initial part
# of previous track's title # of previous track's title
if track[0] == ',': if track and track[0] == ',':
try: try:
off = string.index(self.track[i - 1], off = string.index(self.track[i - 1],
',') ',')
...@@ -152,12 +128,51 @@ class Cddb: ...@@ -152,12 +128,51 @@ class Cddb:
self.track[i] = self.track[i-1][:off] \ self.track[i] = self.track[i-1][:off] \
+ track + track
def _get_id(self, tracklist):
# fill in self.id and self.toc.
# if the argument is a string ending in .rdb, the part
# upto the suffix is taken as the id.
if type(tracklist) == type(''):
if tracklist[-4:] == '.rdb':
self.id = tracklist[:-4]
self.toc = ''
return
t = []
for i in range(2, len(tracklist), 4):
t.append((None, \
(string.atoi(tracklist[i:i+2]), \
string.atoi(tracklist[i+2:i+4]))))
tracklist = t
ntracks = len(tracklist)
self.id = _dbid((ntracks >> 4) & 0xF) + _dbid(ntracks & 0xF)
if ntracks <= _DB_ID_NTRACKS:
nidtracks = ntracks
else:
nidtracks = _DB_ID_NTRACKS - 1
min = 0
sec = 0
for track in tracklist:
start, length = track
min = min + length[0]
sec = sec + length[1]
min = min + sec / 60
sec = sec % 60
self.id = self.id + _dbid(min) + _dbid(sec)
for i in range(nidtracks):
start, length = tracklist[i]
self.id = self.id + _dbid(length[0]) + _dbid(length[1])
self.toc = string.zfill(ntracks, 2)
for track in tracklist:
start, length = track
self.toc = self.toc + string.zfill(length[0], 2) + \
string.zfill(length[1], 2)
def write(self): def write(self):
import posixpath import posixpath
if posix.environ.has_key('CDDB_WRITE_DIR'): if os.environ.has_key('CDDB_WRITE_DIR'):
dir = posix.environ['CDDB_WRITE_DIR'] dir = os.environ['CDDB_WRITE_DIR']
else: else:
dir = posix.environ['HOME'] + '/' + _cddbrc dir = os.environ['HOME'] + '/' + _cddbrc
file = dir + '/' + self.id + '.rdb' file = dir + '/' + self.id + '.rdb'
if posixpath.exists(file): if posixpath.exists(file):
# make backup copy # make backup copy
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment