• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# 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
7# r = readcd.Readcd()
8# c = Cdplayer(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.
16from warnings import warnpy3k
17warnpy3k("the cdplayer module has been removed in Python 3.0", stacklevel=2)
18del warnpy3k
19
20cdplayerrc = '.cdplayerrc'
21
22class Cdplayer:
23    def __init__(self, tracklist):
24        import string
25        self.artist = ''
26        self.title = ''
27        if type(tracklist) == type(''):
28            t = []
29            for i in range(2, len(tracklist), 4):
30                t.append((None, \
31                          (string.atoi(tracklist[i:i+2]), \
32                           string.atoi(tracklist[i+2:i+4]))))
33            tracklist = t
34        self.track = [None] + [''] * len(tracklist)
35        self.id = 'd' + string.zfill(len(tracklist), 2)
36        for track in tracklist:
37            start, length = track
38            self.id = self.id + string.zfill(length[0], 2) + \
39                      string.zfill(length[1], 2)
40        try:
41            import posix
42            f = open(posix.environ['HOME'] + '/' + cdplayerrc, 'r')
43        except IOError:
44            return
45        import re
46        reg = re.compile(r'^([^:]*):\t(.*)')
47        s = self.id + '.'
48        l = len(s)
49        while 1:
50            line = f.readline()
51            if line == '':
52                break
53            if line[:l] == s:
54                line = line[l:]
55                match = reg.match(line)
56                if not match:
57                    print 'syntax error in ~/' + cdplayerrc
58                    continue
59                name, value = match.group(1, 2)
60                if name == 'title':
61                    self.title = value
62                elif name == 'artist':
63                    self.artist = value
64                elif name[:5] == 'track':
65                    trackno = string.atoi(name[6:])
66                    self.track[trackno] = value
67        f.close()
68
69    def write(self):
70        import posix
71        filename = posix.environ['HOME'] + '/' + cdplayerrc
72        try:
73            old = open(filename, 'r')
74        except IOError:
75            old = open('/dev/null', 'r')
76        new = open(filename + '.new', 'w')
77        s = self.id + '.'
78        l = len(s)
79        while 1:
80            line = old.readline()
81            if line == '':
82                break
83            if line[:l] != s:
84                new.write(line)
85        new.write(self.id + '.title:\t' + self.title + '\n')
86        new.write(self.id + '.artist:\t' + self.artist + '\n')
87        for i in range(1, len(self.track)):
88            new.write('%s.track.%r:\t%s\n' % (self.id, i, self.track[i]))
89        old.close()
90        new.close()
91        posix.rename(filename + '.new', filename)
92