Debian Codegraph @

ansible_collections/community/dns/plugins/plugin_utils/_unsafe.py

raw
1 # Copyright (c) 2023, Felix Fontein <[email protected]>
2 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
3 # SPDX-License-Identifier: GPL-3.0-or-later
4
5 # Note that this plugin util is **PRIVATE** to the collection. It can have breaking changes at any time.
6 # Do not use this from other collections or standalone plugins/modules!
7
8 from __future__ import annotations
9
10 import re
11 import typing as t
12 from collections.abc import Mapping, Set
13
14 from ansible.module_utils.common.collections import is_sequence
15 from ansible.utils.unsafe_proxy import AnsibleUnsafe
16 from ansible.utils.unsafe_proxy import wrap_var as _make_unsafe
17
18 _RE_TEMPLATE_CHARS = re.compile("[{}]")
19 _RE_TEMPLATE_CHARS_BYTES = re.compile(b"[{}]")
20
21
22 def make_unsafe(value: t.Any) -> t.Any:
23 if value is None or isinstance(value, AnsibleUnsafe):
24 return value
25
26 if isinstance(value, Mapping):
27 return {make_unsafe(key): make_unsafe(val) for key, val in value.items()}
28 if isinstance(value, Set):
29 return {make_unsafe(elt) for elt in value}
30 if is_sequence(value):
31 return type(value)(make_unsafe(elt) for elt in value)
32 if isinstance(value, bytes):
33 if _RE_TEMPLATE_CHARS_BYTES.search(value):
34 value = _make_unsafe(value)
35 return value
36 if isinstance(value, str):
37 if _RE_TEMPLATE_CHARS.search(value):
38 value = _make_unsafe(value)
39 return value
40
41 return value