From ccab44af56df9c54adb7aa7f0bf0a59803f43bad Mon Sep 17 00:00:00 2001 From: Artem30801 Date: Sun, 5 Jan 2020 17:16:39 +0300 Subject: [PATCH] Perserving dict order in messages and configs --- config.py | 12 +++++++----- messaging_lib.py | 2 +- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/config.py b/config.py index ffec817..9f773c0 100644 --- a/config.py +++ b/config.py @@ -1,4 +1,5 @@ import os +import collections from configobj import ConfigObj, Section, flatten_errors from validate import Validator @@ -60,12 +61,13 @@ class ConfigManager: self.config = config self.validated = False - def validate_config(self, config, copy_defaults=False): + def validate_config(self, config=None, copy_defaults=False): + config = config or self.config vdt = Validator() test = config.validate(vdt, copy=copy_defaults, preserve_errors=True) if test != True: # Important syntax, do no change - raise ValidationError('Some values are wrong: {}'.format(test), config, test) + raise ValidationError('Some config values are wrong: {}'.format(test), config, test) self.config = config self.validated = True @@ -75,7 +77,7 @@ class ConfigManager: if not isinstance(item, Section): return item - data = {} + data = collections.OrderedDict() default_values = item.default_values defaults = item.defaults comments = item.comments @@ -191,11 +193,11 @@ class ConfigManager: @classmethod def _extract_values(cls, d): - result = {} + result = collections.OrderedDict() for key, val in d.items(): if not isinstance(val, dict): # Pure dict option result[key] = val - elif val.get('__option__', False): # Full-dict option with params + elif val.get('__option__', False): # Full-dict option with params if not val.get('unchanged', False): result[key] = val.get('value') else: # Section diff --git a/messaging_lib.py b/messaging_lib.py index 650dcff..43bc0e2 100644 --- a/messaging_lib.py +++ b/messaging_lib.py @@ -71,7 +71,7 @@ class MessageManager: @staticmethod def _json_decode(json_bytes, encoding="utf-8"): with io.TextIOWrapper(io.BytesIO(json_bytes), encoding=encoding, newline="") as tiow: - obj = json.load(tiow) + obj = json.load(tiow, object_pairs_hook=collections.OrderedDict) return obj @classmethod