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 /configuration.py | |
parent | f2b300d9e01547628cd409f9666d457b5ea045ee (diff) |
Reset now reload the full configuration
Diffstat (limited to 'configuration.py')
-rw-r--r-- | configuration.py | 30 |
1 files changed, 17 insertions, 13 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.
|