Debian Codegraph @

ansible_collections/community/proxmox/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
6 import re
7 from collections.abc import Mapping, Set
8
9 from ansible.module_utils.common.collections import is_sequence
10 from ansible.utils.unsafe_proxy import (
11 AnsibleUnsafe,
12 )
13 from ansible.utils.unsafe_proxy import (
14 wrap_var as _make_unsafe,
15 )
16
17 _RE_TEMPLATE_CHARS = re.compile("[{}]")
18 _RE_TEMPLATE_CHARS_BYTES = re.compile(b"[{}]")
19
20
21 def make_unsafe(value): # noqa: PLR0911
22 """Make a value unsafe."""
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 elif isinstance(value, Set):
29 return set(make_unsafe(elt) for elt in value)
30 elif is_sequence(value):
31 return type(value)(make_unsafe(elt) for elt in value)
32 elif isinstance(value, bytes):
33 if _RE_TEMPLATE_CHARS_BYTES.search(value):
34 value = _make_unsafe(value)
35 return value
36 elif isinstance(value, str):
37 if _RE_TEMPLATE_CHARS.search(value):
38 value = _make_unsafe(value)
39 return value
40
41 return value