| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import collections.abc as c |
| 4 |
|
| 5 |
import typing as t |
| 6 |
|
| 7 |
if t.TYPE_CHECKING: |
| 8 |
from ansible.module_utils.compat.typing import TypeGuard |
| 9 |
|
| 10 |
|
| 11 |
INTERMEDIATE_MAPPING_TYPES = (c.Mapping,) |
| 12 |
""" |
| 13 |
Mapping types which are supported for recursion and runtime usage, such as in serialization and templating. |
| 14 |
These will be converted to a simple Python `dict` before serialization or storage as a variable. |
| 15 |
""" |
| 16 |
|
| 17 |
INTERMEDIATE_ITERABLE_TYPES = (tuple, c.Set, c.Sequence) |
| 18 |
""" |
| 19 |
Iterable types which are supported for recursion and runtime usage, such as in serialization and templating. |
| 20 |
These will be converted to a simple Python `list` before serialization or storage as a variable. |
| 21 |
Using the abstract `Set` (rather than concrete `set`/`frozenset`) also covers set-like views such as `dict_keys`. |
| 22 |
CAUTION: Scalar types which are sequences should be excluded when using this. |
| 23 |
""" |
| 24 |
|
| 25 |
ITERABLE_SCALARS_NOT_TO_ITERATE = (str, bytes) |
| 26 |
"""Scalars which are also iterable, and should thus be excluded from iterable checks.""" |
| 27 |
|
| 28 |
|
| 29 |
def is_intermediate_mapping(value: object) -> TypeGuard[c.Mapping]: |
| 30 |
"""Returns `True` if `value` is a type supported for projection to a Python `dict`, otherwise returns `False`.""" |
| 31 |
return isinstance(value, INTERMEDIATE_MAPPING_TYPES) |
| 32 |
|
| 33 |
|
| 34 |
def is_intermediate_iterable(value: object) -> TypeGuard[c.Iterable]: |
| 35 |
"""Returns `True` if `value` is a type supported for projection to a Python `list`, otherwise returns `False`.""" |
| 36 |
return isinstance(value, INTERMEDIATE_ITERABLE_TYPES) and not isinstance(value, ITERABLE_SCALARS_NOT_TO_ITERATE) |
| 37 |
|
| 38 |
|
| 39 |
is_controller: bool = False |
| 40 |
"""Set to True automatically when this module is imported into an Ansible controller context.""" |
| 41 |
|
| 42 |
|
| 43 |
def get_controller_serialize_map() -> dict[type, t.Callable]: |
| 44 |
""" |
| 45 |
Called to augment serialization maps. |
| 46 |
This implementation is replaced with the one from ansible._internal in controller contexts. |
| 47 |
""" |
| 48 |
return {} |
| 49 |
|
| 50 |
|
| 51 |
def import_controller_module(_module_name: str, /) -> t.Any: |
| 52 |
""" |
| 53 |
Called to conditionally import the named module in a controller context, otherwise returns `None`. |
| 54 |
This implementation is replaced with the one from ansible._internal in controller contexts. |
| 55 |
""" |
| 56 |
return None |