mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Eliminate pyparsing. Simplify ParseException
This commit is contained in:
parent
3e2b7691cd
commit
e7d4bec981
@ -195,13 +195,13 @@ class DateSearch(object): # {{{
|
|||||||
try:
|
try:
|
||||||
qd = now() - timedelta(int(num))
|
qd = now() - timedelta(int(num))
|
||||||
except:
|
except:
|
||||||
raise ParseException(query, len(query), 'Number conversion error')
|
raise ParseException(_('Number conversion error: {0}').format(num))
|
||||||
field_count = 3
|
field_count = 3
|
||||||
else:
|
else:
|
||||||
try:
|
try:
|
||||||
qd = parse_date(query, as_utc=False)
|
qd = parse_date(query, as_utc=False)
|
||||||
except:
|
except:
|
||||||
raise ParseException(query, len(query), 'Date conversion error')
|
raise ParseException(_('Date conversion error: {0}').format(query))
|
||||||
if '-' in query:
|
if '-' in query:
|
||||||
field_count = query.count('-') + 1
|
field_count = query.count('-') + 1
|
||||||
else:
|
else:
|
||||||
@ -285,8 +285,8 @@ class NumericSearch(object): # {{{
|
|||||||
try:
|
try:
|
||||||
q = cast(query) * mult
|
q = cast(query) * mult
|
||||||
except:
|
except:
|
||||||
raise ParseException(query, len(query),
|
raise ParseException(
|
||||||
'Non-numeric value in query: %r'%query)
|
_('Non-numeric value in query: {0}').format(query))
|
||||||
|
|
||||||
for val, book_ids in field_iter():
|
for val, book_ids in field_iter():
|
||||||
if val is None:
|
if val is None:
|
||||||
@ -351,8 +351,8 @@ class KeyPairSearch(object): # {{{
|
|||||||
if ':' in query:
|
if ':' in query:
|
||||||
q = [q.strip() for q in query.split(':')]
|
q = [q.strip() for q in query.split(':')]
|
||||||
if len(q) != 2:
|
if len(q) != 2:
|
||||||
raise ParseException(query, len(query),
|
raise ParseException(
|
||||||
'Invalid query format for colon-separated search')
|
_('Invalid query format for colon-separated search: {0}').format(query))
|
||||||
keyq, valq = q
|
keyq, valq = q
|
||||||
keyq_mkind, keyq = _matchkind(keyq)
|
keyq_mkind, keyq = _matchkind(keyq)
|
||||||
valq_mkind, valq = _matchkind(valq)
|
valq_mkind, valq = _matchkind(valq)
|
||||||
@ -465,7 +465,8 @@ class Parser(SearchQueryParser):
|
|||||||
if invert:
|
if invert:
|
||||||
matches = self.all_book_ids - matches
|
matches = self.all_book_ids - matches
|
||||||
return 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
|
# If the user has asked to restrict searching over all field, apply
|
||||||
# that restriction
|
# that restriction
|
||||||
|
@ -12,7 +12,7 @@ from PyQt4.Qt import (QAbstractTableModel, Qt, pyqtSignal, QIcon, QImage,
|
|||||||
QModelIndex, QVariant, QDateTime, QColor, QPixmap)
|
QModelIndex, QVariant, QDateTime, QColor, QPixmap)
|
||||||
|
|
||||||
from calibre.gui2 import NONE, UNDEFINED_QDATETIME, error_dialog
|
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 import fmt_sidx, authors_to_string, string_to_authors
|
||||||
from calibre.ebooks.metadata.book.base import SafeFormat
|
from calibre.ebooks.metadata.book.base import SafeFormat
|
||||||
from calibre.ptempfile import PersistentTemporaryFile
|
from calibre.ptempfile import PersistentTemporaryFile
|
||||||
|
@ -13,7 +13,7 @@ from PyQt4.Qt import (
|
|||||||
from calibre.gui2 import error_dialog, question_dialog
|
from calibre.gui2 import error_dialog, question_dialog
|
||||||
from calibre.gui2.widgets import ComboBoxWithHelp
|
from calibre.gui2.widgets import ComboBoxWithHelp
|
||||||
from calibre.utils.icu import sort_key
|
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
|
from calibre.utils.search_query_parser import saved_searches
|
||||||
|
|
||||||
class SelectNames(QDialog): # {{{
|
class SelectNames(QDialog): # {{{
|
||||||
|
@ -14,7 +14,7 @@ from threading import Thread
|
|||||||
from calibre.utils.config import tweaks, prefs
|
from calibre.utils.config import tweaks, prefs
|
||||||
from calibre.utils.date import parse_date, now, UNDEFINED_DATE, clean_date_for_sort
|
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.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.utils.localization import (canonicalize_lang, lang_map, get_udc)
|
||||||
from calibre.db.search import CONTAINS_MATCH, EQUALS_MATCH, REGEXP_MATCH, _match
|
from calibre.db.search import CONTAINS_MATCH, EQUALS_MATCH, REGEXP_MATCH, _match
|
||||||
from calibre.ebooks.metadata import title_sort, author_to_author_sort
|
from calibre.ebooks.metadata import title_sort, author_to_author_sort
|
||||||
@ -366,25 +366,18 @@ class ResultCache(SearchQueryParser): # {{{
|
|||||||
elif query in self.local_thismonth:
|
elif query in self.local_thismonth:
|
||||||
qd = now()
|
qd = now()
|
||||||
field_count = 2
|
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]
|
num = query[0:-self.local_daysago_len]
|
||||||
try:
|
try:
|
||||||
qd = now() - timedelta(int(num))
|
qd = now() - timedelta(int(num))
|
||||||
except:
|
except:
|
||||||
raise ParseException(query, len(query), 'Number conversion error', self)
|
raise ParseException(_('Number conversion error: {0}').format(num))
|
||||||
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)
|
|
||||||
field_count = 3
|
field_count = 3
|
||||||
else:
|
else:
|
||||||
try:
|
try:
|
||||||
qd = parse_date(query, as_utc=False)
|
qd = parse_date(query, as_utc=False)
|
||||||
except:
|
except:
|
||||||
raise ParseException(query, len(query), 'Date conversion error', self)
|
raise ParseException(_('Date conversion error: {0}').format(query))
|
||||||
if '-' in query:
|
if '-' in query:
|
||||||
field_count = query.count('-') + 1
|
field_count = query.count('-') + 1
|
||||||
else:
|
else:
|
||||||
@ -460,8 +453,7 @@ class ResultCache(SearchQueryParser): # {{{
|
|||||||
try:
|
try:
|
||||||
q = cast(query) * mult
|
q = cast(query) * mult
|
||||||
except:
|
except:
|
||||||
raise ParseException(query, len(query),
|
raise ParseException(_('Non-numeric value in query: {0}').format(query))
|
||||||
'Non-numeric value in query', self)
|
|
||||||
|
|
||||||
for id_ in candidates:
|
for id_ in candidates:
|
||||||
item = self._data[id_]
|
item = self._data[id_]
|
||||||
@ -501,12 +493,13 @@ class ResultCache(SearchQueryParser): # {{{
|
|||||||
return matches
|
return matches
|
||||||
|
|
||||||
def get_keypair_matches(self, location, query, candidates):
|
def get_keypair_matches(self, location, query, candidates):
|
||||||
|
print query
|
||||||
matches = set([])
|
matches = set([])
|
||||||
if query.find(':') >= 0:
|
if query.find(':') >= 0:
|
||||||
q = [q.strip() for q in query.split(':')]
|
q = [q.strip() for q in query.split(':')]
|
||||||
if len(q) != 2:
|
if len(q) != 2:
|
||||||
raise ParseException(query, len(query),
|
raise ParseException(
|
||||||
'Invalid query format for colon-separated search', self)
|
_('Invalid query format for colon-separated search: {0}').format(query))
|
||||||
(keyq, valq) = q
|
(keyq, valq) = q
|
||||||
keyq_mkind, keyq = self._matchkind(keyq)
|
keyq_mkind, keyq = self._matchkind(keyq)
|
||||||
valq_mkind, valq = self._matchkind(valq)
|
valq_mkind, valq = self._matchkind(valq)
|
||||||
@ -655,7 +648,7 @@ class ResultCache(SearchQueryParser): # {{{
|
|||||||
if invert:
|
if invert:
|
||||||
matches = self.universal_set() - matches
|
matches = self.universal_set() - matches
|
||||||
return 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
|
# apply the limit if appropriate
|
||||||
if location == 'all' and prefs['limit_search_columns'] and \
|
if location == 'all' and prefs['limit_search_columns'] and \
|
||||||
|
File diff suppressed because it is too large
Load Diff
@ -18,7 +18,6 @@ If this module is run, it will perform a series of unit tests.
|
|||||||
|
|
||||||
import sys, operator, weakref, re
|
import sys, operator, weakref, re
|
||||||
|
|
||||||
from calibre.utils.pyparsing import ParseException
|
|
||||||
from calibre.constants import preferred_encoding
|
from calibre.constants import preferred_encoding
|
||||||
from calibre.utils.icu import sort_key
|
from calibre.utils.icu import sort_key
|
||||||
from calibre import prints
|
from calibre import prints
|
||||||
@ -197,10 +196,10 @@ class Parser(object):
|
|||||||
self.advance()
|
self.advance()
|
||||||
res = self.or_expression()
|
res = self.or_expression()
|
||||||
if self.token(advance=True) != ')':
|
if self.token(advance=True) != ')':
|
||||||
raise ParseException('missing )')
|
raise ParseException(_('missing )'))
|
||||||
return res
|
return res
|
||||||
if self.token_type() not in [ self.WORD, self.QUOTED_WORD ]:
|
if self.token_type() not in [ self.WORD, self.QUOTED_WORD ]:
|
||||||
raise ParseException('Invalid syntax. Expected a lookup name or a word')
|
raise ParseException(_('Invalid syntax. Expected a lookup name or a word'))
|
||||||
|
|
||||||
return self.base_token()
|
return self.base_token()
|
||||||
|
|
||||||
@ -231,6 +230,14 @@ class Parser(object):
|
|||||||
|
|
||||||
return ['token', 'all', ':'.join(words)]
|
return ['token', 'all', ':'.join(words)]
|
||||||
|
|
||||||
|
class ParseException(Exception):
|
||||||
|
|
||||||
|
@property
|
||||||
|
def msg(self):
|
||||||
|
if len(self.args) > 0:
|
||||||
|
return self.args[0]
|
||||||
|
return ""
|
||||||
|
|
||||||
class SearchQueryParser(object):
|
class SearchQueryParser(object):
|
||||||
'''
|
'''
|
||||||
Parses a search query.
|
Parses a search query.
|
||||||
@ -295,7 +302,7 @@ class SearchQueryParser(object):
|
|||||||
try:
|
try:
|
||||||
res = self.parser.parse(query, self.locations)
|
res = self.parser.parse(query, self.locations)
|
||||||
except RuntimeError:
|
except RuntimeError:
|
||||||
raise ParseException('Failed to parse query, recursion limit reached: %s'%repr(query))
|
raise ParseException(_('Failed to parse query, recursion limit reached: %s')%repr(query))
|
||||||
if candidates is None:
|
if candidates is None:
|
||||||
candidates = self.universal_set()
|
candidates = self.universal_set()
|
||||||
t = self.evaluate(res, candidates)
|
t = self.evaluate(res, candidates)
|
||||||
@ -339,12 +346,16 @@ class SearchQueryParser(object):
|
|||||||
query = query[1:]
|
query = query[1:]
|
||||||
try:
|
try:
|
||||||
if query in self.searches_seen:
|
if query in self.searches_seen:
|
||||||
raise ParseException(query, len(query), 'undefined saved search', self)
|
raise ParseException(_('Recursive saved search: {0}').format(query))
|
||||||
if self.recurse_level > 5:
|
if self.recurse_level > 5:
|
||||||
self.searches_seen.add(query)
|
self.searches_seen.add(query)
|
||||||
return self._parse(saved_searches().lookup(query), candidates)
|
return self._parse(saved_searches().lookup(query), candidates)
|
||||||
|
except ParseException as e:
|
||||||
|
raise e
|
||||||
except: # convert all exceptions (e.g., missing key) to a parse error
|
except: # convert all exceptions (e.g., missing key) to a parse error
|
||||||
raise ParseException(query, len(query), 'undefined saved search', self)
|
import traceback
|
||||||
|
traceback.print_exc()
|
||||||
|
raise ParseException(_('Unknown error in saved search: {0}').format(query))
|
||||||
return self._get_matches(location, query, candidates)
|
return self._get_matches(location, query, candidates)
|
||||||
|
|
||||||
def _get_matches(self, location, query, candidates):
|
def _get_matches(self, location, query, candidates):
|
||||||
|
@ -19,7 +19,7 @@ from calibre.web.feeds.recipes.collection import \
|
|||||||
SchedulerConfig, download_builtin_recipe, update_custom_recipe, \
|
SchedulerConfig, download_builtin_recipe, update_custom_recipe, \
|
||||||
add_custom_recipe, remove_custom_recipe, get_custom_recipe, \
|
add_custom_recipe, remove_custom_recipe, get_custom_recipe, \
|
||||||
get_builtin_recipe
|
get_builtin_recipe
|
||||||
from calibre.utils.pyparsing import ParseException
|
from calibre.utils.search_query_parser import ParseException
|
||||||
|
|
||||||
class NewsTreeItem(object):
|
class NewsTreeItem(object):
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user