From aee5a489679504a58a15ef4ccbdc88af7bdc6734 Mon Sep 17 00:00:00 2001 From: Matthias Blankertz Date: Sun, 14 Dec 2025 12:26:27 +0100 Subject: [PATCH] fix: config: Merge defaults into config at load time Merge defaults into config at load time to ensure that all config options show up in the configuration dialog, even if they were added after the local configuration was last changed. Also use the merge method to merge the local config with the new config in set_config, ensuring the config contains all keys even if the submitted config leaves some out. Signed-off-by: Matthias Blankertz --- software/src/utils/config.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/software/src/utils/config.py b/software/src/utils/config.py index e2677f7..0ab4ad2 100644 --- a/software/src/utils/config.py +++ b/software/src/utils/config.py @@ -30,6 +30,7 @@ class Configuration: try: with open(self.config_path, 'r') as conf_file: self.config = json.load(conf_file) + self._merge_configs(self.DEFAULT_CONFIG, self.config) except OSError as ex: if ex.errno == ENOENT: self.config = Configuration.DEFAULT_CONFIG @@ -51,6 +52,16 @@ class Configuration: raise os.sync() + def _merge_configs(self, default, config): + for k in default.keys(): + if k not in config: + if isinstance(default[k], dict): + config[k] = default[k].copy() + else: + config[k] = default[k] + elif isinstance(default[k], dict): + self._merge_configs(default[k], config[k]) + def _save(self): with open(self.config_path + '.new', 'w') as conf_file: json.dump(self.config, conf_file) @@ -59,7 +70,7 @@ class Configuration: os.sync() def _get(self, key): - return self.config.get(key, self.DEFAULT_CONFIG[key]) + return self.config[key] def get_led_count(self) -> int: return self._get('LED_COUNT') @@ -93,5 +104,6 @@ class Configuration: self._validate(self.DEFAULT_CONFIG, config) if 'TAGMODE' in config and config['TAGMODE'] not in ['tagremains', 'tagstartstop']: raise ValueError("Invalid TAGMODE: Must be 'tagremains' or 'tagstartstop'") + self._merge_configs(self.config, config) self.config = config self._save()