Invenio-Query-Parser

travis-ci badge coveralls.io badge

Search query parser supporting Invenio and SPIRES search syntax.

Contents

Installation

Invenio-Query-Parser is on PyPI so all you need is:

$ pip install invenio-query-parser

Usage

The easiest way is to use pypeg2 directly with Main.

import pypeg2
from invenio_query_parser.parser import Main
pypeg2.parse('author:"Ellis"', Main)

API

API for Invenio-Query-Parser.

Define abstract classes.

class invenio_query_parser.ast.AndOp(left, right)
class invenio_query_parser.ast.BinaryKeywordBase(left, right)
keyword
class invenio_query_parser.ast.BinaryOp(left, right)
accept(visitor)
class invenio_query_parser.ast.DoubleQuotedValue(value)
class invenio_query_parser.ast.EmptyQuery(value)
class invenio_query_parser.ast.GreaterEqualOp(op)
class invenio_query_parser.ast.GreaterOp(op)
class invenio_query_parser.ast.Keyword(value)
class invenio_query_parser.ast.KeywordOp(left, right)
class invenio_query_parser.ast.Leaf(value)
accept(visitor)
class invenio_query_parser.ast.ListOp(children)
accept(visitor)
class invenio_query_parser.ast.LowerEqualOp(op)
class invenio_query_parser.ast.LowerOp(op)
class invenio_query_parser.ast.NestedKeywordsRule(left, right)
class invenio_query_parser.ast.NotOp(op)
keyword
class invenio_query_parser.ast.OrOp(left, right)
class invenio_query_parser.ast.RangeOp(left, right)
class invenio_query_parser.ast.RegexValue(value)
class invenio_query_parser.ast.SingleQuotedValue(value)
class invenio_query_parser.ast.UnaryOp(op)
accept(visitor)
class invenio_query_parser.ast.Value(value)
class invenio_query_parser.ast.ValueQuery(op)

Define parsers.

class invenio_query_parser.parser.And
grammar = (-6, [<_sre.SRE_Pattern object at 0x7f328e8b10d8>, Literal('+')])
class invenio_query_parser.parser.AndQuery
grammar = [((-6, <class 'invenio_query_parser.parser.And'>), [((-6, <class 'invenio_query_parser.parser.Whitespace'>), Attribute(name='op', thing=<class 'invenio_query_parser.parser.NotQuery'>, subtype=None)), ((-6, <class 'invenio_query_parser.parser.Whitespace'>), Attribute(name='op', thing=<class 'invenio_query_parser.parser.SimpleQuery'>, subtype=None)), ((-6, (0, <class 'invenio_query_parser.parser.Whitespace'>)), Attribute(name='op', thing=<class 'invenio_query_parser.parser.ParenthesizedQuery'>, subtype=None))]), ((-6, Literal('+')), Attribute(name='op', thing=<class 'invenio_query_parser.parser.SimpleQuery'>, subtype=None))]
class invenio_query_parser.parser.BinaryRule
class invenio_query_parser.parser.DoubleQuotedString
grammar = (Literal('"'), Attribute(name='value', thing=<_sre.SRE_Pattern object at 0x7f328e9e23c8>, subtype=None), Literal('"'))
class invenio_query_parser.parser.EmptyQueryRule
grammar = Attribute(name='value', thing=<_sre.SRE_Pattern object at 0x7f328e8ab140>, subtype=None)
class invenio_query_parser.parser.ImplicitAndQuery
grammar = [Attribute(name='op', thing=<class 'invenio_query_parser.parser.NotQuery'>, subtype=None), Attribute(name='op', thing=<class 'invenio_query_parser.parser.ParenthesizedQuery'>, subtype=None), Attribute(name='op', thing=<class 'invenio_query_parser.parser.SimpleQuery'>, subtype=None)]
class invenio_query_parser.parser.KeywordQuery
grammar = [(Attribute(name='left', thing=<class 'invenio_query_parser.parser.KeywordRule'>, subtype=None), (-6, ((0, <class 'invenio_query_parser.parser.Whitespace'>), Literal(':'), (0, <class 'invenio_query_parser.parser.Whitespace'>))), Attribute(name='right', thing=<class 'invenio_query_parser.parser.NestedKeywordsRule'>, subtype=None)), (Attribute(name='left', thing=<class 'invenio_query_parser.parser.KeywordRule'>, subtype=None), (-6, (Literal(':'), (0, <class 'invenio_query_parser.parser.Whitespace'>))), Attribute(name='right', thing=<class 'invenio_query_parser.parser.Value'>, subtype=None)), (Attribute(name='left', thing=<class 'invenio_query_parser.parser.KeywordRule'>, subtype=None), (-6, (Literal(':'), (0, <class 'invenio_query_parser.parser.Whitespace'>))), Attribute(name='right', thing=<class 'invenio_query_parser.parser.Query'>, subtype=None))]
class invenio_query_parser.parser.KeywordRule
grammar = Attribute(name='value', thing=<_sre.SRE_Pattern object at 0x7f328ed28730>, subtype=None)
class invenio_query_parser.parser.LeafRule
class invenio_query_parser.parser.ListRule
class invenio_query_parser.parser.Main
grammar = [((-6, (0, <class 'invenio_query_parser.parser.Whitespace'>)), Attribute(name='op', thing=<class 'invenio_query_parser.parser.Query'>, subtype=None), (-6, (0, <class 'invenio_query_parser.parser.Whitespace'>))), Attribute(name='op', thing=<class 'invenio_query_parser.parser.EmptyQueryRule'>, subtype=None)]
initialized = False
class invenio_query_parser.parser.NestableKeyword
grammar = Attribute(name='value', thing=[<_sre.SRE_Pattern object at 0x7f328ea58be0>, <_sre.SRE_Pattern object at 0x7f328e8b1180>], subtype=None)
class invenio_query_parser.parser.NestedKeywordsRule
grammar = Attribute(name='value', thing=<_sre.SRE_Pattern object at 0x7f328ea60c30>, subtype=None)
class invenio_query_parser.parser.Not
grammar = (-6, [(-6, <_sre.SRE_Pattern object at 0x7f328ead7a28>), <_sre.SRE_Pattern object at 0x7f328e8b1030>, Literal('-')])
class invenio_query_parser.parser.NotKeywordValue
class invenio_query_parser.parser.NotQuery
grammar = [((-6, <class 'invenio_query_parser.parser.Not'>), [((-6, <class 'invenio_query_parser.parser.Whitespace'>), Attribute(name='op', thing=<class 'invenio_query_parser.parser.SimpleQuery'>, subtype=None)), ((-6, (0, <class 'invenio_query_parser.parser.Whitespace'>)), Attribute(name='op', thing=<class 'invenio_query_parser.parser.ParenthesizedQuery'>, subtype=None))]), ((-6, Literal('-')), Attribute(name='op', thing=<class 'invenio_query_parser.parser.SimpleQuery'>, subtype=None))]
class invenio_query_parser.parser.Number
grammar = Attribute(name='value', thing=<_sre.SRE_Pattern object at 0x7f328e8ae1f8>, subtype=None)
class invenio_query_parser.parser.Or
grammar = (-6, [<_sre.SRE_Pattern object at 0x7f328e8ae160>, Literal('|')])
class invenio_query_parser.parser.OrQuery
grammar = [((-6, <class 'invenio_query_parser.parser.Or'>), [((-6, <class 'invenio_query_parser.parser.Whitespace'>), Attribute(name='op', thing=<class 'invenio_query_parser.parser.NotQuery'>, subtype=None)), ((-6, <class 'invenio_query_parser.parser.Whitespace'>), Attribute(name='op', thing=<class 'invenio_query_parser.parser.SimpleQuery'>, subtype=None)), ((-6, (0, <class 'invenio_query_parser.parser.Whitespace'>)), Attribute(name='op', thing=<class 'invenio_query_parser.parser.ParenthesizedQuery'>, subtype=None))]), ((-6, Literal('|')), Attribute(name='op', thing=<class 'invenio_query_parser.parser.SimpleQuery'>, subtype=None))]
class invenio_query_parser.parser.ParenthesizedQuery
grammar = ((-6, (Literal('('), (0, <class 'invenio_query_parser.parser.Whitespace'>))), Attribute(name='op', thing=<class 'invenio_query_parser.parser.Query'>, subtype=None), (-6, ((0, <class 'invenio_query_parser.parser.Whitespace'>), Literal(')'))))
class invenio_query_parser.parser.Query
grammar = Attribute(name='children', thing=([<class 'invenio_query_parser.parser.NotQuery'>, <class 'invenio_query_parser.parser.ParenthesizedQuery'>, <class 'invenio_query_parser.parser.SimpleQuery'>], (-1, ((-6, (0, <class 'invenio_query_parser.parser.Whitespace'>)), [<class 'invenio_query_parser.parser.AndQuery'>, <class 'invenio_query_parser.parser.OrQuery'>, <class 'invenio_query_parser.parser.ImplicitAndQuery'>]))), subtype=None)
class invenio_query_parser.parser.RangeOp
grammar = (Attribute(name='left', thing=<class 'invenio_query_parser.parser.RangeValue'>, subtype=None), Literal('->'), Attribute(name='right', thing=<class 'invenio_query_parser.parser.RangeValue'>, subtype=None))
class invenio_query_parser.parser.RangeValue
grammar = Attribute(name='op', thing=[<class 'invenio_query_parser.parser.DoubleQuotedString'>, <class 'invenio_query_parser.parser.SimpleRangeValue'>], subtype=None)
class invenio_query_parser.parser.SimpleQuery
grammar = Attribute(name='op', thing=[<class 'invenio_query_parser.parser.KeywordQuery'>, <class 'invenio_query_parser.parser.ValueQuery'>], subtype=None)
class invenio_query_parser.parser.SimpleRangeValue
grammar = Attribute(name='value', thing=<_sre.SRE_Pattern object at 0x7f328ea013a8>, subtype=None)
class invenio_query_parser.parser.SimpleValue(values)
grammar = (-2, <class 'invenio_query_parser.parser.SimpleValueUnit'>)
class invenio_query_parser.parser.SimpleValueUnit(args)
grammar = [<_sre.SRE_Pattern object at 0x7f328ea58b30>, (<_sre.SRE_Pattern object at 0x7f328eacaa48>, <class 'invenio_query_parser.parser.SimpleValue'>, <_sre.SRE_Pattern object at 0x7f328e8ab0b8>)]
class invenio_query_parser.parser.SingleQuotedString
grammar = (Literal("'"), Attribute(name='value', thing=<_sre.SRE_Pattern object at 0x7f328e9e2310>, subtype=None), Literal("'"))
class invenio_query_parser.parser.SlashQuotedString
grammar = (Literal('/'), Attribute(name='value', thing=<_sre.SRE_Pattern object at 0x7f328e9e2480>, subtype=None), Literal('/'))
class invenio_query_parser.parser.UnaryRule
class invenio_query_parser.parser.Value
grammar = Attribute(name='op', thing=[<class 'invenio_query_parser.parser.RangeOp'>, <class 'invenio_query_parser.parser.SingleQuotedString'>, <class 'invenio_query_parser.parser.DoubleQuotedString'>, <class 'invenio_query_parser.parser.SlashQuotedString'>, <class 'invenio_query_parser.parser.SimpleValue'>], subtype=None)
class invenio_query_parser.parser.ValueQuery
grammar = Attribute(name='op', thing=<class 'invenio_query_parser.parser.Value'>, subtype=None)
class invenio_query_parser.parser.Whitespace
grammar = Attribute(name='value', thing=<_sre.SRE_Pattern object at 0x7f328e8ae0c8>, subtype=None)

Store the actual visitor methods.

class invenio_query_parser.visitor.make_visitor(methods=None)

Make a visitor decorator.

Changes

Version 0.6.0 (released 2016-04-18):

  • Removes check for Flask application context from build_valid_keywords_grammar function in favor of new keywords argument.
  • Elastic search DSL walker returns instance of elasticsearch_dsl.Q instead of dict.

Version 0.5.0 (released 2016-02-29):

  • Allows match unit to iterate over Sequences, i.e. tuple.
  • Removes distinctions between double and single quotes.
  • Adds support for allowed keywords.
  • Removes nestable keywords (to be re-added in the future).
  • Removes optional space after a keyword.
  • AND, OR and NOT are only considered as keywords when written in caps.

Version 0.4.1 (released 2015-11-13):

  • Default key getter returns default value instead of raising key error if the key is not found inside the dictionary.

Version 0.4.0 (released 2015-11-12):

  • Removes support for Python 2.6.
  • Adds experimental support for Elasticsearch query generation.
  • Adds MatchUnit walker for testing data against a query.
  • Adds Python 3.5 classifier and enables it in test matrix.
  • Fixes ‘not’ operator usage at the beginning of the query and after an ‘or’ operator.

Version 0.3.0 (released 2015-07-29):

  • Allows search keywords to contain dots to point to subfield content, i.e. author.name: Ellis.

Version 0.2.0 (released 2014-12-10):

  • Initial public release.
  • Adds Python2/Python3 compatibility layer. (#2)
  • Adds new Sphinx documentation page. (#3)
  • Adds simple inheritance for visitor pattern and separates SPIRES syntax parser to contrib module. (#7)

Version 0.1.0 (release 2014-10-13):

  • Initial private release. Includes code developed by Alessio Deiana and Federico Poli.

Contributing

Bug reports, feature requests, and other contributions are welcome. If you find a demonstrable problem that is caused by the code of this library, please:

  1. Search for already reported problems.
  2. Check if the issue has been fixed or is still reproducible on the latest master branch.
  3. Create an issue with a test case.

If you create a feature branch, you can run the tests to ensure everything is operating correctly:

$ python setup.py test

...

======= 146 passed in 1.90 seconds ======

Authors

Invenio-Query-Parser is developed for the Invenio digital library software.

Contact us at info@invenio-software.org.