79 lines
2.4 KiB
Python
79 lines
2.4 KiB
Python
#!/usr/bin/env python3
|
|
# -*- coding: utf-8 -*-
|
|
|
|
from typing import Any, Optional, Self, Sequence
|
|
from Interfaces.Application.AnPInterface import AnPInterface
|
|
from Utils.Common import Common
|
|
|
|
class SettingsManager:
|
|
|
|
DEFAULT_SETTINGS:dict[str, Any|None] = {
|
|
"default_settings_files" : "/JSON/AnP.settings.json",
|
|
}
|
|
|
|
def __init__(self:Self,
|
|
anp:AnPInterface,
|
|
inputs:Optional[dict[str, Any|None]|Sequence[Any|None]] = None
|
|
) -> None:
|
|
|
|
self.anp:AnPInterface = anp
|
|
self.__inputs:dict[str, Any|None] = Common.get_dictionary(inputs)
|
|
self.__settings:dict[str, Any|None] = {}
|
|
self.__secrets:dict[str, Any|None] = {}
|
|
|
|
self.update()
|
|
|
|
def update(self:Self) -> None:
|
|
|
|
key:str
|
|
|
|
for key in ("default_settings_files", "settings_files", "default_settings", "settings"):
|
|
self.add(self.get(key), True)
|
|
for key in ("default_secrets_files", "secrets_files", "default_secrets", "secrets"):
|
|
self.add_secret(self.get(key), True)
|
|
|
|
def reset(self:Self) -> None:
|
|
|
|
self.__settings = {}
|
|
self.__secrets = {}
|
|
|
|
self.update()
|
|
|
|
def get(self:Self,
|
|
keys:str|Sequence[str],
|
|
inputs:Optional[dict[str, Any|None]|Sequence[Any|None]] = None,
|
|
default:Optional[Any] = None
|
|
) -> Any|None:
|
|
return Common.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:
|
|
|
|
subinputs:dict[str, Any|None]
|
|
|
|
for subinputs in Common.load_json(inputs, True):
|
|
|
|
key:str
|
|
value:Any|None
|
|
|
|
for key, value in subinputs.items():
|
|
if Common.is_mark_key(key) and value is None:
|
|
continue
|
|
if overwrite or key not in self.__settings:
|
|
self.__settings[key] = value
|
|
|
|
def add_secret(self:Self, inputs:Any|None, overwrite:bool = False) -> None:
|
|
|
|
subinputs:dict[str, Any|None]
|
|
|
|
for subinputs in Common.load_json(inputs, True):
|
|
|
|
key:str
|
|
value:Any|None
|
|
|
|
for key, value in subinputs.items():
|
|
if Common.is_mark_key(key) and value is None:
|
|
continue
|
|
if overwrite or key not in self.__secrets:
|
|
self.__secrets[key] = value |