Guido van Rossum | 73b715e | 1992-06-03 16:50:01 +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 |
Sjoerd Mullender | b2e358d | 1993-11-08 15:10:43 +0000 | [diff] [blame] | 7 | # r = readcd.Readcd() |
| 8 | # c = Cdplayer(r.gettrackinfo()) |
Guido van Rossum | 73b715e | 1992-06-03 16:50:01 +0000 | [diff] [blame] | 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 | |
Guido van Rossum | d316607 | 1993-05-24 14:16:22 +0000 | [diff] [blame] | 19 | class Cdplayer: |
Sjoerd Mullender | b2e358d | 1993-11-08 15:10:43 +0000 | [diff] [blame] | 20 | def __init__(self, tracklist): |
Guido van Rossum | 73b715e | 1992-06-03 16:50:01 +0000 | [diff] [blame] | 21 | import string |
| 22 | self.artist = '' |
| 23 | self.title = '' |
Sjoerd Mullender | 4fff13f | 1992-12-14 13:16:55 +0000 | [diff] [blame] | 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 |
Guido van Rossum | 73b715e | 1992-06-03 16:50:01 +0000 | [diff] [blame] | 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: |
Sjoerd Mullender | b2e358d | 1993-11-08 15:10:43 +0000 | [diff] [blame] | 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 | 73b715e | 1992-06-03 16:50:01 +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 | 73b715e | 1992-06-03 16:50:01 +0000 | [diff] [blame] | 54 | print 'syntax error in ~/' + cdplayerrc |
| 55 | continue |
Guido van Rossum | 9694fca | 1997-10-22 21:00:49 +0000 | [diff] [blame] | 56 | name, valye = match.group(1, 2) |
Guido van Rossum | 73b715e | 1992-06-03 16:50:01 +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() |
Guido van Rossum | 73b715e | 1992-06-03 16:50:01 +0000 | [diff] [blame] | 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) |