diff options
Diffstat (limited to 'roles/lib_yaml_editor')
-rw-r--r-- | roles/lib_yaml_editor/build/ansible/yedit.py | 9 | ||||
-rwxr-xr-x | roles/lib_yaml_editor/build/generate.py | 9 | ||||
-rw-r--r-- | roles/lib_yaml_editor/build/src/base.py | 8 | ||||
-rw-r--r-- | roles/lib_yaml_editor/build/src/yedit.py | 157 | ||||
-rw-r--r-- | roles/lib_yaml_editor/build/test/foo.yml | 2 | ||||
-rw-r--r-- | roles/lib_yaml_editor/library/yedit.py | 174 |
6 files changed, 240 insertions, 119 deletions
diff --git a/roles/lib_yaml_editor/build/ansible/yedit.py b/roles/lib_yaml_editor/build/ansible/yedit.py index bf868fb71..dab3d6347 100644 --- a/roles/lib_yaml_editor/build/ansible/yedit.py +++ b/roles/lib_yaml_editor/build/ansible/yedit.py @@ -24,7 +24,7 @@ def main(): yamlfile = Yedit(module.params['src'], module.params['content']) - rval = yamlfile.get() + rval = yamlfile.load() if not rval and state != 'present': module.fail_json(msg='Error opening file [%s]. Verify that the' + \ ' file exists, that it is has correct permissions, and is valid yaml.') @@ -45,13 +45,16 @@ def main(): if rval: rval = yamlfile.put(module.params['key'], value) + if rval[0]: + yamlfile.write() module.exit_json(changed=rval[0], results=rval[1], state="present") if not module.params['content']: rval = yamlfile.create(module.params['key'], value) else: - yamlfile.write() - rval = yamlfile.get() + rval = yamlfile.load() + yamlfile.write() + module.exit_json(changed=rval[0], results=rval[1], state="present") module.exit_json(failed=True, diff --git a/roles/lib_yaml_editor/build/generate.py b/roles/lib_yaml_editor/build/generate.py index 0df4efb92..312e4d0ee 100755 --- a/roles/lib_yaml_editor/build/generate.py +++ b/roles/lib_yaml_editor/build/generate.py @@ -15,19 +15,20 @@ GEN_STR = "#!/usr/bin/env python\n" + \ "# | |) | (_) | | .` | (_) || | | _|| |) | | | |\n" + \ "# |___/ \___/ |_|\_|\___/ |_| |___|___/___| |_|\n" +OPENSHIFT_ANSIBLE_PATH = os.path.dirname(os.path.realpath(__file__)) + FILES = {'yedit.py': ['src/base.py', 'src/yedit.py', 'ansible/yedit.py'], } - def main(): ''' combine the necessary files to create the ansible module ''' - openshift_ansible = ('../library/') + library = os.path.join(OPENSHIFT_ANSIBLE_PATH, '..', 'library/') for fname, parts in FILES.items(): - with open(os.path.join(openshift_ansible, fname), 'w') as afd: + with open(os.path.join(library, fname), 'w') as afd: afd.seek(0) afd.write(GEN_STR) for fpart in parts: - with open(fpart) as pfd: + with open(os.path.join(OPENSHIFT_ANSIBLE_PATH, fpart)) as pfd: # first line is pylint disable so skip it for idx, line in enumerate(pfd): if idx == 0 and 'skip-file' in line: diff --git a/roles/lib_yaml_editor/build/src/base.py b/roles/lib_yaml_editor/build/src/base.py index ad8b041cf..9e43d45dc 100644 --- a/roles/lib_yaml_editor/build/src/base.py +++ b/roles/lib_yaml_editor/build/src/base.py @@ -5,5 +5,13 @@ module for managing yaml files ''' import os +import re + import yaml +# This is here because of a bug that causes yaml +# to incorrectly handle timezone info on timestamps +def timestamp_constructor(_, node): + ''' return timestamps as strings''' + return str(node.value) +yaml.add_constructor(u'tag:yaml.org,2002:timestamp', timestamp_constructor) diff --git a/roles/lib_yaml_editor/build/src/yedit.py b/roles/lib_yaml_editor/build/src/yedit.py index 4f6a91d8b..642733914 100644 --- a/roles/lib_yaml_editor/build/src/yedit.py +++ b/roles/lib_yaml_editor/build/src/yedit.py @@ -6,15 +6,16 @@ class YeditException(Exception): class Yedit(object): ''' Class to modify yaml files ''' + re_valid_key = r"(((\[-?\d+\])|([a-zA-Z-./]+)).?)+$" + re_key = r"(?:\[(-?\d+)\])|([a-zA-Z-./]+)" - def __init__(self, filename=None, content=None): + def __init__(self, filename=None, content=None, content_type='yaml'): self.content = content self.filename = filename self.__yaml_dict = content + self.content_type = content_type if self.filename and not self.content: - self.get() - elif self.filename and self.content: - self.write() + self.load(content_type=self.content_type) @property def yaml_dict(self): @@ -27,58 +28,91 @@ class Yedit(object): self.__yaml_dict = value @staticmethod - def remove_entry(data, keys): - ''' remove an item from a dictionary with key notation a.b.c - d = {'a': {'b': 'c'}}} - keys = a.b - item = c - ''' - if "." in keys: - key, rest = keys.split(".", 1) - if key in data.keys(): - Yedit.remove_entry(data[key], rest) - else: - del data[keys] + def remove_entry(data, key): + ''' remove data at location key ''' + if not (key and re.match(Yedit.re_valid_key, key) and isinstance(data, (list, dict))): + return None + + key_indexes = re.findall(Yedit.re_key, key) + for arr_ind, dict_key in key_indexes[:-1]: + if dict_key and isinstance(data, dict): + data = data.get(dict_key, None) + elif arr_ind and isinstance(data, list) and int(arr_ind) <= len(data) - 1: + data = data[int(arr_ind)] + else: + return None + + # process last index for remove + # expected list entry + if key_indexes[-1][0]: + if isinstance(data, list) and int(key_indexes[-1][0]) <= len(data) - 1: + del data[int(key_indexes[-1][0])] + return True + + # expected dict entry + elif key_indexes[-1][1]: + if isinstance(data, dict): + del data[key_indexes[-1][1]] + return True @staticmethod - def add_entry(data, keys, item): - ''' Add an item to a dictionary with key notation a.b.c + def add_entry(data, key, item=None): + ''' Get an item from a dictionary with key notation a.b.c d = {'a': {'b': 'c'}}} - keys = a.b - item = c + key = a.b + return c ''' - if "." in keys: - key, rest = keys.split(".", 1) - if key not in data: - data[key] = {} + if not (key and re.match(Yedit.re_valid_key, key) and isinstance(data, (list, dict))): + return None + + curr_data = data + + key_indexes = re.findall(Yedit.re_key, key) + for arr_ind, dict_key in key_indexes[:-1]: + if dict_key: + if isinstance(data, dict) and data.has_key(dict_key): + data = data[dict_key] + continue + + data[dict_key] = {} + data = data[dict_key] - if not isinstance(data, dict): - raise YeditException('Invalid add_entry called on a [%s] of type [%s].' % (data, type(data))) + elif arr_ind and isinstance(data, list) and int(arr_ind) <= len(data) - 1: + data = data[int(arr_ind)] else: - Yedit.add_entry(data[key], rest, item) + return None - else: - data[keys] = item + # process last index for add + # expected list entry + if key_indexes[-1][0] and isinstance(data, list) and int(key_indexes[-1][0]) <= len(data) - 1: + data[int(key_indexes[-1][0])] = item + # expected dict entry + elif key_indexes[-1][1] and isinstance(data, dict): + data[key_indexes[-1][1]] = item + + return curr_data @staticmethod - def get_entry(data, keys): + def get_entry(data, key): ''' Get an item from a dictionary with key notation a.b.c d = {'a': {'b': 'c'}}} - keys = a.b + key = a.b return c ''' - if keys and "." in keys: - key, rest = keys.split(".", 1) - if not isinstance(data[key], dict): - raise YeditException('Invalid get_entry called on a [%s] of type [%s].' % (data, type(data))) + if not (key and re.match(Yedit.re_valid_key, key) and isinstance(data, (list, dict))): + return None + key_indexes = re.findall(Yedit.re_key, key) + for arr_ind, dict_key in key_indexes: + if dict_key and isinstance(data, dict): + data = data.get(dict_key, None) + elif arr_ind and isinstance(data, list) and int(arr_ind) <= len(data) - 1: + data = data[int(arr_ind)] else: - return Yedit.get_entry(data[key], rest) - - else: - return data.get(keys, None) + return None + return data def write(self): ''' write to file ''' @@ -107,7 +141,7 @@ class Yedit(object): return False - def get(self): + def load(self, content_type='yaml'): ''' return yaml file ''' contents = self.read() @@ -116,15 +150,27 @@ class Yedit(object): # check if it is yaml try: - self.yaml_dict = yaml.load(contents) + if content_type == 'yaml': + self.yaml_dict = yaml.load(contents) + elif content_type == 'json': + self.yaml_dict = json.loads(contents) except yaml.YAMLError as _: - # Error loading yaml + # Error loading yaml or json return None return self.yaml_dict + def get(self, key): + ''' get a specified key''' + try: + entry = Yedit.get_entry(self.yaml_dict, key) + except KeyError as _: + entry = None + + return entry + def delete(self, key): - ''' put key, value into a yaml file ''' + ''' remove key from a dict''' try: entry = Yedit.get_entry(self.yaml_dict, key) except KeyError as _: @@ -132,12 +178,14 @@ class Yedit(object): if not entry: return (False, self.yaml_dict) - Yedit.remove_entry(self.yaml_dict, key) - self.write() - return (True, self.get()) + result = Yedit.remove_entry(self.yaml_dict, key) + if not result: + return (False, self.yaml_dict) + + return (True, self.yaml_dict) def put(self, key, value): - ''' put key, value into a yaml file ''' + ''' put key, value into a dict ''' try: entry = Yedit.get_entry(self.yaml_dict, key) except KeyError as _: @@ -146,15 +194,16 @@ class Yedit(object): if entry == value: return (False, self.yaml_dict) - Yedit.add_entry(self.yaml_dict, key, value) - self.write() - return (True, self.get()) + result = Yedit.add_entry(self.yaml_dict, key, value) + if not result: + return (False, self.yaml_dict) + + return (True, self.yaml_dict) def create(self, key, value): - ''' create the file ''' + ''' create a yaml file ''' if not self.exists(): self.yaml_dict = {key: value} - self.write() - return (True, self.get()) + return (True, self.yaml_dict) - return (False, self.get()) + return (False, self.yaml_dict) diff --git a/roles/lib_yaml_editor/build/test/foo.yml b/roles/lib_yaml_editor/build/test/foo.yml index 2a7a89ce2..20e9ff3fe 100644 --- a/roles/lib_yaml_editor/build/test/foo.yml +++ b/roles/lib_yaml_editor/build/test/foo.yml @@ -1 +1 @@ -foo: barplus +foo: bar diff --git a/roles/lib_yaml_editor/library/yedit.py b/roles/lib_yaml_editor/library/yedit.py index f375fd8e2..b7ae45b31 100644 --- a/roles/lib_yaml_editor/library/yedit.py +++ b/roles/lib_yaml_editor/library/yedit.py @@ -12,7 +12,15 @@ module for managing yaml files ''' import os +import re + import yaml +# This is here because of a bug that causes yaml +# to incorrectly handle timezone info on timestamps +def timestamp_constructor(_, node): + ''' return timestamps as strings''' + return str(node.value) +yaml.add_constructor(u'tag:yaml.org,2002:timestamp', timestamp_constructor) class YeditException(Exception): @@ -21,15 +29,16 @@ class YeditException(Exception): class Yedit(object): ''' Class to modify yaml files ''' + re_valid_key = r"(((\[-?\d+\])|([a-zA-Z-./]+)).?)+$" + re_key = r"(?:\[(-?\d+)\])|([a-zA-Z-./]+)" - def __init__(self, filename=None, content=None): + def __init__(self, filename=None, content=None, content_type='yaml'): self.content = content self.filename = filename self.__yaml_dict = content + self.content_type = content_type if self.filename and not self.content: - self.get() - elif self.filename and self.content: - self.write() + self.load(content_type=self.content_type) @property def yaml_dict(self): @@ -42,58 +51,91 @@ class Yedit(object): self.__yaml_dict = value @staticmethod - def remove_entry(data, keys): - ''' remove an item from a dictionary with key notation a.b.c - d = {'a': {'b': 'c'}}} - keys = a.b - item = c - ''' - if "." in keys: - key, rest = keys.split(".", 1) - if key in data.keys(): - Yedit.remove_entry(data[key], rest) - else: - del data[keys] + def remove_entry(data, key): + ''' remove data at location key ''' + if not (key and re.match(Yedit.re_valid_key, key) and isinstance(data, (list, dict))): + return None + + key_indexes = re.findall(Yedit.re_key, key) + for arr_ind, dict_key in key_indexes[:-1]: + if dict_key and isinstance(data, dict): + data = data.get(dict_key, None) + elif arr_ind and isinstance(data, list) and int(arr_ind) <= len(data) - 1: + data = data[int(arr_ind)] + else: + return None + + # process last index for remove + # expected list entry + if key_indexes[-1][0]: + if isinstance(data, list) and int(key_indexes[-1][0]) <= len(data) - 1: + del data[int(key_indexes[-1][0])] + return True + + # expected dict entry + elif key_indexes[-1][1]: + if isinstance(data, dict): + del data[key_indexes[-1][1]] + return True @staticmethod - def add_entry(data, keys, item): - ''' Add an item to a dictionary with key notation a.b.c + def add_entry(data, key, item=None): + ''' Get an item from a dictionary with key notation a.b.c d = {'a': {'b': 'c'}}} - keys = a.b - item = c + key = a.b + return c ''' - if "." in keys: - key, rest = keys.split(".", 1) - if key not in data: - data[key] = {} + if not (key and re.match(Yedit.re_valid_key, key) and isinstance(data, (list, dict))): + return None - if not isinstance(data, dict): - raise YeditException('Invalid add_entry called on a [%s] of type [%s].' % (data, type(data))) + curr_data = data + + key_indexes = re.findall(Yedit.re_key, key) + for arr_ind, dict_key in key_indexes[:-1]: + if dict_key: + if isinstance(data, dict) and data.has_key(dict_key): + data = data[dict_key] + continue + + data[dict_key] = {} + data = data[dict_key] + + elif arr_ind and isinstance(data, list) and int(arr_ind) <= len(data) - 1: + data = data[int(arr_ind)] else: - Yedit.add_entry(data[key], rest, item) + return None - else: - data[keys] = item + # process last index for add + # expected list entry + if key_indexes[-1][0] and isinstance(data, list) and int(key_indexes[-1][0]) <= len(data) - 1: + data[int(key_indexes[-1][0])] = item + # expected dict entry + elif key_indexes[-1][1] and isinstance(data, dict): + data[key_indexes[-1][1]] = item + + return curr_data @staticmethod - def get_entry(data, keys): + def get_entry(data, key): ''' Get an item from a dictionary with key notation a.b.c d = {'a': {'b': 'c'}}} - keys = a.b + key = a.b return c ''' - if keys and "." in keys: - key, rest = keys.split(".", 1) - if not isinstance(data[key], dict): - raise YeditException('Invalid get_entry called on a [%s] of type [%s].' % (data, type(data))) + if not (key and re.match(Yedit.re_valid_key, key) and isinstance(data, (list, dict))): + return None + key_indexes = re.findall(Yedit.re_key, key) + for arr_ind, dict_key in key_indexes: + if dict_key and isinstance(data, dict): + data = data.get(dict_key, None) + elif arr_ind and isinstance(data, list) and int(arr_ind) <= len(data) - 1: + data = data[int(arr_ind)] else: - return Yedit.get_entry(data[key], rest) - - else: - return data.get(keys, None) + return None + return data def write(self): ''' write to file ''' @@ -122,7 +164,7 @@ class Yedit(object): return False - def get(self): + def load(self, content_type='yaml'): ''' return yaml file ''' contents = self.read() @@ -131,15 +173,27 @@ class Yedit(object): # check if it is yaml try: - self.yaml_dict = yaml.load(contents) + if content_type == 'yaml': + self.yaml_dict = yaml.load(contents) + elif content_type == 'json': + self.yaml_dict = json.loads(contents) except yaml.YAMLError as _: - # Error loading yaml + # Error loading yaml or json return None return self.yaml_dict + def get(self, key): + ''' get a specified key''' + try: + entry = Yedit.get_entry(self.yaml_dict, key) + except KeyError as _: + entry = None + + return entry + def delete(self, key): - ''' put key, value into a yaml file ''' + ''' remove key from a dict''' try: entry = Yedit.get_entry(self.yaml_dict, key) except KeyError as _: @@ -147,12 +201,14 @@ class Yedit(object): if not entry: return (False, self.yaml_dict) - Yedit.remove_entry(self.yaml_dict, key) - self.write() - return (True, self.get()) + result = Yedit.remove_entry(self.yaml_dict, key) + if not result: + return (False, self.yaml_dict) + + return (True, self.yaml_dict) def put(self, key, value): - ''' put key, value into a yaml file ''' + ''' put key, value into a dict ''' try: entry = Yedit.get_entry(self.yaml_dict, key) except KeyError as _: @@ -161,18 +217,19 @@ class Yedit(object): if entry == value: return (False, self.yaml_dict) - Yedit.add_entry(self.yaml_dict, key, value) - self.write() - return (True, self.get()) + result = Yedit.add_entry(self.yaml_dict, key, value) + if not result: + return (False, self.yaml_dict) + + return (True, self.yaml_dict) def create(self, key, value): - ''' create the file ''' + ''' create a yaml file ''' if not self.exists(): self.yaml_dict = {key: value} - self.write() - return (True, self.get()) + return (True, self.yaml_dict) - return (False, self.get()) + return (False, self.yaml_dict) def main(): ''' @@ -198,7 +255,7 @@ def main(): yamlfile = Yedit(module.params['src'], module.params['content']) - rval = yamlfile.get() + rval = yamlfile.load() if not rval and state != 'present': module.fail_json(msg='Error opening file [%s]. Verify that the' + \ ' file exists, that it is has correct permissions, and is valid yaml.') @@ -219,13 +276,16 @@ def main(): if rval: rval = yamlfile.put(module.params['key'], value) + if rval[0]: + yamlfile.write() module.exit_json(changed=rval[0], results=rval[1], state="present") if not module.params['content']: rval = yamlfile.create(module.params['key'], value) else: - yamlfile.write() - rval = yamlfile.get() + rval = yamlfile.load() + yamlfile.write() + module.exit_json(changed=rval[0], results=rval[1], state="present") module.exit_json(failed=True, |