class AnyTrackQuery(Query): """Query albums with at least one track matching a sub-query.""" _cached_results = {} 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 clause(self): return self.album_query.clause() def match(self, item): return self.album_query.match(item) @classmethod def clear_cache(cls): if cls._cached_results: cls._cached_results = {}
class AlbumQuery(Query): """Query items whose album matches a sub-query.""" _cached_album_results = {} 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 clause(self): return self.item_query.clause() def match(self, item): return self.item_query.match(item) @classmethod def clear_cache(cls): if cls._cached_album_results: cls._cached_album_results = {}
class AllTrackQuery(Query): """Query albums with at least one track matching a sub-query.""" _cached_results = {} 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 clause(self): return self.album_query.clause() def match(self, item): return self.album_query.match(item) @classmethod def clear_cache(cls): if cls._cached_results: cls._cached_results = {}