OXIESEC PANEL
- Current Dir:
/
/
opt
/
gsutil
/
third_party
/
pyparsing
/
examples
Server IP: 2a02:4780:11:1594:0:ef5:22d7:a
Upload:
Create Dir:
Name
Size
Modified
Perms
📁
..
-
12/11/2024 09:39:44 AM
rwxr-xr-x
📄
0README.html
10.75 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
AcManForm.dfm
38.22 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
LAparser.py
20.62 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
Setup.ini
1.41 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
SimpleCalc.py
3.58 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
SingleForm.dfm
42.7 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
TAP.py
7.42 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
__init__.py
0 bytes
08/25/2024 02:43:42 PM
rw-r--r--
📄
adventureEngine.py
21.59 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
antlr_grammar.py
10.88 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
antlr_grammar_tests.py
2.89 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
apicheck.py
2.2 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
bf.py
4.15 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
bigquery_view_parser.py
62.51 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
booleansearchparser.py
15.26 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
btpyparse.py
4.16 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
builtin_parse_action_demo.py
817 bytes
08/25/2024 02:43:42 PM
rw-r--r--
📄
cLibHeader.py
853 bytes
08/25/2024 02:43:42 PM
rw-r--r--
📄
chemical_formulas.py
3.17 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
commasep.py
707 bytes
08/25/2024 02:43:42 PM
rw-r--r--
📄
configParse.py
2 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
cpp_enum_parser.py
1.35 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
cuneiform_python.py
2.57 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
datetime_parse_actions.py
1.9 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
decaf_parser.py
6.87 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
delta_time.py
21.49 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
dfmparse.py
7.05 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
dhcpd_leases_parser.py
2.98 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
dictExample.py
1.73 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
dictExample2.py
2.1 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
directx_x_file_parser.py
5.54 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
ebnf.py
4.15 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
ebnftest.py
2.35 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
email_address_parser.py
1.18 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
eval_arith.py
6.15 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
excel_expr.py
2.2 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
fourFn.py
10.27 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
gen_ctypes.py
5.28 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
getNTPserversNew.py
1.11 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
greeting.py
518 bytes
08/25/2024 02:43:42 PM
rw-r--r--
📄
greetingInGreek.py
425 bytes
08/25/2024 02:43:42 PM
rw-r--r--
📄
greetingInKorean.py
495 bytes
08/25/2024 02:43:42 PM
rw-r--r--
📄
groupUsingListAllMatches.py
509 bytes
08/25/2024 02:43:42 PM
rw-r--r--
📄
hola_mundo.py
2.02 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
html_stripper.py
1.68 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
html_table_parser.py
2.16 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
httpServerLogParser.py
3.48 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
idlParse.py
7.53 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
include_preprocessor.py
2.63 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
indentedGrammarExample.py
1022 bytes
08/25/2024 02:43:42 PM
rw-r--r--
📄
indented_block_example.py
712 bytes
08/25/2024 02:43:42 PM
rw-r--r--
📄
inv_regex.py
8.08 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
javascript_grammar.g
18.1 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
jsonParser.py
4.7 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
left_recursion.py
1.41 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
lineno_example.py
1.49 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
listAllMatches.py
1.57 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
lua_parser.py
7.96 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
lucene_grammar.py
8.46 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
macro_expander.py
1.77 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
make_diagram.py
1.62 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
matchPreviousDemo.py
586 bytes
08/25/2024 02:43:42 PM
rw-r--r--
📄
mongodb_query_expression.py
8.46 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
mozilla.ics
752 bytes
08/25/2024 02:43:42 PM
rw-r--r--
📄
mozillaCalendarParser.py
2.71 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
nested.py
579 bytes
08/25/2024 02:43:42 PM
rw-r--r--
📄
nested_markup.py
1.85 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
number_words.py
3.77 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
numerics.py
1.34 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
oc.py
4.34 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
one_to_ninety_nine.py
1.88 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
parseTabularData.py
1.68 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
parse_python_value.py
2.5 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
parse_results_sum_example.py
853 bytes
08/25/2024 02:43:42 PM
rw-r--r--
📄
partial_gene_match.py
2.33 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
pgn.py
3.34 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
position.py
2.17 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
protobuf_parser.py
3.89 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
pythonGrammarParser.py
8.08 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
railroad_diagram_demo.py
1.55 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
range_check.py
2.84 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
readJson.py
120.73 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
removeLineBreaks.py
1.8 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
roman_numerals.py
3.57 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
rosettacode.py
7.43 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
scanExamples.py
2.36 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
searchParserAppDemo.py
962 bytes
08/25/2024 02:43:42 PM
rw-r--r--
📄
searchparser.py
10.4 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
select_parser.py
8.32 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
sexpParser.py
5.23 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
shapes.py
1.69 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
simpleArith.py
2.42 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
simpleBool.py
3.19 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
simpleSQL.py
3.16 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
simpleWiki.py
1.08 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
snmp_api.h
28.69 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
sql2dot.py
3.07 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
stackish.py
2.71 KB
08/25/2024 02:43:42 PM
rw-r--r--
📁
statemachine
-
08/25/2024 02:43:42 PM
rwxr-xr-x
📄
tag_emitter.py
1.17 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
tag_metadata.py
805 bytes
08/25/2024 02:43:42 PM
rw-r--r--
📄
test_bibparse.py
8.72 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
unicode_denormalizer.py
5.02 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
urlExtractor.py
1.03 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
urlExtractorNew.py
2.4 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
verilog_parse.py
31.15 KB
08/25/2024 02:43:42 PM
rw-r--r--
📄
withAttribute.py
912 bytes
08/25/2024 02:43:42 PM
rw-r--r--
📄
wordsToNum.py
2.92 KB
08/25/2024 02:43:42 PM
rw-r--r--
Editing: mongodb_query_expression.py
Close
# # mongodb_conditional_expression.py # # Pyparsing parser and wrapper method to parse infix arithmetic and boolean # expressions and transform them to MongoDB's nested dict queries with # associated operator tags. # # Example: # mongo_query = transform_query("100 < a <= 200") # print(mongo_query) # # Prints: # {'$and': [{'a': {'$gt': 100}}, {'a': {'$lte': 200}}]} # # Copyright 2024, Paul McGuire # from functools import reduce from operator import or_ from typing import Union, Dict import pyparsing as pp pp.ParserElement.enable_packrat() ppc = pp.common __all__ = [ "query_condition_expr", "query_condition_expr_with_comment", "transform_query", ] class InvalidExpressionException(pp.ParseFatalException): pass def key_phrase(expr: Union[str, pp.ParserElement]) -> pp.ParserElement: if isinstance(expr, str): expr = pp.And(pp.CaselessKeyword.using_each(expr.split())) return pp.Combine(expr, adjacent=False, join_string=" ") ident = ppc.identifier() integer = ppc.integer() num = ppc.number() LBRACK, RBRACK = pp.Suppress.using_each("[]") operand = ident | (pp.QuotedString('"') | pp.QuotedString("'")).set_name("quoted_string") | num operand.set_name("operand") operand_list = pp.Group(LBRACK + pp.DelimitedList(operand) + RBRACK, aslist=True) AND, OR, NOT, IN, CONTAINS, ALL, NONE = pp.CaselessKeyword.using_each( "and or not in contains all none".split() ) NOT_IN = key_phrase(NOT + IN) CONTAINS_ALL = key_phrase(CONTAINS + ALL) CONTAINS_NONE = key_phrase(CONTAINS + NONE) def binary_eq_neq(s, l, tokens): a, op, b = tokens[0] try: {a: None} except TypeError as te: raise InvalidExpressionException( s, l, f"Could not create query expression using field {a!r}" ) from te if op in ("=", "=="): return {a: b} return { a: { "$ne": b } } def binary_comparison_op(s, l, tokens): tokens = tokens[0] binary_map = { "<": "$lt", ">": "$gt", "<=": "$lte", ">=": "$gte", "!=": "$ne", "in": "$in", "not in": "$nin", "contains all": "$all", # add Unicode operators, because we can "≤": "$lte", "≥": "$gte", "≠": "$ne", "⊇": "$all", "∈": "$in", "∉": "$nin", } inequality_inv_map = { "<": "$gt", ">": "$lt", "<=": "$gte", ">=": "$lte", "≤": "$gte", "≥": "$lte", } operator_compatibility_map = { "<": {"<", "<=", "≤"}, ">": {">", ">=", "≥"}, "<=": {"<", "<=", "≤"}, ">=": {">", ">=", "≥"}, "≤": {"<", "<=", "≤"}, "≥": {">", ">=", "≥"}, } try: field, op, value = tokens except ValueError: # special handling for 'x < field < y' if len(tokens) == 5: a, op1, field, op2, b = tokens for op_ in (op1, op2): if op_ not in inequality_inv_map: raise InvalidExpressionException( s, l, f"{op_} cannot be used in a chained expression" ) if op2 not in operator_compatibility_map[op1]: raise InvalidExpressionException( s, l, f"cannot chain {op1!r} and {op2!r} in the same expression" ) op1 = inequality_inv_map[op1] op2 = binary_map[op2] return binary_multi_op( [ [{field: {op1: a}}, "and", {field: {op2: b}}] ] ) raise InvalidExpressionException( s, l, f"{tokens[1]!r} comparison operator may not be chained with more than 2 terms" ) if op == "contains none": return { "$nor": [ {field: {"$elemMatch": {"$eq": v}}} for v in value ] } return {field: {binary_map[op]: value}} def binary_multi_op(tokens): tokens = tokens[0] oper_map = { "and": "$and", "or": "$or", "not": "$not", } op = oper_map[tokens[1]] values = tokens[::2] # detect 'and' with all equality checks, collapse to single dict if ( op == "$and" and not any( isinstance(v, (dict, list)) for dd in values for v in dd.values() ) ): try: ret = reduce(or_, values) except TypeError: # compatibility for pre-Python 3.9 versions ret = {} for v in values: ret = {**ret, **v} return ret return {op: values} def unary_op(tokens): tokens = tokens[0] oper_map = { "not": "$not", } op, value = tokens # detect 'not not' k, v = next(iter(value.items())) if k == "$not": return v return {oper_map[op]: value} comparison_expr = pp.infix_notation( operand | operand_list, [ (pp.one_of("<= >= < > ≤ ≥"), 2, pp.OpAssoc.LEFT, binary_comparison_op), (pp.one_of("= == != ≠"), 2, pp.OpAssoc.LEFT, binary_eq_neq), (IN | NOT_IN | CONTAINS_ALL | CONTAINS_NONE | pp.one_of("⊇ ∈ ∉"), 2, pp.OpAssoc.LEFT, binary_comparison_op), ] ) # "not" operator only matches if not followed by "in" NOT_OP = NOT + ~IN AND_OP = AND | pp.Literal("∧").add_parse_action(pp.replace_with("and")) OR_OP = OR | pp.Literal("∨").add_parse_action(pp.replace_with("or")) query_condition_expr = pp.infix_notation( comparison_expr | ident, [ (NOT_OP, 1, pp.OpAssoc.RIGHT, unary_op), (AND_OP, 2, pp.OpAssoc.LEFT, binary_multi_op), (OR_OP, 2, pp.OpAssoc.LEFT, binary_multi_op), ] ) # add $comment containing the original expression string query_condition_expr_with_comment = pp.And([query_condition_expr]) query_condition_expr_with_comment.add_parse_action( lambda s, l, t: t[0].__setitem__("$comment", s) ) def transform_query(query_string: str, include_comment: bool = False) -> Dict: """ Parse a query string using boolean and arithmetic comparison operations, and convert it to a dict for the expression equivalent using MongoDB query expression structure. Examples: a = 100 and b = 200 {'a': 100, 'b': 200} a==100 and b>=200 {'$and': [{'a': 100}, {'b': {'$gte': 200}}]} a==100 and not (b>=200 or c<200) {'$and': [{'a': 100}, {'$not': {'$or': [{'b': {'$gte': 200}}, {'c': {'$lt': 200}}]}}]} name in ["Alice", "Bob"] {'name': {'$in': ['Alice', 'Bob']}} Also supported: - chained inequalities 100 < a < 200 {'$and': [{'a': {'$gt': 100}}, {'a': {'$lt': 200}}]} - `in` and `not in` name in ["Alice", "Bob"] {'name': {'$in': ['Alice', 'Bob']}} - `contains all` names contains all ["Alice", "Bob"] {'names': {'$all': ['Alice', 'Bob']}} - Unicode operators 100 < a ≤ 200 and 300 > b ≥ 200 or c ≠ -1 100 < a ≤ 200 ∧ 300 > b ≥ 200 ∨ c ≠ -1 name ∈ ["Alice", "Bob"] name ∉ ["Alice", "Bob"] names ⊇ ["Alice", "Bob"] """ generator_expr = ( query_condition_expr_with_comment if include_comment else query_condition_expr ) return generator_expr.parse_string(query_string)[0] def main(): from textwrap import dedent for test in dedent("""\ a = 100 a = 100 and b = 200 a > b a==100 and b>=200 a==100 and b>=200 or c<200 a==100 and (b>=200 or c<200) a==100 and not (b>=200 or c<200) xyz < 2000 and abc > 32 xyz < 2000 and abc > 32 and def == 100 xyz == 2000 and abc == 32 and def == 100 xyz == 2000 or abc == '32' or def == "foo" 100 < a < 200 a==100 and not (100 < b <= 200) 1900 < "wine vintage" < 2000 name > "M" 100 < a ≤ 200 or 300 > b ≥ 200 or c ≠ -1 100 < a ≤ 200 ∧ 300 > b ≥ 200 ∧ c ≠ -1 100 < a ≤ 200 ∨ 300 > b ≥ 200 ∨ c ≠ -1 a==100 and not not (a > 100) a==100 and not not not (a > 100) a==100 and not not not not (a > 100) name in ["Alice", "Bob"] name ∈ ["Alice", "Bob"] name not in ["Alice", "Bob"] name ∉ ["Alice", "Bob"] names contains all ["Alice", "Bob"] names ⊇ ["Alice", "Bob"] names contains none ["Alice", "Bob"] """).splitlines(): print(test) print(transform_query(test)) print() if __name__ == '__main__': main()