Add a CLIArgs Singleton class that will hold the parse cli arguments

This commit is contained in:
Toshio Kuratomi 2018-12-17 18:10:26 -08:00
commit c18da65089
4 changed files with 265 additions and 3 deletions

View file

@ -1,4 +1,5 @@
# Copyright (c), Sviatoslav Sydorenko <ssydoren@redhat.com> 2018
# Copyright: (c) 2018, Sviatoslav Sydorenko <ssydoren@redhat.com>
# Copyright: (c) 2018, Ansible Project
# Simplified BSD License (see licenses/simplified_bsd.txt or https://opensource.org/licenses/BSD-2-Clause)
"""Collection of low-level utility functions."""
@ -7,7 +8,52 @@ __metaclass__ = type
from ansible.module_utils.six import binary_type, text_type
from ansible.module_utils.common._collections_compat import Sequence
from ansible.module_utils.common._collections_compat import Hashable, Mapping, Sequence
class ImmutableDict(Hashable, Mapping):
"""Dictionary that cannot be updated"""
def __init__(self, *args, **kwargs):
self._store = dict(*args, **kwargs)
def __getitem__(self, key):
return self._store[key]
def __iter__(self):
return self._store.__iter__()
def __len__(self):
return self._store.__len__()
def __hash__(self):
return hash(frozenset(self.items()))
def __repr__(self):
return 'ImmutableDict({0})'.format(repr(self._store))
def union(self, overriding_mapping):
"""
Create an ImmutableDict as a combination of the original and overriding_mapping
:arg overriding_mapping: A Mapping of replacement and additional items
:return: A copy of the ImmutableDict with key-value pairs from the overriding_mapping added
If any of the keys in overriding_mapping are already present in the original ImmutableDict,
the overriding_mapping item replaces the one in the original ImmutableDict.
"""
return ImmutableDict(self._store, **overriding_mapping)
def difference(self, subtractive_iterable):
"""
Create an ImmutableDict as a combination of the original minus keys in subtractive_iterable
:arg subtractive_iterable: Any iterable containing keys that should not be present in the
new ImmutableDict
:return: A copy of the ImmutableDict with keys from the subtractive_iterable removed
"""
remove_keys = frozenset(subtractive_iterable)
keys = (k for k in self._store.keys() if k not in remove_keys)
return ImmutableDict((k, self._store[k]) for k in keys)
def is_string(seq):