lastgenre: tunelog better

This commit is contained in:
J0J0 Todos 2026-02-07 17:24:41 +01:00
parent 7aae9a9519
commit 18b6d30b49
3 changed files with 21 additions and 11 deletions

View file

@ -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,
)

View file

@ -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]:

View file

@ -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