Fix #7039 (Search for author doesn't work if the name contains double quotes)

This commit is contained in:
Kovid Goyal 2010-10-03 19:58:47 -06:00
parent d1ba8c78d4
commit c4e3bdd4cc
2 changed files with 406 additions and 276 deletions

File diff suppressed because it is too large Load Diff

View File

@ -18,8 +18,9 @@ If this module is run, it will perform a series of unit tests.
import sys, string, operator import sys, string, operator
from calibre.utils.pyparsing import CaselessKeyword, Group, Forward, CharsNotIn, Suppress, \ from calibre.utils.pyparsing import CaselessKeyword, Group, Forward, \
OneOrMore, MatchFirst, CaselessLiteral, Optional, NoMatch, ParseException CharsNotIn, Suppress, OneOrMore, MatchFirst, CaselessLiteral, \
Optional, NoMatch, ParseException, QuotedString
from calibre.constants import preferred_encoding from calibre.constants import preferred_encoding
@ -127,18 +128,21 @@ class SearchQueryParser(object):
location |= l location |= l
location = Optional(location, default='all') location = Optional(location, default='all')
word_query = CharsNotIn(string.whitespace + '()') word_query = CharsNotIn(string.whitespace + '()')
quoted_query = Suppress('"')+CharsNotIn('"')+Suppress('"') #quoted_query = Suppress('"')+CharsNotIn('"')+Suppress('"')
quoted_query = QuotedString('"', escChar='\\')
query = quoted_query | word_query query = quoted_query | word_query
Token = Group(location + query).setResultsName('token') Token = Group(location + query).setResultsName('token')
if test: if test:
print 'Testing Token parser:' print 'Testing Token parser:'
Token.validate()
failed = SearchQueryParser.run_tests(Token, 'token', failed = SearchQueryParser.run_tests(Token, 'token',
( (
('tag:asd', ['tag', 'asd']), ('tag:asd', ['tag', 'asd']),
('ddsä', ['all', 'ddsä']), (u'ddsä', ['all', u'ddsä']),
('"one two"', ['all', 'one two']), ('"one \\"two"', ['all', 'one "two']),
('title:"one two"', ['title', 'one two']), ('title:"one \\"1.5\\" two"', ['title', 'one "1.5" two']),
('title:abc"def', ['title', 'abc"def']),
) )
) )
@ -167,7 +171,7 @@ class SearchQueryParser(object):
).setResultsName("or") | And) ).setResultsName("or") | And)
if test: if test:
Or.validate() #Or.validate()
self._tests_failed = bool(failed) self._tests_failed = bool(failed)
self._parser = Or self._parser = Or
@ -240,6 +244,8 @@ class SearchQueryParser(object):
''' '''
return set([]) return set([])
# Testing {{{
class Tester(SearchQueryParser): class Tester(SearchQueryParser):
texts = { texts = {
@ -599,3 +605,6 @@ def main(args=sys.argv):
if __name__ == '__main__': if __name__ == '__main__':
sys.exit(main()) sys.exit(main())
# }}}