Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions beetsplug/lastgenre/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ def __init__(self) -> None:
"min_weight": 10,
"count": 1,
"fallback": None,
"fallback_original": False,
"canonical": False,
"cleanup_existing": False,
"source": "album",
Expand Down Expand Up @@ -404,8 +405,14 @@ def _try_resolve_stage(
if result := _try_resolve_stage("cleanup", keep_genres, []):
return result

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

# If cleanup_existing is not set, the pre-populated tags are
# returned as-is.
Expand Down Expand Up @@ -495,6 +502,11 @@ def _try_resolve_stage(
):
return result

# If configured, keep the original genre information if the genres
# could not be resolved/cleaned up or nothing survived the whitelist.
if genres and self.config["force"] and self.config["fallback_original"]:
return genres, "fallback + original, force"

# Return fallback as a list.
if fallback := self.config["fallback"].get():
return [fallback], "fallback"
Expand Down
3 changes: 3 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ Unreleased
New features
~~~~~~~~~~~~

- :doc:`plugins/lastgenre`: Added ``fallback_original`` configuration flag. This
allows users to keep the original genre information, if no genres remain after
canonicalization and whitelisting.
- :doc:`plugins/lastgenre`: Added ``cleanup_existing`` configuration flag to
allow whitelist canonicalization of existing genres.
- Add native support for multiple genres per album/track. The ``genres`` field
Expand Down
7 changes: 5 additions & 2 deletions docs/plugins/lastgenre.rst
Original file line number Diff line number Diff line change
Expand Up @@ -173,12 +173,15 @@ file. The available options are:
- **cleanup_existing**: This option only takes effect with ``force: no``,
Setting this to ``yes`` will result in cleanup of existing genres. That
includes canonicalization and whitelisting, if enabled. If no matching genre
can be determined, the ``fallback`` is used instead. Default: ``no``
(disabled).
can be determined, the ``fallback`` is used instead, or ``fallback_original``,
if set. Default: ``no`` (disabled).
- **count**: Number of genres to fetch. Default: 1
- **fallback**: A string to use as a fallback genre when no genre is found
``or`` the original genre is not desired to be kept (``keep_existing: no``).
You can use the empty string ``''`` to reset the genre. Default: None.
- **fallback_original**: Setting this to ``yes`` will preserve existing genres
instead of deleting them or setting them to ``fallback`` when no genres are
found or when none match the canonical whitelist. Default: ``no``.
- **force**: By default, lastgenre will fetch new genres for empty tags only,
enable this option to always try to fetch new last.fm genres. Enable the
``keep_existing`` option to combine existing and new genres. (see `Handling
Expand Down
35 changes: 34 additions & 1 deletion test/plugins/test_lastgenre.py
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,40 @@ def config(config):
},
(["fallback genre"], "fallback"),
),
# fallback to fallback if no original
# Keep the original genre when force is on, whitelist is enabled,
# but no valid genre can be found and fallback_original is on.
(
{
"force": True,
"keep_existing": False,
"source": "track",
"whitelist": True,
"canonical": True,
"fallback_original": True,
},
["Jazzers-invalid"],
{
"track": None,
"album": None,
"artist": None,
},
(["Jazzers-invalid"], "fallback + original, force"),
),
# Keep the original genre when force is off, whitelist and cleanup_existing is
# enabled, but no valid genre can be found and fallback_original is on.
(
{
"force": False,
"cleanup_existing": True,
"whitelist": True,
"fallback_original": True,
"canonical": True,
},
["Jazzers-invalid"],
{},
(["Jazzers-invalid"], "fallback + original, no-force"),
),
# Fallback to fallback if no original
(
{
"force": True,
Expand Down
Loading