Skip to content

Commit 91dbea3

Browse files
committed
feat(lastgenre): Add fallback_original
Adds the `fallback_original` flag. When no genres can be found or no genres match the canonical whitelist, setting this to `yes` will result in existing genres to be preserved, instead of deleting them or setting them to `fallback`. This is for people that would rather have a non-canonicalized genre info than fully loose the existing genre data.
1 parent edbf737 commit 91dbea3

4 files changed

Lines changed: 56 additions & 5 deletions

File tree

beetsplug/lastgenre/__init__.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ def __init__(self) -> None:
106106
"min_weight": 10,
107107
"count": 1,
108108
"fallback": None,
109+
"fallback_original": False,
109110
"canonical": False,
110111
"cleanup_existing": False,
111112
"source": "album",
@@ -404,8 +405,14 @@ def _try_resolve_stage(
404405
if result := _try_resolve_stage("cleanup", keep_genres, []):
405406
return result
406407

407-
# Return fallback string (None if not set).
408-
return self.config["fallback"].get(), "fallback"
408+
# If configured, keep the original genre information if the genres
409+
# could not be resolved/cleaned up or nothing survived the whitelist.
410+
#
411+
# Otherwise return the fallback string (None if not set).
412+
if self.config["fallback_original"]:
413+
return genres, "fallback + original, no-force"
414+
else:
415+
return self.config["fallback"].get(), "fallback + no-force"
409416

410417
# If cleanup_existing is not set, the pre-populated tags are
411418
# returned as-is.
@@ -495,6 +502,11 @@ def _try_resolve_stage(
495502
):
496503
return result
497504

505+
# If configured, keep the original genre information if the genres
506+
# could not be resolved/cleaned up or nothing survived the whitelist.
507+
if genres and self.config["force"] and self.config["fallback_original"]:
508+
return genres, "fallback + original, force"
509+
498510
# Return fallback as a list.
499511
if fallback := self.config["fallback"].get():
500512
return [fallback], "fallback"

docs/changelog.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ Unreleased
1212
New features
1313
~~~~~~~~~~~~
1414

15+
- :doc:`plugins/lastgenre`: Added ``fallback_original`` configuration flag. This
16+
allows users to keep the original genre information, if no genres remain after
17+
canonicalization and whitelisting.
1518
- :doc:`plugins/lastgenre`: Added ``cleanup_existing`` configuration flag to
1619
allow whitelist canonicalization of existing genres.
1720
- Add native support for multiple genres per album/track. The ``genres`` field

docs/plugins/lastgenre.rst

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,12 +173,15 @@ file. The available options are:
173173
- **cleanup_existing**: This option only takes effect with ``force: no``,
174174
Setting this to ``yes`` will result in cleanup of existing genres. That
175175
includes canonicalization and whitelisting, if enabled. If no matching genre
176-
can be determined, the ``fallback`` is used instead. Default: ``no``
177-
(disabled).
176+
can be determined, the ``fallback`` is used instead, or ``fallback_original``,
177+
if set. Default: ``no`` (disabled).
178178
- **count**: Number of genres to fetch. Default: 1
179179
- **fallback**: A string to use as a fallback genre when no genre is found
180180
``or`` the original genre is not desired to be kept (``keep_existing: no``).
181181
You can use the empty string ``''`` to reset the genre. Default: None.
182+
- **fallback_original**: Setting this to ``yes`` will preserve existing genres
183+
instead of deleting them or setting them to ``fallback`` when no genres are
184+
found or when none match the canonical whitelist. Default: ``no``.
182185
- **force**: By default, lastgenre will fetch new genres for empty tags only,
183186
enable this option to always try to fetch new last.fm genres. Enable the
184187
``keep_existing`` option to combine existing and new genres. (see `Handling

test/plugins/test_lastgenre.py

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,40 @@ def config(config):
419419
},
420420
(["fallback genre"], "fallback"),
421421
),
422-
# fallback to fallback if no original
422+
# Keep the original genre when force is on, whitelist is enabled,
423+
# but no valid genre can be found and fallback_original is on.
424+
(
425+
{
426+
"force": True,
427+
"keep_existing": False,
428+
"source": "track",
429+
"whitelist": True,
430+
"canonical": True,
431+
"fallback_original": True,
432+
},
433+
["Jazzers-invalid"],
434+
{
435+
"track": None,
436+
"album": None,
437+
"artist": None,
438+
},
439+
(["Jazzers-invalid"], "fallback + original, force"),
440+
),
441+
# Keep the original genre when force is off, whitelist and cleanup_existing is
442+
# enabled, but no valid genre can be found and fallback_original is on.
443+
(
444+
{
445+
"force": False,
446+
"cleanup_existing": True,
447+
"whitelist": True,
448+
"fallback_original": True,
449+
"canonical": True,
450+
},
451+
["Jazzers-invalid"],
452+
{},
453+
(["Jazzers-invalid"], "fallback + original, no-force"),
454+
),
455+
# Fallback to fallback if no original
423456
(
424457
{
425458
"force": True,

0 commit comments

Comments
 (0)