diff --git a/morituri/common/program.py b/morituri/common/program.py index 787914e..5c49d86 100644 --- a/morituri/common/program.py +++ b/morituri/common/program.py @@ -180,7 +180,8 @@ class Program(log.Loggable): def saveRipResult(self): self._presult.persist() - def getPath(self, outdir, template, mbdiscid, i, profile=None): + def getPath(self, outdir, template, mbdiscid, i, profile=None, + disambiguate=False): """ Based on the template, get a complete path for the given track, minus extension. @@ -213,6 +214,8 @@ class Program(log.Loggable): v['d'] = mbdiscid # fallback for title v['r'] = 'unknown' v['R'] = 'Unknown' + v['B'] = '' # barcode + v['C'] = '' # catalog number v['x'] = profile and profile.extension or 'unknown' v['X'] = v['x'].upper() @@ -229,6 +232,8 @@ class Program(log.Loggable): v['A'] = filterForPath(self.metadata.artist) v['S'] = filterForPath(self.metadata.sortName) v['d'] = filterForPath(self.metadata.title) + v['B'] = self.metadata.barcode + v['C'] = self.metadata.catalogNumber if self.metadata.releaseType: v['R'] = self.metadata.releaseType v['r'] = self.metadata.releaseType.lower() @@ -245,10 +250,24 @@ class Program(log.Loggable): # htoa defaults to disc's artist v['a'] = filterForPath(self.metadata.artist) + # when disambiguating, use catalogNumber then barcode + if disambiguate: + templateParts = list(os.path.split(template)) + if self.metadata.catalogNumber: + templateParts[-2] += ' (%s)' % self.metadata.catalogNumber + elif self.metadata.barcode: + templateParts[-2] += ' (%s)' % self.metadata.barcode + template = os.path.join(*templateParts) + self.debug('Disambiguated template to %r' % template) + import re template = re.sub(r'%(\w)', r'%(\1)s', template) - return os.path.join(outdir, template % v) + ret = os.path.join(outdir, template % v) + + + + return ret def getCDDB(self, cddbdiscid): """ diff --git a/morituri/rip/cd.py b/morituri/rip/cd.py index 79ed8f7..436b6ba 100644 --- a/morituri/rip/cd.py +++ b/morituri/rip/cd.py @@ -22,6 +22,7 @@ import os import math +import glob import gobject gobject.threads_init() @@ -256,13 +257,31 @@ Log files will log the path to tracks relative to this directory. self.program.outdir = self.options.output_directory.decode('utf-8') self.program.result.offset = int(self.options.offset) - ### write disc files - discName = self.program.getPath(self.program.outdir, self.options.disc_template, - self.mbdiscid, 0, profile=profile) - dirname = os.path.dirname(discName) - if not os.path.exists(dirname): - self.stdout.write("Creating output directory %s\n" % dirname) - os.makedirs(dirname) + ### write disc files + disambiguate = False + while True: + discName = self.program.getPath(self.program.outdir, self.options.disc_template, + self.mbdiscid, 0, profile=profile, disambiguate=disambiguate) + dirname = os.path.dirname(discName) + if os.path.exists(dirname): + self.stdout.write("Output directory %s already exists\n" % + dirname) + logs = glob.glob(os.path.join(dirname, '*.log')) + if logs: + self.stdout.write("Output directory %s is a finished rip\n" % + dirname) + if not disambiguate: + disambiguate = True + continue + return + else: + break + + else: + self.stdout.write("Creating output directory %s\n" % dirname) + os.makedirs(dirname) + break + # FIXME: say when we're continuing a rip # FIXME: disambiguate if the pre-existing rip is different @@ -281,7 +300,7 @@ Log files will log the path to tracks relative to this directory. trackResult.filename) path = self.program.getPath(self.program.outdir, self.options.track_template, - self.mbdiscid, number, profile=profile) + '.' + profile.extension + self.mbdiscid, number, profile=profile, disambiguate=disambiguate) + '.' + profile.extension self.debug('ripIfNotRipped: path %r' % path) trackResult.number = number @@ -388,7 +407,7 @@ Log files will log the path to tracks relative to this directory. ### write disc files discName = self.program.getPath(self.program.outdir, self.options.disc_template, - self.mbdiscid, 0, profile=profile) + self.mbdiscid, 0, profile=profile, disambiguate=disambiguate) dirname = os.path.dirname(discName) if not os.path.exists(dirname): os.makedirs(dirname) @@ -419,7 +438,7 @@ Log files will log the path to tracks relative to this directory. continue path = self.program.getPath(self.program.outdir, self.options.track_template, - self.mbdiscid, i + 1, profile=profile) + '.' + profile.extension + self.mbdiscid, i + 1, profile=profile, disambiguate=disambiguate) + '.' + profile.extension writeFile(handle, path, self.itable.getTrackLength(i + 1) / common.FRAMES_PER_SECOND)