def test_build_queries(self): spl = SmartPlaylistPlugin() self.assertEqual(spl._matched_playlists, None) self.assertEqual(spl._unmatched_playlists, None) config['smartplaylist']['playlists'].set([]) spl.build_queries() self.assertEqual(spl._matched_playlists, set()) self.assertEqual(spl._unmatched_playlists, set()) config['smartplaylist']['playlists'].set([ {'name': 'foo', 'query': 'FOO foo'}, {'name': 'bar', 'album_query': ['BAR bar1', 'BAR bar2']}, {'name': 'baz', 'query': 'BAZ baz', 'album_query': 'BAZ baz'} ]) spl.build_queries() self.assertEqual(spl._matched_playlists, set()) foo_foo = parse_query_string('FOO foo', Item) baz_baz = parse_query_string('BAZ baz', Item) baz_baz2 = parse_query_string('BAZ baz', Album) bar_bar = OrQuery((parse_query_string('BAR bar1', Album)[0], parse_query_string('BAR bar2', Album)[0])) self.assertEqual(spl._unmatched_playlists, set([ ('foo', foo_foo, (None, None)), ('baz', baz_baz, baz_baz2), ('bar', (None, None), (bar_bar, None)), ]))
def __init__(self, query_string): self.query_string = query_string if self.query_string in self._cached_results: albums = self.albums = self._cached_results[self.query_string] else: query, _ = parse_query_string(query_string, Item) album_only_query = AndQuery( [query, NotQuery(MatchQuery(u'album_id', 0))]) items = self.lib.items(album_only_query) def item_album(i): return i.album_id items_by_album = sorted(items, key=item_album) grouped_items = groupby(items_by_album, item_album) albums = set() for album_id, items in grouped_items: items = list(items) album = items[0]._cached_album() all_items = album.items() if len(items) == len(all_items): albums.add(album_id) self._cached_results[self.query_string] = self.albums = albums self.album_query = OrQuery([MatchQuery('id', id) for id in albums])
def __init__(self, query_string): self.query_string = query_string if self.query_string in self._cached_results: albums = self.albums = self._cached_results[self.query_string] else: query, _ = parse_query_string(query_string, Item) album_only_query = AndQuery( [query, NotQuery(MatchQuery(u'album_id', 0))]) items = self.lib.items(album_only_query) albums = set(i.album_id for i in items) self._cached_results[self.query_string] = self.albums = albums self.album_query = OrQuery([MatchQuery('id', id) for id in albums])
def __init__(self, query_string): self.query_string = query_string if self.query_string in self._cached_album_results: albums = self.albums = self._cached_album_results[ self.query_string] else: query, _ = parse_query_string(query_string, Album) albums = self.lib.albums(query) self._cached_album_results[ self.query_string] = self.albums = albums self.item_query = OrQuery( [MatchQuery('album_id', album.id) for album in albums])
def build_queries(self): """ Instanciate queries for the playlists. Each playlist has 2 queries: one or items one for albums, each with a sort. We must also remember its name. _unmatched_playlists is a set of tuples (name, (q, q_sort), (album_q, album_q_sort)). sort may be any sort, or NullSort, or None. None and NullSort are equivalent and both eval to False. More precisely - it will be NullSort when a playlist query ('query' or 'album_query') is a single item or a list with 1 element - it will be None when there are multiple items i a query """ self._unmatched_playlists = set() self._matched_playlists = set() for playlist in self.config['playlists'].get(list): playlist_data = (playlist['name'], ) for key, Model in (('query', Item), ('album_query', Album)): qs = playlist.get(key) if qs is None: query_and_sort = None, None elif isinstance(qs, basestring): query_and_sort = parse_query_string(qs, Model) elif len(qs) == 1: query_and_sort = parse_query_string(qs[0], Model) else: # multiple queries and sorts queries, sorts = zip(*(parse_query_string(q, Model) for q in qs)) query = OrQuery(queries) final_sorts = [] for s in sorts: if s: if isinstance(s, MultipleSort): final_sorts += s.sorts else: final_sorts.append(s) if not final_sorts: sort = None elif len(final_sorts) == 1: sort, = final_sorts else: sort = MultipleSort(final_sorts) query_and_sort = query, sort playlist_data += (query_and_sort, ) self._unmatched_playlists.add(playlist_data)
def build_queries(self): """ Instantiate queries for the playlists. Each playlist has 2 queries: one or items one for albums, each with a sort. We must also remember its name. _unmatched_playlists is a set of tuples (name, (q, q_sort), (album_q, album_q_sort)). sort may be any sort, or NullSort, or None. None and NullSort are equivalent and both eval to False. More precisely - it will be NullSort when a playlist query ('query' or 'album_query') is a single item or a list with 1 element - it will be None when there are multiple items i a query """ self._unmatched_playlists = set() self._matched_playlists = set() for playlist in self.config['playlists'].get(list): if 'name' not in playlist: self._log.warning(u"playlist configuration is missing name") continue playlist_data = (playlist['name'], ) try: for key, model_cls in (('query', Item), ('album_query', Album)): qs = playlist.get(key) if qs is None: query_and_sort = None, None elif isinstance(qs, six.string_types): query_and_sort = parse_query_string(qs, model_cls) elif len(qs) == 1: query_and_sort = parse_query_string(qs[0], model_cls) else: # multiple queries and sorts queries, sorts = zip( *(parse_query_string(q, model_cls) for q in qs)) query = OrQuery(queries) final_sorts = [] for s in sorts: if s: if isinstance(s, MultipleSort): final_sorts += s.sorts else: final_sorts.append(s) if not final_sorts: sort = None elif len(final_sorts) == 1: sort, = final_sorts else: sort = MultipleSort(final_sorts) query_and_sort = query, sort playlist_data += (query_and_sort, ) except ParsingError as exc: self._log.warning(u"invalid query in playlist {}: {}", playlist['name'], exc) continue self._unmatched_playlists.add(playlist_data)