Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding fallback support for pyyaml. #3383

Merged
merged 3 commits into from Feb 17, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
75 changes: 56 additions & 19 deletions roles/lib_openshift/library/oadm_manage_node.py
Expand Up @@ -33,14 +33,19 @@

from __future__ import print_function
import atexit
import copy
import json
import os
import re
import shutil
import subprocess
import tempfile
# pylint: disable=import-error
import ruamel.yaml as yaml
try:
import ruamel.yaml as yaml
except ImportError:
import yaml

from ansible.module_utils.basic import AnsibleModule

# -*- -*- -*- End included fragment: lib/import.py -*- -*- -*-
Expand Down Expand Up @@ -330,11 +335,15 @@ def write(self):
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)

Expand Down Expand Up @@ -374,10 +383,16 @@ def load(self, content_type='yaml'):
# 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:
Expand Down Expand Up @@ -542,12 +557,19 @@ def put(self, path, value):
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)
Expand All @@ -560,11 +582,17 @@ def create(self, path, value):
''' 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
Expand Down Expand Up @@ -1006,7 +1034,12 @@ def create_tmp_file_from_contents(rname, data, ftype='yaml'):
tmp = Utils.create_tmpfile(prefix=rname)

if ftype == 'yaml':
Utils._write(tmp, yaml.dump(data, Dumper=yaml.RoundTripDumper))
# pylint: disable=no-member
if hasattr(yaml, 'RoundTripDumper'):
Utils._write(tmp, yaml.dump(data, Dumper=yaml.RoundTripDumper))
else:
Utils._write(tmp, yaml.safe_dump(data, default_flow_style=False))

elif ftype == 'json':
Utils._write(tmp, json.dumps(data))
else:
Expand Down Expand Up @@ -1088,7 +1121,11 @@ def get_resource_file(sfile, sfile_type='yaml'):
contents = sfd.read()

if sfile_type == 'yaml':
contents = yaml.load(contents, yaml.RoundTripLoader)
# pylint: disable=no-member
if hasattr(yaml, 'RoundTripLoader'):
contents = yaml.load(contents, yaml.RoundTripLoader)
else:
contents = yaml.safe_load(contents)
elif sfile_type == 'json':
contents = json.loads(contents)

Expand Down
75 changes: 56 additions & 19 deletions roles/lib_openshift/library/oc_edit.py
Expand Up @@ -33,14 +33,19 @@

from __future__ import print_function
import atexit
import copy
import json
import os
import re
import shutil
import subprocess
import tempfile
# pylint: disable=import-error
import ruamel.yaml as yaml
try:
import ruamel.yaml as yaml
except ImportError:
import yaml

from ansible.module_utils.basic import AnsibleModule

# -*- -*- -*- End included fragment: lib/import.py -*- -*- -*-
Expand Down Expand Up @@ -358,11 +363,15 @@ def write(self):
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)

Expand Down Expand Up @@ -402,10 +411,16 @@ def load(self, content_type='yaml'):
# 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:
Expand Down Expand Up @@ -570,12 +585,19 @@ def put(self, path, value):
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)
Expand All @@ -588,11 +610,17 @@ def create(self, path, value):
''' 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
Expand Down Expand Up @@ -1034,7 +1062,12 @@ def create_tmp_file_from_contents(rname, data, ftype='yaml'):
tmp = Utils.create_tmpfile(prefix=rname)

if ftype == 'yaml':
Utils._write(tmp, yaml.dump(data, Dumper=yaml.RoundTripDumper))
# pylint: disable=no-member
if hasattr(yaml, 'RoundTripDumper'):
Utils._write(tmp, yaml.dump(data, Dumper=yaml.RoundTripDumper))
else:
Utils._write(tmp, yaml.safe_dump(data, default_flow_style=False))

elif ftype == 'json':
Utils._write(tmp, json.dumps(data))
else:
Expand Down Expand Up @@ -1116,7 +1149,11 @@ def get_resource_file(sfile, sfile_type='yaml'):
contents = sfd.read()

if sfile_type == 'yaml':
contents = yaml.load(contents, yaml.RoundTripLoader)
# pylint: disable=no-member
if hasattr(yaml, 'RoundTripLoader'):
contents = yaml.load(contents, yaml.RoundTripLoader)
else:
contents = yaml.safe_load(contents)
elif sfile_type == 'json':
contents = json.loads(contents)

Expand Down