home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / pyth_os2.zip / python-1.0.2 / Lib / sgi / cddb.py < prev    next >
Text File  |  1993-11-08  |  4KB  |  159 lines

  1. # This file implements a class which forms an interface to the .cddb
  2. # directory that is maintained by SGI's cdman program.
  3. #
  4. # Usage is as follows:
  5. #
  6. # import readcd
  7. # r = readcd.Readcd()
  8. # c = Cddb(r.gettrackinfo())
  9. #
  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
  12. # string.
  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
  15. # .cdplayerrc file.
  16.  
  17. import string, posix
  18.  
  19. _cddbrc = '.cddb'
  20. _DB_ID_NTRACKS = 5
  21. _dbid_map = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ@_=+abcdefghijklmnopqrstuvwxyz'
  22. def _dbid(v):
  23.     if v >= len(_dbid_map):
  24.         return string.zfill(v, 2)
  25.     else:
  26.         return _dbid_map[v]
  27.  
  28. def tochash(toc):
  29.     if type(toc) == type(''):
  30.         tracklist = []
  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]))))
  35.     else:
  36.         tracklist = toc
  37.     ntracks = len(tracklist)
  38.     hash = _dbid((ntracks >> 4) & 0xF) + _dbid(ntracks & 0xF)
  39.     if ntracks <= _DB_ID_NTRACKS:
  40.         nidtracks = ntracks
  41.     else:
  42.         nidtracks = _DB_ID_NTRACKS - 1
  43.         min = 0
  44.         sec = 0
  45.         for track in tracklist:
  46.             start, length = track
  47.             min = min + length[0]
  48.             sec = sec + length[1]
  49.         min = min + sec / 60
  50.         sec = sec % 60
  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])
  55.     return hash
  56.     
  57. class Cddb:
  58.     def __init__(self, tracklist):
  59.         if posix.environ.has_key('CDDB_PATH'):
  60.             path = posix.environ['CDDB_PATH']
  61.             cddb_path = string.splitfields(path, ',')
  62.         else:
  63.             home = posix.environ['HOME']
  64.             cddb_path = [home + '/' + _cddbrc]
  65.         self.artist = ''
  66.         self.title = ''
  67.         if type(tracklist) == type(''):
  68.             t = []
  69.             for i in range(2, len(tracklist), 4):
  70.                 t.append((None, \
  71.                       (string.atoi(tracklist[i:i+2]), \
  72.                        string.atoi(tracklist[i+2:i+4]))))
  73.             tracklist = t
  74.         ntracks = len(tracklist)
  75.         self.track = [None] + [''] * ntracks
  76.         self.id = _dbid((ntracks >> 4) & 0xF) + _dbid(ntracks & 0xF)
  77.         if ntracks <= _DB_ID_NTRACKS:
  78.             nidtracks = ntracks
  79.         else:
  80.             nidtracks = _DB_ID_NTRACKS - 1
  81.             min = 0
  82.             sec = 0
  83.             for track in tracklist:
  84.                 start, length = track
  85.                 min = min + length[0]
  86.                 sec = sec + length[1]
  87.             min = min + sec / 60
  88.             sec = sec % 60
  89.             self.id = self.id + _dbid(min) + _dbid(sec)
  90.         for i in range(nidtracks):
  91.             start, length = tracklist[i]
  92.             self.id = self.id + _dbid(length[0]) + _dbid(length[1])
  93.         self.toc = string.zfill(ntracks, 2)
  94.         for track in tracklist:
  95.             start, length = track
  96.             self.toc = self.toc + string.zfill(length[0], 2) + \
  97.                   string.zfill(length[1], 2)
  98.         for dir in cddb_path:
  99.             file = dir + '/' + self.id + '.rdb'
  100.             try:
  101.                 f = open(file, 'r')
  102.                 self.file = file
  103.                 break
  104.             except IOError:
  105.                 pass
  106.         if not hasattr(self, 'file'):
  107.             return
  108.         import regex
  109.         reg = regex.compile('^\\([^.]*\\)\\.\\([^:]*\\):\t\\(.*\\)')
  110.         while 1:
  111.             line = f.readline()
  112.             if not line:
  113.                 break
  114.             if reg.match(line) == -1:
  115.                 print 'syntax error in ' + file
  116.                 continue
  117.             name1 = line[reg.regs[1][0]:reg.regs[1][1]]
  118.             name2 = line[reg.regs[2][0]:reg.regs[2][1]]
  119.             value = line[reg.regs[3][0]:reg.regs[3][1]]
  120.             if name1 == 'album':
  121.                 if name2 == 'artist':
  122.                     self.artist = value
  123.                 elif name2 == 'title':
  124.                     self.title = value
  125.                 elif name2 == 'toc':
  126.                     if self.toc != value:
  127.                         print 'toc\'s don\'t match'
  128.             elif name1[:5] == 'track':
  129.                 try:
  130.                     trackno = string.atoi(name1[5:])
  131.                 except strings.atoi_error:
  132.                     print 'syntax error in ' + file
  133.                     continue
  134.                 if trackno > ntracks:
  135.                     print 'track number ' + `trackno` + \
  136.                           ' in file ' + file + \
  137.                           ' out of range'
  138.                     continue
  139.                 self.track[trackno] = value
  140.         f.close()
  141.  
  142.     def write(self):
  143.         import posixpath
  144.         if posix.environ.has_key('CDDB_WRITE_DIR'):
  145.             dir = posix.environ['CDDB_WRITE_DIR']
  146.         else:
  147.             dir = posix.environ['HOME'] + '/' + _cddbrc
  148.         file = dir + '/' + self.id + '.rdb'
  149.         if posixpath.exists(file):
  150.             # make backup copy
  151.             posix.rename(file, file + '~')
  152.         f = open(file, 'w')
  153.         f.write('album.title:\t' + self.title + '\n')
  154.         f.write('album.artist:\t' + self.artist + '\n')
  155.         f.write('album.toc:\t' + self.toc + '\n')
  156.         for i in range(1, len(self.track)):
  157.             f.write('track' + `i` + '.title:\t' + self.track[i] + '\n')
  158.         f.close()
  159.