diff --git a/beetsplug/lastgenre/__init__.py b/beetsplug/lastgenre/__init__.py index 4374310ba..2f660206e 100644 --- a/beetsplug/lastgenre/__init__.py +++ b/beetsplug/lastgenre/__init__.py @@ -138,9 +138,18 @@ class LastGenrePlugin(plugins.BeetsPlugin): # Read the genres tree for canonicalization if enabled. self.c14n_branches = [] c14n_filename = self.config['canonical'].get() - if c14n_filename in (True, ''): # Default tree. + self.canonicalize = c14n_filename is not False + + # Default tree + if c14n_filename in (True, ''): c14n_filename = C14N_TREE + elif not self.canonicalize and self.config['prefer_specific'].get(): + # prefer_specific requires a tree, load default tree + c14n_filename = C14N_TREE + + # Read the tree if c14n_filename: + self._log.debug('Loading canonicalization tree {0}', c14n_filename) c14n_filename = normpath(c14n_filename) with codecs.open(c14n_filename, 'r', encoding='utf-8') as f: genres_tree = yaml.load(f) @@ -186,7 +195,7 @@ class LastGenrePlugin(plugins.BeetsPlugin): return None count = self.config['count'].get(int) - if self.c14n_branches: + if self.canonicalize: # Extend the list to consider tags parents in the c14n tree tags_all = [] for tag in tags: diff --git a/docs/changelog.rst b/docs/changelog.rst index bb21f21be..640add8fa 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -23,9 +23,12 @@ Fixes: are missing. Thanks to :user:`autrimpo`. :bug:`2757` -* Display the artist credit when matching albums if the ref:`artist_credit` +* Display the artist credit when matching albums if the :ref:`artist_credit` configuration option is set. :bug:`2953` +* LastGenre: Allow to set the configuration option ``prefer_specific`` + without setting ``canonical``. + :bug:`2973` 1.4.7 (May 29, 2018) diff --git a/test/test_lastgenre.py b/test/test_lastgenre.py index a70c65ca1..a8e6318b7 100644 --- a/test/test_lastgenre.py +++ b/test/test_lastgenre.py @@ -36,9 +36,11 @@ class LastGenrePluginTest(unittest.TestCase, TestHelper): def tearDown(self): self.teardown_beets() - def _setup_config(self, whitelist=False, canonical=False, count=1): + def _setup_config(self, whitelist=False, canonical=False, count=1, + prefer_specific=False): config['lastgenre']['canonical'] = canonical config['lastgenre']['count'] = count + config['lastgenre']['prefer_specific'] = prefer_specific if isinstance(whitelist, (bool, six.string_types)): # Filename, default, or disabled. config['lastgenre']['whitelist'] = whitelist @@ -136,6 +138,21 @@ class LastGenrePluginTest(unittest.TestCase, TestHelper): self.assertEqual(self.plugin._resolve_genres(['iota blues']), u'') + def test_prefer_specific_loads_tree(self): + """When prefer_specific is enabled but canonical is not the + tree still has to be loaded. + """ + self._setup_config(prefer_specific=True, canonical=False) + self.assertNotEqual(self.plugin.c14n_branches, []) + + def test_prefer_specific_without_canonical(self): + """Prefer_specific works without canonical. + """ + self._setup_config(prefer_specific=True, canonical=False, count=4) + self.assertEqual(self.plugin._resolve_genres( + ['math rock', 'post-rock']), + u'Post-Rock, Math Rock') + def test_no_duplicate(self): """Remove duplicated genres. """