templates.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. from typing import TYPE_CHECKING
  20. import jinja2.nativetypes
  21. import jinja2.sandbox
  22. if TYPE_CHECKING:
  23. import datetime
  24. class _AirflowEnvironmentMixin:
  25. def __init__(self, **kwargs):
  26. super().__init__(**kwargs)
  27. self.filters.update(FILTERS)
  28. def is_safe_attribute(self, obj, attr, value):
  29. """
  30. Allow access to ``_`` prefix vars (but not ``__``).
  31. Unlike the stock SandboxedEnvironment, we allow access to "private" attributes (ones starting with
  32. ``_``) whilst still blocking internal or truly private attributes (``__`` prefixed ones).
  33. """
  34. return not jinja2.sandbox.is_internal_attribute(obj, attr)
  35. class NativeEnvironment(_AirflowEnvironmentMixin, jinja2.nativetypes.NativeEnvironment):
  36. """NativeEnvironment for Airflow task templates."""
  37. class SandboxedEnvironment(_AirflowEnvironmentMixin, jinja2.sandbox.SandboxedEnvironment):
  38. """SandboxedEnvironment for Airflow task templates."""
  39. def ds_filter(value: datetime.date | datetime.time | None) -> str | None:
  40. """Date filter."""
  41. if value is None:
  42. return None
  43. return value.strftime("%Y-%m-%d")
  44. def ds_nodash_filter(value: datetime.date | datetime.time | None) -> str | None:
  45. """Date filter without dashes."""
  46. if value is None:
  47. return None
  48. return value.strftime("%Y%m%d")
  49. def ts_filter(value: datetime.date | datetime.time | None) -> str | None:
  50. """Timestamp filter."""
  51. if value is None:
  52. return None
  53. return value.isoformat()
  54. def ts_nodash_filter(value: datetime.date | datetime.time | None) -> str | None:
  55. """Timestamp filter without dashes."""
  56. if value is None:
  57. return None
  58. return value.strftime("%Y%m%dT%H%M%S")
  59. def ts_nodash_with_tz_filter(value: datetime.date | datetime.time | None) -> str | None:
  60. """Timestamp filter with timezone."""
  61. if value is None:
  62. return None
  63. return value.isoformat().replace("-", "").replace(":", "")
  64. FILTERS = {
  65. "ds": ds_filter,
  66. "ds_nodash": ds_nodash_filter,
  67. "ts": ts_filter,
  68. "ts_nodash": ts_nodash_filter,
  69. "ts_nodash_with_tz": ts_nodash_with_tz_filter,
  70. }