From 7dc458aaf54f46b2a57f0afd581bc37a72ca179f Mon Sep 17 00:00:00 2001 From: "nathdwek@laptop" Date: Mon, 21 Nov 2016 13:43:00 +0100 Subject: [PATCH] tagger: finish generator-decorator simplifications * Propagate generators upwards without realizing them * Use plugins.notify_info_yielded decorator --- beets/autotag/hooks.py | 32 ++++++++++++-------------------- beets/autotag/match.py | 16 ++++++++-------- 2 files changed, 20 insertions(+), 28 deletions(-) diff --git a/beets/autotag/hooks.py b/beets/autotag/hooks.py index a65316505..1a9aaf908 100644 --- a/beets/autotag/hooks.py +++ b/beets/autotag/hooks.py @@ -562,6 +562,7 @@ def tracks_for_id(track_id): yield t +@plugins.notify_info_yielded(u'albuminfo_received') def album_candidates(items, artist, album, va_likely): """Search for album matches. ``items`` is a list of Item objects that make up the album. ``artist`` and ``album`` are the respective @@ -569,51 +570,42 @@ def album_candidates(items, artist, album, va_likely): entered by the user. ``va_likely`` is a boolean indicating whether the album is likely to be a "various artists" release. """ - out = [] - # Base candidates if we have album and artist to match. if artist and album: try: - out.extend(mb.match_album(artist, album, len(items))) + for candidate in mb.match_album(artist, album, len(items)): + yield candidate except mb.MusicBrainzAPIError as exc: exc.log(log) # Also add VA matches from MusicBrainz where appropriate. if va_likely and album: try: - out.extend(mb.match_album(None, album, len(items))) + for candidate in mb.match_album(None, album, len(items)): + yield candidate except mb.MusicBrainzAPIError as exc: exc.log(log) # Candidates from plugins. - out.extend(plugins.candidates(items, artist, album, va_likely)) - - # Notify subscribed plugins about fetched album info - for a in out: - plugins.send(u'albuminfo_received', info=a) - - return out + for candidate in plugins.candidates(items, artist, album, va_likely): + yield candidate +@plugins.notify_info_yielded(u'trackinfo_received') def item_candidates(item, artist, title): """Search for item matches. ``item`` is the Item to be matched. ``artist`` and ``title`` are strings and either reflect the item or are specified by the user. """ - out = [] # MusicBrainz candidates. if artist and title: try: - out.extend(mb.match_track(artist, title)) + for candidate in mb.match_track(artist, title): + yield candidate except mb.MusicBrainzAPIError as exc: exc.log(log) # Plugin candidates. - out.extend(plugins.item_candidates(item, artist, title)) - - # Notify subscribed plugins about fetched track info - for i in out: - plugins.send(u'trackinfo_received', info=i) - - return out + for candidate in plugins.item_candidates(item, artist, title): + yield candidate diff --git a/beets/autotag/match.py b/beets/autotag/match.py index 5585c7f5c..493fd20c9 100644 --- a/beets/autotag/match.py +++ b/beets/autotag/match.py @@ -409,10 +409,10 @@ def tag_album(items, search_artist=None, search_album=None, # Search by explicit ID. if search_ids: - search_cands = [] for search_id in search_ids: log.debug(u'Searching for album ID: {0}', search_id) - search_cands.extend(hooks.albums_for_id(search_id)) + for id_candidate in hooks.albums_for_id(search_id): + _add_candidate(items, candidates, id_candidate) # Use existing metadata or text search. else: @@ -444,13 +444,13 @@ def tag_album(items, search_artist=None, search_album=None, log.debug(u'Album might be VA: {0}', va_likely) # Get the results from the data sources. - search_cands = hooks.album_candidates(items, search_artist, - search_album, va_likely) - - log.debug(u'Evaluating {0} candidates.', len(search_cands)) - for info in search_cands: - _add_candidate(items, candidates, info) + for matched_candidate in hooks.album_candidates(items, + search_artist, + search_album, + va_likely): + _add_candidate(items, candidates, matched_candidate) + log.debug(u'Evaluating {0} candidates.', len(candidates)) # Sort and get the recommendation. candidates = _sort_candidates(candidates.values()) rec = _recommendation(candidates)