blob: 0e27468451e8d6805b78fd44a208a02135cde532 [file] [log] [blame]
Guido van Rossum73b715e1992-06-03 16:50:01 +00001# This file implements a class which forms an interface to the .cdplayerrc
2# file that is maintained by SGI's cdplayer program.
3#
4# Usage is as follows:
5#
6# import readcd
Sjoerd Mullenderb2e358d1993-11-08 15:10:43 +00007# r = readcd.Readcd()
8# c = Cdplayer(r.gettrackinfo())
Guido van Rossum73b715e1992-06-03 16:50:01 +00009#
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
17cdplayerrc = '.cdplayerrc'
18
Guido van Rossumd3166071993-05-24 14:16:22 +000019class Cdplayer:
Sjoerd Mullenderb2e358d1993-11-08 15:10:43 +000020 def __init__(self, tracklist):
Guido van Rossum73b715e1992-06-03 16:50:01 +000021 import string
22 self.artist = ''
23 self.title = ''
Sjoerd Mullender4fff13f1992-12-14 13:16:55 +000024 if type(tracklist) == type(''):
25 t = []
26 for i in range(2, len(tracklist), 4):
27 t.append((None, \
28 (string.atoi(tracklist[i:i+2]), \
29 string.atoi(tracklist[i+2:i+4]))))
30 tracklist = t
Guido van Rossum73b715e1992-06-03 16:50:01 +000031 self.track = [None] + [''] * len(tracklist)
32 self.id = 'd' + string.zfill(len(tracklist), 2)
33 for track in tracklist:
34 start, length = track
35 self.id = self.id + string.zfill(length[0], 2) + \
36 string.zfill(length[1], 2)
37 try:
38 import posix
39 f = open(posix.environ['HOME'] + '/' + cdplayerrc, 'r')
40 except IOError:
Sjoerd Mullenderb2e358d1993-11-08 15:10:43 +000041 return
Guido van Rossum9694fca1997-10-22 21:00:49 +000042 import re
43 reg = re.compile(r'^([^:]*):\t(.*)')
Guido van Rossum73b715e1992-06-03 16:50:01 +000044 s = self.id + '.'
45 l = len(s)
46 while 1:
47 line = f.readline()
48 if line == '':
49 break
50 if line[:l] == s:
51 line = line[l:]
Guido van Rossum9694fca1997-10-22 21:00:49 +000052 match = reg.match(line)
53 if not match:
Guido van Rossum73b715e1992-06-03 16:50:01 +000054 print 'syntax error in ~/' + cdplayerrc
55 continue
Guido van Rossum9694fca1997-10-22 21:00:49 +000056 name, valye = match.group(1, 2)
Guido van Rossum73b715e1992-06-03 16:50:01 +000057 if name == 'title':
58 self.title = value
59 elif name == 'artist':
60 self.artist = value
61 elif name[:5] == 'track':
62 trackno = string.atoi(name[6:])
63 self.track[trackno] = value
64 f.close()
Guido van Rossum73b715e1992-06-03 16:50:01 +000065
66 def write(self):
67 import posix
68 filename = posix.environ['HOME'] + '/' + cdplayerrc
69 try:
70 old = open(filename, 'r')
71 except IOError:
72 old = open('/dev/null', 'r')
73 new = open(filename + '.new', 'w')
74 s = self.id + '.'
75 l = len(s)
76 while 1:
77 line = old.readline()
78 if line == '':
79 break
80 if line[:l] <> s:
81 new.write(line)
82 new.write(self.id + '.title:\t' + self.title + '\n')
83 new.write(self.id + '.artist:\t' + self.artist + '\n')
84 for i in range(1, len(self.track)):
85 new.write(self.id + '.track.' + `i` + ':\t' + \
86 self.track[i] + '\n')
87 old.close()
88 new.close()
89 posix.rename(filename + '.new', filename)