184 lines
6.6 KiB
Python
184 lines
6.6 KiB
Python
from typing import Optional
|
|
|
|
import omegaconf
|
|
class Config:
|
|
_config: Optional[omegaconf.DictConfig] = None
|
|
|
|
def __init__(self, config_path: str):
|
|
"""
|
|
Loads the configuration file and stores it for future access.
|
|
|
|
Args:
|
|
config_path (str): Path to the YAML configuration file.
|
|
"""
|
|
self._config = omegaconf.OmegaConf.load(config_path)
|
|
|
|
@property
|
|
def institution_name(self)->str:
|
|
if self._config is None:
|
|
raise RuntimeError("Configuration not loaded")
|
|
return self._config.institution_name
|
|
@institution_name.setter
|
|
def institution_name(self, value: str):
|
|
if self._config is None:
|
|
raise RuntimeError("Configuration not loaded")
|
|
self._config.institution_name = value
|
|
@property
|
|
def loan_duration(self)->int:
|
|
if self._config is None:
|
|
raise RuntimeError("Configuration not loaded")
|
|
return self._config.default_loan_duration
|
|
@loan_duration.setter
|
|
def loan_duration(self, value: int):
|
|
if self._config is None:
|
|
raise RuntimeError("Configuration not loaded")
|
|
self._config.default_loan_duration = value
|
|
@property
|
|
def delete_inactive_user_duration(self)->int:
|
|
if self._config is None:
|
|
raise RuntimeError("Configuration not loaded")
|
|
return self._config.inactive_user_deletion
|
|
@delete_inactive_user_duration.setter
|
|
def delete_inactive_user_duration(self, value: int):
|
|
if self._config is None:
|
|
raise RuntimeError("Configuration not loaded")
|
|
self._config.inactive_user_deletion = value
|
|
@property
|
|
def catalogue(self)->bool:
|
|
if self._config is None:
|
|
raise RuntimeError("Configuration not loaded")
|
|
return self._config.catalogue
|
|
@catalogue.setter
|
|
def catalogue(self, value: bool):
|
|
if self._config is None:
|
|
raise RuntimeError("Configuration not loaded")
|
|
self._config.catalogue = value
|
|
|
|
@property
|
|
def shortcuts(self)->omegaconf.DictConfig:
|
|
if self._config is None:
|
|
raise RuntimeError("Configuration not loaded")
|
|
return self._config.shortcuts
|
|
@shortcuts.setter
|
|
def shortcuts(self, value: omegaconf.DictConfig):
|
|
if self._config is None:
|
|
raise RuntimeError("Configuration not loaded")
|
|
self._config.shortcuts = value
|
|
|
|
@property
|
|
def advanced_refresh(self)->omegaconf.DictConfig:
|
|
if self._config is None:
|
|
raise RuntimeError("Configuration not loaded")
|
|
return self._config.advanced_refresh
|
|
|
|
@advanced_refresh.setter
|
|
def advanced_refresh(self, value: omegaconf.DictConfig):
|
|
if self._config is None:
|
|
raise RuntimeError("Configuration not loaded")
|
|
self._config.advanced_refresh = value
|
|
|
|
@property
|
|
def database(self)->omegaconf.DictConfig:
|
|
if self._config is None:
|
|
raise RuntimeError("Configuration not loaded")
|
|
return self._config.database
|
|
@database.setter
|
|
def database(self, value: omegaconf.DictConfig):
|
|
if self._config is None:
|
|
raise RuntimeError("Configuration not loaded")
|
|
self._config.database = value
|
|
@property
|
|
def report(self)->omegaconf.DictConfig:
|
|
if self._config is None:
|
|
raise RuntimeError("Configuration not loaded")
|
|
return self._config.report
|
|
@report.setter
|
|
def report(self, value: omegaconf.DictConfig):
|
|
if self._config is None:
|
|
raise RuntimeError("Configuration not loaded")
|
|
self._config.report = value
|
|
@property
|
|
def debug(self)->bool:
|
|
if self._config is None:
|
|
raise RuntimeError("Configuration not loaded")
|
|
return self._config.debug
|
|
@debug.setter
|
|
def debug(self, value: bool):
|
|
if self._config is None:
|
|
raise RuntimeError("Configuration not loaded")
|
|
self._config.debug = value
|
|
|
|
def save(self):
|
|
if self._config is None:
|
|
raise RuntimeError("Configuration not loaded")
|
|
omegaconf.OmegaConf.save(self._config, "config/settings.yaml")
|
|
|
|
def apply_options(self, options:list):
|
|
if self._config is None:
|
|
raise RuntimeError("Configuration not loaded")
|
|
for option in options:
|
|
if option in self._config:
|
|
self._config[option] = True
|
|
else:
|
|
raise KeyError(f"Option {option} not found in configuration")
|
|
|
|
@property
|
|
def documentation(self)->bool:
|
|
if self._config is None:
|
|
raise RuntimeError("Configuration not loaded")
|
|
return self._config.documentation
|
|
@documentation.setter
|
|
def documentation(self, value: bool):
|
|
if self._config is None:
|
|
raise RuntimeError("Configuration not loaded")
|
|
self._config.documentation = value
|
|
|
|
|
|
def to_Omegaconf(self):
|
|
return omegaconf.OmegaConf.create(self._config)
|
|
|
|
def updateValue(self, key:str, value):
|
|
if self._config is None:
|
|
raise RuntimeError("Configuration not loaded")
|
|
if "." in key:
|
|
keys = key.split(".")
|
|
if keys[0] in self._config:
|
|
self._config[keys[0]][keys[1]] = value
|
|
else:
|
|
raise KeyError(f"Option {keys[0]} not found in configuration")
|
|
else:
|
|
self._config[key] = value
|
|
|
|
def get_changes(self, other):
|
|
# compare self to other, return changes
|
|
changes = {}
|
|
for key in self._config:
|
|
if self._config[key] != other[key]:
|
|
changes[key] = self._config[key]
|
|
return changes
|
|
if __name__ == "__main__":
|
|
cfg = Config("config/settings.yaml")
|
|
other = {
|
|
"institution_name": "Test",
|
|
"default_loan_duration": 7,
|
|
"inactive_user_deletion": 365,
|
|
"database": {
|
|
"path": "./database",
|
|
"name": "library.db",
|
|
"backupLocation": "./backup",
|
|
"do_backup": True,
|
|
},
|
|
"report": {"generate_report": True, "path": "./report", "report_day": 0},
|
|
"shortcuts": [
|
|
{"name": "Rueckgabemodus", "default": "F5", "current": "F5"},
|
|
{"name": "Nutzer", "default": "F6", "current": "F6"},
|
|
{"name": "Hilfe", "default": "F1", "current": "F1"},
|
|
{"name": "Bericht_erstellen", "default": "F7", "current": "F7"},
|
|
{"name": "Ausleihhistorie", "default": "F8", "current": "F8"},
|
|
],
|
|
"advanced_refresh": False,
|
|
"catalogue": True,
|
|
"debug": True,
|
|
"documentation": True,
|
|
}
|
|
print(cfg.get_changes(other)) |