Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c9d73d1

Browse files
Implemented support for CDDB_PATH and CDDB_WRITE_DIR environment
variables. Added auxiliary routine tochash to convert a table-of-contents to a hashed toc.
1 parent d96ec44 commit c9d73d1

2 files changed

Lines changed: 104 additions & 20 deletions

File tree

Lib/irix5/cddb.py

Lines changed: 52 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414
# You can then use c.write() to write out the changed values to the
1515
# .cdplayerrc file.
1616

17-
import string
17+
import string, posix
1818

19-
_cddbrc = '.cddb/'
19+
_cddbrc = '.cddb'
2020
_DB_ID_NTRACKS = 5
2121
_dbid_map = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ@_=+abcdefghijklmnopqrstuvwxyz'
2222
def _dbid(v):
@@ -25,8 +25,43 @@ def _dbid(v):
2525
else:
2626
return _dbid_map[v]
2727

28+
def tochash(toc):
29+
if type(toc) == type(''):
30+
tracklist = []
31+
for i in range(2, len(toc), 4):
32+
tracklist.append((None,
33+
(string.atoi(toc[i:i+2]),
34+
string.atoi(toc[i+2:i+4]))))
35+
else:
36+
tracklist = toc
37+
ntracks = len(tracklist)
38+
hash = _dbid((ntracks >> 4) & 0xF) + _dbid(ntracks & 0xF)
39+
if ntracks <= _DB_ID_NTRACKS:
40+
nidtracks = ntracks
41+
else:
42+
nidtracks = _DB_ID_NTRACKS - 1
43+
min = 0
44+
sec = 0
45+
for track in tracklist:
46+
start, length = track
47+
min = min + length[0]
48+
sec = sec + length[1]
49+
min = min + sec / 60
50+
sec = sec % 60
51+
hash = hash + _dbid(min) + _dbid(sec)
52+
for i in range(nidtracks):
53+
start, length = tracklist[i]
54+
hash = hash + _dbid(length[0]) + _dbid(length[1])
55+
return hash
56+
2857
class Cddb:
2958
def init(self, tracklist):
59+
if posix.environ.has_key('CDDB_PATH'):
60+
path = posix.environ['CDDB_PATH']
61+
cddb_path = string.splitfields(path, ',')
62+
else:
63+
home = posix.environ['HOME']
64+
cddb_path = [home + '/' + _cddbrc]
3065
self.artist = ''
3166
self.title = ''
3267
if type(tracklist) == type(''):
@@ -60,11 +95,15 @@ def init(self, tracklist):
6095
start, length = track
6196
self.toc = self.toc + string.zfill(length[0], 2) + \
6297
string.zfill(length[1], 2)
63-
try:
64-
import posix
65-
file = posix.environ['HOME'] + '/' + _cddbrc + self.id + '.rdb'
66-
f = open(file, 'r')
67-
except IOError:
98+
for dir in cddb_path:
99+
file = dir + '/' + self.id + '.rdb'
100+
try:
101+
f = open(file, 'r')
102+
self.file = file
103+
break
104+
except IOError:
105+
pass
106+
if not hasattr(self, 'file'):
68107
return self
69108
import regex
70109
reg = regex.compile('^\\([^.]*\\)\\.\\([^:]*\\):\t\\(.*\\)')
@@ -93,7 +132,7 @@ def init(self, tracklist):
93132
print 'syntax error in ' + file
94133
continue
95134
if trackno > ntracks:
96-
print 'track number ' + trackno + \
135+
print 'track number ' + `trackno` + \
97136
' in file ' + file + \
98137
' out of range'
99138
continue
@@ -102,8 +141,11 @@ def init(self, tracklist):
102141
return self
103142

104143
def write(self):
105-
import posix
106-
file = posix.environ['HOME'] + '/' + _cddbrc + self.id + '.rdb'
144+
if posix.environ.has_key('CDDB_WRITE_DIR'):
145+
dir = posix.environ['CDDB_WRITE_DIR']
146+
else:
147+
dir = posix.environ['HOME'] + '/' + _cddbrc
148+
file = dir + '/' + self.id + '.rdb'
107149
f = open(file, 'w')
108150
f.write('album.title:\t' + self.title + '\n')
109151
f.write('album.artist:\t' + self.artist + '\n')

Lib/plat-irix5/cddb.py

Lines changed: 52 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414
# You can then use c.write() to write out the changed values to the
1515
# .cdplayerrc file.
1616

17-
import string
17+
import string, posix
1818

19-
_cddbrc = '.cddb/'
19+
_cddbrc = '.cddb'
2020
_DB_ID_NTRACKS = 5
2121
_dbid_map = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ@_=+abcdefghijklmnopqrstuvwxyz'
2222
def _dbid(v):
@@ -25,8 +25,43 @@ def _dbid(v):
2525
else:
2626
return _dbid_map[v]
2727

28+
def tochash(toc):
29+
if type(toc) == type(''):
30+
tracklist = []
31+
for i in range(2, len(toc), 4):
32+
tracklist.append((None,
33+
(string.atoi(toc[i:i+2]),
34+
string.atoi(toc[i+2:i+4]))))
35+
else:
36+
tracklist = toc
37+
ntracks = len(tracklist)
38+
hash = _dbid((ntracks >> 4) & 0xF) + _dbid(ntracks & 0xF)
39+
if ntracks <= _DB_ID_NTRACKS:
40+
nidtracks = ntracks
41+
else:
42+
nidtracks = _DB_ID_NTRACKS - 1
43+
min = 0
44+
sec = 0
45+
for track in tracklist:
46+
start, length = track
47+
min = min + length[0]
48+
sec = sec + length[1]
49+
min = min + sec / 60
50+
sec = sec % 60
51+
hash = hash + _dbid(min) + _dbid(sec)
52+
for i in range(nidtracks):
53+
start, length = tracklist[i]
54+
hash = hash + _dbid(length[0]) + _dbid(length[1])
55+
return hash
56+
2857
class Cddb:
2958
def init(self, tracklist):
59+
if posix.environ.has_key('CDDB_PATH'):
60+
path = posix.environ['CDDB_PATH']
61+
cddb_path = string.splitfields(path, ',')
62+
else:
63+
home = posix.environ['HOME']
64+
cddb_path = [home + '/' + _cddbrc]
3065
self.artist = ''
3166
self.title = ''
3267
if type(tracklist) == type(''):
@@ -60,11 +95,15 @@ def init(self, tracklist):
6095
start, length = track
6196
self.toc = self.toc + string.zfill(length[0], 2) + \
6297
string.zfill(length[1], 2)
63-
try:
64-
import posix
65-
file = posix.environ['HOME'] + '/' + _cddbrc + self.id + '.rdb'
66-
f = open(file, 'r')
67-
except IOError:
98+
for dir in cddb_path:
99+
file = dir + '/' + self.id + '.rdb'
100+
try:
101+
f = open(file, 'r')
102+
self.file = file
103+
break
104+
except IOError:
105+
pass
106+
if not hasattr(self, 'file'):
68107
return self
69108
import regex
70109
reg = regex.compile('^\\([^.]*\\)\\.\\([^:]*\\):\t\\(.*\\)')
@@ -93,7 +132,7 @@ def init(self, tracklist):
93132
print 'syntax error in ' + file
94133
continue
95134
if trackno > ntracks:
96-
print 'track number ' + trackno + \
135+
print 'track number ' + `trackno` + \
97136
' in file ' + file + \
98137
' out of range'
99138
continue
@@ -102,8 +141,11 @@ def init(self, tracklist):
102141
return self
103142

104143
def write(self):
105-
import posix
106-
file = posix.environ['HOME'] + '/' + _cddbrc + self.id + '.rdb'
144+
if posix.environ.has_key('CDDB_WRITE_DIR'):
145+
dir = posix.environ['CDDB_WRITE_DIR']
146+
else:
147+
dir = posix.environ['HOME'] + '/' + _cddbrc
148+
file = dir + '/' + self.id + '.rdb'
107149
f = open(file, 'w')
108150
f.write('album.title:\t' + self.title + '\n')
109151
f.write('album.artist:\t' + self.artist + '\n')

0 commit comments

Comments
 (0)