more natural-looking queries for comp field

You can now use "comp:true" or "comp:yes" as well as "comp:1".
This commit is contained in:
Adrian Sampson 2011-04-10 22:55:25 -07:00
parent 7d21b27554
commit 7503c1174a
2 changed files with 26 additions and 1 deletions

View file

@ -327,6 +327,16 @@ class SubstringQuery(FieldQuery):
def match(self, item):
return self.pattern.lower() in getattr(item, self.field).lower()
class BooleanQuery(MatchQuery):
"""Matches a boolean field. Pattern should either be a boolean or a
string reflecting a boolean.
"""
def __init__(self, field, pattern):
super(BooleanQuery, self).__init__(field, pattern)
if isinstance(pattern, basestring):
self.pattern = util.str2bool(pattern)
self.pattern = int(self.pattern)
class SingletonQuery(Query):
"""Matches either singleton or non-singleton items."""
def __init__(self, sense):
@ -409,6 +419,8 @@ class CollectionQuery(Query):
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':

View file

@ -1,5 +1,5 @@
# This file is part of beets.
# Copyright 2010, Adrian Sampson.
# Copyright 2011, Adrian Sampson.
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
@ -174,6 +174,7 @@ class MemoryGetTest(unittest.TestCase, AssertsMixin):
self.album_item.title = 'album item'
self.single_item = _common.item()
self.single_item.title = 'singleton item'
self.single_item.comp = False
self.lib = beets.library.Library(':memory:')
self.lib.add(self.single_item)
@ -191,6 +192,18 @@ class MemoryGetTest(unittest.TestCase, AssertsMixin):
self.assert_matched(results, 'album item')
self.assert_done(results)
def test_compilation_true(self):
q = 'comp:true'
results = self.lib.get(q)
self.assert_matched(results, 'album item')
self.assert_done(results)
def test_compilation_false(self):
q = 'comp:false'
results = self.lib.get(q)
self.assert_matched(results, 'singleton item')
self.assert_done(results)
class BrowseTest(unittest.TestCase, AssertsMixin):
def setUp(self):
self.lib = beets.library.Library('rsrc' + os.sep + 'test.blb')