67 lines
2.3 KiB
Python
67 lines
2.3 KiB
Python
#!/usr/bin/env python3
|
|
# -*- coding: utf-8 -*-
|
|
|
|
from typing import Any, Optional, Self
|
|
from Interfaces.Application.CXCVInterface import CXCVInterface
|
|
from Utils.Utils import Utils
|
|
|
|
class SettingsManager:
|
|
|
|
__DEFAULT_SETTINGS:dict[str, Any|None] = {
|
|
"sql_builder_file" : "/SQLite/CXCV.lite.sql",
|
|
"database_file_path" : "/Data/CXCV.sqlite.db",
|
|
"default_settings_files" : ["/JSON/CXCV.settings.json"],
|
|
"default_secrets_files" : ["/JSON/CXCV.secrets.json"]
|
|
}
|
|
|
|
def __init__(self:Self,
|
|
cxcv:CXCVInterface,
|
|
inputs:Optional[dict[str, Any|None]|list[Any|None]|tuple[Any|None, ...]] = None
|
|
) -> None:
|
|
|
|
key:str
|
|
|
|
self.cxcv:CXCVInterface = cxcv
|
|
self.__inputs:dict[str, Any|None] = Utils.get_dictionary(inputs)
|
|
self.__settings:dict[str, Any|None] = {}
|
|
self.__secrets:dict[str, Any|None] = {}
|
|
|
|
for key in ("default_settings_files", "default_settings", "settings_files", "settings"):
|
|
self.add(self.get(key), True)
|
|
for key in ("default_secrets_files", "default_secrets", "secrets_files", "secrets"):
|
|
self.add_secrets(self.get(key), True)
|
|
|
|
def get(self:Self,
|
|
keys:str|list[str]|tuple[str, ...],
|
|
inputs:Optional[dict[str, Any|None]|list[Any|None]|tuple[Any|None, ...]] = None,
|
|
default:Any|None = None
|
|
) -> Any|None:
|
|
return Utils.get_value(keys, (
|
|
inputs, self.__inputs, self.__secrets, self.__settings, self.__DEFAULT_SETTINGS
|
|
), default)
|
|
|
|
def add(self:Self, inputs:Any|None, overwrite:bool = False) -> None:
|
|
|
|
dictionary:dict[str, Any|None]
|
|
|
|
for dictionary in self.cxcv.files.load_json(inputs):
|
|
|
|
key:str
|
|
value:Any|None
|
|
|
|
for key, value in dictionary.items():
|
|
if overwrite or key not in self.__settings:
|
|
self.__settings[key] = value
|
|
|
|
def add_secrets(self:Self, inputs:Any|None, overwrite:bool = False) -> None:
|
|
|
|
dictionary:dict[str, Any|None]
|
|
|
|
for dictionary in self.cxcv.files.load_json(inputs):
|
|
|
|
key:str
|
|
value:Any|None
|
|
|
|
for key, value in dictionary.items():
|
|
if overwrite or key not in self.__secrets:
|
|
self.__secrets[key] = value |