Debian Codegraph @

lib/ansible/_internal/_templating/_utils.py

raw
1 from __future__ import annotations
2
3 import dataclasses
4 import typing as t
5
6 from ansible.module_utils._internal import _ambient_context, _datatag
7
8 if t.TYPE_CHECKING:
9 from ._engine import TemplateEngine, TemplateOptions
10
11
12 @dataclasses.dataclass(kw_only=True, slots=True, frozen=True)
13 class LazyOptions:
14 """Templating options that apply to lazy containers, which are inherited by descendent lazy containers."""
15
16 DEFAULT: t.ClassVar[t.Self]
17 """A shared instance with the default options to minimize instance creation for arg defaults."""
18 SKIP_TEMPLATES: t.ClassVar[t.Self]
19 """A shared instance with only `template=False` set to minimize instance creation for arg defaults."""
20 SKIP_TEMPLATES_AND_ACCESS: t.ClassVar[t.Self]
21 """A shared instance with both `template=False` and `access=False` set to minimize instance creation for arg defaults."""
22
23 template: bool = True
24 """Enable/disable templating."""
25
26 access: bool = True
27 """Enable/disables access calls."""
28
29 unmask_type_names: frozenset[str] = frozenset()
30 """Disables template transformations for the provided type names."""
31
32
33 LazyOptions.DEFAULT = LazyOptions()
34 LazyOptions.SKIP_TEMPLATES = LazyOptions(template=False)
35 LazyOptions.SKIP_TEMPLATES_AND_ACCESS = LazyOptions(template=False, access=False)
36
37
38 class TemplateContext(_ambient_context.AmbientContextBase):
39 def __init__(
40 self,
41 *,
42 template_value: t.Any,
43 templar: TemplateEngine,
44 options: TemplateOptions,
45 stop_on_template: bool = False,
46 _render_jinja_const_template: bool = False,
47 ):
48 self._template_value = template_value
49 self._templar = templar
50 self._options = options
51 self._stop_on_template = stop_on_template
52 self._parent_ctx = TemplateContext.current(optional=True)
53 self._render_jinja_const_template = _render_jinja_const_template
54
55 @property
56 def is_top_level(self) -> bool:
57 return not self._parent_ctx
58
59 @property
60 def template_value(self) -> t.Any:
61 return self._template_value
62
63 @property
64 def templar(self) -> TemplateEngine:
65 return self._templar
66
67 @property
68 def options(self) -> TemplateOptions:
69 return self._options
70
71 @property
72 def stop_on_template(self) -> bool:
73 return self._stop_on_template
74
75
76 class _OmitType:
77 """
78 A placeholder singleton used to dynamically omit items from a dict/list/tuple/set when the value is `Omit`.
79
80 The `Omit` singleton is accessible from all Ansible templating contexts via the Jinja global name `omit`.
81 The `Omit` placeholder value will be visible to Jinja plugins during templating.
82 Jinja plugins requiring omit behavior are responsible for handling encountered `Omit` values.
83 `Omit` values remaining in template results will be automatically dropped during template finalization.
84 When a finalized template renders to a scalar `Omit`, `AnsibleValueOmittedError` will be raised.
85 Passing a value other than `Omit` for `value_for_omit` to the `template` call allows that value to be substituted instead of raising.
86 """
87
88 __slots__ = ()
89
90 def __new__(cls):
91 return Omit
92
93 def __repr__(self):
94 return "<<Omit>>"
95
96
97 Omit = object.__new__(_OmitType)
98
99 _datatag._untaggable_types.add(_OmitType)
100
101
102 IGNORE_SCALAR_VAR_TYPES = {value for value in _datatag._ANSIBLE_ALLOWED_SCALAR_VAR_TYPES if not issubclass(value, str)}
103 """Scalar variable types that short-circuit bypass templating."""
104
105 PASS_THROUGH_SCALAR_VAR_TYPES = _datatag._ANSIBLE_ALLOWED_SCALAR_VAR_TYPES | {
106 _OmitType, # allow pass through of omit for later handling after top-level finalize completes
107 }
108 """Scalar variable types which are allowed to appear in finalized template results."""