Debian Codegraph @

lib/ansible/_internal/__init__.py

raw
1 from __future__ import annotations
2
3 import importlib
4 import typing as t
5
6 from ansible.module_utils import _internal
7 from ansible.module_utils._internal._json import _profiles
8
9
10 def get_controller_serialize_map() -> dict[type, t.Callable]:
11 """
12 Injected into module_utils code to augment serialization maps with controller-only types.
13 This implementation replaces the no-op version in module_utils._internal in controller contexts.
14 """
15 from ansible._internal._templating import _lazy_containers
16 from ansible.parsing.vault import EncryptedString
17
18 return {
19 _lazy_containers._AnsibleLazyTemplateDict: _profiles._JSONSerializationProfile.discard_tags,
20 _lazy_containers._AnsibleLazyTemplateList: _profiles._JSONSerializationProfile.discard_tags,
21 EncryptedString: str, # preserves tags since this is an instance of EncryptedString; if tags should be discarded from str, another entry will handle it
22 }
23
24
25 def import_controller_module(module_name: str, /) -> t.Any:
26 """
27 Injected into module_utils code to import and return the specified module.
28 This implementation replaces the no-op version in module_utils._internal in controller contexts.
29 """
30 return importlib.import_module(module_name)
31
32
33 def experimental[T](obj: T) -> T:
34 """
35 Decorator for experimental types and methods outside the `_internal` package which accept or expose internal types.
36 As with internal APIs, these are subject to change at any time without notice.
37 """
38 return obj
39
40
41 def setup() -> None:
42 """No-op function to ensure that side-effect only imports of this module are not flagged/removed as 'unused'."""
43
44
45 # DTFIX-FUTURE: this is really fragile- disordered/incorrect imports (among other things) can mess it up. Consider a hosting-env-managed context
46 # with an enum with at least Controller/Target/Unknown values, and possibly using lazy-init module shims or some other mechanism to allow controller-side
47 # notification/augmentation of this kind of metadata.
48 _internal.get_controller_serialize_map = get_controller_serialize_map
49 _internal.import_controller_module = import_controller_module
50 _internal.is_controller = True