コード例 #1
0
 def _render_prefix(self, ti: "TaskInstance") -> str:
     if self.prefix_jinja_template:
         jinja_context = ti.get_template_context()
         return render_template_to_string(self.prefix_jinja_template,
                                          jinja_context)
     logging.warning(
         "'task_log_prefix_template' is in invalid format, ignoring the variable value"
     )
     return ""
コード例 #2
0
    def _render_filename(self, ti: "TaskInstance", try_number: int) -> str:
        if self.filename_jinja_template:
            if hasattr(ti, "task"):
                context = ti.get_template_context()
            else:
                context = Context(ti=ti, ts=ti.get_dagrun().logical_date.isoformat())
            context["try_number"] = try_number
            return render_template_to_string(self.filename_jinja_template, context)

        return self.filename_template.format(
            dag_id=ti.dag_id,
            task_id=ti.task_id,
            execution_date=ti.get_dagrun().logical_date.isoformat(),
            try_number=try_number,
        )
コード例 #3
0
    def _render_filename(self, ti: "TaskInstance", try_number: int) -> str:
        with create_session() as session:
            dag_run = ti.get_dagrun(session=session)
            template = dag_run.get_log_template(session=session).filename
        str_tpl, jinja_tpl = parse_template_string(template)

        if jinja_tpl:
            if hasattr(ti, "task"):
                context = ti.get_template_context()
            else:
                context = Context(ti=ti, ts=dag_run.logical_date.isoformat())
            context["try_number"] = try_number
            return render_template_to_string(jinja_tpl, context)
        elif str_tpl:
            dag = ti.task.dag
            assert dag is not None  # For Mypy.
            try:
                data_interval: Tuple[
                    datetime, datetime] = dag.get_run_data_interval(dag_run)
            except AttributeError:  # ti.task is not always set.
                data_interval = (dag_run.data_interval_start,
                                 dag_run.data_interval_end)
            if data_interval[0]:
                data_interval_start = data_interval[0].isoformat()
            else:
                data_interval_start = ""
            if data_interval[1]:
                data_interval_end = data_interval[1].isoformat()
            else:
                data_interval_end = ""
            return str_tpl.format(
                dag_id=ti.dag_id,
                task_id=ti.task_id,
                run_id=ti.run_id,
                data_interval_start=data_interval_start,
                data_interval_end=data_interval_end,
                execution_date=ti.get_dagrun().logical_date.isoformat(),
                try_number=try_number,
            )
        else:
            raise RuntimeError(
                f"Unable to render log filename for {ti}. This should never happen"
            )
コード例 #4
0
    def render_template(
        self,
        content: Any,
        context: Context,
        jinja_env: Optional["jinja2.Environment"] = None,
        seen_oids: Optional[Set] = None,
    ) -> Any:
        """Render a templated string.

        If *content* is a collection holding multiple templated strings, strings
        in the collection will be templated recursively.

        :param content: Content to template. Only strings can be templated (may
            be inside a collection).
        :param context: Dict with values to apply on templated content
        :param jinja_env: Jinja environment. Can be provided to avoid
            re-creating Jinja environments during recursion.
        :param seen_oids: template fields already rendered (to avoid
            *RecursionError* on circular dependencies)
        :return: Templated content
        """
        # "content" is a bad name, but we're stuck to it being public API.
        value = content
        del content

        if not jinja_env:
            jinja_env = self.get_template_env()

        from airflow.models.param import DagParam
        from airflow.models.xcom_arg import XComArg

        if isinstance(value, str):
            if any(value.endswith(ext)
                   for ext in self.template_ext):  # A filepath.
                template = jinja_env.get_template(value)
            else:
                template = jinja_env.from_string(value)
            dag = self.get_dag()
            if dag and dag.render_template_as_native_obj:
                return render_template_as_native(template, context)
            return render_template_to_string(template, context)

        if isinstance(value, (DagParam, XComArg)):
            return value.resolve(context)

        # Fast path for common built-in collections.
        if value.__class__ is tuple:
            return tuple(
                self.render_template(element, context, jinja_env)
                for element in value)
        elif isinstance(value, tuple):  # Special case for named tuples.
            return value.__class__(
                *(self.render_template(el, context, jinja_env)
                  for el in value))
        elif isinstance(value, list):
            return [
                self.render_template(element, context, jinja_env)
                for element in value
            ]
        elif isinstance(value, dict):
            return {
                key: self.render_template(value, context, jinja_env)
                for key, value in value.items()
            }
        elif isinstance(value, set):
            return {
                self.render_template(element, context, jinja_env)
                for element in value
            }

        # More complex collections.
        if seen_oids is None:
            oids = set()
        else:
            oids = seen_oids
        self._render_nested_template_fields(value, context, jinja_env, oids)
        return value