From f28826b474df693c45ff59c64e0536bad4e1c35f Mon Sep 17 00:00:00 2001 From: Devan Goodwin Date: Thu, 28 Jul 2016 15:10:10 -0300 Subject: Fixed a bug in modify_yaml module. When adding a property to a section that does not yet exist, the property was being inserted into the section above where it should have due to the pointer not being adjusted for the new section we created. Adds a test sub-directory, would like to eventually move all the ooinstall tests here as well so nosetests just works from top level directory. --- library/modify_yaml.py | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) (limited to 'library') diff --git a/library/modify_yaml.py b/library/modify_yaml.py index a4be10ca3..63b507a72 100755 --- a/library/modify_yaml.py +++ b/library/modify_yaml.py @@ -20,6 +20,24 @@ EXAMPLES = ''' yaml_value: 2 ''' + +# pylint: disable=missing-docstring +def set_key(yaml_data, yaml_key, yaml_value): + changes = [] + ptr = yaml_data + for key in yaml_key.split('.'): + if key not in ptr and key != yaml_key.split('.')[-1]: + ptr[key] = {} + ptr = ptr[key] + elif key == yaml_key.split('.')[-1]: + if (key in ptr and module.safe_eval(ptr[key]) != yaml_value) or (key not in ptr): + ptr[key] = yaml_value + changes.append((yaml_key, yaml_value)) + else: + ptr = ptr[key] + return changes + + def main(): ''' Modify key (supplied in jinja2 dot notation) in yaml file, setting the key to the desired value. @@ -53,22 +71,12 @@ def main(): yaml.add_representer(type(None), none_representer) try: - changes = [] yaml_file = open(dest) yaml_data = yaml.safe_load(yaml_file.read()) yaml_file.close() - ptr = yaml_data - for key in yaml_key.split('.'): - if key not in ptr and key != yaml_key.split('.')[-1]: - ptr[key] = {} - elif key == yaml_key.split('.')[-1]: - if (key in ptr and module.safe_eval(ptr[key]) != yaml_value) or (key not in ptr): - ptr[key] = yaml_value - changes.append((yaml_key, yaml_value)) - else: - ptr = ptr[key] + changes = set_key(yaml_data, yaml_key, yaml_value) if len(changes) > 0: if backup: -- cgit v1.2.1