87 lines
3.5 KiB
Python
87 lines
3.5 KiB
Python
#!/usr/bin/env python3
|
|
|
|
from typing import Self, Any, Optional
|
|
from Managers.SettingsManager import SettingsManager
|
|
from Managers.I18NManager import I18NManager
|
|
from Drivers.HTTPSocketDriver import HTTPSocketDriver
|
|
from Drivers.FilesDriver import FilesDriver
|
|
from traceback import format_stack as trace_format_stack
|
|
from traceback import extract_tb as extract_traceback
|
|
from Utils.Patterns import RE
|
|
from Utils.Utils import Utils
|
|
from datetime import datetime
|
|
from re import Match as REMatch
|
|
|
|
class OpoTests:
|
|
|
|
def __init__(self:Self, inputs:Optional[dict[str, Any|None]|list[Any|None]|tuple[Any|None]] = None) -> None:
|
|
|
|
self.__print_format:str = "[{type}] {yyyy}{mm}{dd} {hh}{ii}{ss} [{line}]{file}({method}): {message}"
|
|
self.__exception_format:str = " '{file}({method})[{line}]'{lines}\n\n{exception_message}"
|
|
|
|
self.settings:SettingsManager = SettingsManager(self, inputs)
|
|
|
|
self.__print_format = self.settings.get("print_format", None, self.__print_format)
|
|
self.__exception_format = self.settings.get("exception_format", None, self.__exception_format)
|
|
|
|
self.i18n:I18NManager = I18NManager(self)
|
|
self.files:FilesDriver = FilesDriver(self)
|
|
self.http_server:HTTPSocketDriver = HTTPSocketDriver(self)
|
|
|
|
self.http_server.start()
|
|
|
|
def print(self:Self, _type:str, string:str, inputs:Optional[dict[str, Any|None]|list[Any|None]|tuple[Any|None]] = None, default_message:str = None, i:int = 0) -> None:
|
|
|
|
date:datetime = datetime.now()
|
|
own:dict[str, Any|None] = {
|
|
"raw_type" : _type,
|
|
"type" : _type.upper()[:4],
|
|
"i18n" : Utils.get_strings(string)[0],
|
|
"message" : self.i18n.get(string, inputs, default_message),
|
|
**Utils.get_action_data(i + 1),
|
|
**Utils.get_dictionary(inputs)
|
|
}
|
|
|
|
while len(own["type"]) < 4:
|
|
own["type"] = " " + own["type"] if len(own["type"]) % 2 else own["type"] + " "
|
|
|
|
for key in ("year", "month", "day", "hour", "minute", "second"):
|
|
|
|
k:str = key[0] if key != "minute" else "i"
|
|
|
|
own[k] = own[key] = getattr(date, key)
|
|
own[k + k] = ("00" + str(own[key] % 100))[-2:]
|
|
|
|
own["yyyy"] = ("0000" + str(own["year"]))[-4:]
|
|
|
|
print(Utils.string_variables(self.__print_format, own))
|
|
|
|
def exception(self,
|
|
exception:Exception,
|
|
message:Optional[str|list|tuple] = None,
|
|
variables:Optional[dict[str, Any]|list|tuple] = None,
|
|
i:Optional[int] = 1
|
|
) -> None:
|
|
|
|
lines:list[str]|None = extract_traceback(exception.__traceback__).format()
|
|
line_matches:REMatch[str]|None = RE.EXCEPTION.match(lines[-1])
|
|
key:str
|
|
value:Any|None
|
|
block:str|None
|
|
data:dict[str, Any|None] = {
|
|
**{key : value for subset in (variables if isinstance(variables, (list, tuple)) else (variables,)) for key, value in (subset if isinstance(subset, dict) else {}).items()},
|
|
**Utils.get_action_data(1),
|
|
"lines" : "",
|
|
"exception_message" : str(exception),
|
|
"method" : line_matches.group(3),
|
|
"line" : line_matches.group(2),
|
|
"file" : line_matches.group(1)
|
|
}
|
|
|
|
for block in trace_format_stack()[:-2] + lines:
|
|
if block:
|
|
data["lines"] += "\n " + RE.BREAK_LINES.split(block.strip())[0]
|
|
|
|
data["end"] = Utils.string_variables(self.__exception_format, data)
|
|
|
|
message and self.print("exception", message, data, i + 1) |