From eb8349178863776dbb8ea97c4b6ccfe1f51b14d9 Mon Sep 17 00:00:00 2001 From: J0J0 Todos Date: Thu, 27 Mar 2025 23:23:39 +0100 Subject: [PATCH] lastgenre: Fix "original fallback" conditions This was not thought through clearly before. It now behaves as follows which I suppose is least surprising to a user: - force is on, keep_existing is on, but the whitelist is DISABLED - no stage found anything on last.fm - fall back to the original genre If in this example the whitelist would be ENABLED, the behaviour changes: Only if the existing genre passes the whitelist test the original is kept. --- beetsplug/lastgenre/__init__.py | 9 ++++--- test/plugins/test_lastgenre.py | 43 ++++++++++++++++++++++++++++++++- 2 files changed, 47 insertions(+), 5 deletions(-) diff --git a/beetsplug/lastgenre/__init__.py b/beetsplug/lastgenre/__init__.py index 1a56d3779..30b44e187 100644 --- a/beetsplug/lastgenre/__init__.py +++ b/beetsplug/lastgenre/__init__.py @@ -441,11 +441,12 @@ class LastGenrePlugin(plugins.BeetsPlugin): label = f"keep + {label}" return self._format_and_stringify(resolved_genres), label - # Nothing found, leave original. - if obj.genre: - return obj.genre, "original fallback" + # Nothing found, leave original if configured and valid. + if obj.genre and self.config["keep_existing"]: + if not self.whitelist or self._is_valid(obj.genre.lower()): + return obj.genre, "original fallback" - # No original, return fallback string. + # Return fallback string. if fallback := self.config["fallback"].get(): return fallback, "fallback" diff --git a/test/plugins/test_lastgenre.py b/test/plugins/test_lastgenre.py index ad2dedf5b..49d219de9 100644 --- a/test/plugins/test_lastgenre.py +++ b/test/plugins/test_lastgenre.py @@ -292,7 +292,8 @@ class LastGenrePluginTest(BeetsTestCase): }, ("Unknown Genre, Jazz", "keep + artist, any"), ), - # 7 - fallback to any original when nothing found and whitelist disabled + # 7 - Keep the original genre when force and keep_existing are on, and + # whitelist is disabled ( { "force": True, @@ -303,6 +304,46 @@ class LastGenrePluginTest(BeetsTestCase): "canonical": False, "prefer_specific": False, }, + "any existing", + { + "track": None, + "album": None, + "artist": None, + }, + ("any existing", "original fallback"), + ), + # 7.1 - Keep the original genre when force and keep_existing are on, and + # whitelist is enabled, and genre is valid. + ( + { + "force": True, + "keep_existing": True, + "source": "track", + "whitelist": True, + "fallback": "fallback genre", + "canonical": False, + "prefer_specific": False, + }, + "Jazz", + { + "track": None, + "album": None, + "artist": None, + }, + ("Jazz", "original fallback"), + ), + # 7.2 - Return the configured fallback when force is on but + # keep_existing is not. + ( + { + "force": True, + "keep_existing": False, + "source": "track", + "whitelist": True, + "fallback": "fallback genre", + "canonical": False, + "prefer_specific": False, + }, "Jazz", { "track": None,