85 lines
2.8 KiB
Python
85 lines
2.8 KiB
Python
#!/usr/bin/env python3
|
|
# -*- coding: utf-8 -*-
|
|
|
|
from typing import Self, Any, Optional, Sequence
|
|
from Interfaces.Application.NucelarMonitorInterface import NucelarMonitorInterface
|
|
from Utils.Utils import Utils
|
|
|
|
class SettingsManager:
|
|
|
|
DEFAULT_SETTINGS:dict[str, Any|None] = {
|
|
"autostart" : True,
|
|
"print_format" : "[{type}] {yyyy}{mm}{dd} {hh}{ii}{ss} [{line}]{file}({method}): {message}",
|
|
"exception_format" : " '[{line}]{file}({method})'{lines}\n\n{exception_message}",
|
|
"print_types" : [
|
|
["unkn", "unknown"],
|
|
["info", "information"],
|
|
["warn", "warning"],
|
|
["erro", "error", "wrong", "failure", "fail", "no"],
|
|
["exce", "exception", "except"],
|
|
[" ok ", "ok", "success", "succeed", "yes"],
|
|
["test", "debug"]
|
|
],
|
|
"default_settings_files" : "/JSON/NucelarMonitor.settings.json",
|
|
}
|
|
|
|
def __init__(self:Self,
|
|
nucelar_monitor:NucelarMonitorInterface,
|
|
inputs:Optional[dict[str, Any|None]|Sequence[Any|None]] = None
|
|
) -> None:
|
|
self.nucelar_monitor:NucelarMonitorInterface = nucelar_monitor
|
|
|
|
key:str
|
|
|
|
self.__inputs:dict[str, Any|None] = Utils.get_dictionary(inputs)
|
|
self.__secrets:dict[str, Any|None] = {}
|
|
self.__settings:dict[str, Any|None] = {}
|
|
|
|
for key in (
|
|
"default_settings_files", "settings_files",
|
|
"default_settings", "settings"
|
|
):
|
|
self.add(self.get(key, None, []), True)
|
|
|
|
for key in (
|
|
"default_secrets_files", "secrets_files",
|
|
"default_secrets", "secrets"
|
|
):
|
|
self.add_secrets(self.get(key, None, []), True)
|
|
|
|
def get(self:Self,
|
|
keys:str|Sequence[str],
|
|
inputs:Optional[dict[str, Any|None]|Sequence[Any|None]] = None,
|
|
default:Any = None
|
|
) -> dict[str, 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:
|
|
|
|
subinputs:dict[str, Any|None]
|
|
|
|
print(inputs)
|
|
|
|
for subinputs in self.nucelar_monitor.files.load_json(inputs):
|
|
|
|
key:str
|
|
value:Any|None
|
|
|
|
for key, value in subinputs.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:
|
|
|
|
subinputs:dict[str, Any|None]
|
|
|
|
for subinputs in self.nucelar_monitor.files.load_json(inputs):
|
|
|
|
key:str
|
|
value:Any|None
|
|
|
|
for key, value in subinputs.items():
|
|
if overwrite or key not in self.__secrets:
|
|
self.__secrets[key] = value |