Skip to content

Commit acaf88a

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 3d0414c commit acaf88a

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,
@@ -401,7 +402,19 @@ def _try_resolve_stage(
401402
genres = self._get_existing_genres(obj)
402403

403404
if genres and not self.config["force"]:
404-
# Without force pre-populated tags are returned as-is.
405+
# Without force, but cleanup_existing enabled, we attempt
406+
# to canonicalize pre-populated tags before returning them.
407+
# If none are found, we use the fallback (if set).
408+
if self.config["cleanup_existing"]:
409+
keep_genres = [g.lower() for g in genres]
410+
if result := _try_resolve_stage("original", keep_genres, []):
411+
return result
412+
413+
# Return fallback string (None if not set).
414+
return self.config["fallback"].get(), "fallback"
415+
416+
# If cleanup_existing is not set, the pre-populated tags are
417+
# returned as-is.
405418
label = "keep any, no-force"
406419
if isinstance(obj, library.Item):
407420
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
@@ -83,6 +83,8 @@ been dropped.
8383
New features
8484
~~~~~~~~~~~~
8585

86+
- :doc:`plugins/lastgenre`: Added ``cleanup_existing`` configuration flag to
87+
allow whitelist canonicalization of existing genres.
8688
- :doc:`plugins/fetchart`: Added config setting for a fallback cover art image.
8789
- :doc:`plugins/ftintitle`: Added argument for custom feat. words in ftintitle.
8890
- :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
@@ -171,6 +171,11 @@ 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+
Setting this to ``yes`` will result in cleanup of existing genres. That
176+
includes canonicalization and whitelisting, if enabled. If no matching genre
177+
can be determined, the ``fallback`` is used instead. Default: ``no``
178+
(disabled).
174179
- **count**: Number of genres to fetch. Default: 1
175180
- **fallback**: A string to use as a fallback genre when no genre is found
176181
``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
@@ -304,6 +304,30 @@ def test_sort_by_depth(self):
304304
},
305305
("Jazzin", "album, any"),
306306
),
307+
# 5.1 - Canonicalize original genre when force is **off** and
308+
# whitelist, canonical and cleanup_existing are on.
309+
# "Cosmic Disco" is not in the default whitelist, thus gets resolved "up" in the
310+
# tree to "Disco" and "Electronic".
311+
(
312+
{
313+
"force": False,
314+
"keep_existing": False,
315+
"source": "artist",
316+
"whitelist": True,
317+
"canonical": True,
318+
"cleanup_existing": True,
319+
"prefer_specific": False,
320+
"count": 10,
321+
},
322+
"Cosmic Disco",
323+
{
324+
"artist": [],
325+
},
326+
(
327+
"Disco, Electronic",
328+
"keep + original, whitelist",
329+
),
330+
),
307331
# 6 - fallback to next stages until found
308332
(
309333
{

0 commit comments

Comments
 (0)