From 1e1490452f3f17248fd5a0c4cabfa71157b34271 Mon Sep 17 00:00:00 2001 From: J0J0 Todos Date: Sat, 11 Jan 2025 10:44:46 +0100 Subject: [PATCH] Refactor and rename _is_valid() helper --- beetsplug/lastgenre/__init__.py | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/beetsplug/lastgenre/__init__.py b/beetsplug/lastgenre/__init__.py index 34463b895c..c6824e5628 100644 --- a/beetsplug/lastgenre/__init__.py +++ b/beetsplug/lastgenre/__init__.py @@ -225,7 +225,7 @@ def _resolve_genres(self, tags): parents = [ x for x in find_parents(tag, self.c14n_branches) - if self._is_allowed(x) + if self._is_valid(x) ] else: parents = [find_parents(tag, self.c14n_branches)[-1]] @@ -248,7 +248,7 @@ def _resolve_genres(self, tags): # c14n only adds allowed genres but we may have had forbidden genres in # the original tags list - return [x for x in tags if self._is_allowed(x)] + return [x for x in tags if self._is_valid(x)] def _format_tag(self, tag): if self.config["title_case"]: @@ -262,16 +262,15 @@ def fetch_genre(self, lastfm_obj): min_weight = self.config["min_weight"].get(int) return self._tags_for(lastfm_obj, min_weight) - def _is_allowed(self, genre): - """Returns True if genre in whitelist or whitelist disabled.""" - allowed = False - if genre is None: - allowed = False - elif not self.whitelist: - allowed = True - elif genre.lower() in self.whitelist: - allowed = True - return allowed + def _is_valid(self, genre) -> bool: + """Check if the genre is valid. + + Depending on the whitelist property, valid means a genre is in the + whitelist or any genre is allowed. + """ + if genre and (not self.whitelist or genre.lower() in self.whitelist): + return True + return False # Cached last.fm entity lookups. @@ -341,7 +340,7 @@ def _dedup_genres(self, genres, whitelist_only=False): Makes sure genres are handled all lower case.""" if whitelist_only: return deduplicate( - [g.lower() for g in genres if self._is_allowed(g)] + [g.lower() for g in genres if self._is_valid(g)] ) return deduplicate([g.lower() for g in genres])