From cf5d76dd108c854ac82f91a27d1e6f1f62c98fba Mon Sep 17 00:00:00 2001 From: miruka Date: Wed, 24 Jul 2019 17:20:21 -0400 Subject: [PATCH] Recursively merge default and user config --- src/python/config_files.py | 6 +++++- src/python/utils.py | 13 ++++++++++++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/python/config_files.py b/src/python/config_files.py index b654623a..7b489d09 100644 --- a/src/python/config_files.py +++ b/src/python/config_files.py @@ -11,6 +11,7 @@ from dataclasses import dataclass, field from .backend import Backend from .theme_parser import convert_to_qml +from .utils import dict_update_recursive JsonData = Dict[str, Any] @@ -59,7 +60,9 @@ class JSONConfigFile(ConfigFile): except json.JSONDecodeError: data = {} - return {**await self.default_data(), **data} + all_data = await self.default_data() + dict_update_recursive(all_data, data) + return all_data async def write(self, data: JsonData) -> None: @@ -108,6 +111,7 @@ class UISettings(JSONConfigFile): "scrollUp": ["Alt+Up", "Alt+K"], "scrollDown": ["Alt+Down", "Alt+J"], "startDebugger": ["Alt+Shift+D"], + "reloadConfig": ["Alt+R"], }, } diff --git a/src/python/utils.py b/src/python/utils.py index 373a6b1a..f1d0d6b2 100644 --- a/src/python/utils.py +++ b/src/python/utils.py @@ -1,8 +1,9 @@ # Copyright 2019 miruka # This file is part of harmonyqml, licensed under LGPLv3. -from enum import Enum +import collections from enum import auto as autostr +from enum import Enum auto = autostr # pylint: disable=invalid-name @@ -11,3 +12,13 @@ class AutoStrEnum(Enum): @staticmethod def _generate_next_value_(name, *_): return name + + +def dict_update_recursive(dict1, dict2): + # https://gist.github.com/angstwad/bf22d1822c38a92ec0a9 + for k in dict2: + if (k in dict1 and isinstance(dict1[k], dict) and + isinstance(dict2[k], collections.Mapping)): + dict_update_recursive(dict1[k], dict2[k]) + else: + dict1[k] = dict2[k]