aboutsummaryrefslogtreecommitdiff
path: root/configuration.py
diff options
context:
space:
mode:
authorSébastien Dailly <sebastien@dailly.me>2025-05-04 17:22:50 +0200
committerSébastien Dailly <sebastien@chimrod.com>2025-05-05 17:36:00 +0200
commitac4fa300658b962c43768bc0867a1f46fdb23272 (patch)
treee9c30dd3a6538ef3879d7da3bee44c458d173ceb /configuration.py
parentc15dc5a0b4f061de73b5d97fa09b311bdb976743 (diff)
Updated the main code
Diffstat (limited to 'configuration.py')
-rw-r--r--configuration.py47
1 files changed, 47 insertions, 0 deletions
diff --git a/configuration.py b/configuration.py
new file mode 100644
index 0000000..fceef14
--- /dev/null
+++ b/configuration.py
@@ -0,0 +1,47 @@
+from collections import OrderedDict
+from os import path
+import json
+class Mapping():
+
+ def __init__(self, configuration):
+ self.init_mapping = configuration["mapping"]
+ self.mapping = OrderedDict()
+ tmp_mapping = dict(self.init_mapping)
+ for key in tmp_mapping.keys() :
+ json_file = self.init_mapping[key]
+ if not path.exists(json_file):
+ print("The file '%s' does not exists" % json_file)
+ continue
+ with open(json_file, "r") as file:
+ json_data = file.read()
+ j = json.loads(json_data)
+ self.mapping[key] = j
+
+ 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):
+ return self.mapping.get(key, default)
+
+ def __getitem__(self, key):
+ return self.mapping.get(key, None)
+
+ def __setitem__(self, key, value):
+ self.mapping[key] = value
+
+ def items(self):
+ """ Implement the keys items from the dictionnary
+ """
+ return self.mapping.items()
+
+ def keys(self):
+ """ Implement the keys method from the dictionnary
+ """
+ return self.mapping.keys()
+