omlish 0.0.0.dev45__py3-none-any.whl → 0.0.0.dev47__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- omlish/.manifests.json +12 -0
- omlish/__about__.py +2 -2
- omlish/specs/__init__.py +0 -1
- omlish/specs/jmespath/LICENSE +16 -0
- omlish/specs/jmespath/__init__.py +20 -0
- omlish/specs/jmespath/__main__.py +11 -0
- omlish/specs/jmespath/ast.py +90 -0
- omlish/specs/jmespath/cli.py +64 -0
- omlish/specs/jmespath/exceptions.py +116 -0
- omlish/specs/jmespath/functions.py +372 -0
- omlish/specs/jmespath/lexer.py +312 -0
- omlish/specs/jmespath/parser.py +587 -0
- omlish/specs/jmespath/visitor.py +344 -0
- {omlish-0.0.0.dev45.dist-info → omlish-0.0.0.dev47.dist-info}/METADATA +1 -1
- {omlish-0.0.0.dev45.dist-info → omlish-0.0.0.dev47.dist-info}/RECORD +19 -9
- {omlish-0.0.0.dev45.dist-info → omlish-0.0.0.dev47.dist-info}/LICENSE +0 -0
- {omlish-0.0.0.dev45.dist-info → omlish-0.0.0.dev47.dist-info}/WHEEL +0 -0
- {omlish-0.0.0.dev45.dist-info → omlish-0.0.0.dev47.dist-info}/entry_points.txt +0 -0
- {omlish-0.0.0.dev45.dist-info → omlish-0.0.0.dev47.dist-info}/top_level.txt +0 -0
omlish/.manifests.json
CHANGED
@@ -34,5 +34,17 @@
|
|
34
34
|
"mod_name": "omlish.diag.pycharm"
|
35
35
|
}
|
36
36
|
}
|
37
|
+
},
|
38
|
+
{
|
39
|
+
"module": ".specs.jmespath.__main__",
|
40
|
+
"attr": "_CLI_MODULE",
|
41
|
+
"file": "omlish/specs/jmespath/__main__.py",
|
42
|
+
"line": 1,
|
43
|
+
"value": {
|
44
|
+
"$omdev.cli.types.CliModule": {
|
45
|
+
"cmd_name": "jp",
|
46
|
+
"mod_name": "omlish.specs.jmespath.__main__"
|
47
|
+
}
|
48
|
+
}
|
37
49
|
}
|
38
50
|
]
|
omlish/__about__.py
CHANGED
omlish/specs/__init__.py
CHANGED
@@ -0,0 +1,16 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2013 Amazon.com, Inc. or its affiliates. All Rights Reserved
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
|
6
|
+
documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
|
7
|
+
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit
|
8
|
+
persons to whom the Software is furnished to do so, subject to the following conditions:
|
9
|
+
|
10
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
|
11
|
+
Software.
|
12
|
+
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
|
14
|
+
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
15
|
+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
16
|
+
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
@@ -0,0 +1,20 @@
|
|
1
|
+
"""
|
2
|
+
TODO:
|
3
|
+
- @omlish-lite
|
4
|
+
"""
|
5
|
+
from . import exceptions # noqa
|
6
|
+
from . import functions # noqa
|
7
|
+
from . import lexer # noqa
|
8
|
+
from . import parser # noqa
|
9
|
+
|
10
|
+
from .parser import ( # noqa
|
11
|
+
compile,
|
12
|
+
search,
|
13
|
+
)
|
14
|
+
|
15
|
+
from .visitor import ( # noqa
|
16
|
+
Options,
|
17
|
+
)
|
18
|
+
|
19
|
+
|
20
|
+
__version__ = '1.0.1'
|
@@ -0,0 +1,90 @@
|
|
1
|
+
# AST nodes have this structure:
|
2
|
+
# {'type': <node type>', children: [], 'value': ''}
|
3
|
+
|
4
|
+
|
5
|
+
def comparator(name, first, second):
|
6
|
+
return {'type': 'comparator', 'children': [first, second], 'value': name}
|
7
|
+
|
8
|
+
|
9
|
+
def current_node():
|
10
|
+
return {'type': 'current', 'children': []}
|
11
|
+
|
12
|
+
|
13
|
+
def expref(expression):
|
14
|
+
return {'type': 'expref', 'children': [expression]}
|
15
|
+
|
16
|
+
|
17
|
+
def function_expression(name, args):
|
18
|
+
return {'type': 'function_expression', 'children': args, 'value': name}
|
19
|
+
|
20
|
+
|
21
|
+
def field(name):
|
22
|
+
return {'type': 'field', 'children': [], 'value': name}
|
23
|
+
|
24
|
+
|
25
|
+
def filter_projection(left, right, comparator):
|
26
|
+
return {'type': 'filter_projection', 'children': [left, right, comparator]}
|
27
|
+
|
28
|
+
|
29
|
+
def flatten(node):
|
30
|
+
return {'type': 'flatten', 'children': [node]}
|
31
|
+
|
32
|
+
|
33
|
+
def identity():
|
34
|
+
return {'type': 'identity', 'children': []}
|
35
|
+
|
36
|
+
|
37
|
+
def index(index):
|
38
|
+
return {'type': 'index', 'value': index, 'children': []}
|
39
|
+
|
40
|
+
|
41
|
+
def index_expression(children):
|
42
|
+
return {'type': 'index_expression', 'children': children}
|
43
|
+
|
44
|
+
|
45
|
+
def key_val_pair(key_name, node):
|
46
|
+
return {'type': 'key_val_pair', 'children': [node], 'value': key_name}
|
47
|
+
|
48
|
+
|
49
|
+
def literal(literal_value):
|
50
|
+
return {'type': 'literal', 'value': literal_value, 'children': []}
|
51
|
+
|
52
|
+
|
53
|
+
def multi_select_dict(nodes):
|
54
|
+
return {'type': 'multi_select_dict', 'children': nodes}
|
55
|
+
|
56
|
+
|
57
|
+
def multi_select_list(nodes):
|
58
|
+
return {'type': 'multi_select_list', 'children': nodes}
|
59
|
+
|
60
|
+
|
61
|
+
def or_expression(left, right):
|
62
|
+
return {'type': 'or_expression', 'children': [left, right]}
|
63
|
+
|
64
|
+
|
65
|
+
def and_expression(left, right):
|
66
|
+
return {'type': 'and_expression', 'children': [left, right]}
|
67
|
+
|
68
|
+
|
69
|
+
def not_expression(expr):
|
70
|
+
return {'type': 'not_expression', 'children': [expr]}
|
71
|
+
|
72
|
+
|
73
|
+
def pipe(left, right):
|
74
|
+
return {'type': 'pipe', 'children': [left, right]}
|
75
|
+
|
76
|
+
|
77
|
+
def projection(left, right):
|
78
|
+
return {'type': 'projection', 'children': [left, right]}
|
79
|
+
|
80
|
+
|
81
|
+
def subexpression(children):
|
82
|
+
return {'type': 'subexpression', 'children': children}
|
83
|
+
|
84
|
+
|
85
|
+
def slice(start, end, step): # noqa
|
86
|
+
return {'type': 'slice', 'children': [start, end, step]}
|
87
|
+
|
88
|
+
|
89
|
+
def value_projection(left, right):
|
90
|
+
return {'type': 'value_projection', 'children': [left, right]}
|
@@ -0,0 +1,64 @@
|
|
1
|
+
#!/usr/bin/env python3
|
2
|
+
import argparse
|
3
|
+
import json
|
4
|
+
import pprint
|
5
|
+
import sys
|
6
|
+
|
7
|
+
from . import exceptions
|
8
|
+
from .parser import compile
|
9
|
+
from .parser import search
|
10
|
+
|
11
|
+
|
12
|
+
def _main():
|
13
|
+
parser = argparse.ArgumentParser()
|
14
|
+
parser.add_argument('expression')
|
15
|
+
parser.add_argument(
|
16
|
+
'-f',
|
17
|
+
'--filename',
|
18
|
+
help='The filename containing the input data. If a filename is not given then data is read from stdin.',
|
19
|
+
)
|
20
|
+
parser.add_argument(
|
21
|
+
'--ast',
|
22
|
+
action='store_true',
|
23
|
+
help='Pretty print the AST, do not search the data.',
|
24
|
+
)
|
25
|
+
args = parser.parse_args()
|
26
|
+
|
27
|
+
expression = args.expression
|
28
|
+
if args.ast:
|
29
|
+
# Only print the AST
|
30
|
+
expression = compile(args.expression)
|
31
|
+
sys.stdout.write(pprint.pformat(expression.parsed))
|
32
|
+
sys.stdout.write('\n')
|
33
|
+
return 0
|
34
|
+
|
35
|
+
if args.filename:
|
36
|
+
with open(args.filename) as f:
|
37
|
+
data = json.load(f)
|
38
|
+
else:
|
39
|
+
data = sys.stdin.read()
|
40
|
+
data = json.loads(data)
|
41
|
+
|
42
|
+
try:
|
43
|
+
sys.stdout.write(json.dumps(search(expression, data), indent=4, ensure_ascii=False))
|
44
|
+
sys.stdout.write('\n')
|
45
|
+
|
46
|
+
except exceptions.ArityError as e:
|
47
|
+
sys.stderr.write(f'invalid-arity: {e}\n')
|
48
|
+
return 1
|
49
|
+
|
50
|
+
except exceptions.JmespathTypeError as e:
|
51
|
+
sys.stderr.write(f'invalid-type: {e}\n')
|
52
|
+
return 1
|
53
|
+
|
54
|
+
except exceptions.UnknownFunctionError as e:
|
55
|
+
sys.stderr.write(f'unknown-function: {e}\n')
|
56
|
+
return 1
|
57
|
+
|
58
|
+
except exceptions.ParseError as e:
|
59
|
+
sys.stderr.write(f'syntax-error: {e}\n')
|
60
|
+
return 1
|
61
|
+
|
62
|
+
|
63
|
+
if __name__ == '__main__':
|
64
|
+
sys.exit(_main())
|
@@ -0,0 +1,116 @@
|
|
1
|
+
class JmespathError(ValueError):
|
2
|
+
pass
|
3
|
+
|
4
|
+
|
5
|
+
class ParseError(JmespathError):
|
6
|
+
_ERROR_MESSAGE = 'Invalid jmespath expression'
|
7
|
+
|
8
|
+
def __init__(
|
9
|
+
self,
|
10
|
+
lex_position,
|
11
|
+
token_value,
|
12
|
+
token_type,
|
13
|
+
msg=_ERROR_MESSAGE,
|
14
|
+
):
|
15
|
+
super().__init__(lex_position, token_value, token_type)
|
16
|
+
self.lex_position = lex_position
|
17
|
+
self.token_value = token_value
|
18
|
+
self.token_type = token_type.upper()
|
19
|
+
self.msg = msg
|
20
|
+
# Whatever catches the ParseError can fill in the full expression
|
21
|
+
self.expression = None
|
22
|
+
|
23
|
+
def __str__(self):
|
24
|
+
# self.lex_position +1 to account for the starting double quote char.
|
25
|
+
underline = ' ' * (self.lex_position + 1) + '^'
|
26
|
+
return (
|
27
|
+
f'{self.msg}: Parse error at column {self.lex_position}, '
|
28
|
+
f'token "{self.token_value}" ({self.token_type}), for expression:\n"{self.expression}"\n{underline}'
|
29
|
+
)
|
30
|
+
|
31
|
+
|
32
|
+
class IncompleteExpressionError(ParseError):
|
33
|
+
def set_expression(self, expression):
|
34
|
+
self.expression = expression
|
35
|
+
self.lex_position = len(expression)
|
36
|
+
self.token_type = None
|
37
|
+
self.token_value = None
|
38
|
+
|
39
|
+
def __str__(self):
|
40
|
+
# self.lex_position +1 to account for the starting double quote char.
|
41
|
+
underline = ' ' * (self.lex_position + 1) + '^'
|
42
|
+
return (
|
43
|
+
f'Invalid jmespath expression: Incomplete expression:\n'
|
44
|
+
f'"{self.expression}"\n{underline}'
|
45
|
+
)
|
46
|
+
|
47
|
+
|
48
|
+
class LexerError(ParseError):
|
49
|
+
def __init__(self, lexer_position, lexer_value, message, expression=None):
|
50
|
+
self.lexer_position = lexer_position
|
51
|
+
self.lexer_value = lexer_value
|
52
|
+
self.message = message
|
53
|
+
super().__init__(lexer_position, lexer_value, message)
|
54
|
+
# Whatever catches LexerError can set this.
|
55
|
+
self.expression = expression
|
56
|
+
|
57
|
+
def __str__(self):
|
58
|
+
underline = ' ' * self.lexer_position + '^'
|
59
|
+
return f'Bad jmespath expression: {self.message}:\n{self.expression}\n{underline}'
|
60
|
+
|
61
|
+
|
62
|
+
class ArityError(ParseError):
|
63
|
+
def __init__(self, expected, actual, name):
|
64
|
+
self.expected_arity = expected
|
65
|
+
self.actual_arity = actual
|
66
|
+
self.function_name = name
|
67
|
+
self.expression = None
|
68
|
+
|
69
|
+
def __str__(self):
|
70
|
+
return (
|
71
|
+
f"Expected {self.expected_arity} {self._pluralize('argument', self.expected_arity)} "
|
72
|
+
f'for function {self.function_name}(), received {self.actual_arity}'
|
73
|
+
)
|
74
|
+
|
75
|
+
def _pluralize(self, word, count):
|
76
|
+
if count == 1:
|
77
|
+
return word
|
78
|
+
else:
|
79
|
+
return word + 's'
|
80
|
+
|
81
|
+
|
82
|
+
class VariadicArityError(ArityError):
|
83
|
+
def __str__(self):
|
84
|
+
return (
|
85
|
+
f"Expected at least {self.expected_arity} {self._pluralize('argument', self.expected_arity)} "
|
86
|
+
f'for function {self.function_name}(), received {self.actual_arity}'
|
87
|
+
)
|
88
|
+
|
89
|
+
|
90
|
+
class JmespathTypeError(JmespathError):
|
91
|
+
def __init__(
|
92
|
+
self,
|
93
|
+
function_name,
|
94
|
+
current_value,
|
95
|
+
actual_type,
|
96
|
+
expected_types,
|
97
|
+
):
|
98
|
+
self.function_name = function_name
|
99
|
+
self.current_value = current_value
|
100
|
+
self.actual_type = actual_type
|
101
|
+
self.expected_types = expected_types
|
102
|
+
|
103
|
+
def __str__(self):
|
104
|
+
return (
|
105
|
+
f'In function {self.function_name}(), invalid type for value: {self.current_value}, '
|
106
|
+
f'expected one of: {self.expected_types}, received: "{self.actual_type}"'
|
107
|
+
)
|
108
|
+
|
109
|
+
|
110
|
+
class EmptyExpressionError(JmespathError):
|
111
|
+
def __init__(self):
|
112
|
+
super().__init__('Invalid Jmespath expression: cannot be empty.')
|
113
|
+
|
114
|
+
|
115
|
+
class UnknownFunctionError(JmespathError):
|
116
|
+
pass
|