Skip to content

Commit

Permalink
Workaround yaml key sorting on rhel-8 [fix psss#207]
Browse files Browse the repository at this point in the history
Instead of using the 'sort_keys' parameter which is available only
in newer versions of pyyaml disable sorting globally on rhel-8.
  • Loading branch information
psss committed Apr 16, 2020
1 parent c63b0cf commit af3a19f
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions tmt/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,23 @@ def dict_to_yaml(data, width=None, sort=False):
width=width, indent=4, default_flow_style=False)
return output.getvalue()

# FIXME: Temporary workaround for rhel-8 to disable key sorting
# https://stackoverflow.com/questions/31605131/
# https://github.com/psss/tmt/issues/207
try:
output = dict_to_yaml(dict(one=1, two=2, three=3))
except TypeError:
representer = lambda self, data: self.represent_mapping(
'tag:yaml.org,2002:map', data.items())
yaml.add_representer(dict, representer, Dumper=yaml.SafeDumper)
def dict_to_yaml(data, width=None, sort=False):
""" Convert dictionary into yaml (ignore sort) """
output = io.StringIO()
yaml.safe_dump(
data, output, encoding='utf-8', allow_unicode=True,
width=width, indent=4, default_flow_style=False)
return output.getvalue()


def yaml_to_dict(data):
""" Convert yaml into dictionary """
Expand Down

0 comments on commit af3a19f

Please sign in to comment.