Improved PyhtonEvaluator in order to use methods that need context
This commit is contained in:
@@ -11,6 +11,28 @@ from evaluators.BaseEvaluator import OneReturnValueEvaluator
|
||||
from parsers.PythonParser import PythonNode
|
||||
|
||||
|
||||
def inject_context(context):
|
||||
"""
|
||||
function Decorator used to inject the context in methods that needed
|
||||
:param context:
|
||||
:return:
|
||||
"""
|
||||
|
||||
def wrapped(func):
|
||||
def inner(*args, **kwargs):
|
||||
return func(context, *args, **kwargs)
|
||||
|
||||
return inner
|
||||
|
||||
return wrapped
|
||||
|
||||
|
||||
class Expando:
|
||||
def __init__(self, bag):
|
||||
for k, v in bag.items():
|
||||
setattr(self, k, v)
|
||||
|
||||
|
||||
class PythonEvaluator(OneReturnValueEvaluator):
|
||||
NAME = "Python"
|
||||
|
||||
@@ -65,14 +87,12 @@ class PythonEvaluator(OneReturnValueEvaluator):
|
||||
|
||||
def get_globals(self, context, node):
|
||||
my_locals = {
|
||||
"sheerka": context.sheerka,
|
||||
# "desc": context.sheerka.dump_handler.dump_desc,
|
||||
# "concepts": context.sheerka.dump_handler.dump_concepts,
|
||||
# "history": context.sheerka.dump_handler.dump_history,
|
||||
# "state": context.sheerka.dump_handler.dump_state,
|
||||
"Concept": core.concept.Concept,
|
||||
"BuiltinConcepts": core.builtin_concepts.BuiltinConcepts,
|
||||
}
|
||||
|
||||
method_from_sheerka = self.update_globals_with_sheerka_methods(my_locals, context)
|
||||
|
||||
if context.obj:
|
||||
context.log(f"Concept '{context.obj}' is in context. Adding it and its properties to locals.", self.name)
|
||||
|
||||
@@ -123,8 +143,26 @@ class PythonEvaluator(OneReturnValueEvaluator):
|
||||
if self.locals: # when exta values are given. Add them
|
||||
my_locals.update(self.locals)
|
||||
|
||||
my_locals["sheerka"] = Expando(method_from_sheerka) # it's the last, so I cannot be overridden
|
||||
return my_locals
|
||||
|
||||
@staticmethod
|
||||
def update_globals_with_sheerka_methods(my_locals, context):
|
||||
methods_from_sheerka = {}
|
||||
|
||||
# Make sure that methods that need the concept are correctly wrapped
|
||||
for method_name, method in context.sheerka.sheerka_methods.items():
|
||||
if method_name in context.sheerka.methods_with_context:
|
||||
methods_from_sheerka[method_name] = inject_context(context)(method)
|
||||
else:
|
||||
methods_from_sheerka[method_name] = method
|
||||
|
||||
# Add all the methods as a direct access
|
||||
for method_name, method in methods_from_sheerka.items():
|
||||
my_locals[method_name] = method
|
||||
|
||||
return methods_from_sheerka # to allow access using prefix "sheerka."
|
||||
|
||||
@staticmethod
|
||||
def resolve_name(to_resolve):
|
||||
"""
|
||||
|
||||
Reference in New Issue
Block a user