Remove json configuration and replace it by yaml configuration files.

This commit is contained in:
adator
2025-10-26 21:00:50 +01:00
parent b7b61081be
commit ffb30f12ec
7 changed files with 76 additions and 82 deletions

View File

@@ -1,3 +1,5 @@
import sys
import yaml
from json import load
from sys import exit
from os import sep
@@ -13,49 +15,43 @@ class Configuration:
self.Loader = loader
self.Logs = loader.Logs
self._config_model: MConfig = self.__load_service_configuration()
self.configuration_model = self.__load_service_configuration()
loader.ServiceLogging.set_file_handler_level(self._config_model.DEBUG_LEVEL)
loader.ServiceLogging.set_stdout_handler_level(self._config_model.DEBUG_LEVEL)
loader.ServiceLogging.update_handler_format(self._config_model.DEBUG_HARD)
return None
def get_config_model(self) -> MConfig:
@property
def configuration_model(self) -> MConfig:
return self._config_model
@configuration_model.setter
def configuration_model(self, conf_model: MConfig):
self._config_model = conf_model
def __load_json_service_configuration(self) -> Optional[dict[str, Any]]:
def __load_config_file(self) -> Optional[dict[str, Any]]:
try:
conf_filename = f'config{sep}configuration.json'
with open(conf_filename, 'r') as configuration_data:
configuration: dict[str, Union[str, int, list, dict]] = load(configuration_data)
return configuration
conf_filename = f'config{sep}configuration.yaml'
with open(conf_filename, 'r') as conf:
configuration: dict[str, dict[str, Any]] = yaml.safe_load(conf)
return configuration.get('configuration', None)
except FileNotFoundError as fe:
self.Logs.error(f'FileNotFound: {fe}')
self.Logs.error('Configuration file not found please create config/configuration.json')
exit(0)
except KeyError as ke:
self.Logs.error(f'Key Error: {ke}')
self.Logs.error('The key must be defined in core/configuration.json')
self.Logs.error('Configuration file not found please create config/configuration.yaml')
exit("Configuration file not found please create config/configuration.yaml")
def __load_service_configuration(self) -> MConfig:
try:
import_config = self.__load_json_service_configuration()
import_config = self.__load_config_file()
if import_config is None:
self.Logs.error("Error While importing configuration file!", exc_info=True)
raise Exception("Error While importing yaml configuration")
model_keys = MConfig().to_dict()
model_key_list: list = []
json_config_key_list: list = []
for key in model_keys:
model_key_list.append(key)
for key in import_config:
json_config_key_list.append(key)
for json_conf in json_config_key_list:
if not json_conf in model_key_list:
import_config.pop(json_conf, None)
self.Logs.warning(f"[!] The key {json_conf} is not expected, it has been removed from the system ! please remove it from configuration.json file [!]")
list_key_to_remove: list[str] = [key_to_del for key_to_del in import_config if key_to_del not in MConfig().get_attributes()]
for key_to_remove in list_key_to_remove:
import_config.pop(key_to_remove, None)
self.Logs.warning(f"[!] The key {key_to_remove} is not expected, it has been removed from the system ! please remove it from configuration.json file [!]")
self.Logs.debug(f"[LOADING CONFIGURATION]: Loading configuration with {len(import_config)} parameters!")
return MConfig(**import_config)