From 5cae5cf60f8774b4d44981fc6f99990e9ad10f29 Mon Sep 17 00:00:00 2001 From: Thomas Vander Stichele Date: Fri, 15 May 2009 08:08:22 +0000 Subject: [PATCH] * morituri/image/table.py: Update .cue writing: - customise program name - add DISCID - use counter for FILE lines - put FILE line before TRACK if track does not have INDEX 00 Take counter into account for setFile * examples/readdisc.py: Add --track-template. Pass counter to setFile. --- ChangeLog | 13 ++++++++++++ examples/readdisc.py | 44 +++++++++++++++++++++++++++++++++++------ morituri/image/table.py | 29 ++++++++++++++++++++------- 3 files changed, 73 insertions(+), 13 deletions(-) diff --git a/ChangeLog b/ChangeLog index a5a77c5..f5cdaa0 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,16 @@ +2009-05-15 Thomas Vander Stichele + + * morituri/image/table.py: + Update .cue writing: + - customise program name + - add DISCID + - use counter for FILE lines + - put FILE line before TRACK if track does not have INDEX 00 + Take counter into account for setFile + * examples/readdisc.py: + Add --track-template. + Pass counter to setFile. + 2009-05-14 Thomas Vander Stichele * morituri/image/table.py: diff --git a/examples/readdisc.py b/examples/readdisc.py index 427d838..814afce 100644 --- a/examples/readdisc.py +++ b/examples/readdisc.py @@ -144,6 +144,31 @@ def musicbrainz(discid): return metadata +def getPath(template, metadata, i): + # returns without extension + + v = {} + + v['t'] = '%02d' % (i + 1) + + # default values + v['A'] = 'Unknown Artist' + v['d'] = 'Unknown Disc' + + v['a'] = v['A'] + v['n'] = 'Unknown Track' + + if metadata: + v['A'] = filterForPath(metadata.artist) + v['d'] = filterForPath(metadata.title) + v['a'] = filterForPath(metadata.tracks[i].artist) + v['n'] = filterForPath(metadata.tracks[i].title) + + import re + template = re.sub(r'%(\w)', r'%(\1)s', template) + + return template % v + def main(argv): parser = optparse.OptionParser() @@ -165,6 +190,11 @@ def main(argv): action="store", dest="toc_pickle", help="pickle to use for reading and writing the TOC", default=default) + default = '%A - %d/%t. %a - %n' + parser.add_option('', '--track-template', + action="store", dest="track_template", + help="template for track file naming (default %s)" % default, + default=default) options, args = parser.parse_args(argv[1:]) @@ -198,14 +228,16 @@ def main(argv): metadata = musicbrainz(itable.getMusicBrainzDiscId()) for i, track in enumerate(itable.tracks): - path = 'track%02d.wav' % (i + 1) - if metadata: - path = filterForPath('%s - %s.wav' % (metadata.tracks[i].artist, - metadata.tracks[i].title)) + path = getPath(options.track_template, metadata, i) + '.wav' + dirname = os.path.dirname(path) + if not os.path.exists(dirname): + os.makedirs(dirname) + # FIXME: optionally allow overriding reripping if not os.path.exists(path): print 'Ripping track %d: %s' % (i + 1, os.path.basename(path)) - t = cdparanoia.ReadVerifyTrackTask(path, ittoc, ittoc.getTrackStart(i + 1), + t = cdparanoia.ReadVerifyTrackTask(path, ittoc, + ittoc.getTrackStart(i + 1), ittoc.getTrackEnd(i + 1), offset=int(options.offset)) t.description = 'Reading Track %d' % (i + 1) @@ -214,7 +246,7 @@ def main(argv): print 'Checksums match for track %d' % (i + 1) # overlay this rip onto the IndexTable - itable.setFile(i + 1, 1, path, ittoc.getTrackLength(i + 1)) + itable.setFile(i + 1, 1, path, ittoc.getTrackLength(i + 1), i + 1) discName = 'morituri' if metadata: diff --git a/morituri/image/table.py b/morituri/image/table.py index 0b97f71..f2d8825 100644 --- a/morituri/image/table.py +++ b/morituri/image/table.py @@ -321,7 +321,7 @@ class IndexTable(object, log.Loggable): discId1[-1], discId1[-2], discId1[-3], len(self.tracks), discId1, discId2, self.getCDDBDiscId()) - def cue(self): + def cue(self, program='Morituri'): """ Dump our internal representation to a .cue file content. """ @@ -334,7 +334,8 @@ class IndexTable(object, log.Loggable): if key not in main and self.cdtext.has_key(key): lines.append(" %s %s" % (key, track.cdtext[key])) - lines.append('REM COMMENT "Morituri"') + lines.append('REM DISCID %s' % self.getCDDBDiscId().upper()) + lines.append('REM COMMENT "%s"' % program) if self.catalog: lines.append("CATALOG %s" % self.catalog) @@ -345,10 +346,18 @@ class IndexTable(object, log.Loggable): # add the first FILE line path = self.tracks[0].getFirstIndex().path + counter = self.tracks[0].getFirstIndex().counter currentPath = path lines.append('FILE "%s" WAVE' % path) for i, track in enumerate(self.tracks): + # if there is no index 0, but there is a new file, advance + # FILE line here + if not track.indexes.has_key(0): + index = track.indexes[1] + if index.counter != counter: + lines.append('FILE "%s" WAVE' % index.path) + counter = index.counter lines.append(" TRACK %02d %s" % (i + 1, 'AUDIO')) for key in CDTEXT_FIELDS: if track.cdtext.has_key(key): @@ -362,8 +371,9 @@ class IndexTable(object, log.Loggable): for number in indexes: index = track.indexes[number] - if index.path != currentPath: + if index.counter != counter: lines.append('FILE "%s" WAVE' % index.path) + counter = index.counter lines.append(" INDEX %02d %s" % (number, common.framesToMSF(index.relative))) @@ -398,7 +408,7 @@ class IndexTable(object, log.Loggable): break - def setFile(self, track, index, path, length): + def setFile(self, track, index, path, length, counter=None): """ Sets the given file as the source from the given index on. Will loop over all indexes that fall within the given length, @@ -406,18 +416,23 @@ class IndexTable(object, log.Loggable): Assumes all indexes have an absolute offset and will raise if not. """ + self.debug('setFile: track %d, index %d, path %s, ' + 'length %d, counter %d', track, index, path, length, counter) + t = self.tracks[track - 1] i = t.indexes[index] start = i.absolute assert start is not None, "index %r is missing absolute offset" % i - end = start + length + end = start + length - 1 # last sector that should come from this file # FIXME: check border conditions here, esp. wrt. toc's off-by-one bug while i.absolute <= end: - self.debug('Setting path and relative on track %d, index %d', - track, index) i.path = path i.relative = i.absolute - start + i.counter = counter + self.debug('Setting path %s, relative %d on ' + 'track %d, index %d, counter %d', + path, i.relative, track, index, counter) try: track, index = self.getNextTrackIndex(track, index) t = self.tracks[track - 1]