From 18b6d30b497c2e26098fabfe860d136d4cd0f0f7 Mon Sep 17 00:00:00 2001 From: J0J0 Todos Date: Sat, 7 Feb 2026 17:24:41 +0100 Subject: [PATCH] lastgenre: tunelog better --- beetsplug/lastgenre/__init__.py | 9 ++++----- beetsplug/lastgenre/client.py | 5 +++-- beetsplug/lastgenre/utils.py | 18 ++++++++++++++---- 3 files changed, 21 insertions(+), 11 deletions(-) diff --git a/beetsplug/lastgenre/__init__.py b/beetsplug/lastgenre/__init__.py index f75fa0312..cec41bffa 100644 --- a/beetsplug/lastgenre/__init__.py +++ b/beetsplug/lastgenre/__init__.py @@ -36,7 +36,7 @@ from beets.library import Album, Item from beets.util import plurality, unique_list from .client import LastFmClient -from .utils import tunelog +from .utils import make_tunelog if TYPE_CHECKING: import optparse @@ -115,6 +115,7 @@ class LastGenrePlugin(plugins.BeetsPlugin): if self.config["auto"]: self.import_stages = [self.imported] + self._tunelog = make_tunelog(self._log) self.client = LastFmClient( self._log, self.config["min_weight"].get(int) ) @@ -378,15 +379,13 @@ class LastGenrePlugin(plugins.BeetsPlugin): new_genres = self.client.fetch_artist_genre(obj.albumartist) stage_label = "album artist" if not new_genres: - tunelog( - self._log, + self._tunelog( 'No album artist genre found for "{}", ' "trying multi-valued field...", obj.albumartist, ) for albumartist in obj.albumartists: - tunelog( - self._log, + self._tunelog( 'Fetching artist genre for "{}"', albumartist, ) diff --git a/beetsplug/lastgenre/client.py b/beetsplug/lastgenre/client.py index b7444cae8..21a0bff72 100644 --- a/beetsplug/lastgenre/client.py +++ b/beetsplug/lastgenre/client.py @@ -24,7 +24,7 @@ import pylast from beets import plugins -from .utils import tunelog +from .utils import make_tunelog if TYPE_CHECKING: from collections.abc import Callable @@ -49,6 +49,7 @@ class LastFmClient: The min_weight parameter filters tags by their minimum weight. """ self._log = log + self._tunelog = make_tunelog(log) self._min_weight = min_weight self._genre_cache: dict[str, list[str]] = {} @@ -121,7 +122,7 @@ class LastFmClient: self._genre_cache[key] = self.fetch_genre(method(*args_replaced)) genre = self._genre_cache[key] - tunelog(self._log, "last.fm (unfiltered) {} tags: {}", entity, genre) + self._tunelog("last.fm (unfiltered) {} tags: {}", entity, genre) return genre def fetch_album_genre(self, albumartist: str, albumtitle: str) -> list[str]: diff --git a/beetsplug/lastgenre/utils.py b/beetsplug/lastgenre/utils.py index 4c7846f23..7ae96e11a 100644 --- a/beetsplug/lastgenre/utils.py +++ b/beetsplug/lastgenre/utils.py @@ -22,10 +22,20 @@ from typing import TYPE_CHECKING, Any from beets import config if TYPE_CHECKING: + from collections.abc import Callable + from beets.logging import Logger -def tunelog(log: Logger, msg: str, *args: Any, **kwargs: Any) -> None: - """Log tuning messages at DEBUG level when verbosity level is high enough.""" - if config["verbose"].as_number() >= 3: - log.debug(msg, *args, **kwargs) +def make_tunelog(log: Logger) -> Callable[..., None]: + """Create a tunelog function bound to a specific logger. + + Returns a callable that logs tuning messages at DEBUG level when + verbosity is high enough. + """ + + def tunelog(msg: str, *args: Any, **kwargs: Any) -> None: + if config["verbose"].as_number() >= 3: + log.debug(msg, *args, **kwargs) + + return tunelog