From fb038ecc3037495dc094ac879967256707dccfbd Mon Sep 17 00:00:00 2001 From: Adrian Sampson Date: Sun, 9 Mar 2014 13:22:34 -0700 Subject: [PATCH] changelog & style tweaks for album_query (#595) --- beetsplug/smartplaylist.py | 48 +++++++++++++++++++++----------------- docs/changelog.rst | 3 ++- 2 files changed, 29 insertions(+), 22 deletions(-) diff --git a/beetsplug/smartplaylist.py b/beetsplug/smartplaylist.py index ff8d0ed11..dae5561fb 100644 --- a/beetsplug/smartplaylist.py +++ b/beetsplug/smartplaylist.py @@ -27,27 +27,33 @@ import os database_changed = False -def query_from_parameter(lib, playlist, parameter, album=False): - if playlist.has_key(parameter): - # Parse quer(ies). If it's a list, join the queries with OR. - query_strings = playlist[parameter] - if not isinstance(query_strings, (list, tuple)): - query_strings = [query_strings] - model = library.Album if album else library.Item - query = dbcore.OrQuery( - [library.get_query(q, model) for q in query_strings] - ) - # Execute query, depending on type - if album: - result = [] - for album in lib.albums(query): - result.extend(album.items()) - return result - else: - return lib.items(query) - else: +def _items_for_query(lib, playlist, album=False): + """Get the matching items for a playlist's configured queries. + `album` indicates whether to process the item-level query or the + album-level query (if any). + """ + key = 'album_query' if album else 'query' + if key not in playlist: return [] + # Parse quer(ies). If it's a list, join the queries with OR. + query_strings = playlist[key] + if not isinstance(query_strings, (list, tuple)): + query_strings = [query_strings] + model = library.Album if album else library.Item + query = dbcore.OrQuery( + [library.get_query(q, model) for q in query_strings] + ) + + # Execute query, depending on type. + if album: + result = [] + for album in lib.albums(query): + result.extend(album.items()) + return result + else: + return lib.items(query) + def update_playlists(lib): ui.print_("Updating smart playlists...") @@ -59,8 +65,8 @@ def update_playlists(lib): for playlist in playlists: items = [] - items.extend(query_from_parameter(lib, playlist, 'album_query', True)) - items.extend(query_from_parameter(lib, playlist, 'query', False)) + items.extend(_items_for_query(lib, playlist, True)) + items.extend(_items_for_query(lib, playlist, False)) m3us = {} basename = playlist['name'].encode('utf8') diff --git a/docs/changelog.rst b/docs/changelog.rst index 25a29540a..28cdb4cf8 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -20,7 +20,8 @@ New stuff: * The importer interface now shows the URL for MusicBrainz matches. Thanks to johtso. * :doc:`/plugins/smartplaylist`: Playlists can now be generated from multiple - queries (combined with "or" logic). Thanks to brilnius. + queries (combined with "or" logic). Album-level queries are also now + possible. Thanks to brilnius. * :doc:`/plugins/echonest`: Echo Nest similarity now weights the tempo in better proportion to other metrics. Also, options were added to specify custom thresholds and output formats. Thanks to Adam M.