Debian Codegraph @

ansible_collections/community/hrobot/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 from __future__ import (absolute_import, division, print_function)
6 __metaclass__ = type
7
8 import re
9
10 from collections.abc import Mapping, Set
11 from ansible.module_utils.common.collections import is_sequence
12 from ansible.utils.unsafe_proxy import (
13 AnsibleUnsafe,
14 wrap_var as _make_unsafe,
15 )
16
17 binary_type = bytes
18 text_type = str
19
20 _RE_TEMPLATE_CHARS = re.compile(u'[{}]')
21 _RE_TEMPLATE_CHARS_BYTES = re.compile(b'[{}]')
22
23
24 def make_unsafe(value):
25 if value is None or isinstance(value, AnsibleUnsafe):
26 return value
27
28 if isinstance(value, Mapping):
29 return dict((make_unsafe(key), make_unsafe(val)) for key, val in value.items())
30 elif isinstance(value, Set):
31 return set(make_unsafe(elt) for elt in value)
32 elif is_sequence(value):
33 return type(value)(make_unsafe(elt) for elt in value)
34 elif isinstance(value, binary_type):
35 if _RE_TEMPLATE_CHARS_BYTES.search(value):
36 value = _make_unsafe(value)
37 return value
38 elif isinstance(value, text_type):
39 if _RE_TEMPLATE_CHARS.search(value):
40 value = _make_unsafe(value)
41 return value
42
43 return value