def evaluate(self, state, _local_scope): """ Execute this function """ TemplateStats.instance = TemplateStats(self._template) if self.is_file(): template = self._env.get_template(self._template) else: template = Template(self._content) try: variables = {} for var in self._get_variables(): name = str(var) variables[name] = DynamicProxy.return_value(state.get_ref(var).value) value = template.render(variables) result = TemplateResult(value) if TemplateStats.instance is not None: result.stats = TemplateStats.instance.get_stats() result.template = self._template TemplateStats.instance = None return result except UnknownException as e: return e.unknown
def __call__(self, *args): """ The function call itself """ try: self.check_requirements() new_args = [] for arg in args: new_args.append(DynamicProxy.return_value(arg)) if self._context >= 0: context = Context(self._graph, self._scope, self._compiler, self) new_args.insert(self._context, context) value = self.call(*new_args) if self._return is not None and not isinstance(value, Unknown): valid = False exception = None try: valid = (value is None or self._is_instance(value, self._return)) except Exception as exp: exception = exp if not valid: msg = "" if exception is not None: msg = "\n\tException details: " + str(exception) raise Exception("Plugin %s should return value of type %s ('%s' was returned) %s" % (self.__class__.__function_name__, self._return, value, msg)) return DynamicProxy.return_value(value) except UnknownException as e: # just pass it along return e.unknown
def get_variable(self, name, scope): """ Get the given variable """ return DynamicProxy.return_value(self.scope.get_variable(name, scope).value)