Guido van Rossum | 1ce7c6f | 1997-01-15 19:19:19 +0000 | [diff] [blame] | 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. |
| 16 | |
| 17 | cdplayerrc = '.cdplayerrc' |
| 18 | |
| 19 | class Cdplayer: |
| 20 | def __init__(self, tracklist): |
| 21 | import string |
| 22 | self.artist = '' |
| 23 | self.title = '' |
| 24 | 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 |
| 31 | 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: |
| 41 | return |
Guido van Rossum | 9694fca | 1997-10-22 21:00:49 +0000 | [diff] [blame] | 42 | import re |
| 43 | reg = re.compile(r'^([^:]*):\t(.*)') |
Guido van Rossum | 1ce7c6f | 1997-01-15 19:19:19 +0000 | [diff] [blame] | 44 | 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 Rossum | 9694fca | 1997-10-22 21:00:49 +0000 | [diff] [blame] | 52 | match = reg.match(line) |
| 53 | if not match: |
Guido van Rossum | 1ce7c6f | 1997-01-15 19:19:19 +0000 | [diff] [blame] | 54 | print 'syntax error in ~/' + cdplayerrc |
| 55 | continue |
Thomas Wouters | c533e4a | 2000-07-16 11:57:20 +0000 | [diff] [blame] | 56 | name, value = match.group(1, 2) |
Guido van Rossum | 1ce7c6f | 1997-01-15 19:19:19 +0000 | [diff] [blame] | 57 | 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() |
| 65 | |
| 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) |