from core.builtin_concepts import BuiltinConcepts from evaluators.BaseEvaluator import OneReturnValueEvaluator class PrepareEvalBodyEvaluator(OneReturnValueEvaluator): """ To recognize when the user input is an (body) evaluation """ NAME = "PrepareEvalBody" def __init__(self, **kwargs): super().__init__(self.NAME, [BuiltinConcepts.BEFORE_PARSING], 90) self.text = None def matches(self, context, return_value): if not (return_value.status and context.sheerka.isinstance(return_value.body, BuiltinConcepts.USER_INPUT) and isinstance(return_value.body.body, str)): return False text = return_value.body.body.strip() if not text.startswith("eval "): return False self.text = text return True def eval(self, context, return_value): sheerka = context.sheerka new_text_to_parse = sheerka.ret( self.name, True, sheerka.new(BuiltinConcepts.USER_INPUT, body=self.text[5:], user_name=context.event.user_id)) root = context.get_parents(lambda ec: ec.action == BuiltinConcepts.PROCESS_INPUT) root = root[0] if root else context root.protected_hints.add(BuiltinConcepts.EVAL_BODY_REQUESTED) root.protected_hints.add(BuiltinConcepts.EVAL_WHERE_REQUESTED) root.protected_hints.add(BuiltinConcepts.RETURN_BODY_REQUESTED) return new_text_to_parse