cdplayer.py :  » Language-Interface » ChinesePython » chinesepython2.1.3-0.4 » Lib » plat-irix5 » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Language Interface » ChinesePython 
ChinesePython » chinesepython2.1.3 0.4 » Lib » plat irix5 » cdplayer.py
# This file implements a class which forms an interface to the .cdplayerrc
# file that is maintained by SGI's cdplayer program.
#
# Usage is as follows:
#
# import readcd
# r = readcd.Readcd()
# c = Cdplayer(r.gettrackinfo())
#
# Now you can use c.artist, c.title and c.track[trackno] (where trackno
# starts at 1).  When the CD is not recognized, all values will be the empty
# string.
# It is also possible to set the above mentioned variables to new values.
# You can then use c.write() to write out the changed values to the
# .cdplayerrc file.

cdplayerrc = '.cdplayerrc'

class Cdplayer:
  def __init__(self, tracklist):
    import string
    self.artist = ''
    self.title = ''
    if type(tracklist) == type(''):
      t = []
      for i in range(2, len(tracklist), 4):
        t.append((None, \
            (string.atoi(tracklist[i:i+2]), \
             string.atoi(tracklist[i+2:i+4]))))
      tracklist = t
    self.track = [None] + [''] * len(tracklist)
    self.id = 'd' + string.zfill(len(tracklist), 2)
    for track in tracklist:
      start, length = track
      self.id = self.id + string.zfill(length[0], 2) + \
          string.zfill(length[1], 2)
    try:
      import posix
      f = open(posix.environ['HOME'] + '/' + cdplayerrc, 'r')
    except IOError:
      return
    import re
    reg = re.compile(r'^([^:]*):\t(.*)')
    s = self.id + '.'
    l = len(s)
    while 1:
      line = f.readline()
      if line == '':
        break
      if line[:l] == s:
        line = line[l:]
        match = reg.match(line)
        if not match:
          print 'syntax error in ~/' + cdplayerrc
          continue
        name, value = match.group(1, 2)
        if name == 'title':
          self.title = value
        elif name == 'artist':
          self.artist = value
        elif name[:5] == 'track':
          trackno = string.atoi(name[6:])
          self.track[trackno] = value
    f.close()

  def write(self):
    import posix
    filename = posix.environ['HOME'] + '/' + cdplayerrc
    try:
      old = open(filename, 'r')
    except IOError:
      old = open('/dev/null', 'r')
    new = open(filename + '.new', 'w')
    s = self.id + '.'
    l = len(s)
    while 1:
      line = old.readline()
      if line == '':
        break
      if line[:l] != s:
        new.write(line)
    new.write(self.id + '.title:\t' + self.title + '\n')
    new.write(self.id + '.artist:\t' + self.artist + '\n')
    for i in range(1, len(self.track)):
      new.write(self.id + '.track.' + `i` + ':\t' + \
          self.track[i] + '\n')
    old.close()
    new.close()
    posix.rename(filename + '.new', filename)
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.