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 +++++++++++++++++------------- macropad.pyw | 17 +++++++++++------ 2 files changed, 28 insertions(+), 19 deletions(-) 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. diff --git a/macropad.pyw b/macropad.pyw index a175931..98e97e4 100755 --- a/macropad.pyw +++ b/macropad.pyw @@ -28,9 +28,6 @@ config = configparser.ConfigParser(delimiters="=") config.read(config_file) -# -# Guess the platform and the load the corresponding event listener -# # # How to connect to the peripherical @@ -97,7 +94,10 @@ class Icon(): self.show_hide.set() def reset(self): - mapping.reset() + """ Read the configuration file again, and update the mapping list. + """ + config.read(config_file) + mapping.set(config) class Application(): """ The main application. @@ -190,7 +190,11 @@ class Application(): self.visible = not self.visible @component.adapter(IMessage) - def log(self, message : str): + def log(self, message : IMessage): + """ Log a message. + The message is printed in the console, and displayed in the + application window. + """ print(message.content) try: self.text.insert("1.0", "\n") @@ -220,6 +224,7 @@ if __name__ == '__main__': # Start the main application, Initializing the message listener before # listening desktop events app = Application() + # The application is started, ready to display any error message. mapping = Mapping(config) component.provideUtility(mapping, interfaces.configuration.IConfiguration) app.connect_desktop() @@ -228,7 +233,7 @@ if __name__ == '__main__': app.exec() try: app.window.mainloop() - except BaseException as e: + except BaseException: app.running = False app.window.destroy() app.icon.quit() -- cgit v1.2.3