Make SearchQueryParser accept a dynamic list of locations

This commit is contained in:
Kovid Goyal 2010-05-01 11:36:38 -06:00
parent b060a20f71
commit bb7edaa57b
3 changed files with 12 additions and 7 deletions

View File

@ -155,7 +155,9 @@ class ResultCache(SearchQueryParser):
self._map = self._map_filtered = self._data = []
self.first_sort = True
self.search_restriction = ''
SearchQueryParser.__init__(self, [c for c in cc_label_map])
SearchQueryParser.__init__(self,
locations=SearchQueryParser.DEFAULT_LOCATIONS +
[c for c in cc_label_map])
self.build_relop_dict()
def build_relop_dict(self):

View File

@ -73,7 +73,7 @@ class SearchQueryParser(object):
When no operator is specified between two tokens, `and` is assumed.
Each token is a string of the form `location:query`. `location` is a string
from :member:`LOCATIONS`. It is optional. If it is omitted, it is assumed to
from :member:`DEFAULT_LOCATIONS`. It is optional. If it is omitted, it is assumed to
be `all`. `query` is an arbitrary string that must not contain parentheses.
If it contains whitespace, it should be quoted by enclosing it in `"` marks.
@ -86,7 +86,7 @@ class SearchQueryParser(object):
* `(author:Asimov or author:Hardy) and not tag:read` [search for unread books by Asimov or Hardy]
'''
LOCATIONS = [
DEFAULT_LOCATIONS = [
'tag',
'title',
'author',
@ -116,10 +116,13 @@ class SearchQueryParser(object):
failed.append(test[0])
return failed
def __init__(self, custcols=[], test=False):
def __init__(self, locations=None, test=False):
if locations is None:
locations = self.DEFAULT_LOCATIONS
self._tests_failed = False
# Define a token
standard_locations = map(lambda x : CaselessLiteral(x)+Suppress(':'), self.LOCATIONS+custcols)
standard_locations = map(lambda x : CaselessLiteral(x)+Suppress(':'),
locations)
location = NoMatch()
for l in standard_locations:
location |= l
@ -228,7 +231,7 @@ class SearchQueryParser(object):
'''
Should return the set of matches for :param:'location` and :param:`query`.
:param:`location` is one of the items in :member:`SearchQueryParser.LOCATIONS`.
:param:`location` is one of the items in :member:`SearchQueryParser.DEFAULT_LOCATIONS`.
:param:`query` is a string literal.
'''
return set([])

View File

@ -121,7 +121,7 @@ class RecipeModel(QAbstractItemModel, SearchQueryParser):
def __init__(self, db, *args):
QAbstractItemModel.__init__(self, *args)
SearchQueryParser.__init__(self)
SearchQueryParser.__init__(self, locations=['all'])
self.db = db
self.default_icon = QVariant(QIcon(I('news.svg')))
self.custom_icon = QVariant(QIcon(I('user_profile.svg')))