1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- import pyparsing as pp
- class QueryParser:
- def __init__(self):
- """
- Constructs a parser for all named queries using PyParsing.
- """
- extractor = pp.Keyword("random") ^ pp.Keyword("first") ^ pp.Keyword("last")
-
- selector_no_avg = pp.Keyword("most_used") ^ pp.Keyword("least_used") ^ pp.Keyword("all")
-
- attributes_no_avg = pp.Keyword("ipaddress") ^ pp.Keyword("macaddress") ^ pp.Keyword("portnumber") ^\
- pp.Keyword("protocolname") ^ pp.Keyword("winsize") ^ pp.Keyword("ipclass")
-
- attributes_avg = pp.Keyword("ttlvalue") ^ pp.Keyword("mssvalue") ^\
- pp.Keyword("pktssent") ^ pp.Keyword("pktsreceived") ^ pp.Keyword("mss") ^\
- pp.Keyword("kbytesreceived") ^ pp.Keyword("kbytessent")
-
- attributes_all = attributes_no_avg ^ attributes_avg
-
- simple_selector_query = (selector_no_avg + pp.Suppress("(") + attributes_all + pp.Suppress(")")) ^\
- (pp.Keyword("avg") + pp.Suppress("(") + attributes_avg + pp.Suppress(")"))
-
- param_selectors = pp.Keyword("ipaddress").setParseAction(pp.replaceWith("ipaddress_param")) ^\
- pp.Keyword("macaddress").setParseAction(pp.replaceWith("macaddress_param"))
-
- operators = pp.Literal("<=") ^ pp.Literal("<") ^ pp.Literal("=") ^\
- pp.Literal(">=") ^ pp.Literal(">") ^ pp.CaselessLiteral("in")
-
- expr = pp.Forward()
-
- simple_value = pp.Word(pp.alphanums + ".:")
-
-
- parameterized_value = simple_value ^\
- (pp.Suppress("[") + pp.Group(pp.Empty().addParseAction(pp.replaceWith('list')) +
- pp.delimitedList(simple_value)) + pp.Suppress("]"))
-
- comparison = pp.Group(attributes_all + operators + (parameterized_value ^ expr))
-
- parameterized_query = param_selectors + pp.Suppress("(") + pp.Group(pp.delimitedList(comparison)) + pp.Suppress(")")
-
- all_selector_queries = (simple_selector_query ^ parameterized_query)
-
- extractor_selector_query = extractor + pp.Suppress("(") + all_selector_queries + pp.Suppress(")")
-
- named_query = (extractor_selector_query ^ all_selector_queries)
-
- expr << pp.Group(named_query)
-
- self.full_query = named_query + pp.Suppress(";")
- def parse_query(self, querystring: str) -> pp.ParseResults:
- """
- Parses the passed query with a pre-constructed parser.
- :param querystring: The named query to be executed
- :return: A ParseResults-object, which essentially is a list of tokens
- """
- return self.full_query.parseString(querystring)
|