Refactoring inventory generation data manipulation

Segmenting out changes to the inventory generation script which are
related to datastructure manipulations in preparation for segmenting
out storage-related operations.

Change-Id: I3160862b4fea92bce4c951107e154fd3ddc21234
This commit is contained in:
Steve Lewis 2016-10-31 12:53:32 -07:00
parent 1dd8884dca
commit 249db3ae5c
2 changed files with 41 additions and 39 deletions

View File

@ -17,12 +17,44 @@
#
def merge_dict(base_items, new_items):
"""Recursively merge new_items into some base_items.
If an empty dictionary is provided as a new value, it will
completely replace the existing dictionary.
:param base_items: ``dict``
:param new_items: ``dict``
:return dictionary:
"""
for key, value in new_items.iteritems():
if isinstance(value, dict) and value:
base_merge = merge_dict(base_items.get(key, {}), value)
base_items[key] = base_merge
else:
base_items[key] = new_items[key]
return base_items
def append_if(array, item):
"""Append an ``item`` to an ``array`` if its not already in it.
:param array: ``list`` List object to append to
:param item: ``object`` Object to append to the list
:returns bool: Flag indicating whether the append happend (True)
or not (False)
"""
if item not in array:
array.append(item)
return True
return False
def recursive_list_removal(inventory, purge_list):
"""Remove items from a list.
Keyword arguments:
inventory -- inventory dictionary
purge_list -- list of items to remove
:param inventory: ``dict`` Dictionary representing the inventory
:param purge_list: ``list`` List of items to remove
"""
for item in purge_list:
for _item in inventory:
@ -34,8 +66,8 @@ def recursive_dict_removal(inventory, purge_list):
"""Remove items from a dictionary.
Keyword arguments:
inventory -- inventory dictionary
purge_list -- list of items to remove
:param inventory: ``dict`` Dictionary representing the inventory
:param purge_list: ``list`` List of items to remove
"""
for key, value in inventory.iteritems():
if isinstance(value, dict):

View File

@ -28,6 +28,9 @@ import uuid
import warnings
import yaml
from dictutils import append_if
from dictutils import merge_dict
logger = logging.getLogger('osa-inventory')
USED_IPS = set()
@ -901,39 +904,6 @@ def _parse_global_variables(user_cidr, inventory, user_defined_config):
del inventory['all']['vars'][key]
def append_if(array, item):
"""Append an ``item`` to an ``array`` if its not already in it.
:param array: ``list`` List object to append to
:param item: ``object`` Object to append to the list
:returns bool: Flag indicating whether the append happend (True)
or not (False)
"""
if item not in array:
array.append(item)
return True
return False
def _merge_dict(base_items, new_items):
"""Recursively merge new_items into some base_items.
If an empty dictionary is provided as a new value, it will
completely replace the existing dictionary.
:param base_items: ``dict``
:param new_items: ``dict``
:return dictionary:
"""
for key, value in new_items.iteritems():
if isinstance(value, dict) and value:
base_merge = _merge_dict(base_items.get(key, {}), value)
base_items[key] = base_merge
else:
base_items[key] = new_items[key]
return base_items
def _extra_config(user_defined_config, base_dir):
"""Discover new items in any extra directories and add the new values.
@ -944,7 +914,7 @@ def _extra_config(user_defined_config, base_dir):
for name in files:
if name.endswith(('.yml', '.yaml')):
with open(os.path.join(root_dir, name), 'rb') as f:
_merge_dict(
merge_dict(
user_defined_config,
yaml.safe_load(f.read()) or {}
)