From 40bfed756b8cef2ce52b9449602511f78cab8e11 Mon Sep 17 00:00:00 2001 From: Diego Moreda Date: Thu, 19 Nov 2015 18:04:47 +0100 Subject: [PATCH] Revise not query syntax, cleanup, modify docstring MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Revise the NotQuery syntax, replacing the '¬' character with '^'. Fix tests to conform to this change, and cleanup the PARSE_QUERY_PART_REGEX. * Modify parse_query_part() docstring to mention the negate parameter on the returned tuple, and added an example. --- beets/dbcore/queryparse.py | 17 ++++++++--------- test/test_query.py | 6 +++--- 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/beets/dbcore/queryparse.py b/beets/dbcore/queryparse.py index ca01f0433..c7b75a90c 100644 --- a/beets/dbcore/queryparse.py +++ b/beets/dbcore/queryparse.py @@ -23,9 +23,7 @@ import beets PARSE_QUERY_PART_REGEX = re.compile( # Non-capturing optional segment for the keyword. - r'(?:' - ur'(-|\u00ac)' # Negation prefixes - r')?' + r'(-|\^)?' # Negation prefixes. r'(?:' r'(\S+?)' # The field key. @@ -41,9 +39,9 @@ PARSE_QUERY_PART_REGEX = re.compile( def parse_query_part(part, query_classes={}, prefixes={}, default_class=query.SubstringQuery): """Take a query in the form of a key/value pair separated by a - colon and return a tuple of `(key, value, cls)`. `key` may be None, + colon and return a tuple of `(key, value, cls, negate)`. `key` may be None, indicating that any field may be matched. `cls` is a subclass of - `FieldQuery`. + `FieldQuery`. `negate` is a boolean indicating if the query is negated. The optional `query_classes` parameter maps field names to default query types; `default_class` is the fallback. `prefixes` is a map @@ -57,10 +55,11 @@ def parse_query_part(part, query_classes={}, prefixes={}, class is available, `default_class` is used. For instance, - 'stapler' -> (None, 'stapler', SubstringQuery) - 'color:red' -> ('color', 'red', SubstringQuery) - ':^Quiet' -> (None, '^Quiet', RegexpQuery) - 'color::b..e' -> ('color', 'b..e', RegexpQuery) + 'stapler' -> (None, 'stapler', SubstringQuery, False) + 'color:red' -> ('color', 'red', SubstringQuery, False) + ':^Quiet' -> (None, '^Quiet', RegexpQuery, False) + 'color::b..e' -> ('color', 'b..e', RegexpQuery, False) + '-color:red' -> ('color', 'red', SubstringQuery, True) Prefixes may be "escaped" with a backslash to disable the keying behavior. diff --git a/test/test_query.py b/test/test_query.py index 5720c65cf..c415b7f1c 100644 --- a/test/test_query.py +++ b/test/test_query.py @@ -890,7 +890,7 @@ class NotQueryTest(DummyDataTestCase): def test_get_prefixes_keyed(self): """Test both negation prefixes on a keyed query.""" q0 = '-title:qux' - q1 = '\u00actitle:qux' + q1 = '^title:qux' results0 = self.lib.items(q0) results1 = self.lib.items(q1) self.assert_items_matched(results0, ['foo bar', 'beets 4 eva']) @@ -899,13 +899,13 @@ class NotQueryTest(DummyDataTestCase): def test_get_prefixes_unkeyed(self): """Test both negation prefixes on an unkeyed query.""" q0 = '-qux' - q1 = '\u00acqux' + q1 = '^qux' results0 = self.lib.items(q0) results1 = self.lib.items(q1) self.assert_items_matched(results0, ['foo bar', 'beets 4 eva']) self.assert_items_matched(results1, ['foo bar', 'beets 4 eva']) - def test_get_keyed_regexp(self): + def test_get_one_keyed_regexp(self): q = r'-artist::t.+r' results = self.lib.items(q) self.assert_items_matched(results, ['foo bar', 'baz qux'])