From 68dfe65da02bdd0d8a287196283bf0ff43cdda67 Mon Sep 17 00:00:00 2001 From: Sébastien Dailly Date: Fri, 18 Jul 2025 08:43:39 +0200 Subject: Reset now reload the full configuration --- configuration.py | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) (limited to 'configuration.py') diff --git a/configuration.py b/configuration.py index 78cce27..fd73fad 100644 --- a/configuration.py +++ b/configuration.py @@ -6,33 +6,37 @@ from os import path import json from zope import component from interfaces.message import Error + class Mapping(): """Represent the configuration. This class is provided as an utility and is used in the IDesktop interfaces. """ def __init__(self, configuration): - self.init_mapping = configuration["mapping"] + self.set(configuration) + + def set(self, configuration): + """ Associate the mapping defined in the dictionnary. + + Only the lines pointing to a valid file or configuration will be + loaded. + """ + init_mapping = configuration["mapping"] self.mapping = OrderedDict() - tmp_mapping = dict(self.init_mapping) + tmp_mapping = dict(init_mapping) for key in tmp_mapping.keys() : - json_file = self.init_mapping[key] + json_file = init_mapping[key] if not path.exists(json_file): component.handle(Error(f"The file '{json_file}' does not exists")) continue with open(json_file, "r") as file: json_data = file.read() - j = json.loads(json_data) - self.mapping[key] = j + try: + j = json.loads(json_data) + self.mapping[key] = j + except json.decoder.JSONDecodeError: + component.handle(Error(f"Json syntax error in '{json_file}'")) - def reset(self): - """ Remove all the keys added and not in the configuration file. - """ - # Create a copy of the dictonnary before updating the keys - tmp_mapping = dict(self.mapping) - for key in tmp_mapping.keys() : - if key not in self.init_mapping.keys(): - del self.mapping[key] def get(self, key, default): """ This function return the mapping associated with the given key. -- cgit v1.2.3