Skip to content

Commit

Permalink
Refactor and rename _is_valid() helper
Browse files Browse the repository at this point in the history
  • Loading branch information
JOJ0 committed Jan 12, 2025
1 parent 042788e commit 1e14904
Showing 1 changed file with 12 additions and 13 deletions.
25 changes: 12 additions & 13 deletions beetsplug/lastgenre/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]]
Expand All @@ -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"]:
Expand All @@ -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.

Expand Down Expand Up @@ -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])

Expand Down

0 comments on commit 1e14904

Please sign in to comment.