Skip to content

Commit

Permalink
Fixed tests for pyyaml vs ruamel. Added import logic. Fixed safe load.
Browse files Browse the repository at this point in the history
  • Loading branch information
Kenny Woodson committed Feb 16, 2017
1 parent 42af186 commit d4db2dd
Show file tree
Hide file tree
Showing 18 changed files with 297 additions and 95 deletions.
21 changes: 16 additions & 5 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 @@ -338,7 +343,7 @@ def write(self):
if hasattr(yaml, 'RoundTripDumper'):
Yedit._write(self.filename, yaml.dump(self.yaml_dict, Dumper=yaml.RoundTripDumper))
else:
Yedit._write(self.filename, yaml.dump(self.yaml_dict, default_flow_style=False))
Yedit._write(self.filename, yaml.safe_dump(self.yaml_dict, default_flow_style=False))

return (True, self.yaml_dict)

Expand Down Expand Up @@ -381,7 +386,7 @@ def load(self, content_type='yaml'):
if hasattr(yaml, 'RoundTripLoader'):
self.yaml_dict = yaml.load(contents, yaml.RoundTripLoader)
else:
self.yaml_dict = yaml.load(contents)
self.yaml_dict = yaml.safe_load(contents)
# pylint: disable=no-member
if hasattr(self.yaml_dict, 'fa'):
self.yaml_dict.fa.set_block_style()
Expand Down Expand Up @@ -1023,7 +1028,10 @@ 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))
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 @@ -1105,7 +1113,10 @@ def get_resource_file(sfile, sfile_type='yaml'):
contents = sfd.read()

if sfile_type == 'yaml':
contents = yaml.load(contents, yaml.RoundTripLoader)
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
21 changes: 16 additions & 5 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 @@ -366,7 +371,7 @@ def write(self):
if hasattr(yaml, 'RoundTripDumper'):
Yedit._write(self.filename, yaml.dump(self.yaml_dict, Dumper=yaml.RoundTripDumper))
else:
Yedit._write(self.filename, yaml.dump(self.yaml_dict, default_flow_style=False))
Yedit._write(self.filename, yaml.safe_dump(self.yaml_dict, default_flow_style=False))

return (True, self.yaml_dict)

Expand Down Expand Up @@ -409,7 +414,7 @@ def load(self, content_type='yaml'):
if hasattr(yaml, 'RoundTripLoader'):
self.yaml_dict = yaml.load(contents, yaml.RoundTripLoader)
else:
self.yaml_dict = yaml.load(contents)
self.yaml_dict = yaml.safe_load(contents)
# pylint: disable=no-member
if hasattr(self.yaml_dict, 'fa'):
self.yaml_dict.fa.set_block_style()
Expand Down Expand Up @@ -1051,7 +1056,10 @@ 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))
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 @@ -1133,7 +1141,10 @@ def get_resource_file(sfile, sfile_type='yaml'):
contents = sfd.read()

if sfile_type == 'yaml':
contents = yaml.load(contents, yaml.RoundTripLoader)
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
21 changes: 16 additions & 5 deletions roles/lib_openshift/library/oc_env.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 @@ -333,7 +338,7 @@ def write(self):
if hasattr(yaml, 'RoundTripDumper'):
Yedit._write(self.filename, yaml.dump(self.yaml_dict, Dumper=yaml.RoundTripDumper))
else:
Yedit._write(self.filename, yaml.dump(self.yaml_dict, default_flow_style=False))
Yedit._write(self.filename, yaml.safe_dump(self.yaml_dict, default_flow_style=False))

return (True, self.yaml_dict)

Expand Down Expand Up @@ -376,7 +381,7 @@ def load(self, content_type='yaml'):
if hasattr(yaml, 'RoundTripLoader'):
self.yaml_dict = yaml.load(contents, yaml.RoundTripLoader)
else:
self.yaml_dict = yaml.load(contents)
self.yaml_dict = yaml.safe_load(contents)
# pylint: disable=no-member
if hasattr(self.yaml_dict, 'fa'):
self.yaml_dict.fa.set_block_style()
Expand Down Expand Up @@ -1018,7 +1023,10 @@ 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))
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 @@ -1100,7 +1108,10 @@ def get_resource_file(sfile, sfile_type='yaml'):
contents = sfd.read()

if sfile_type == 'yaml':
contents = yaml.load(contents, yaml.RoundTripLoader)
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
21 changes: 16 additions & 5 deletions roles/lib_openshift/library/oc_label.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 @@ -342,7 +347,7 @@ def write(self):
if hasattr(yaml, 'RoundTripDumper'):
Yedit._write(self.filename, yaml.dump(self.yaml_dict, Dumper=yaml.RoundTripDumper))
else:
Yedit._write(self.filename, yaml.dump(self.yaml_dict, default_flow_style=False))
Yedit._write(self.filename, yaml.safe_dump(self.yaml_dict, default_flow_style=False))

return (True, self.yaml_dict)

Expand Down Expand Up @@ -385,7 +390,7 @@ def load(self, content_type='yaml'):
if hasattr(yaml, 'RoundTripLoader'):
self.yaml_dict = yaml.load(contents, yaml.RoundTripLoader)
else:
self.yaml_dict = yaml.load(contents)
self.yaml_dict = yaml.safe_load(contents)
# pylint: disable=no-member
if hasattr(self.yaml_dict, 'fa'):
self.yaml_dict.fa.set_block_style()
Expand Down Expand Up @@ -1027,7 +1032,10 @@ 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))
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 @@ -1109,7 +1117,10 @@ def get_resource_file(sfile, sfile_type='yaml'):
contents = sfd.read()

if sfile_type == 'yaml':
contents = yaml.load(contents, yaml.RoundTripLoader)
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
21 changes: 16 additions & 5 deletions roles/lib_openshift/library/oc_obj.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 @@ -345,7 +350,7 @@ def write(self):
if hasattr(yaml, 'RoundTripDumper'):
Yedit._write(self.filename, yaml.dump(self.yaml_dict, Dumper=yaml.RoundTripDumper))
else:
Yedit._write(self.filename, yaml.dump(self.yaml_dict, default_flow_style=False))
Yedit._write(self.filename, yaml.safe_dump(self.yaml_dict, default_flow_style=False))

return (True, self.yaml_dict)

Expand Down Expand Up @@ -388,7 +393,7 @@ def load(self, content_type='yaml'):
if hasattr(yaml, 'RoundTripLoader'):
self.yaml_dict = yaml.load(contents, yaml.RoundTripLoader)
else:
self.yaml_dict = yaml.load(contents)
self.yaml_dict = yaml.safe_load(contents)
# pylint: disable=no-member
if hasattr(self.yaml_dict, 'fa'):
self.yaml_dict.fa.set_block_style()
Expand Down Expand Up @@ -1030,7 +1035,10 @@ 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))
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 @@ -1112,7 +1120,10 @@ def get_resource_file(sfile, sfile_type='yaml'):
contents = sfd.read()

if sfile_type == 'yaml':
contents = yaml.load(contents, yaml.RoundTripLoader)
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
21 changes: 16 additions & 5 deletions roles/lib_openshift/library/oc_process.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 @@ -334,7 +339,7 @@ def write(self):
if hasattr(yaml, 'RoundTripDumper'):
Yedit._write(self.filename, yaml.dump(self.yaml_dict, Dumper=yaml.RoundTripDumper))
else:
Yedit._write(self.filename, yaml.dump(self.yaml_dict, default_flow_style=False))
Yedit._write(self.filename, yaml.safe_dump(self.yaml_dict, default_flow_style=False))

return (True, self.yaml_dict)

Expand Down Expand Up @@ -377,7 +382,7 @@ def load(self, content_type='yaml'):
if hasattr(yaml, 'RoundTripLoader'):
self.yaml_dict = yaml.load(contents, yaml.RoundTripLoader)
else:
self.yaml_dict = yaml.load(contents)
self.yaml_dict = yaml.safe_load(contents)
# pylint: disable=no-member
if hasattr(self.yaml_dict, 'fa'):
self.yaml_dict.fa.set_block_style()
Expand Down Expand Up @@ -1019,7 +1024,10 @@ 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))
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 @@ -1101,7 +1109,10 @@ def get_resource_file(sfile, sfile_type='yaml'):
contents = sfd.read()

if sfile_type == 'yaml':
contents = yaml.load(contents, yaml.RoundTripLoader)
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

0 comments on commit d4db2dd

Please sign in to comment.