Recursively merge default and user config

This commit is contained in:
miruka 2019-07-24 17:20:21 -04:00
parent efa4ad0ae0
commit cf5d76dd10
2 changed files with 17 additions and 2 deletions

View File

@ -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"],
},
}

View File

@ -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]