From 1b0f49fc3bcc3a528374f4bf55e2005959824289 Mon Sep 17 00:00:00 2001 From: J0J0 Todos Date: Thu, 29 Dec 2022 14:26:02 +0100 Subject: [PATCH] Simplify MetadataSourcePlugin.get_artist method - Originally a list of "artists" was generated and joined together to a final string later. - This simplifies by concatinating to a final string within the main loop. - Also this commit gets rid of a mysterious replacement code (` ,' -> ',') --- beets/plugins.py | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/beets/plugins.py b/beets/plugins.py index aa62d7003..fe8bb532a 100644 --- a/beets/plugins.py +++ b/beets/plugins.py @@ -684,9 +684,8 @@ class MetadataSourcePlugin(metaclass=abc.ABCMeta): :rtype: str """ artist_id = None - artist_names = [] - joined = False - for artist in artists: + artist_string = "" + for idx, artist in enumerate(artists): if not artist_id: artist_id = artist[id_key] name = artist[name_key] @@ -695,17 +694,14 @@ class MetadataSourcePlugin(metaclass=abc.ABCMeta): # Move articles to the front. name = re.sub(r'^(.*?), (a|an|the)$', r'\2 \1', name, flags=re.I) # Use a join keyword if requested and available. - if join_key and artist.get(join_key, None): - name += " " + artist[join_key] - joined = True - artist_names.append(name) - # Concat using spaces only if join_key was passed and the join_key was - # ever found. Otherwise join comma-separated. - if join_key and joined is True: - artist = ' '.join(artist_names) or None - else: - artist = ', '.join(artist_names).replace(' ,', ',') or None - return artist, artist_id + if idx < (len(artists) - 1): # Skip joining on last. + if join_key and artist.get(join_key, None): + name += f" {artist[join_key]} " + else: + name += ', ' + artist_string += name + + return artist_string, artist_id def _get_id(self, url_type, id_): """Parse an ID from its URL if necessary.