Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/airflow/templates.py: 45%

33 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2023-06-07 06:35 +0000

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. 

18from __future__ import annotations 

19 

20import datetime 

21 

22import jinja2.nativetypes 

23import jinja2.sandbox 

24 

25 

26class _AirflowEnvironmentMixin: 

27 def __init__(self, **kwargs): 

28 super().__init__(**kwargs) 

29 

30 self.filters.update(FILTERS) 

31 

32 def is_safe_attribute(self, obj, attr, value): 

33 """ 

34 Allow access to ``_`` prefix vars (but not ``__``). 

35 

36 Unlike the stock SandboxedEnvironment, we allow access to "private" attributes (ones starting with 

37 ``_``) whilst still blocking internal or truly private attributes (``__`` prefixed ones). 

38 """ 

39 return not jinja2.sandbox.is_internal_attribute(obj, attr) 

40 

41 

42class NativeEnvironment(_AirflowEnvironmentMixin, jinja2.nativetypes.NativeEnvironment): 

43 """NativeEnvironment for Airflow task templates.""" 

44 

45 

46class SandboxedEnvironment(_AirflowEnvironmentMixin, jinja2.sandbox.SandboxedEnvironment): 

47 """SandboxedEnvironment for Airflow task templates.""" 

48 

49 

50def ds_filter(value: datetime.date | datetime.time | None) -> str | None: 

51 """Date filter.""" 

52 if value is None: 

53 return None 

54 return value.strftime("%Y-%m-%d") 

55 

56 

57def ds_nodash_filter(value: datetime.date | datetime.time | None) -> str | None: 

58 """Date filter without dashes.""" 

59 if value is None: 

60 return None 

61 return value.strftime("%Y%m%d") 

62 

63 

64def ts_filter(value: datetime.date | datetime.time | None) -> str | None: 

65 """Timestamp filter.""" 

66 if value is None: 

67 return None 

68 return value.isoformat() 

69 

70 

71def ts_nodash_filter(value: datetime.date | datetime.time | None) -> str | None: 

72 """Timestamp filter without dashes.""" 

73 if value is None: 

74 return None 

75 return value.strftime("%Y%m%dT%H%M%S") 

76 

77 

78def ts_nodash_with_tz_filter(value: datetime.date | datetime.time | None) -> str | None: 

79 """Timestamp filter with timezone.""" 

80 if value is None: 

81 return None 

82 return value.isoformat().replace("-", "").replace(":", "") 

83 

84 

85FILTERS = { 

86 "ds": ds_filter, 

87 "ds_nodash": ds_nodash_filter, 

88 "ts": ts_filter, 

89 "ts_nodash": ts_nodash_filter, 

90 "ts_nodash_with_tz": ts_nodash_with_tz_filter, 

91}