Skip to content

Commit

Permalink
Merge new search query parser from calibre_temp
Browse files Browse the repository at this point in the history
  • Loading branch information
Charles Haley committed Apr 20, 2013
2 parents 3631011 + e7d4bec commit 4fc4145
Show file tree
Hide file tree
Showing 8 changed files with 180 additions and 3,821 deletions.
Binary file removed resources/images/empty.png
Binary file not shown.
15 changes: 8 additions & 7 deletions src/calibre/db/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,13 +195,13 @@ def __call__(self, query, field_iter):
try:
qd = now() - timedelta(int(num))
except:
raise ParseException(query, len(query), 'Number conversion error')
raise ParseException(_('Number conversion error: {0}').format(num))
field_count = 3
else:
try:
qd = parse_date(query, as_utc=False)
except:
raise ParseException(query, len(query), 'Date conversion error')
raise ParseException(_('Date conversion error: {0}').format(query))
if '-' in query:
field_count = query.count('-') + 1
else:
Expand Down Expand Up @@ -285,8 +285,8 @@ def __call__(self, query, field_iter, location, datatype, candidates, is_many=Fa
try:
q = cast(query) * mult
except:
raise ParseException(query, len(query),
'Non-numeric value in query: %r'%query)
raise ParseException(
_('Non-numeric value in query: {0}').format(query))

for val, book_ids in field_iter():
if val is None:
Expand Down Expand Up @@ -351,8 +351,8 @@ def __call__(self, query, field_iter, candidates, use_primary_find):
if ':' in query:
q = [q.strip() for q in query.split(':')]
if len(q) != 2:
raise ParseException(query, len(query),
'Invalid query format for colon-separated search')
raise ParseException(
_('Invalid query format for colon-separated search: {0}').format(query))
keyq, valq = q
keyq_mkind, keyq = _matchkind(keyq)
valq_mkind, valq = _matchkind(valq)
Expand Down Expand Up @@ -465,7 +465,8 @@ def get_matches(self, location, query, candidates=None,
if invert:
matches = self.all_book_ids - matches
return matches
raise ParseException(query, len(query), 'Recursive query group detected')
raise ParseException(
_('Recursive query group detected: {0}').format(query))

# If the user has asked to restrict searching over all field, apply
# that restriction
Expand Down
2 changes: 1 addition & 1 deletion src/calibre/gui2/library/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
QModelIndex, QVariant, QDateTime, QColor, QPixmap)

from calibre.gui2 import NONE, UNDEFINED_QDATETIME, error_dialog
from calibre.utils.pyparsing import ParseException
from calibre.utils.search_query_parser import ParseException
from calibre.ebooks.metadata import fmt_sidx, authors_to_string, string_to_authors
from calibre.ebooks.metadata.book.base import SafeFormat
from calibre.ptempfile import PersistentTemporaryFile
Expand Down
4 changes: 2 additions & 2 deletions src/calibre/gui2/search_restriction_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from calibre.gui2 import error_dialog, question_dialog
from calibre.gui2.widgets import ComboBoxWithHelp
from calibre.utils.icu import sort_key
from calibre.utils.pyparsing import ParseException
from calibre.utils.search_query_parser import ParseException
from calibre.utils.search_query_parser import saved_searches

class SelectNames(QDialog): # {{{
Expand Down Expand Up @@ -299,7 +299,7 @@ class SearchRestrictionMixin(object):

def __init__(self):
self.checked = QIcon(I('ok.png'))
self.empty = QIcon(I('empty.png'))
self.empty = QIcon(I('blank.png'))
self.search_based_vl_name = None
self.search_based_vl = None

Expand Down
25 changes: 9 additions & 16 deletions src/calibre/library/caches.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from calibre.utils.config import tweaks, prefs
from calibre.utils.date import parse_date, now, UNDEFINED_DATE, clean_date_for_sort
from calibre.utils.search_query_parser import SearchQueryParser
from calibre.utils.pyparsing import ParseException
from calibre.utils.search_query_parser import ParseException
from calibre.utils.localization import (canonicalize_lang, lang_map, get_udc)
from calibre.db.search import CONTAINS_MATCH, EQUALS_MATCH, REGEXP_MATCH, _match
from calibre.ebooks.metadata import title_sort, author_to_author_sort
Expand Down Expand Up @@ -366,25 +366,18 @@ def get_dates_matches(self, location, query, candidates):
elif query in self.local_thismonth:
qd = now()
field_count = 2
elif query.endswith(self.local_daysago):
elif query.endswith(self.local_daysago) or query.endswith(self.untrans_daysago):
num = query[0:-self.local_daysago_len]
try:
qd = now() - timedelta(int(num))
except:
raise ParseException(query, len(query), 'Number conversion error', self)
field_count = 3
elif query.endswith(self.untrans_daysago):
num = query[0:-self.untrans_daysago_len]
try:
qd = now() - timedelta(int(num))
except:
raise ParseException(query, len(query), 'Number conversion error', self)
raise ParseException(_('Number conversion error: {0}').format(num))
field_count = 3
else:
try:
qd = parse_date(query, as_utc=False)
except:
raise ParseException(query, len(query), 'Date conversion error', self)
raise ParseException(_('Date conversion error: {0}').format(query))
if '-' in query:
field_count = query.count('-') + 1
else:
Expand Down Expand Up @@ -460,8 +453,7 @@ def get_numeric_matches(self, location, query, candidates, val_func = None):
try:
q = cast(query) * mult
except:
raise ParseException(query, len(query),
'Non-numeric value in query', self)
raise ParseException(_('Non-numeric value in query: {0}').format(query))

for id_ in candidates:
item = self._data[id_]
Expand Down Expand Up @@ -501,12 +493,13 @@ def get_user_category_matches(self, location, query, candidates):
return matches

def get_keypair_matches(self, location, query, candidates):
print query
matches = set([])
if query.find(':') >= 0:
q = [q.strip() for q in query.split(':')]
if len(q) != 2:
raise ParseException(query, len(query),
'Invalid query format for colon-separated search', self)
raise ParseException(
_('Invalid query format for colon-separated search: {0}').format(query))
(keyq, valq) = q
keyq_mkind, keyq = self._matchkind(keyq)
valq_mkind, valq = self._matchkind(valq)
Expand Down Expand Up @@ -655,7 +648,7 @@ def get_matches(self, location, query, candidates=None,
if invert:
matches = self.universal_set() - matches
return matches
raise ParseException(query, len(query), 'Recursive query group detected', self)
raise ParseException(_('Recursive query group detected: {0}').format(query))

# apply the limit if appropriate
if location == 'all' and prefs['limit_search_columns'] and \
Expand Down
Loading

0 comments on commit 4fc4145

Please sign in to comment.