diff options
author | Scott Dodson <sdodson@redhat.com> | 2017-01-18 13:39:43 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-01-18 13:39:43 -0500 |
commit | 7a0228f432a0d68bd63a68b940a7913d4a6a7e7c (patch) | |
tree | 351507d1744326f27bcf23e523e0bacc6de2a7fe /roles/lib_utils/library/yedit.py | |
parent | b4d76724b764d0f18ace622bafb9ce390fdd9f19 (diff) | |
parent | 3fd3cd3a07d9f000c8cb8bd1b7a49ac2af675696 (diff) | |
download | openshift-7a0228f432a0d68bd63a68b940a7913d4a6a7e7c.tar.gz openshift-7a0228f432a0d68bd63a68b940a7913d4a6a7e7c.tar.bz2 openshift-7a0228f432a0d68bd63a68b940a7913d4a6a7e7c.tar.xz openshift-7a0228f432a0d68bd63a68b940a7913d4a6a7e7c.zip |
Merge pull request #3092 from kwoodson/lib_openshift_3.3
lib_openshift. This is the initial commit that has oc_route.
Diffstat (limited to 'roles/lib_utils/library/yedit.py')
-rw-r--r-- | roles/lib_utils/library/yedit.py | 108 |
1 files changed, 55 insertions, 53 deletions
diff --git a/roles/lib_utils/library/yedit.py b/roles/lib_utils/library/yedit.py index fb545c7c8..d882c983e 100644 --- a/roles/lib_utils/library/yedit.py +++ b/roles/lib_utils/library/yedit.py @@ -24,7 +24,6 @@ # limitations under the License. # - # pylint: disable=wrong-import-order import json import os @@ -163,6 +162,7 @@ EXAMPLES = ''' # b: # c: d ''' +# noqa: E301,E302 class YeditException(Exception): @@ -170,6 +170,7 @@ class YeditException(Exception): pass +# pylint: disable=too-many-public-methods class Yedit(object): ''' Class to modify yaml files ''' re_valid_key = r"(((\[-?\d+\])|([0-9a-zA-Z%s/_-]+)).?)+$" @@ -590,6 +591,48 @@ class Yedit(object): return (False, self.yaml_dict) + @staticmethod + def get_curr_value(invalue, val_type): + '''return the current value''' + if invalue is None: + return None + + curr_value = invalue + if val_type == 'yaml': + curr_value = yaml.load(invalue) + elif val_type == 'json': + curr_value = json.loads(invalue) + + return curr_value + + @staticmethod + def parse_value(inc_value, vtype=''): + '''determine value type passed''' + true_bools = ['y', 'Y', 'yes', 'Yes', 'YES', 'true', 'True', 'TRUE', + 'on', 'On', 'ON', ] + false_bools = ['n', 'N', 'no', 'No', 'NO', 'false', 'False', 'FALSE', + 'off', 'Off', 'OFF'] + + # It came in as a string but you didn't specify value_type as string + # we will convert to bool if it matches any of the above cases + if isinstance(inc_value, str) and 'bool' in vtype: + if inc_value not in true_bools and inc_value not in false_bools: + raise YeditException('Not a boolean type. str=[%s] vtype=[%s]' + % (inc_value, vtype)) + elif isinstance(inc_value, bool) and 'str' in vtype: + inc_value = str(inc_value) + + # If vtype is not str then go ahead and attempt to yaml load it. + if isinstance(inc_value, str) and 'str' not in vtype: + try: + inc_value = yaml.load(inc_value) + except Exception: + raise YeditException('Could not determine type of incoming ' + + 'value. value=[%s] vtype=[%s]' + % (type(inc_value), vtype)) + + return inc_value + # pylint: disable=too-many-return-statements,too-many-branches @staticmethod def run_ansible(module): @@ -610,8 +653,8 @@ class Yedit(object): if module.params['state'] == 'list': if module.params['content']: - content = parse_value(module.params['content'], - module.params['content_type']) + content = Yedit.parse_value(module.params['content'], + module.params['content_type']) yamlfile.yaml_dict = content if module.params['key']: @@ -621,8 +664,8 @@ class Yedit(object): elif module.params['state'] == 'absent': if module.params['content']: - content = parse_value(module.params['content'], - module.params['content_type']) + content = Yedit.parse_value(module.params['content'], + module.params['content_type']) yamlfile.yaml_dict = content if module.params['update']: @@ -639,8 +682,8 @@ class Yedit(object): elif module.params['state'] == 'present': # check if content is different than what is in the file if module.params['content']: - content = parse_value(module.params['content'], - module.params['content_type']) + content = Yedit.parse_value(module.params['content'], + module.params['content_type']) # We had no edits to make and the contents are the same if yamlfile.yaml_dict == content and \ @@ -653,12 +696,13 @@ class Yedit(object): # we were passed a value; parse it if module.params['value']: - value = parse_value(module.params['value'], - module.params['value_type']) + value = Yedit.parse_value(module.params['value'], + module.params['value_type']) key = module.params['key'] if module.params['update']: # pylint: disable=line-too-long - curr_value = get_curr_value(parse_value(module.params['curr_value']), module.params['curr_value_format']) # noqa: #501 + curr_value = Yedit.get_curr_value(Yedit.parse_value(module.params['curr_value']), # noqa: E501 + module.params['curr_value_format']) # noqa: E501 rval = yamlfile.update(key, value, module.params['index'], curr_value) # noqa: E501 @@ -684,48 +728,6 @@ class Yedit(object): return {'failed': True, 'msg': 'Unkown state passed'} -def get_curr_value(invalue, val_type): - '''return the current value''' - if invalue is None: - return None - - curr_value = invalue - if val_type == 'yaml': - curr_value = yaml.load(invalue) - elif val_type == 'json': - curr_value = json.loads(invalue) - - return curr_value - - -def parse_value(inc_value, vtype=''): - '''determine value type passed''' - true_bools = ['y', 'Y', 'yes', 'Yes', 'YES', 'true', 'True', 'TRUE', - 'on', 'On', 'ON', ] - false_bools = ['n', 'N', 'no', 'No', 'NO', 'false', 'False', 'FALSE', - 'off', 'Off', 'OFF'] - - # It came in as a string but you didn't specify value_type as string - # we will convert to bool if it matches any of the above cases - if isinstance(inc_value, str) and 'bool' in vtype: - if inc_value not in true_bools and inc_value not in false_bools: - raise YeditException('Not a boolean type. str=[%s] vtype=[%s]' - % (inc_value, vtype)) - elif isinstance(inc_value, bool) and 'str' in vtype: - inc_value = str(inc_value) - - # If vtype is not str then go ahead and attempt to yaml load it. - if isinstance(inc_value, str) and 'str' not in vtype: - try: - inc_value = yaml.load(inc_value) - except Exception: - raise YeditException('Could not determine type of incoming ' + - 'value. value=[%s] vtype=[%s]' - % (type(inc_value), vtype)) - - return inc_value - - # pylint: disable=too-many-branches def main(): ''' ansible oc module for secrets ''' @@ -757,7 +759,7 @@ def main(): rval = Yedit.run_ansible(module) if 'failed' in rval and rval['failed']: - module.fail_json(msg=rval['msg']) + module.fail_json(**rval) module.exit_json(**rval) |