diff options
author | Scott Dodson <sdodson@redhat.com> | 2017-02-16 20:23:03 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-02-16 20:23:03 -0500 |
commit | 43659a5b19508336efa6901c7a38a6fa39a5a144 (patch) | |
tree | 9ffa334505dd3be680f2973446f0194d1c7c2393 /roles/lib_utils/library | |
parent | c455be5a4d773bedbdc7870cbc33b049205524c4 (diff) | |
parent | c4e712ba3cc40d1deed87f56c71b337193597422 (diff) | |
download | openshift-43659a5b19508336efa6901c7a38a6fa39a5a144.tar.gz openshift-43659a5b19508336efa6901c7a38a6fa39a5a144.tar.bz2 openshift-43659a5b19508336efa6901c7a38a6fa39a5a144.tar.xz openshift-43659a5b19508336efa6901c7a38a6fa39a5a144.zip |
Merge pull request #3383 from kwoodson/yedit_yaml_support
Adding fallback support for pyyaml.
Diffstat (limited to 'roles/lib_utils/library')
-rw-r--r-- | roles/lib_utils/library/repoquery.py | 9 | ||||
-rw-r--r-- | roles/lib_utils/library/yedit.py | 64 |
2 files changed, 53 insertions, 20 deletions
diff --git a/roles/lib_utils/library/repoquery.py b/roles/lib_utils/library/repoquery.py index 7f0105290..cc7aa04fd 100644 --- a/roles/lib_utils/library/repoquery.py +++ b/roles/lib_utils/library/repoquery.py @@ -29,13 +29,18 @@ # pylint: disable=wrong-import-order,wrong-import-position,unused-import from __future__ import print_function # noqa: F401 +import copy # noqa: F401 import json # noqa: F401 import os # noqa: F401 import re # noqa: F401 -# pylint: disable=import-error -import ruamel.yaml as yaml # noqa: F401 import shutil # noqa: F401 +# pylint: disable=import-error +try: + import ruamel.yaml as yaml # noqa: F401 +except ImportError: + import yaml # noqa: F401 + from ansible.module_utils.basic import AnsibleModule # -*- -*- -*- End included fragment: lib/import.py -*- -*- -*- diff --git a/roles/lib_utils/library/yedit.py b/roles/lib_utils/library/yedit.py index 1c74b4d3f..a358e980e 100644 --- a/roles/lib_utils/library/yedit.py +++ b/roles/lib_utils/library/yedit.py @@ -29,13 +29,18 @@ # pylint: disable=wrong-import-order,wrong-import-position,unused-import from __future__ import print_function # noqa: F401 +import copy # noqa: F401 import json # noqa: F401 import os # noqa: F401 import re # noqa: F401 -# pylint: disable=import-error -import ruamel.yaml as yaml # noqa: F401 import shutil # noqa: F401 +# pylint: disable=import-error +try: + import ruamel.yaml as yaml # noqa: F401 +except ImportError: + import yaml # noqa: F401 + from ansible.module_utils.basic import AnsibleModule # -*- -*- -*- End included fragment: lib/import.py -*- -*- -*- @@ -375,11 +380,15 @@ class Yedit(object): if self.backup and self.file_exists(): shutil.copy(self.filename, self.filename + '.orig') - # pylint: disable=no-member - if hasattr(self.yaml_dict, 'fa'): - self.yaml_dict.fa.set_block_style() + if hasattr(yaml, 'RoundTripDumper'): + # pylint: disable=no-member + if hasattr(self.yaml_dict, 'fa'): + self.yaml_dict.fa.set_block_style() - Yedit._write(self.filename, yaml.dump(self.yaml_dict, Dumper=yaml.RoundTripDumper)) + # pylint: disable=no-member + Yedit._write(self.filename, yaml.dump(self.yaml_dict, Dumper=yaml.RoundTripDumper)) + else: + Yedit._write(self.filename, yaml.safe_dump(self.yaml_dict, default_flow_style=False)) return (True, self.yaml_dict) @@ -419,10 +428,16 @@ class Yedit(object): # check if it is yaml try: if content_type == 'yaml' and contents: - self.yaml_dict = yaml.load(contents, yaml.RoundTripLoader) + # pylint: disable=no-member + if hasattr(yaml, 'RoundTripLoader'): + self.yaml_dict = yaml.load(contents, yaml.RoundTripLoader) + else: + self.yaml_dict = yaml.safe_load(contents) + # pylint: disable=no-member if hasattr(self.yaml_dict, 'fa'): self.yaml_dict.fa.set_block_style() + elif content_type == 'json' and contents: self.yaml_dict = json.loads(contents) except yaml.YAMLError as err: @@ -587,12 +602,19 @@ class Yedit(object): return (False, self.yaml_dict) # deepcopy didn't work - tmp_copy = yaml.load(yaml.round_trip_dump(self.yaml_dict, - default_flow_style=False), - yaml.RoundTripLoader) - # pylint: disable=no-member - if hasattr(self.yaml_dict, 'fa'): - tmp_copy.fa.set_block_style() + if hasattr(yaml, 'round_trip_dump'): + # pylint: disable=no-member + tmp_copy = yaml.load(yaml.round_trip_dump(self.yaml_dict, + default_flow_style=False), + yaml.RoundTripLoader) + + # pylint: disable=no-member + if hasattr(self.yaml_dict, 'fa'): + tmp_copy.fa.set_block_style() + + else: + tmp_copy = copy.deepcopy(self.yaml_dict) + result = Yedit.add_entry(tmp_copy, path, value, self.separator) if not result: return (False, self.yaml_dict) @@ -605,11 +627,17 @@ class Yedit(object): ''' create a yaml file ''' if not self.file_exists(): # deepcopy didn't work - tmp_copy = yaml.load(yaml.round_trip_dump(self.yaml_dict, default_flow_style=False), # noqa: E501 - yaml.RoundTripLoader) - # pylint: disable=no-member - if hasattr(self.yaml_dict, 'fa'): - tmp_copy.fa.set_block_style() + if hasattr(yaml, 'round_trip_dump'): + # pylint: disable=no-member + tmp_copy = yaml.load(yaml.round_trip_dump(self.yaml_dict, default_flow_style=False), # noqa: E501 + yaml.RoundTripLoader) + + # pylint: disable=no-member + if hasattr(self.yaml_dict, 'fa'): + tmp_copy.fa.set_block_style() + else: + tmp_copy = copy.deepcopy(self.yaml_dict) + result = Yedit.add_entry(tmp_copy, path, value, self.separator) if result: self.yaml_dict = tmp_copy |