Ejemplo n.º 1
0
    def replace_immediately_referenced_variables(self, task_name, when_expr, publish_dict):
        # Check for context variable references that are used in the 'when'
        # expression
        variables_in_when = self.extract_context_variables(when_expr)

        variables_in_publish = set(publish_dict.keys())

        immediately_referenced_variables = variables_in_when & variables_in_publish

        # Replace the context variable references in the 'when'
        # expression with their expression from the 'publish' block
        for variable in immediately_referenced_variables:
            # First off, make sure they are the same type of expression,
            # we don't want to inject a Jinja expression in the middle
            # of a YAQL expression
            when_expr_type = ExpressionConverter.expression_type(when_expr)
            publish_expr_type = ExpressionConverter.expression_type(publish_dict[variable])

            if when_expr_type == publish_expr_type:
                # Grab the variable expression
                converter = ExpressionConverter.get_converter(publish_dict[variable])
                unwrapped_expr = converter.unwrap_expression(publish_dict[variable])

                # Replace double parentheses
                # ((ctx().variable))    ->  (result().result['variable'] + 1)
                variable_reference_dp = r'\(\(ctx\(\).{var}\)\)'.format(var=variable)
                when_expr = re.sub(variable_reference_dp, "({})".format(unwrapped_expr), when_expr)

                # Don't add in parentheses if they already exist
                # (ctx().variable)      ->  (result().result['variable'] + 1)
                variable_reference_dp = r'\(ctx\(\).{var}\)'.format(var=variable)
                when_expr = re.sub(variable_reference_dp, "({})".format(unwrapped_expr), when_expr)

                # Keep surrounding context and add surrounding parentheses
                # (ctx().variable ...   ->  ((result().result['variable'] + 1) ...
                # ... ctx().variable)   ->  ... (result().result['variable'] + 1))
                # ...ctx().variable...  ->  ...(result().result['variable'] + 1)...
                variable_reference_ep = r'(.)\bctx\(\).{var}\b(.)'.format(var=variable)
                when_expr = re.sub(variable_reference_ep, r"\1({})\2".format(unwrapped_expr), when_expr)

                # Keep double enclosing internal parentheses
                # (ctx(variable))  ->  (result().result['variable'] + 1)
                variable_reference_eip = r'\(ctx\({var}\)\)'.format(var=variable)
                when_expr = re.sub(variable_reference_eip, "({})".format(unwrapped_expr), when_expr)

            else:
                warnings.warn("The transition \"{when_expr}\" in {task_name} "
                              "references the '{variable}' context variable, "
                              "which is published in the same transition. You "
                              "will need to manually convert the {variable} "
                              "expression in the transition."
                              .format(when_expr=when_expr,
                                      task_name=task_name,
                                      variable=variable)),
        return when_expr
Ejemplo n.º 2
0
 def test_expression_type_yaql(self):
     expr = "<% $.test %>"
     result = ExpressionConverter.expression_type(expr)
     self.assertEqual(result, 'yaql')
Ejemplo n.º 3
0
 def test_expression_type_none(self):
     expr = "test"
     result = ExpressionConverter.expression_type(expr)
     self.assertIsNone(result)
Ejemplo n.º 4
0
 def test_expression_type_jinja(self):
     expr = "{{ _.test }}"
     result = ExpressionConverter.expression_type(expr)
     self.assertEqual(result, 'jinja')