106 lines
3.7 KiB
Python
106 lines
3.7 KiB
Python
#!/usr/bin/env python
|
|
# -*- coding: utf-8 -*-
|
|
|
|
from re import compile as re_compile
|
|
|
|
if "LibreTranslatePlus" not in globals():
|
|
class LibreTranslatePlus:pass
|
|
|
|
class Anonymous(LibreTranslatePlus.Abstracts.Base): # LibreTranslatePlus.Settings
|
|
|
|
re_marks_lines = re_compile(r'^[a-zA-Z0-9_]+$')
|
|
|
|
__default_settings = {
|
|
"nulls" : False,
|
|
"default_value" : None,
|
|
"overwrite" : False,
|
|
"autostart" : True,
|
|
"default_settings_files" : [
|
|
"/JSON/LibreTranslatePlus.py.settings.json",
|
|
"/JSON/LibreTranslatePlus.py.settings.secrets.json"
|
|
],
|
|
"print_format" : "[{type}] {yyyy}{mm}{dd} {hh}{ii}{ss} {line}-{file}({method}): {message}",
|
|
"show_exception" : True
|
|
}
|
|
|
|
def __init__(self, ltp, _input = None):
|
|
super().__init__(ltp)
|
|
|
|
self._print("info", "ltp_settings_building")
|
|
|
|
self.__started = False
|
|
self.__input = _input
|
|
self.__custom = {}
|
|
self.__show_exception = None
|
|
|
|
self.__set_default_variables()
|
|
|
|
self._print("ok", "ltp_settings_built")
|
|
|
|
def start(self, callback = None):
|
|
|
|
self._print("info", "ltp_settings_starting")
|
|
|
|
end = lambda status:callable(callback) and callback(status)
|
|
|
|
if self.__started:
|
|
self._print("warn", "ltp_settings_already_started")
|
|
end(False)
|
|
return False
|
|
self.__started = True
|
|
|
|
self.__set_default_variables()
|
|
|
|
for key in ("default_settings_files", "settings_files"):
|
|
self.set(self.get(key), True)
|
|
|
|
end(True)
|
|
|
|
self._print("ok", "ltp_settings_started")
|
|
|
|
return True
|
|
|
|
def __set_default_variables(self):
|
|
self.__show_exception = self.get(("settings_show_exception", "show_exception"))
|
|
|
|
def nulls(self, nulls):
|
|
return nulls if isinstance(nulls, bool) else self.get("nulls", None, False, False)
|
|
|
|
def default_value(self, default = None, nulls = None):
|
|
return default if default != None or self.nulls(nulls) else self.get("default_value", None, None, True)
|
|
|
|
def get(self, keys, inputs = None, default = None, nulls = None):
|
|
|
|
keys = tuple([key for key in (keys if isinstance(keys, (list, tuple)) else [keys]) if isinstance(key, str) and key])
|
|
|
|
if len(keys):
|
|
|
|
nulls = self.nulls(nulls)
|
|
|
|
for _input in tuple((
|
|
[subinput for subinput in inputs if isinstance(subinput, dict)] if isinstance(inputs, (list, tuple)) else
|
|
[inputs] if isinstance(inputs, dict) else
|
|
[]) + [subinput for subinput in [self.__input, self.__custom, LibreTranslatePlus.Settings.__default_settings] if isinstance(subinput, dict)]):
|
|
keys_done = []
|
|
for key in keys:
|
|
if key not in keys_done:
|
|
if key in _input and (nulls or _input[key] != None):
|
|
return _input[key]
|
|
keys_done.append(key)
|
|
return self.default_value(default, nulls)
|
|
|
|
def set(self, inputs, overwrite = None):
|
|
if isinstance(inputs, (list, tuple)):
|
|
for subinputs in inputs:
|
|
self.set(subinputs, overwrite)
|
|
elif isinstance(inputs, dict):
|
|
if not isinstance(overwrite, bool):
|
|
overwrite = self.get(("settings_overwrite", "overwrite"))
|
|
for key, value in inputs.items():
|
|
if key != "__comments__" and overwrite or key not in self.__custom:
|
|
self.__custom[key] = value
|
|
elif isinstance(inputs, str) and not LibreTranslatePlus.Settings.re_marks_lines.match(inputs):
|
|
self.set(self.ltp.load_json(inputs, self.__show_exception)[0], overwrite)
|
|
|
|
LibreTranslatePlus.Settings = Anonymous
|
|
del globals()["Anonymous"] |