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
@@ -0,0 +1,344 @@
|
|
1
|
+
import numbers
|
2
|
+
import operator
|
3
|
+
import typing as ta
|
4
|
+
|
5
|
+
from . import functions
|
6
|
+
|
7
|
+
|
8
|
+
def _equals(x, y):
|
9
|
+
if _is_special_number_case(x, y):
|
10
|
+
return False
|
11
|
+
else:
|
12
|
+
return x == y
|
13
|
+
|
14
|
+
|
15
|
+
def _is_special_number_case(x, y):
|
16
|
+
# We need to special case comparing 0 or 1 to True/False. While normally comparing any integer other than 0/1 to
|
17
|
+
# True/False will always return False. However 0/1 have this:
|
18
|
+
# >>> 0 == True
|
19
|
+
# False
|
20
|
+
# >>> 0 == False
|
21
|
+
# True
|
22
|
+
# >>> 1 == True
|
23
|
+
# True
|
24
|
+
# >>> 1 == False
|
25
|
+
# False
|
26
|
+
#
|
27
|
+
# Also need to consider that:
|
28
|
+
# >>> 0 in [True, False]
|
29
|
+
# True
|
30
|
+
if _is_actual_number(x) and x in (0, 1):
|
31
|
+
return isinstance(y, bool)
|
32
|
+
|
33
|
+
elif _is_actual_number(y) and y in (0, 1):
|
34
|
+
return isinstance(x, bool)
|
35
|
+
|
36
|
+
else:
|
37
|
+
return None
|
38
|
+
|
39
|
+
|
40
|
+
def _is_comparable(x):
|
41
|
+
# The spec doesn't officially support string types yet, but enough people are relying on this behavior that it's
|
42
|
+
# been added back. This should eventually become part of the official spec.
|
43
|
+
return _is_actual_number(x) or isinstance(x, str)
|
44
|
+
|
45
|
+
|
46
|
+
def _is_actual_number(x):
|
47
|
+
# We need to handle python's quirkiness with booleans, specifically:
|
48
|
+
#
|
49
|
+
# >>> isinstance(False, int)
|
50
|
+
# True
|
51
|
+
# >>> isinstance(True, int)
|
52
|
+
# True
|
53
|
+
if isinstance(x, bool):
|
54
|
+
return False
|
55
|
+
return isinstance(x, numbers.Number)
|
56
|
+
|
57
|
+
|
58
|
+
class Options:
|
59
|
+
"""Options to control how a Jmespath function is evaluated."""
|
60
|
+
|
61
|
+
def __init__(self, dict_cls=None, custom_functions=None):
|
62
|
+
#: The class to use when creating a dict. The interpreter may create dictionaries during the evaluation of a
|
63
|
+
# Jmespath expression. For example, a multi-select hash will create a dictionary. By default we use a dict()
|
64
|
+
# type. You can set this value to change what dict type is used. The most common reason you would change this
|
65
|
+
# is if you want to set a collections.OrderedDict so that you can have predictable key ordering.
|
66
|
+
self.dict_cls = dict_cls
|
67
|
+
self.custom_functions = custom_functions
|
68
|
+
|
69
|
+
|
70
|
+
class _Expression:
|
71
|
+
def __init__(self, expression, interpreter):
|
72
|
+
self.expression = expression
|
73
|
+
self.interpreter = interpreter
|
74
|
+
|
75
|
+
def visit(self, node, *args, **kwargs):
|
76
|
+
return self.interpreter.visit(node, *args, **kwargs)
|
77
|
+
|
78
|
+
|
79
|
+
class Visitor:
|
80
|
+
def __init__(self):
|
81
|
+
self._method_cache = {}
|
82
|
+
|
83
|
+
def visit(self, node, *args, **kwargs):
|
84
|
+
node_type = node['type']
|
85
|
+
method = self._method_cache.get(node_type)
|
86
|
+
if method is None:
|
87
|
+
method = getattr(self, f'visit_{node["type"]}', self.default_visit)
|
88
|
+
self._method_cache[node_type] = method
|
89
|
+
return method(node, *args, **kwargs) # type: ignore
|
90
|
+
|
91
|
+
def default_visit(self, node, *args, **kwargs):
|
92
|
+
raise NotImplementedError('default_visit')
|
93
|
+
|
94
|
+
|
95
|
+
class TreeInterpreter(Visitor):
|
96
|
+
COMPARATOR_FUNC: ta.Mapping[str, ta.Callable] = {
|
97
|
+
'eq': _equals,
|
98
|
+
'ne': lambda x, y: not _equals(x, y),
|
99
|
+
'lt': operator.lt,
|
100
|
+
'gt': operator.gt,
|
101
|
+
'lte': operator.le,
|
102
|
+
'gte': operator.ge,
|
103
|
+
}
|
104
|
+
|
105
|
+
_EQUALITY_OPS: ta.Sequence[str] = ['eq', 'ne']
|
106
|
+
|
107
|
+
MAP_TYPE = dict
|
108
|
+
|
109
|
+
def __init__(self, options=None):
|
110
|
+
super().__init__()
|
111
|
+
|
112
|
+
self._dict_cls = self.MAP_TYPE
|
113
|
+
|
114
|
+
if options is None:
|
115
|
+
options = Options()
|
116
|
+
self._options = options
|
117
|
+
|
118
|
+
if options.dict_cls is not None:
|
119
|
+
self._dict_cls = self._options.dict_cls
|
120
|
+
|
121
|
+
if options.custom_functions is not None:
|
122
|
+
self._functions = self._options.custom_functions
|
123
|
+
else:
|
124
|
+
self._functions = functions.Functions()
|
125
|
+
|
126
|
+
def default_visit(self, node, *args, **kwargs):
|
127
|
+
raise NotImplementedError(node['type'])
|
128
|
+
|
129
|
+
def visit_subexpression(self, node, value):
|
130
|
+
result = value
|
131
|
+
for child in node['children']:
|
132
|
+
result = self.visit(child, result)
|
133
|
+
return result
|
134
|
+
|
135
|
+
def visit_field(self, node, value):
|
136
|
+
try:
|
137
|
+
return value.get(node['value'])
|
138
|
+
except AttributeError:
|
139
|
+
return None
|
140
|
+
|
141
|
+
def visit_comparator(self, node, value):
|
142
|
+
# Common case: comparator is == or !=
|
143
|
+
comparator_func = self.COMPARATOR_FUNC[node['value']]
|
144
|
+
if node['value'] in self._EQUALITY_OPS:
|
145
|
+
return comparator_func(
|
146
|
+
self.visit(node['children'][0], value),
|
147
|
+
self.visit(node['children'][1], value),
|
148
|
+
)
|
149
|
+
|
150
|
+
else:
|
151
|
+
# Ordering operators are only valid for numbers. Evaluating any other type with a comparison operator will
|
152
|
+
# yield a None value.
|
153
|
+
left = self.visit(node['children'][0], value)
|
154
|
+
right = self.visit(node['children'][1], value)
|
155
|
+
# num_types = (int, float)
|
156
|
+
if not (_is_comparable(left) and _is_comparable(right)):
|
157
|
+
return None
|
158
|
+
return comparator_func(left, right)
|
159
|
+
|
160
|
+
def visit_current(self, node, value):
|
161
|
+
return value
|
162
|
+
|
163
|
+
def visit_expref(self, node, value):
|
164
|
+
return _Expression(node['children'][0], self)
|
165
|
+
|
166
|
+
def visit_function_expression(self, node, value):
|
167
|
+
resolved_args = []
|
168
|
+
for child in node['children']:
|
169
|
+
current = self.visit(child, value)
|
170
|
+
resolved_args.append(current)
|
171
|
+
|
172
|
+
return self._functions.call_function(node['value'], resolved_args)
|
173
|
+
|
174
|
+
def visit_filter_projection(self, node, value):
|
175
|
+
base = self.visit(node['children'][0], value)
|
176
|
+
if not isinstance(base, list):
|
177
|
+
return None
|
178
|
+
|
179
|
+
comparator_node = node['children'][2]
|
180
|
+
collected = []
|
181
|
+
for element in base:
|
182
|
+
if self._is_true(self.visit(comparator_node, element)):
|
183
|
+
current = self.visit(node['children'][1], element)
|
184
|
+
if current is not None:
|
185
|
+
collected.append(current)
|
186
|
+
|
187
|
+
return collected
|
188
|
+
|
189
|
+
def visit_flatten(self, node, value):
|
190
|
+
base = self.visit(node['children'][0], value)
|
191
|
+
if not isinstance(base, list):
|
192
|
+
# Can't flatten the object if it's not a list.
|
193
|
+
return None
|
194
|
+
|
195
|
+
merged_list = []
|
196
|
+
for element in base:
|
197
|
+
if isinstance(element, list):
|
198
|
+
merged_list.extend(element)
|
199
|
+
else:
|
200
|
+
merged_list.append(element)
|
201
|
+
|
202
|
+
return merged_list
|
203
|
+
|
204
|
+
def visit_identity(self, node, value):
|
205
|
+
return value
|
206
|
+
|
207
|
+
def visit_index(self, node, value):
|
208
|
+
# Even though we can index strings, we don't want to support that.
|
209
|
+
if not isinstance(value, list):
|
210
|
+
return None
|
211
|
+
|
212
|
+
try:
|
213
|
+
return value[node['value']]
|
214
|
+
except IndexError:
|
215
|
+
return None
|
216
|
+
|
217
|
+
def visit_index_expression(self, node, value):
|
218
|
+
result = value
|
219
|
+
for child in node['children']:
|
220
|
+
result = self.visit(child, result)
|
221
|
+
|
222
|
+
return result
|
223
|
+
|
224
|
+
def visit_slice(self, node, value):
|
225
|
+
if not isinstance(value, list):
|
226
|
+
return None
|
227
|
+
s = slice(*node['children'])
|
228
|
+
return value[s]
|
229
|
+
|
230
|
+
def visit_key_val_pair(self, node, value):
|
231
|
+
return self.visit(node['children'][0], value)
|
232
|
+
|
233
|
+
def visit_literal(self, node, value):
|
234
|
+
return node['value']
|
235
|
+
|
236
|
+
def visit_multi_select_dict(self, node, value):
|
237
|
+
if value is None:
|
238
|
+
return None
|
239
|
+
|
240
|
+
collected = self._dict_cls()
|
241
|
+
for child in node['children']:
|
242
|
+
collected[child['value']] = self.visit(child, value)
|
243
|
+
|
244
|
+
return collected
|
245
|
+
|
246
|
+
def visit_multi_select_list(self, node, value):
|
247
|
+
if value is None:
|
248
|
+
return None
|
249
|
+
|
250
|
+
collected = []
|
251
|
+
for child in node['children']:
|
252
|
+
collected.append(self.visit(child, value))
|
253
|
+
|
254
|
+
return collected
|
255
|
+
|
256
|
+
def visit_or_expression(self, node, value):
|
257
|
+
matched = self.visit(node['children'][0], value)
|
258
|
+
|
259
|
+
if self._is_false(matched):
|
260
|
+
matched = self.visit(node['children'][1], value)
|
261
|
+
|
262
|
+
return matched
|
263
|
+
|
264
|
+
def visit_and_expression(self, node, value):
|
265
|
+
matched = self.visit(node['children'][0], value)
|
266
|
+
|
267
|
+
if self._is_false(matched):
|
268
|
+
return matched
|
269
|
+
|
270
|
+
return self.visit(node['children'][1], value)
|
271
|
+
|
272
|
+
def visit_not_expression(self, node, value):
|
273
|
+
original_result = self.visit(node['children'][0], value)
|
274
|
+
|
275
|
+
if _is_actual_number(original_result) and original_result == 0:
|
276
|
+
# Special case for 0, !0 should be false, not true. 0 is not a special cased integer in jmespath.
|
277
|
+
return False
|
278
|
+
|
279
|
+
return not original_result
|
280
|
+
|
281
|
+
def visit_pipe(self, node, value):
|
282
|
+
result = value
|
283
|
+
for child in node['children']:
|
284
|
+
result = self.visit(child, result)
|
285
|
+
return result
|
286
|
+
|
287
|
+
def visit_projection(self, node, value):
|
288
|
+
base = self.visit(node['children'][0], value)
|
289
|
+
if not isinstance(base, list):
|
290
|
+
return None
|
291
|
+
|
292
|
+
collected = []
|
293
|
+
for element in base:
|
294
|
+
current = self.visit(node['children'][1], element)
|
295
|
+
if current is not None:
|
296
|
+
collected.append(current)
|
297
|
+
|
298
|
+
return collected
|
299
|
+
|
300
|
+
def visit_value_projection(self, node, value):
|
301
|
+
base = self.visit(node['children'][0], value)
|
302
|
+
try:
|
303
|
+
base = base.values()
|
304
|
+
except AttributeError:
|
305
|
+
return None
|
306
|
+
|
307
|
+
collected = []
|
308
|
+
for element in base:
|
309
|
+
current = self.visit(node['children'][1], element)
|
310
|
+
if current is not None:
|
311
|
+
collected.append(current)
|
312
|
+
|
313
|
+
return collected
|
314
|
+
|
315
|
+
def _is_false(self, value):
|
316
|
+
# This looks weird, but we're explicitly using equality checks because the truth/false values are different
|
317
|
+
# between python and jmespath.
|
318
|
+
return (value == '' or value == [] or value == {} or value is None or value is False) # noqa
|
319
|
+
|
320
|
+
def _is_true(self, value):
|
321
|
+
return not self._is_false(value)
|
322
|
+
|
323
|
+
|
324
|
+
class GraphvizVisitor(Visitor):
|
325
|
+
def __init__(self):
|
326
|
+
super().__init__()
|
327
|
+
self._lines = []
|
328
|
+
self._count = 1
|
329
|
+
|
330
|
+
def visit(self, node, *args, **kwargs):
|
331
|
+
self._lines.append('digraph AST {')
|
332
|
+
current = f"{node['type']}{self._count}"
|
333
|
+
self._count += 1
|
334
|
+
self._visit(node, current)
|
335
|
+
self._lines.append('}')
|
336
|
+
return '\n'.join(self._lines)
|
337
|
+
|
338
|
+
def _visit(self, node, current):
|
339
|
+
self._lines.append('%s [label="%s(%s)"]' % (current, node['type'], node.get('value', ''))) # noqa
|
340
|
+
for child in node.get('children', []):
|
341
|
+
child_name = f"{child['type']}{self._count}"
|
342
|
+
self._count += 1
|
343
|
+
self._lines.append(f' {current} -> {child_name}')
|
344
|
+
self._visit(child, child_name)
|
@@ -1,5 +1,5 @@
|
|
1
|
-
omlish/.manifests.json,sha256=
|
2
|
-
omlish/__about__.py,sha256=
|
1
|
+
omlish/.manifests.json,sha256=sAddTbCUKdX0UYA4PeZ_z9JKF0vyzEpFYWq1tAAl9bw,1091
|
2
|
+
omlish/__about__.py,sha256=_RRLEqykT0ujsHiOho919ore-XgZvArOXLbbLo2v0oo,2919
|
3
3
|
omlish/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
4
4
|
omlish/argparse.py,sha256=Vr70_85EVLJLgEkRtwOr264tMRtqtlN7ncFfXRUk5aM,6914
|
5
5
|
omlish/c3.py,sha256=4vogWgwPb8TbNS2KkZxpoWbwjj7MuHG2lQG-hdtkvjI,8062
|
@@ -262,7 +262,17 @@ omlish/secrets/openssl.py,sha256=wxA_wIlxtuOUy71ABxAJgavh-UI_taOfm-A0dVlmSwM,621
|
|
262
262
|
omlish/secrets/passwords.py,sha256=3r-vEK6Gp6aq4L5Csnd06QnrjO9xfzHJP-g_7I9W_ao,4101
|
263
263
|
omlish/secrets/secrets.py,sha256=ClD7t_mkmWkseVk4ahLzYLuLXeTxiwwPiidYm42vLh4,6871
|
264
264
|
omlish/secrets/subprocesses.py,sha256=EcnKlHHtnUMHGrBWXDfu8tv28wlgZx4P4GOiuPW9Vo8,1105
|
265
|
-
omlish/specs/__init__.py,sha256=
|
265
|
+
omlish/specs/__init__.py,sha256=Xl4fT1o1MlcEIAjMt5EifgMuO4UBSa9Suj5NE9eMX1A,87
|
266
|
+
omlish/specs/jmespath/LICENSE,sha256=IH-ZZlZkS8XMkf_ubNVD1aYHQ2l_wd0tmHtXrCcYpRU,1113
|
267
|
+
omlish/specs/jmespath/__init__.py,sha256=JFNIeqDvkuZQ036_SVD_uQiPLIThOmkLApdS_0GAtNk,281
|
268
|
+
omlish/specs/jmespath/__main__.py,sha256=pZ4PnDWEtvTSpVOsNswkE9esg6Wu1i1b3P2aLvwBnKc,186
|
269
|
+
omlish/specs/jmespath/ast.py,sha256=mclYjkd1b7-xYTLhzJ5QRRrYMKvDqRtnAj-xZhAHj1Q,2138
|
270
|
+
omlish/specs/jmespath/cli.py,sha256=ORM5tEeHr9pUtRfAHImfB0ObQQ8VOTR4X48U4-kNoOM,1633
|
271
|
+
omlish/specs/jmespath/exceptions.py,sha256=5cCVkGFh524Rj42JVWu7PznqK1odXK9h9sCwCpx-5nY,3697
|
272
|
+
omlish/specs/jmespath/functions.py,sha256=VxGkGHRyUvo3UCP-BS0NMUirpB7fbxUD-rBmWGiUdf4,12273
|
273
|
+
omlish/specs/jmespath/lexer.py,sha256=6ug4Mw1PtxYP6FUo41f6Pcdh7rWWrkasztK6uJ4B3Xw,9735
|
274
|
+
omlish/specs/jmespath/parser.py,sha256=qSpTkjmR9MSdKShVjUqSPI9eV6WoB1t8DkKTdxFZfQE,19308
|
275
|
+
omlish/specs/jmespath/visitor.py,sha256=u0poV2BkJmYJzFqjA2Hb86K8LXKWiic4ICKM-FffC2o,10672
|
266
276
|
omlish/specs/jsonrpc/__init__.py,sha256=E0EogYSKmbj1D-V57tBgPDTyVuD8HosHqVg0Vh1CVwM,416
|
267
277
|
omlish/specs/jsonrpc/errors.py,sha256=-Zgmlo6bV6J8w5f8h9axQgLquIFBHDgIwcpufEH5NsE,707
|
268
278
|
omlish/specs/jsonrpc/marshal.py,sha256=iXZNR7n0vfL_yiPFFYN-ZyGlzknNXExs2qC1HFChGPU,1913
|
@@ -328,9 +338,9 @@ omlish/text/delimit.py,sha256=ubPXcXQmtbOVrUsNh5gH1mDq5H-n1y2R4cPL5_DQf68,4928
|
|
328
338
|
omlish/text/glyphsplit.py,sha256=Ug-dPRO7x-OrNNr8g1y6DotSZ2KH0S-VcOmUobwa4B0,3296
|
329
339
|
omlish/text/indent.py,sha256=6Jj6TFY9unaPa4xPzrnZemJ-fHsV53IamP93XGjSUHs,1274
|
330
340
|
omlish/text/parts.py,sha256=7vPF1aTZdvLVYJ4EwBZVzRSy8XB3YqPd7JwEnNGGAOo,6495
|
331
|
-
omlish-0.0.0.
|
332
|
-
omlish-0.0.0.
|
333
|
-
omlish-0.0.0.
|
334
|
-
omlish-0.0.0.
|
335
|
-
omlish-0.0.0.
|
336
|
-
omlish-0.0.0.
|
341
|
+
omlish-0.0.0.dev47.dist-info/LICENSE,sha256=B_hVtavaA8zCYDW99DYdcpDLKz1n3BBRjZrcbv8uG8c,1451
|
342
|
+
omlish-0.0.0.dev47.dist-info/METADATA,sha256=fBgDiQQnKFFc4L2qA3Dc_rh2d2mKKmKT7U3UeeiBPwA,3817
|
343
|
+
omlish-0.0.0.dev47.dist-info/WHEEL,sha256=GV9aMThwP_4oNCtvEC2ec3qUYutgWeAzklro_0m4WJQ,91
|
344
|
+
omlish-0.0.0.dev47.dist-info/entry_points.txt,sha256=Lt84WvRZJskWCAS7xnQGZIeVWksprtUHj0llrvVmod8,35
|
345
|
+
omlish-0.0.0.dev47.dist-info/top_level.txt,sha256=pePsKdLu7DvtUiecdYXJ78iO80uDNmBlqe-8hOzOmfs,7
|
346
|
+
omlish-0.0.0.dev47.dist-info/RECORD,,
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|