From eb89d3a850e80465c2efeb6198f75d9869a0b870 Mon Sep 17 00:00:00 2001 From: Adrian Sampson Date: Mon, 15 Sep 2014 18:37:09 -0700 Subject: [PATCH] The demise of get_query_sort (#953) The type tests now live where they ought to live. --- beets/dbcore/db.py | 9 ++++--- beets/dbcore/query.py | 3 --- beets/library.py | 58 ++++++++++++++++--------------------------- 3 files changed, 26 insertions(+), 44 deletions(-) diff --git a/beets/dbcore/db.py b/beets/dbcore/db.py index 2927186b4..be0e3a93a 100644 --- a/beets/dbcore/db.py +++ b/beets/dbcore/db.py @@ -25,7 +25,7 @@ import collections import beets from beets.util.functemplate import Template from beets.dbcore import types -from .query import MatchQuery, NullSort +from .query import MatchQuery, NullSort, TrueQuery class FormattedMapping(collections.Mapping): @@ -732,14 +732,15 @@ class Database(object): # Querying. - def _fetch(self, model_cls, query, sort=None): + def _fetch(self, model_cls, query=None, sort=None): """Fetch the objects of type `model_cls` matching the given query. The query may be given as a string, string sequence, a Query object, or None (to fetch everything). `sort` is an - optional Sort object. + `Sort` object. """ + query = query or TrueQuery() # A null query. + sort = sort or NullSort() # Unsorted. where, subvals = query.clause() - sort = sort or NullSort() order_by = sort.order_clause() sql = ("SELECT * FROM {0} WHERE {1} {2}").format( diff --git a/beets/dbcore/query.py b/beets/dbcore/query.py index 1f1a9a26a..0ca749bb5 100644 --- a/beets/dbcore/query.py +++ b/beets/dbcore/query.py @@ -654,6 +654,3 @@ class NullSort(Sort): """No sorting. Leave results unsorted.""" def sort(items): return items - - def __nonzero__(self): - return False diff --git a/beets/library.py b/beets/library.py index 59b177ac7..69f8a1eb8 100644 --- a/beets/library.py +++ b/beets/library.py @@ -968,27 +968,6 @@ def parse_query_string(s, model_cls): return parse_query_parts(parts, model_cls) -def get_query_sort(val, model_cls): - """Take a value which may be None, a query string, a query string - list, or a Query object, and return a suitable Query object and Sort - object. - - `model_cls` is the subclass of Model indicating which entity this - is a query for (i.e., Album or Item) and is used to determine which - fields are searched. - """ - if isinstance(val, basestring): - return parse_query_string(val, model_cls) - elif val is None: - return dbcore.query.TrueQuery(), None - elif isinstance(val, list) or isinstance(val, tuple): - return parse_query_parts(val, model_cls) - elif isinstance(val, dbcore.Query): - return val, None - else: - raise ValueError('query must be None or have type Query or str') - - # The Library: interface to the database. class Library(dbcore.Database): @@ -1050,30 +1029,35 @@ class Library(dbcore.Database): # Querying. - def _fetch(self, model_cls, query, sort_order=None): - """Parse a query and fetch. If a order specification is present in the - query string the sort_order argument is ignored. - """ - query, sort = get_query_sort(query, model_cls) - sort = sort or sort_order + def _fetch(self, model_cls, query, sort=None): + """Parse a query and fetch. If a order specification is present + in the query string the `sort` argument is ignored. + """ + # Parse the query, if necessary. + parsed_sort = None + if isinstance(query, basestring): + query, parsed_sort = parse_query_string(query, model_cls) + elif isinstance(query, (list, tuple)): + query, parsed_sort = parse_query_parts(query, model_cls) + + # Any non-null sort specified by the parsed query overrides the + # provided sort. + if parsed_sort and not isinstance(parsed_sort, dbcore.query.NullSort): + sort = parsed_sort return super(Library, self)._fetch( model_cls, query, sort ) - def albums(self, query=None, sort_order=None): - """Get a sorted list of :class:`Album` objects matching the - given sort order. If a order specification is present in the query - string the sort_order argument is ignored. + def albums(self, query=None, sort=None): + """Get :class:`Album` objects matching the query. """ - return self._fetch(Album, query, sort_order) + return self._fetch(Album, query, sort) - def items(self, query=None, sort_order=None): - """Get a sorted list of :class:`Item` objects matching the given - given sort order. If a order specification is present in the query - string the sort_order argument is ignored. + def items(self, query=None, sort=None): + """Get :class:`Item` objects matching the query. """ - return self._fetch(Item, query, sort_order) + return self._fetch(Item, query, sort) # Convenience accessors.