diff --git a/config/scheme.py b/config/scheme.py index b3c7429..26b8203 100644 --- a/config/scheme.py +++ b/config/scheme.py @@ -52,7 +52,7 @@ class DataClass(Munch): raise Exception(f'Unknown key "{key}"') else: if isinstance(value, dict) and not isinstance(value, Munch): - value = DataClass.fromDict(value) + value = Munch.fromDict(value) results[key] = value if values: if validate: @@ -129,6 +129,19 @@ class ProfilesSection(DataClass): current: str default: Profile + @classmethod + def transform(cls, values: Mapping[str, Any], validate: bool = True): + results = {} + for k, v in values.items(): + if k == 'current': + results[k] = v + continue + results[k] = Profile.fromDict(v, validate=True) + return results + + def update(self, d, validate: bool = True): + Munch.update(self, self.transform(d, validate=validate)) + @munchclass() class Config(DataClass): @@ -146,7 +159,7 @@ class Config(DataClass): for name, _class in cls._type_hints.items(): if name not in values: if not allow_incomplete: - raise Exception(f'Config key {name} not found') + raise Exception(f'Config key "{name}" not in input dictionary') continue value = values.pop(name) if not isinstance(value, _class): diff --git a/config/test_config.py b/config/test_config.py index 0c3cac3..e34cb80 100644 --- a/config/test_config.py +++ b/config/test_config.py @@ -1,6 +1,7 @@ import pytest import os +import pickle import toml from tempfile import mktemp, gettempdir as get_system_tempdir @@ -168,6 +169,17 @@ def test_config_scheme_modified(): assert c.wrapper.type == 'none' +def test_configstate_profile_pickle(): + c = ConfigStateHolder() + assert c.file.wrapper + assert c.file.profiles + # add new profile to check it doesn't error out due to unknown keys + c.file.profiles['graphical'] = {'username': 'kupfer123', 'hostname': 'test123'} + p = pickle.dumps(c) + unpickled = pickle.loads(p) + assert c.file == unpickled.file + + def test_profile(): p = None p = Profile.fromDict(PROFILE_DEFAULTS)