From 9abb66503740a2b2c612cd84947dc7d4f82fbe78 Mon Sep 17 00:00:00 2001 From: Steve Lewis Date: Mon, 31 Oct 2016 12:39:27 -0700 Subject: [PATCH] Refactoring inventory management data manipulation Segmenting out changes to the inventory manage script which are related to datastructure manipulations in preparation for segmenting out storage-related operations. Change-Id: I8469659a206c69ec8bc710315cc638b58429e704 --- lib/dictutils.py | 50 ++++++++++++++++++++++++++++++++++++++++++++++++ lib/manage.py | 36 +++------------------------------- 2 files changed, 53 insertions(+), 33 deletions(-) create mode 100644 lib/dictutils.py diff --git a/lib/dictutils.py b/lib/dictutils.py new file mode 100644 index 0000000000..27d9bcc282 --- /dev/null +++ b/lib/dictutils.py @@ -0,0 +1,50 @@ +# Copyright 2014, Rackspace US, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# (c) 2014, Kevin Carter +# (c) 2015, Major Hayden +# + + +def recursive_list_removal(inventory, purge_list): + """Remove items from a list. + + Keyword arguments: + inventory -- inventory dictionary + purge_list -- list of items to remove + """ + for item in purge_list: + for _item in inventory: + if item == _item: + inventory.pop(inventory.index(item)) + + +def recursive_dict_removal(inventory, purge_list): + """Remove items from a dictionary. + + Keyword arguments: + inventory -- inventory dictionary + purge_list -- list of items to remove + """ + for key, value in inventory.iteritems(): + if isinstance(value, dict): + for _key, _value in value.iteritems(): + if isinstance(_value, dict): + for item in purge_list: + if item in _value: + del(_value[item]) + elif isinstance(_value, list): + recursive_list_removal(_value, purge_list) + elif isinstance(value, list): + recursive_list_removal(value, purge_list) diff --git a/lib/manage.py b/lib/manage.py index f46535a30a..acdcb0fc2e 100644 --- a/lib/manage.py +++ b/lib/manage.py @@ -23,6 +23,8 @@ import json import os import prettytable +from dictutils import recursive_dict_removal + def file_find(filename, user_file=None, pass_exception=False): """Return the path to a file. @@ -57,39 +59,6 @@ def file_find(filename, user_file=None, pass_exception=False): 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 - """ - for item in purge_list: - for _item in inventory: - if item == _item: - inventory.pop(inventory.index(item)) - - -def recursive_dict_removal(inventory, purge_list): - """Remove items from a dictionary. - - Keyword arguments: - inventory -- inventory dictionary - purge_list -- list of items to remove - """ - for key, value in inventory.iteritems(): - if isinstance(value, dict): - for _key, _value in value.iteritems(): - if isinstance(_value, dict): - for item in purge_list: - if item in _value: - del(_value[item]) - elif isinstance(_value, list): - recursive_list_removal(_value, purge_list) - elif isinstance(value, list): - recursive_list_removal(value, purge_list) - - def args(): """Setup argument Parsing.""" parser = argparse.ArgumentParser( @@ -366,5 +335,6 @@ def main(): f_handle.write(json.dumps(inventory, indent=2)) print('Success. . .') + if __name__ == "__main__": main()