Пример #1
0
def parse_query_string(s, model_cls):
    """Given a beets query string, return the `Query` and `Sort` they
    represent.

    The string is split into components using shell-like syntax.
    """
    assert isinstance(s, unicode), u"Query is not unicode: {0!r}".format(s)
    try:
        parts = util.shlex_split(s)
    except ValueError as exc:
        raise dbcore.InvalidQueryError(s, exc)
    return parse_query_parts(parts, model_cls)
Пример #2
0
def parse_query_string(s, model_cls):
    """Given a beets query string, return the `Query` and `Sort` they
    represent.

    The string is split into components using shell-like syntax.
    """
    # A bug in Python < 2.7.3 prevents correct shlex splitting of
    # Unicode strings.
    # http://bugs.python.org/issue6988
    if isinstance(s, unicode):
        s = s.encode('utf8')
    try:
        parts = [p.decode('utf8') for p in shlex.split(s)]
    except ValueError as exc:
        raise dbcore.InvalidQueryError(s, exc)
    return parse_query_parts(parts, model_cls)
Пример #3
0
    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.
        try:
            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)
        except dbcore.query.InvalidQueryArgumentTypeError as exc:
            raise dbcore.InvalidQueryError(query, exc)

        # 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)