diff --git a/beetsplug/lastgenre/__init__.py b/beetsplug/lastgenre/__init__.py index cd8401d2f6..9508d3f507 100644 --- a/beetsplug/lastgenre/__init__.py +++ b/beetsplug/lastgenre/__init__.py @@ -322,16 +322,51 @@ def _dedup_genres(self, genres, whitelist_only=True): else: return deduplicate([g for g in genres]) + def _combine_and_label_genres( + self, new_genres: str, keep_genres: list, separator: str, log_label: str + ) -> tuple: + """Combines genres and returns them with a logging label. + + Parameters: + new_genres (str): The new genre string result to process. + keep_genres (list): Existing genres to combine with new ones + separator (str): Separator used to split and join genre strings. + log_label (str): A label (like "track", "album") we possibly + combine with a prefix. For example resulting in something like + "keep + track" or just "track". + + Returns: + tuple: A tuple containing the combined genre string and the "logging + label". + """ + if new_genres and keep_genres: + split_genres = new_genres.split(separator) + combined_genres = deduplicate(keep_genres + split_genres) + return separator.join(combined_genres), f"keep + {log_label}" + elif new_genres: + return new_genres, log_label + return "", log_label + def _get_genre(self, obj): - """Get the genre string for an Album or Item object based on - self.sources. Return a `(genre, source)` pair. The - prioritization order is: + """Get the final genre string for an Album or Item object + + `self.sources` specifies allowed genre sources, prioritized as follows: - track (for Items only) - album - artist - original - fallback - None + + Parameters: + obj: Either an Album or Item object. + + Returns: + tuple: A `(genre, label)` pair, where `label` is a string used for + logging that describes the result. For example, "keep + artist" + indicates that existing genres were combined with new last.fm + genres, while "artist" means only new last.fm genres are + included. """ separator = self.config["separator"].get() @@ -359,31 +394,25 @@ def _get_genre(self, obj): # Track genre (for Items only). if isinstance(obj, library.Item) and "track" in self.sources: - result = self.fetch_track_genre(obj) - if result and keep_genres: - results = result.split(separator) - combined_genres = deduplicate(keep_genres + results) - return separator.join(combined_genres), "keep + track" - elif result: - return result, "track" + new_genres = self.fetch_track_genre(obj) + return self._combine_and_label_genres( + new_genres, keep_genres, separator, "track" + ) # Album genre. if "album" in self.sources: - result = self.fetch_album_genre(obj) - if result and keep_genres: - results = result.split(separator) - combined_genres = deduplicate(keep_genres + results) - return separator.join(combined_genres), "keep + album" - elif result: - return result, "album" + new_genres = self.fetch_album_genre(obj) + return self._combine_and_label_genres( + new_genres, keep_genres, separator, "album" + ) # Artist (or album artist) genre. if "artist" in self.sources: - result = None + new_genres = None if isinstance(obj, library.Item): - result = self.fetch_artist_genre(obj) + new_genres = self.fetch_artist_genre(obj) elif obj.albumartist != config["va_name"].as_str(): - result = self.fetch_album_artist_genre(obj) + new_genres = self.fetch_album_artist_genre(obj) else: # For "Various Artists", pick the most popular track genre. item_genres = [] @@ -396,14 +425,11 @@ def _get_genre(self, obj): if item_genre: item_genres.append(item_genre) if item_genres: - result, _ = plurality(item_genres) - - if result and keep_genres: - results = result.split(separator) - combined_genres = deduplicate(keep_genres + results) - return separator.join(combined_genres), "keep + artist" - elif result: - return result, "artist" + new_genres, _ = plurality(item_genres) + + return self._combine_and_label_genres( + new_genres, keep_genres, separator, "artist" + ) # Filter the existing genre. if obj.genre: