Skip to content

Commit ef58634

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 - `cleanup_existing: True Depending on whether `whitelist: True` or `canonical: True`, the genres are then canonicalized and/or whitelisting is applied
1 parent 5d74d3e commit ef58634

File tree

4 files changed

+45
-1
lines changed

4 files changed

+45
-1
lines changed

beetsplug/lastgenre/__init__.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ def default_configuration() -> dict[str, Any]:
111111
"count": 1,
112112
"fallback": None,
113113
"canonical": False,
114+
"cleanup_existing": False,
114115
"source": "album",
115116
"force": False,
116117
"keep_existing": False,
@@ -397,7 +398,19 @@ def _try_resolve_stage(
397398
genres = self._get_existing_genres(obj)
398399

399400
if genres and not self.config["force"]:
400-
# Without force pre-populated tags are returned as-is.
401+
# Without force, but cleanup_existing enabled, we attempt
402+
# to canonicalize pre-populated tags before returning them.
403+
# If none are found, we use the fallback (if set).
404+
if self.config["cleanup_existing"]:
405+
keep_genres = [g.lower() for g in genres]
406+
if result := _try_resolve_stage("original", keep_genres, []):
407+
return result
408+
409+
# Return fallback string (None if not set).
410+
return self.config["fallback"].get(), "fallback"
411+
412+
# If cleanup_existing is not set, the pre-populated tags are
413+
# returned as-is.
401414
return genres, "keep any, no-force"
402415

403416
if self.config["force"]:

docs/changelog.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,8 @@ been dropped.
114114
New features
115115
~~~~~~~~~~~~
116116

117+
- :doc:`plugins/lastgenre`: Added ``cleanup_existing`` configuration flag to
118+
allow whitelist canonicalization of existing genres.
117119
- :doc:`plugins/fetchart`: Added config setting for a fallback cover art image.
118120
- :doc:`plugins/ftintitle`: Added argument for custom feat. words in ftintitle.
119121
- :doc:`plugins/ftintitle`: Added album template value ``album_artist_no_feat``.

docs/plugins/lastgenre.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,11 @@ file. The available options are:
170170
- **canonical**: Use a canonicalization tree. Setting this to ``yes`` will use a
171171
built-in tree. You can also set it to a path, like the ``whitelist`` config
172172
value, to use your own tree. Default: ``no`` (disabled).
173+
- **cleanup_existing**: This option only takes effect with ``force: no``,
174+
Setting this to ``yes`` will result in cleanup of existing genres. That
175+
includes canonicalization and whitelisting, if enabled. If no matching genre
176+
can be determined, the ``fallback`` is used instead. Default: ``no``
177+
(disabled).
173178
- **count**: Number of genres to fetch. Default: 1
174179
- **fallback**: A string to use as a fallback genre when no genre is found
175180
``or`` the original genre is not desired to be kept (``keep_existing: no``).

test/plugins/test_lastgenre.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,30 @@ def test_sort_by_depth(self):
306306
},
307307
(["Jazzin"], "album, any"),
308308
),
309+
# Canonicalize original genre when force is **off** and
310+
# whitelist, canonical and cleanup_existing are on.
311+
# "Cosmic Disco" is not in the default whitelist, thus gets resolved "up" in the
312+
# tree to "Disco" and "Electronic".
313+
(
314+
{
315+
"force": False,
316+
"keep_existing": False,
317+
"source": "artist",
318+
"whitelist": True,
319+
"canonical": True,
320+
"cleanup_existing": True,
321+
"prefer_specific": False,
322+
"count": 10,
323+
},
324+
["Cosmic Disco"],
325+
{
326+
"artist": [],
327+
},
328+
(
329+
["Disco", "Electronic"],
330+
"keep + original, whitelist",
331+
),
332+
),
309333
# fallback to next stages until found
310334
(
311335
{

0 commit comments

Comments
 (0)