3
3
from jmespath import exceptions
4
4
from jmespath import functions
5
5
from jmespath .compat import string_type
6
- from jmespath .compat import with_str_method
7
6
from jmespath .scope import ScopedChainDict
8
7
from numbers import Number
9
8
@@ -84,10 +83,9 @@ def __init__(self, dict_cls=None,
84
83
85
84
86
85
class _Expression (object ):
87
- def __init__ (self , expression , interpreter , context ):
86
+ def __init__ (self , expression , interpreter ):
88
87
self .expression = expression
89
88
self .interpreter = interpreter
90
- self .context = context
91
89
92
90
def visit (self , node , * args , ** kwargs ):
93
91
return self .interpreter .visit (node , * args , ** kwargs )
@@ -144,11 +142,16 @@ def __init__(self, options=None):
144
142
self ._functions = self ._options .custom_functions
145
143
else :
146
144
self ._functions = functions .Functions ()
145
+ self ._root = None
147
146
self ._scope = ScopedChainDict ()
148
147
149
148
def default_visit (self , node , * args , ** kwargs ):
150
149
raise NotImplementedError (node ['type' ])
151
150
151
+ def evaluate (self , ast , root ):
152
+ self ._root = root
153
+ return self .visit (ast , root )
154
+
152
155
def visit_subexpression (self , node , value ):
153
156
result = value
154
157
for node in node ['children' ]:
@@ -158,23 +161,10 @@ def visit_subexpression(self, node, value):
158
161
return result
159
162
160
163
def visit_field (self , node , value , * args , ** kwargs ):
161
- identifier = node ['value' ]
162
- scopes = kwargs .get ('scopes' )
163
-
164
164
try :
165
- result = value .get (identifier )
166
- if result == None :
167
- result = self ._get_from_scopes (
168
- identifier , * args , scopes = scopes )
169
- return result
165
+ return value .get (node ['value' ])
170
166
except AttributeError :
171
- return self ._get_from_scopes (
172
- identifier , * args , scopes = scopes )
173
-
174
- def _get_from_scopes (self , identifier , * args , ** kwargs ):
175
- if 'scopes' in kwargs :
176
- return kwargs ['scopes' ].getValue (identifier )
177
- return None
167
+ return None
178
168
179
169
def visit_comparator (self , node , value ):
180
170
# Common case: comparator is == or !=
@@ -212,13 +202,11 @@ def visit_arithmetic(self, node, value):
212
202
def visit_current (self , node , value ):
213
203
return value
214
204
215
- def visit_root (self , * args , ** kwargs ):
216
- if 'scopes' in kwargs :
217
- return kwargs ['scopes' ].getRoot ()
218
- return None
205
+ def visit_root (self , node , value ):
206
+ return self ._root
219
207
220
208
def visit_expref (self , node , value ):
221
- return _Expression (node ['children' ][0 ], self , value )
209
+ return _Expression (node ['children' ][0 ], self )
222
210
223
211
def visit_function_expression (self , node , value , * args , ** kwargs ):
224
212
resolved_args = []
@@ -417,48 +405,3 @@ def _visit(self, node, current):
417
405
self ._count += 1
418
406
self ._lines .append (' %s -> %s' % (current , child_name ))
419
407
self ._visit (child , child_name )
420
-
421
-
422
- @with_str_method
423
- class Scopes :
424
- def __init__ (self , root ):
425
- self ._scopes = []
426
- self ._root = root
427
-
428
- def pushScope (self , scope ):
429
- self ._scopes .append (scope )
430
-
431
- def popScope (self ):
432
- if len (self ._scopes ) > 0 :
433
- self ._scopes .pop ()
434
-
435
- def getValue (self , identifier ):
436
- for scope in self ._scopes [::- 1 ]:
437
- if scope .get (identifier ) != None :
438
- return scope [identifier ]
439
- return None
440
-
441
- def getRoot (self ):
442
- return self ._root
443
-
444
- def __str__ (self ):
445
- return '{}' .format (self ._scopes )
446
-
447
-
448
- class ScopedInterpreter (TreeInterpreter ):
449
- def __init__ (self , options = None ):
450
- super ().__init__ (options )
451
-
452
- def evaluate (self , ast , root_scope ):
453
- self ._scopes = Scopes (root_scope )
454
- return self .visit (ast , root_scope )
455
-
456
- def visit (self , node , * args , ** kwargs ):
457
- scoped_types = ['field' , 'function_expression' , 'root' ]
458
- if (node ['type' ] in scoped_types ):
459
- kwargs .update ({'scopes' : self ._scopes })
460
- else :
461
- if 'scopes' in kwargs :
462
- kwargs .pop ('scopes' )
463
-
464
- return super ().visit (node , * args , ** kwargs )
0 commit comments