Skip to content

Commit 394a76a

Browse files
committed
feat(lastgenre): cleanup_existing
Introduce a new lastgenre `cleanup_existing` flag. It handles the case where canonicalization is desired on existing tags. The new logic triggers if: - `force`: False - `whitelist`: True - `canonical`: True - `cleanup_existing: True
1 parent a327c28 commit 394a76a

File tree

4 files changed

+43
-1
lines changed

4 files changed

+43
-1
lines changed

beetsplug/lastgenre/__init__.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ def __init__(self) -> None:
106106
"count": 1,
107107
"fallback": None,
108108
"canonical": False,
109+
"cleanup_existing": False,
109110
"source": "album",
110111
"force": False,
111112
"keep_existing": False,
@@ -399,7 +400,19 @@ def _try_resolve_stage(
399400
genres = self._get_existing_genres(obj)
400401

401402
if genres and not self.config["force"]:
402-
# Without force pre-populated tags are returned as-is.
403+
# Without force, but cleanup_existing enabled, we attempt
404+
# to canonicalize pre-populated tags before returning them.
405+
# If none are found, we use the fallback (if set).
406+
if self.config["cleanup_existing"]:
407+
keep_genres = [g.lower() for g in genres]
408+
if result := _try_resolve_stage("original", keep_genres, []):
409+
return result
410+
411+
# Return fallback string (None if not set).
412+
return self.config["fallback"].get(), "fallback"
413+
414+
# If cleanup_existing is not set, the pre-populated tags are
415+
# returned as-is.
403416
label = "keep any, no-force"
404417
if isinstance(obj, library.Item):
405418
return obj.get("genre", with_album=False), label

docs/changelog.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ been dropped.
1212

1313
New features:
1414

15+
- :doc:`plugins/lastgenre`: Added ``cleanup_existing`` configuration flag to
16+
allow whitelist canonicalization of existing genres.
1517
- :doc:`plugins/fetchart`: Added config setting for a fallback cover art image.
1618
- :doc:`plugins/ftintitle`: Added argument for custom feat. words in ftintitle.
1719
- :doc:`plugins/ftintitle`: Added album template value ``album_artist_no_feat``.

docs/plugins/lastgenre.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,10 @@ file. The available options are:
171171
- **canonical**: Use a canonicalization tree. Setting this to ``yes`` will use a
172172
built-in tree. You can also set it to a path, like the ``whitelist`` config
173173
value, to use your own tree. Default: ``no`` (disabled).
174+
- **cleanup_existing**: This option only takes effect with ``force: no``,
175+
``canonical: yes`` and ``whitelist: yes``. Setting this to ``yes`` will result
176+
in attempted canonicalization of existing genres. If this fails, the
177+
``fallback`` is used isntead. Default: ``no`` (disabled).
174178
- **count**: Number of genres to fetch. Default: 1
175179
- **fallback**: A string to use as a fallback genre when no genre is found
176180
``or`` the original genre is not desired to be kept (``keep_existing: no``).

test/plugins/test_lastgenre.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,29 @@ def test_sort_by_depth(self):
302302
},
303303
("Jazzin", "album, any"),
304304
),
305+
# 5.1 - Canonicalize original genre when force is **off** and
306+
# whitelist, canonical and cleanup_existing are on.
307+
# "Cosmic Disco" is not in the default whitelist, thus gets resolved "up" in the
308+
# tree to "Disco" and "Electronic".
309+
(
310+
{
311+
"force": False,
312+
"keep_existing": False,
313+
"source": "artist",
314+
"whitelist": True,
315+
"canonical": True,
316+
"cleanup_existing": True,
317+
"prefer_specific": False,
318+
},
319+
"Cosmic Disco",
320+
{
321+
"artist": [],
322+
},
323+
(
324+
"Disco, Electronic",
325+
"keep + original, whitelist",
326+
),
327+
),
305328
# 6 - fallback to next stages until found
306329
(
307330
{

0 commit comments

Comments
 (0)