def evaluate(cls, expression, data_context): opts = {'undefined_to_none': False} ctx = expression_utils.get_jinja_context(data_context) try: result = cls._env.compile_expression(expression, **opts)(**ctx) # For StrictUndefined values, UndefinedError only gets raised when # the value is accessed, not when it gets created. The simplest way # to access it is to try and cast it to string. str(result) except Exception as e: # NOTE(rakhmerov): if we hit a database error then we need to # re-raise the initial exception so that upper layers had a # chance to handle it properly (e.g. in case of DB deadlock # the operations needs to retry. Essentially, such situation # indicates a problem with DB rather than with the expression # syntax or values. if isinstance(e, db_exc.DBError): LOG.error( "Failed to evaluate Jinja expression due to a database" " error, re-raising initial exception [expression=%s," " error=%s, data=%s]", expression, str(e), data_context) raise e raise exc.JinjaEvaluationException( "Can not evaluate Jinja expression [expression=%s, error=%s" ", data=%s]" % (expression, str(e), data_context)) return result
def validate(cls, expression): if not isinstance(expression, str): raise exc.JinjaEvaluationException("Unsupported type '%s'." % type(expression)) try: cls._env.parse(expression) except jinja2.exceptions.TemplateError as e: raise exc.JinjaGrammarException("Syntax error '%s'." % str(e))
def validate(cls, expression): LOG.debug("Validating Jinja expression [expression='%s']", expression) if not isinstance(expression, six.string_types): raise exc.JinjaEvaluationException("Unsupported type '%s'." % type(expression)) try: cls._env.parse(expression) except jinja2.exceptions.TemplateError as e: raise exc.JinjaGrammarException("Syntax error '%s'." % str(e))
def validate(cls, expression): if not isinstance(expression, str): raise exc.JinjaEvaluationException("Unsupported type '%s'." % type(expression)) try: parser = jinja_parse.Parser(cls._env, expression, state='variable') parser.parse_expression() except jinja2.exceptions.TemplateError as e: raise exc.JinjaGrammarException("Syntax error '%s'." % str(e))
def evaluate(cls, expression, data_context): LOG.debug( "Start to evaluate Jinja expression. " "[expression='%s', context=%s]", expression, data_context ) patterns = cls.find_expression_pattern.findall(expression) try: if patterns[0][0] == expression: result = JinjaEvaluator.evaluate(patterns[0][1], data_context) else: ctx = get_jinja_context(data_context) result = cls._env.from_string(expression).render(**ctx) except Exception as e: # NOTE(rakhmerov): if we hit a database error then we need to # re-raise the initial exception so that upper layers had a # chance to handle it properly (e.g. in case of DB deadlock # the operations needs to retry. Essentially, such situation # indicates a problem with DB rather than with the expression # syntax or values. if isinstance(e, db_exc.DBError): LOG.error( "Failed to evaluate Jinja expression due to a database" " error, re-raising initial exception [expression=%s," " error=%s, data=%s]", expression, str(e), data_context ) raise e raise exc.JinjaEvaluationException( "Can not evaluate Jinja expression [expression=%s, error=%s" ", data=%s]" % (expression, str(e), data_context) ) LOG.debug( "Finished evaluation. [expression='%s', result: %s]", expression, result ) return result
def evaluate(cls, expression, data_context): opts = {'undefined_to_none': False} ctx = expression_utils.get_jinja_context(data_context) try: result = cls._env.compile_expression(expression, **opts)(**ctx) # For StrictUndefined values, UndefinedError only gets raised when # the value is accessed, not when it gets created. The simplest way # to access it is to try and cast it to string. str(result) except Exception as e: raise exc.JinjaEvaluationException( "Can not evaluate Jinja expression [expression=%s, error=%s" ", data=%s]" % (expression, str(e), data_context)) return result
def evaluate(cls, expression, data_context): LOG.debug("Evaluating Jinja expression [expression='%s', context=%s]" % (expression, data_context)) opts = {'undefined_to_none': False} ctx = expression_utils.get_jinja_context(data_context) try: result = cls._env.compile_expression(expression, **opts)(**ctx) # For StrictUndefined values, UndefinedError only gets raised when # the value is accessed, not when it gets created. The simplest way # to access it is to try and cast it to string. str(result) except jinja2.exceptions.UndefinedError as e: raise exc.JinjaEvaluationException("Undefined error '%s'." % str(e)) LOG.debug("Jinja expression result: %s" % result) return result