diff options
author | Sébastien Dailly <sebastien@dailly.me> | 2025-07-18 08:43:39 +0200 |
---|---|---|
committer | Sébastien Dailly <sebastien@dailly.me> | 2025-07-18 08:43:39 +0200 |
commit | 68dfe65da02bdd0d8a287196283bf0ff43cdda67 (patch) | |
tree | 696a38e4f1412f7384157b3c36508f3402e757a1 | |
parent | f2b300d9e01547628cd409f9666d457b5ea045ee (diff) |
Reset now reload the full configuration
-rw-r--r-- | configuration.py | 30 | ||||
-rwxr-xr-x | 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() |