diff --git a/whipper/common/program.py b/whipper/common/program.py index 5f084b8..ea223de 100644 --- a/whipper/common/program.py +++ b/whipper/common/program.py @@ -170,6 +170,14 @@ class Program: def saveRipResult(self): self._presult.persist() + def addDisambiguation(self, template_part, metadata): + "Add disambiguation to template path part string." + if metadata.catalogNumber: + template_part += ' (%s)' % metadata.catalogNumber + elif metadata.barcode: + template_part += ' (%s)' % metadata.barcode + return template_part + def getPath(self, outdir, template, mbdiscid, i, disambiguate=False): """ Based on the template, get a complete path for the given track, @@ -242,11 +250,15 @@ class Program: # 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 + templateParts = template.split(os.sep) + # Find the section of the template with the release name + for i, part in enumerate(templateParts): + if "%d" in part: + templateParts[i] = self.addDisambiguation(part, self.metadata) # noqa: E501 + break + else: + # No parts of the template contain the release + templateParts[-1] = self.addDisambiguation(templateParts[-1], self.metadata) # noqa: E501 template = os.path.join(*templateParts) logger.debug('Disambiguated template to %r' % template) diff --git a/whipper/test/test_common_program.py b/whipper/test/test_common_program.py index 170bbcc..dfa1867 100644 --- a/whipper/test/test_common_program.py +++ b/whipper/test/test_common_program.py @@ -119,3 +119,87 @@ class PathTestCase(unittest.TestCase): path = prog.getPath(u'/tmp', u'%A/%d', 'mbdiscid', 0) self.assertEquals(path, u'/tmp/Jeff Buckley/Grace') + + def testDisambiguateOnRelease(self): + """Test that disambiguation gets placed in the same part of the path + as the release name. + + See https://github.com/JoeLametta/whipper/issues/127""" + prog = program.Program(config.Config()) + md = mbngs.DiscMetadata() + md.artist = 'Guy Davis' + md.sortName = 'Davis, Guy' + md.title = 'Call Down the Thunder' + md.release = '1996' + md.catalogNumber = 'RHR CD 89' + prog.metadata = md + templates = { + u'%A/%d - %y': u'Guy Davis/Call Down the Thunder - 1996 (RHR CD 89)', # noqa: E501 + u'%A - %d - %y': u'Guy Davis - Call Down the Thunder - 1996 (RHR CD 89)', # noqa: E501 + u'%A/%y/%d': u'Guy Davis/1996/Call Down the Thunder (RHR CD 89)', + u'%y/%d/%A': u'1996/Call Down the Thunder (RHR CD 89)/Guy Davis', + u'%d/%A/%y': u'Call Down the Thunder (RHR CD 89)/Guy Davis/1996', + } + + for template, expected_path in templates.iteritems(): + path = prog.getPath(u'/tmp', template, 'mbdiscid', 0, disambiguate=True) # noqa: E501 + self.assertEquals(path, u'/tmp/' + expected_path) + + def testDisambiguateOnReleaseOnlyOnce(self): + """Test that disambiguation gets added only once.""" + prog = program.Program(config.Config()) + md = mbngs.DiscMetadata() + md.artist = 'Guy Davis' + md.sortName = 'Davis, Guy' + md.title = 'Call Down the Thunder' + md.release = '1996' + md.catalogNumber = 'RHR CD 89' + prog.metadata = md + template = u'%A/%d - %y/%d/%d' + + path = prog.getPath(u'/tmp', template, 'mbdiscid', 0, disambiguate=True) # noqa: E501 + self.assertEquals(path, + u'/tmp/Guy Davis/Call Down the Thunder - 1996 (RHR CD 89)/Call Down the Thunder/Call Down the Thunder') # noqa: E501 + + def testDisambiguateOnNoReleaseTitle(self): + """Test that disambiguation gets added even if there's no release + title in the template.""" + prog = program.Program(config.Config()) + md = mbngs.DiscMetadata() + md.artist = 'Guy Davis' + md.sortName = 'Davis, Guy' + md.title = 'Call Down the Thunder' + md.release = '1996' + md.catalogNumber = 'RHR CD 89' + prog.metadata = md + templates = { + u'%A/%y': u'Guy Davis/1996 (RHR CD 89)', + u'%A - %y': u'Guy Davis - 1996 (RHR CD 89)', + u'%y/%A': u'1996/Guy Davis (RHR CD 89)', + } + + for template, expected_path in templates.iteritems(): + path = prog.getPath(u'/tmp', template, 'mbdiscid', 0, disambiguate=True) # noqa: E501 + self.assertEquals(path, u'/tmp/' + expected_path) + + def testAddDisambiguationUnitTest(self): + """Unit test for Program.addDisambiguation().""" + prog = program.Program(config.Config()) + md = mbngs.DiscMetadata() + + # No relevant disambiguation metadata + self.assertEquals( + prog.addDisambiguation(u'Test', md), + u'Test') + + # Only barcode available + md.barcode = '033651008927' + self.assertEquals( + prog.addDisambiguation(u'Test', md), + u'Test (033651008927)') + + # Both catalog number and barcode available + md.catalogNumber = 'RHR CD 89' + self.assertEquals( + prog.addDisambiguation(u'Test', md), + u'Test (RHR CD 89)')