PageRenderTime 58ms CodeModel.GetById 33ms RepoModel.GetById 0ms app.codeStats 0ms

/lib-python/2.7/plat-irix6/cdplayer.py

https://bitbucket.org/yrttyr/pypy
Python | 91 lines | 74 code | 2 blank | 15 comment | 0 complexity | 93f060e79ea72d14abce6f1296f08df6 MD5 | raw file
  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. from warnings import warnpy3k
  17. warnpy3k("the cdplayer module has been removed in Python 3.0", stacklevel=2)
  18. del warnpy3k
  19. cdplayerrc = '.cdplayerrc'
  20. class Cdplayer:
  21. def __init__(self, tracklist):
  22. import string
  23. self.artist = ''
  24. self.title = ''
  25. if type(tracklist) == type(''):
  26. t = []
  27. for i in range(2, len(tracklist), 4):
  28. t.append((None, \
  29. (int(tracklist[i:i+2]), \
  30. int(tracklist[i+2:i+4]))))
  31. tracklist = t
  32. self.track = [None] + [''] * len(tracklist)
  33. self.id = 'd' + string.zfill(len(tracklist), 2)
  34. for track in tracklist:
  35. start, length = track
  36. self.id = self.id + string.zfill(length[0], 2) + \
  37. string.zfill(length[1], 2)
  38. try:
  39. import posix
  40. f = open(posix.environ['HOME'] + '/' + cdplayerrc, 'r')
  41. except IOError:
  42. return
  43. import re
  44. reg = re.compile(r'^([^:]*):\t(.*)')
  45. s = self.id + '.'
  46. l = len(s)
  47. while 1:
  48. line = f.readline()
  49. if line == '':
  50. break
  51. if line[:l] == s:
  52. line = line[l:]
  53. match = reg.match(line)
  54. if not match:
  55. print 'syntax error in ~/' + cdplayerrc
  56. continue
  57. name, value = match.group(1, 2)
  58. if name == 'title':
  59. self.title = value
  60. elif name == 'artist':
  61. self.artist = value
  62. elif name[:5] == 'track':
  63. trackno = int(name[6:])
  64. self.track[trackno] = value
  65. f.close()
  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('%s.track.%r:\t%s\n' % (self.id, i, self.track[i]))
  86. old.close()
  87. new.close()
  88. posix.rename(filename + '.new', filename)