Debian Codegraph @

lib/ansible/utils/context_objects.py

raw
1 # Copyright: (c) 2018, Ansible Project
2 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
3
4 """
5 Hold command line arguments for use in other modules
6 """
7 from __future__ import annotations
8
9 from abc import ABCMeta
10 from collections.abc import Container, Mapping, Sequence, Set
11
12 from ansible.module_utils._internal import _no_six
13 from ansible.module_utils.common.collections import ImmutableDict
14 from ansible.utils.singleton import Singleton
15
16
17 def _make_immutable(obj):
18 """Recursively convert a container and objects inside of it into immutable data types"""
19 if isinstance(obj, (str, bytes)):
20 # Strings first because they are also sequences
21 return obj
22 elif isinstance(obj, Mapping):
23 temp_dict = {}
24 for key, value in obj.items():
25 if isinstance(value, Container):
26 temp_dict[key] = _make_immutable(value)
27 else:
28 temp_dict[key] = value
29 return ImmutableDict(temp_dict)
30 elif isinstance(obj, Set):
31 temp_set = set()
32 for value in obj:
33 if isinstance(value, Container):
34 temp_set.add(_make_immutable(value))
35 else:
36 temp_set.add(value)
37 return frozenset(temp_set)
38 elif isinstance(obj, Sequence):
39 temp_sequence = []
40 for value in obj:
41 if isinstance(value, Container):
42 temp_sequence.append(_make_immutable(value))
43 else:
44 temp_sequence.append(value)
45 return tuple(temp_sequence)
46
47 return obj
48
49
50 class _ABCSingleton(Singleton, ABCMeta):
51 """
52 Combine ABCMeta based classes with Singleton based classes
53
54 Combine Singleton and ABCMeta so we have a metaclass that unambiguously knows which can override
55 the other. Useful for making new types of containers which are also Singletons.
56 """
57 pass
58
59
60 class CLIArgs(ImmutableDict):
61 """
62 Hold a parsed copy of cli arguments
63
64 We have both this non-Singleton version and the Singleton, GlobalCLIArgs, version to leave us
65 room to implement a Context object in the future. Whereas there should only be one set of args
66 in a global context, individual Context objects might want to pretend that they have different
67 command line switches to trigger different behaviour when they run. So if we support Contexts
68 in the future, they would use CLIArgs instead of GlobalCLIArgs to store their version of command
69 line flags.
70 """
71 def __init__(self, mapping):
72 toplevel = {}
73 for key, value in mapping.items():
74 toplevel[key] = _make_immutable(value)
75 super(CLIArgs, self).__init__(toplevel)
76
77 @classmethod
78 def from_options(cls, options):
79 return cls(vars(options))
80
81
82 class GlobalCLIArgs(CLIArgs, metaclass=_ABCSingleton):
83 """
84 Globally hold a parsed copy of cli arguments.
85
86 Only one of these exist per program as it is for global context
87 """
88 pass
89
90
91 def __getattr__(importable_name):
92 return _no_six.deprecate(importable_name, __name__, "binary_type", "text_type", "add_metaclass")