Esempio n. 1
0
 def from_strings(cls, query_parts, default_fields=None, all_keys=ITEM_KEYS):
     """Creates a query from a list of strings in the format used by
     _parse_query_part. If default_fields are specified, they are the
     fields to be searched by unqualified search terms. Otherwise,
     all fields are searched for those terms.
     """
     subqueries = []
     for part in query_parts:
         res = cls._parse_query_part(part)
         if not res:
             continue
         key, pattern = res
         if key is None: # No key specified.
             if os.sep in pattern and 'path' in all_keys:
                 # This looks like a path.
                 subqueries.append(PathQuery(pattern))
             else:
                 # Match any field.
                 subqueries.append(AnySubstringQuery(pattern,
                                                     default_fields))
         elif key.lower() == 'comp': # a boolean field
             subqueries.append(BooleanQuery(key.lower(), pattern))
         elif key.lower() == 'path' and 'path' in all_keys:
             subqueries.append(PathQuery(pattern))
         elif key.lower() in all_keys: # ignore unrecognized keys
             subqueries.append(SubstringQuery(key.lower(), pattern))
         elif key.lower() == 'singleton':
             subqueries.append(SingletonQuery(util.str2bool(pattern)))
     if not subqueries: # no terms in query
         subqueries = [TrueQuery()]
     return cls(subqueries)
Esempio n. 2
0
 def from_strings(cls, query_parts, default_fields=None, all_keys=ITEM_KEYS):
     """Creates a query from a list of strings in the format used by
     _parse_query_part. If default_fields are specified, they are the
     fields to be searched by unqualified search terms. Otherwise,
     all fields are searched for those terms.
     """
     subqueries = []
     for part in query_parts:
         res = cls._parse_query_part(part)
         if not res:
             continue
         key, pattern = res
         if key is None: # No key specified.
             if os.sep in pattern:
                 # This looks like a path.
                 subqueries.append(PathQuery(pattern))
             else:
                 # Match any field.
                 subqueries.append(AnySubstringQuery(pattern,
                                                     default_fields))
         elif key.lower() == 'comp': # a boolean field
             subqueries.append(BooleanQuery(key.lower(), pattern))
         elif key.lower() == 'path':
             subqueries.append(PathQuery(pattern))
         elif key.lower() in all_keys: # ignore unrecognized keys
             subqueries.append(SubstringQuery(key.lower(), pattern))
         elif key.lower() == 'singleton':
             subqueries.append(SingletonQuery(util.str2bool(pattern)))
     if not subqueries: # no terms in query
         subqueries = [TrueQuery()]
     return cls(subqueries)
Esempio n. 3
0
def construct_query_part(query_part, default_fields, all_keys):
    """Create a query from a single query component. Return a Query
    instance or None if the value cannot be parsed.
    """
    parsed = parse_query_part(query_part)
    if not parsed:
        return

    key, pattern, query_class = parsed

    # No key specified.
    if key is None:
        if os.sep in pattern and 'path' in all_keys:
            # This looks like a path.
            return PathQuery(pattern)
        elif issubclass(query_class, dbcore.FieldQuery):
            # The query type matches a specific field, but none was
            # specified. So we use a version of the query that matches
            # any field.
            return AnyFieldQuery(pattern, default_fields, query_class)
        else:
            # Other query type.
            return query_class(pattern)

    key = key.lower()

    # A boolean field.
    if key.lower() == 'comp':
        return BooleanQuery(key, pattern)

    # Path field.
    elif key == 'path' and 'path' in all_keys:
        if query_class is SubstringQuery:
            # By default, use special path matching logic.
            return PathQuery(pattern)
        else:
            # Specific query type requested.
            return query_class('path', pattern)

    # Singleton query (not a real field).
    elif key == 'singleton':
        return SingletonQuery(util.str2bool(pattern))

    # Other field.
    else:
        return query_class(key.lower(), pattern, key in all_keys)
Esempio n. 4
0
 def from_string(cls, query_string, default_fields=None):
     """Creates a query from a string in the format used by
     _parse_query. If default_fields are specified, they are the
     fields to be searched by unqualified search terms. Otherwise,
     all fields are searched for those terms.
     """
     subqueries = []
     for key, pattern in cls._parse_query(query_string):
         if key is None: # no key specified; match any field
             subqueries.append(AnySubstringQuery(pattern, default_fields))
         elif key.lower() == 'comp': # a boolean field
             subqueries.append(BooleanQuery(key.lower(), pattern))
         elif key.lower() in ITEM_KEYS: # ignore unrecognized keys
             subqueries.append(SubstringQuery(key.lower(), pattern))
         elif key.lower() == 'singleton':
             subqueries.append(SingletonQuery(util.str2bool(pattern)))
     if not subqueries: # no terms in query
         subqueries = [TrueQuery()]
     return cls(subqueries)
Esempio n. 5
0
 def from_string(cls, query_string, default_fields=None):
     """Creates a query from a string in the format used by
     _parse_query. If default_fields are specified, they are the
     fields to be searched by unqualified search terms. Otherwise,
     all fields are searched for those terms.
     """
     subqueries = []
     for key, pattern in cls._parse_query(query_string):
         if key is None:  # no key specified; match any field
             subqueries.append(AnySubstringQuery(pattern, default_fields))
         elif key.lower() == 'comp':  # a boolean field
             subqueries.append(BooleanQuery(key.lower(), pattern))
         elif key.lower() in ITEM_KEYS:  # ignore unrecognized keys
             subqueries.append(SubstringQuery(key.lower(), pattern))
         elif key.lower() == 'singleton':
             subqueries.append(SingletonQuery(util.str2bool(pattern)))
     if not subqueries:  # no terms in query
         subqueries = [TrueQuery()]
     return cls(subqueries)
Esempio n. 6
0
def _convert_type(key, value, album=False):
    """Convert a string to the appropriate type for the given field.
    `album` indicates whether to use album or item field definitions.
    """
    fields = library.ALBUM_FIELDS if album else library.ITEM_FIELDS
    typ = [f[1] for f in fields if f[0] == key][0]

    if typ is bool:
        return util.str2bool(value)
    elif typ is datetime:
        fmt = config['time_format'].get(unicode)
        try:
            return time.mktime(time.strptime(value, fmt))
        except ValueError:
            raise ui.UserError(u'{0} must have format {1}'.format(key, fmt))
    else:
        try:
            return typ(value)
        except ValueError:
            raise ui.UserError(u'{0} must be a {1}'.format(key, typ))
Esempio n. 7
0
def _convert_type(key, value, album=False):
    """Convert a string to the appropriate type for the given field.
    `album` indicates whether to use album or item field definitions.
    """
    fields = library.ALBUM_FIELDS if album else library.ITEM_FIELDS
    typ = [f[1] for f in fields if f[0] == key][0]

    if typ is bool:
        return util.str2bool(value)
    elif typ is datetime:
        fmt = config['time_format'].get(unicode)
        try:
            return time.mktime(time.strptime(value, fmt))
        except ValueError:
            raise ui.UserError(u'{0} must have format {1}'.format(key, fmt))
    else:
        try:
            return typ(value)
        except ValueError:
            raise ui.UserError(u'{0} must be a {1}'.format(key, typ))
Esempio n. 8
0
def construct_query_part(query_part, model_cls):
    """Create a query from a single query component, `query_part`, for
    querying instances of `model_cls`. Return a `Query` instance.
    """
    # Shortcut for empty query parts.
    if not query_part:
        return dbcore.query.TrueQuery()

    # Set up and parse the string.
    query_classes = dict((k, t.query) for (k, t) in model_cls._fields.items())
    prefixes = {':': dbcore.query.RegexpQuery}
    prefixes.update(plugins.queries())
    key, pattern, query_class = \
            parse_query_part(query_part, query_classes, prefixes)

    # No key specified.
    if key is None:
        if os.sep in pattern and 'path' in model_cls._fields:
            # This looks like a path.
            return PathQuery('path', pattern)
        elif issubclass(query_class, dbcore.FieldQuery):
            # The query type matches a specific field, but none was
            # specified. So we use a version of the query that matches
            # any field.
            return dbcore.query.AnyFieldQuery(pattern,
                                              model_cls._search_fields,
                                              query_class)
        else:
            # Other query type.
            return query_class(pattern)

    key = key.lower()

    # Singleton query (not a real field).
    if key == 'singleton':
        return SingletonQuery(util.str2bool(pattern))

    # Other field.
    else:
        return query_class(key.lower(), pattern, key in model_cls._fields)
Esempio n. 9
0
def construct_query_part(query_part, model_cls):
    """Create a query from a single query component, `query_part`, for
    querying instances of `model_cls`. Return a `Query` instance.
    """
    # Shortcut for empty query parts.
    if not query_part:
        return dbcore.query.TrueQuery()

    # Set up and parse the string.
    query_classes = dict((k, t.query) for (k, t) in model_cls._fields.items())
    prefixes = {':': dbcore.query.RegexpQuery}
    prefixes.update(plugins.queries())
    key, pattern, query_class = \
            parse_query_part(query_part, query_classes, prefixes)

    # No key specified.
    if key is None:
        if os.sep in pattern and 'path' in model_cls._fields:
            # This looks like a path.
            return PathQuery('path', pattern)
        elif issubclass(query_class, dbcore.FieldQuery):
            # The query type matches a specific field, but none was
            # specified. So we use a version of the query that matches
            # any field.
            return dbcore.query.AnyFieldQuery(pattern,
                                              model_cls._search_fields,
                                              query_class)
        else:
            # Other query type.
            return query_class(pattern)

    key = key.lower()

    # Singleton query (not a real field).
    if key == 'singleton':
        return SingletonQuery(util.str2bool(pattern))

    # Other field.
    else:
        return query_class(key.lower(), pattern, key in model_cls._fields)
Esempio n. 10
0
 def __init__(self, field, pattern, fast=True):
     super(BooleanQuery, self).__init__(field, pattern, fast)
     if isinstance(pattern, basestring):
         self.pattern = util.str2bool(pattern)
     self.pattern = int(self.pattern)
Esempio n. 11
0
 def __init__(self, field, pattern, fast=True):
     super(BooleanQuery, self).__init__(field, pattern, fast)
     if isinstance(pattern, basestring):
         self.pattern = util.str2bool(pattern)
     self.pattern = int(self.pattern)
Esempio n. 12
0
File: types.py Progetto: JDLH/beets
 def parse(self, string):
     return str2bool(string)
Esempio n. 13
0
 def parse(self, string):
     return str2bool(string)
Esempio n. 14
0
 def __init__(self, field, pattern, fast=True):
     super().__init__(field, pattern, fast)
     if isinstance(pattern, str):
         self.pattern = util.str2bool(pattern)
     self.pattern = int(self.pattern)