From af3a19f43a10659973e644891927a998720e99d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20=C5=A0pl=C3=ADchal?= Date: Thu, 16 Apr 2020 16:54:38 +0200 Subject: [PATCH] Workaround yaml key sorting on rhel-8 [fix #207] Instead of using the 'sort_keys' parameter which is available only in newer versions of pyyaml disable sorting globally on rhel-8. --- tmt/utils.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/tmt/utils.py b/tmt/utils.py index 273bd6d0..9c892606 100644 --- a/tmt/utils.py +++ b/tmt/utils.py @@ -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 """