""" The configuration from the user. """ from collections import OrderedDict 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.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(init_mapping) for key in tmp_mapping.keys() : 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() 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 get(self, key, default): """ This function return the mapping associated with the given key. """ 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()