dag_parsing_context.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. # Licensed to the Apache Software Foundation (ASF) under one
  2. # or more contributor license agreements. See the NOTICE file
  3. # distributed with this work for additional information
  4. # regarding copyright ownership. The ASF licenses this file
  5. # to you under the Apache License, Version 2.0 (the
  6. # "License"); you may not use this file except in compliance
  7. # with the License. You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing,
  12. # software distributed under the License is distributed on an
  13. # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  14. # KIND, either express or implied. See the License for the
  15. # specific language governing permissions and limitations
  16. # under the License.
  17. from __future__ import annotations
  18. import os
  19. from contextlib import contextmanager
  20. from typing import NamedTuple
  21. class AirflowParsingContext(NamedTuple):
  22. """
  23. Context of parsing for the DAG.
  24. If these values are not None, they will contain the specific DAG and Task ID that Airflow is requesting to
  25. execute. You can use these for optimizing dynamically generated DAG files.
  26. """
  27. dag_id: str | None
  28. task_id: str | None
  29. _AIRFLOW_PARSING_CONTEXT_DAG_ID = "_AIRFLOW_PARSING_CONTEXT_DAG_ID"
  30. _AIRFLOW_PARSING_CONTEXT_TASK_ID = "_AIRFLOW_PARSING_CONTEXT_TASK_ID"
  31. @contextmanager
  32. def _airflow_parsing_context_manager(dag_id: str | None = None, task_id: str | None = None):
  33. old_dag_id = os.environ.get(_AIRFLOW_PARSING_CONTEXT_DAG_ID)
  34. old_task_id = os.environ.get(_AIRFLOW_PARSING_CONTEXT_TASK_ID)
  35. if dag_id is not None:
  36. os.environ[_AIRFLOW_PARSING_CONTEXT_DAG_ID] = dag_id
  37. if task_id is not None:
  38. os.environ[_AIRFLOW_PARSING_CONTEXT_TASK_ID] = task_id
  39. yield
  40. if old_task_id is not None:
  41. os.environ[_AIRFLOW_PARSING_CONTEXT_TASK_ID] = old_task_id
  42. if old_dag_id is not None:
  43. os.environ[_AIRFLOW_PARSING_CONTEXT_DAG_ID] = old_dag_id
  44. def get_parsing_context() -> AirflowParsingContext:
  45. """Return the current (DAG) parsing context info."""
  46. return AirflowParsingContext(
  47. dag_id=os.environ.get(_AIRFLOW_PARSING_CONTEXT_DAG_ID),
  48. task_id=os.environ.get(_AIRFLOW_PARSING_CONTEXT_TASK_ID),
  49. )