__init__.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. #
  2. # Licensed to the Apache Software Foundation (ASF) under one
  3. # or more contributor license agreements. See the NOTICE file
  4. # distributed with this work for additional information
  5. # regarding copyright ownership. The ASF licenses this file
  6. # to you under the Apache License, Version 2.0 (the
  7. # "License"); you may not use this file except in compliance
  8. # with the License. You may obtain a copy of the License at
  9. #
  10. # http://www.apache.org/licenses/LICENSE-2.0
  11. #
  12. # Unless required by applicable law or agreed to in writing,
  13. # software distributed under the License is distributed on an
  14. # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. # KIND, either express or implied. See the License for the
  16. # specific language governing permissions and limitations
  17. # under the License.
  18. from __future__ import annotations
  19. __version__ = "2.10.5"
  20. import os
  21. import sys
  22. import warnings
  23. from typing import TYPE_CHECKING
  24. if os.environ.get("_AIRFLOW_PATCH_GEVENT"):
  25. # If you are using gevents and start airflow webserver, you might want to run gevent monkeypatching
  26. # as one of the first thing when Airflow is started. This allows gevent to patch networking and other
  27. # system libraries to make them gevent-compatible before anything else patches them (for example boto)
  28. from gevent.monkey import patch_all
  29. patch_all()
  30. if sys.platform == "win32":
  31. warnings.warn(
  32. "Airflow currently can be run on POSIX-compliant Operating Systems. For development, "
  33. "it is regularly tested on fairly modern Linux Distros and recent versions of macOS. "
  34. "On Windows you can run it via WSL2 (Windows Subsystem for Linux 2) or via Linux Containers. "
  35. "The work to add Windows support is tracked via https://github.com/apache/airflow/issues/10388, "
  36. "but it is not a high priority.",
  37. category=RuntimeWarning,
  38. stacklevel=1,
  39. )
  40. # The configuration module initializes and validates the conf object as a side effect the first
  41. # time it is imported. If it is not imported before importing the settings module, the conf
  42. # object will then be initted/validated as a side effect of it being imported in settings,
  43. # however this can cause issues since those modules are very tightly coupled and can
  44. # very easily cause import cycles in the conf init/validate code (since downstream code from
  45. # those functions likely import settings).
  46. # configuration is therefore initted early here, simply by importing it.
  47. from airflow import configuration, settings
  48. __all__ = [
  49. "__version__",
  50. "DAG",
  51. "Dataset",
  52. "XComArg",
  53. ]
  54. # Make `airflow` a namespace package, supporting installing
  55. # airflow.providers.* in different locations (i.e. one in site, and one in user
  56. # lib.)
  57. __path__ = __import__("pkgutil").extend_path(__path__, __name__) # type: ignore
  58. # Perform side-effects unless someone has explicitly opted out before import
  59. # WARNING: DO NOT USE THIS UNLESS YOU REALLY KNOW WHAT YOU'RE DOING.
  60. # This environment variable prevents proper initialization, and things like
  61. # configs, logging, the ORM, etc. will be broken. It is only useful if you only
  62. # access certain trivial constants and free functions (e.g. `__version__`).
  63. if not os.environ.get("_AIRFLOW__AS_LIBRARY", None):
  64. settings.initialize()
  65. # Things to lazy import in form {local_name: ('target_module', 'target_name', 'deprecated')}
  66. __lazy_imports: dict[str, tuple[str, str, bool]] = {
  67. "DAG": (".models.dag", "DAG", False),
  68. "Dataset": (".datasets", "Dataset", False),
  69. "XComArg": (".models.xcom_arg", "XComArg", False),
  70. "version": (".version", "", False),
  71. # Deprecated lazy imports
  72. "AirflowException": (".exceptions", "AirflowException", True),
  73. }
  74. if TYPE_CHECKING:
  75. # These objects are imported by PEP-562, however, static analyzers and IDE's
  76. # have no idea about typing of these objects.
  77. # Add it under TYPE_CHECKING block should help with it.
  78. from airflow.models.dag import DAG
  79. from airflow.models.dataset import Dataset
  80. from airflow.models.xcom_arg import XComArg
  81. def __getattr__(name: str):
  82. # PEP-562: Lazy loaded attributes on python modules
  83. module_path, attr_name, deprecated = __lazy_imports.get(name, ("", "", False))
  84. if not module_path:
  85. if name.startswith("PY3") and (py_minor := name[3:]) in ("6", "7", "8", "9", "10", "11", "12"):
  86. warnings.warn(
  87. f"Python version constraint {name!r} is deprecated and will be removed in the future. "
  88. f"Please get version info from the 'sys.version_info'.",
  89. DeprecationWarning,
  90. stacklevel=2,
  91. )
  92. return sys.version_info >= (3, int(py_minor))
  93. raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
  94. elif deprecated:
  95. warnings.warn(
  96. f"Import {name!r} directly from the airflow module is deprecated and "
  97. f"will be removed in the future. Please import it from 'airflow{module_path}.{attr_name}'.",
  98. DeprecationWarning,
  99. stacklevel=2,
  100. )
  101. import importlib
  102. mod = importlib.import_module(module_path, __name__)
  103. if attr_name:
  104. val = getattr(mod, attr_name)
  105. else:
  106. val = mod
  107. # Store for next time
  108. globals()[name] = val
  109. return val
  110. if not settings.LAZY_LOAD_PROVIDERS:
  111. from airflow.providers_manager import ProvidersManager
  112. manager = ProvidersManager()
  113. manager.initialize_providers_list()
  114. manager.initialize_providers_hooks()
  115. manager.initialize_providers_extra_links()
  116. if not settings.LAZY_LOAD_PLUGINS:
  117. from airflow import plugins_manager
  118. plugins_manager.ensure_plugins_loaded()