Skip to content

Commit

Permalink
Adding fallback support for pyyaml.
Browse files Browse the repository at this point in the history
  • Loading branch information
Kenny Woodson committed Feb 16, 2017
1 parent 43330fb commit a6ad57b
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 32 deletions.
9 changes: 7 additions & 2 deletions roles/lib_utils/library/repoquery.py
Expand Up @@ -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

from ansible.module_utils.basic import AnsibleModule

# -*- -*- -*- End included fragment: lib/import.py -*- -*- -*-
Expand Down
52 changes: 37 additions & 15 deletions roles/lib_utils/library/yedit.py
Expand Up @@ -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

from ansible.module_utils.basic import AnsibleModule

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

return (True, self.yaml_dict)

Expand Down Expand Up @@ -419,7 +428,10 @@ 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)
if hasattr(yaml, 'RoundTripLoader'):
self.yaml_dict = yaml.load(contents, yaml.RoundTripLoader)
else:
self.yaml_dict = yaml.load(contents)
# pylint: disable=no-member
if hasattr(self.yaml_dict, 'fa'):
self.yaml_dict.fa.set_block_style()
Expand Down Expand Up @@ -587,12 +599,18 @@ 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'):
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 @@ -605,11 +623,15 @@ 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'):
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
43 changes: 30 additions & 13 deletions roles/lib_utils/src/class/yedit.py
Expand Up @@ -198,7 +198,11 @@ def write(self):
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
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))

return (True, self.yaml_dict)

Expand Down Expand Up @@ -238,7 +242,10 @@ 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)
if hasattr(yaml, 'RoundTripLoader'):
self.yaml_dict = yaml.load(contents, yaml.RoundTripLoader)
else:
self.yaml_dict = yaml.load(contents)
# pylint: disable=no-member
if hasattr(self.yaml_dict, 'fa'):
self.yaml_dict.fa.set_block_style()
Expand Down Expand Up @@ -406,12 +413,18 @@ 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'):
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 @@ -424,11 +437,15 @@ 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'):
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
9 changes: 7 additions & 2 deletions roles/lib_utils/src/lib/import.py
Expand Up @@ -4,11 +4,16 @@
# 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

from ansible.module_utils.basic import AnsibleModule

0 comments on commit a6ad57b

Please sign in to comment.