def parse(cls, search=False):
"""Parse the main query text. This method will also set the
class attribute `parsed_search` to the parsed query, and it will
return it too.
:param cls: The class object, since it is a static method
:type cls: object
:param search: Search text string if a custom search string is to be
used. False if the `cls.search` class attribute is to be used.
:type search: str
:returns: Parsed query
:rtype: list
>>> print(DocMatcher.parse('hello author = einstein'))
[['hello'], ['author', '=', 'einstein']]
>>> print(DocMatcher.parse(''))
[]
>>> print(\
DocMatcher.parse(\
'"hello world whatever =" tags = \\\'hello ====\\\''))
[['hello world whatever ='], ['tags', '=', 'hello ====']]
>>> print(DocMatcher.parse('hello'))
[['hello']]
"""
import pyparsing
cls.logger.debug('Parsing search')
search = search or cls.search
papis_alphas = pyparsing.printables.replace('=', '')
papis_key = pyparsing.Word(pyparsing.alphanums + '-')
papis_value = pyparsing.QuotedString(
quoteChar='"', escChar='\\', escQuote='\\'
) ^ pyparsing.QuotedString(
quoteChar="'", escChar='\\', escQuote='\\'
) ^ papis_key
equal = pyparsing.ZeroOrMore(" ") + \
pyparsing.Literal('=') + \
pyparsing.ZeroOrMore(" ")
papis_query = pyparsing.ZeroOrMore(
pyparsing.Group(
pyparsing.ZeroOrMore(
papis_key + equal
) + papis_value
)
)
parsed = papis_query.parseString(search)
cls.logger.debug('Parsed search = %s' % parsed)
cls.parsed_search = parsed
return cls.parsed_search
评论列表
文章目录