Exemplo n.º 1
0
def render_template(template: Any, context: Context, *, native: bool) -> Any:
    """Render a Jinja2 template with given Airflow context.

    The default implementation of ``jinja2.Template.render()`` converts the
    input context into dict eagerly many times, which triggers deprecation
    messages in our custom context class. This takes the implementation apart
    and retain the context mapping without resolving instead.

    :param template: A Jinja2 template to render.
    :param context: The Airflow task context to render the template with.
    :param native: If set to *True*, render the template into a native type. A
        DAG can enable this with ``render_template_as_native_obj=True``.
    :returns: The render result.
    """
    context = copy.copy(context)
    env = template.environment
    if template.globals:
        context.update(
            (k, v) for k, v in template.globals.items() if k not in context)
    try:
        nodes = template.root_render_func(
            env.context_class(env, context, template.name, template.blocks))
    except Exception:
        env.handle_exception()  # Rewrite traceback to point to the template.
    if native:
        return jinja2.nativetypes.native_concat(nodes)
    return "".join(nodes)
Exemplo n.º 2
0
    def poke(self, context: Context) -> bool:
        context.update(self.op_kwargs)
        context['templates_dict'] = self.templates_dict
        self.op_kwargs = determine_kwargs(self.python_callable, self.op_args,
                                          context)

        self.log.info("Poking callable: %s", str(self.python_callable))
        return_value = self.python_callable(*self.op_args, **self.op_kwargs)
        return bool(return_value)
Exemplo n.º 3
0
    def execute(self, context: Context) -> Any:
        context.update(self.op_kwargs)
        context['templates_dict'] = self.templates_dict

        self.op_kwargs = self.determine_kwargs(context)

        return_value = self.execute_callable()
        if self.show_return_value_in_logs:
            self.log.info("Done. Returned value was: %s", return_value)
        else:
            self.log.info("Done. Returned value not shown")

        return return_value