From e01b84fa73afaa33ac9be8626654226703bf863c Mon Sep 17 00:00:00 2001 From: deadc0de6 Date: Wed, 5 Jun 2019 09:13:04 +0200 Subject: [PATCH] disable sort_keys on yaml only for version >= 5.1 --- dotdrop/cfg_yaml.py | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/dotdrop/cfg_yaml.py b/dotdrop/cfg_yaml.py index 630f291..8b63e70 100644 --- a/dotdrop/cfg_yaml.py +++ b/dotdrop/cfg_yaml.py @@ -733,6 +733,18 @@ class CfgYaml: new[k] = newv return new + def _yaml_version(self): + """returns True if version >= 5.1""" + minv = [5, 1] + try: + cur = list(map(int, yaml.__version__.split('.'))) + if cur.pop(0) >= minv.pop(0) and \ + cur.pop(1) >= minv.pop(1): + return True + except: + return False + return False + def save(self): """save this instance and return True if saved""" if not self.dirty: @@ -748,10 +760,12 @@ class CfgYaml: if self.key_profiles not in content: content[self.key_profiles] = None + opts = {'default_flow_style': False, 'indent': 2} + if self._yaml_version(): + opts['sort_keys'] = False + data = yaml.safe_dump(content, **opts) + # ensure no null are displayed - data = yaml.safe_dump(content, - default_flow_style=False, - indent=2, sort_keys=False) data = data.replace('null', '') # save to file