72 lines
2.5 KiB
Python
72 lines
2.5 KiB
Python
from typing import Self, Any, Optional, Sequence
|
|
import datetime
|
|
from AetherAgentsServer.Utils.Common import Common
|
|
|
|
class Printer:
|
|
|
|
def print(self:Self,
|
|
_type:str,
|
|
message:str|Sequence[str],
|
|
inputs:Optional[dict[str, Any|None]|Sequence[Any|None]] = None,
|
|
i:int = 0
|
|
) -> None:
|
|
|
|
date:datetime.datetime = datetime.datetime.now()
|
|
own:dict[str, Any] = {
|
|
"raw_type" : _type,
|
|
"type": self.print_types.get(_type),
|
|
"message": self.i18n.get(message, inputs),
|
|
**Common.get_dictionary(inputs),
|
|
**Common.get_action_data(i + 1)
|
|
}
|
|
|
|
for key in ("year", "month", "day", "hour", "minute", "second"):
|
|
|
|
k:str = "i" if key == "minute" else key[0]
|
|
|
|
own[k] = own[key] = getattr(date, key)
|
|
own[k + k] = ("00" + str(own[key]))[-2:]
|
|
|
|
own["yyyy"] = ("0000" + str(own["year"]))[-4:]
|
|
|
|
for root_path in self.__root_projects_paths:
|
|
if own["file"].startswith(root_path):
|
|
own["file"] = own["file"][len(root_path):]
|
|
break
|
|
|
|
if any(own["type"].startswith(end_type) for end_type in self.__end_print_types) and "end" in own:
|
|
own["message"] += own["end"]
|
|
|
|
print(Common.string_variables(self.__print_format, own))
|
|
|
|
def exception(self:Self,
|
|
exception:Exception,
|
|
message:str|Sequence[str],
|
|
inputs:Optional[dict[str, Any|None]|Sequence[Any|None]] = None,
|
|
i:int = 0
|
|
) -> None:
|
|
|
|
lines:list[str] = extract_traceback(exception.__traceback__).format()
|
|
matches:REMatch = RE.EXCEPTION.match(lines[-1])
|
|
data:dict[str, Any|None] = {
|
|
**Common.get_dictionary(inputs),
|
|
"lines" : "",
|
|
"exception_message" : str(exception),
|
|
"method" : matches.group(3),
|
|
"line" : matches.group(2),
|
|
"file" : matches.group(1)
|
|
}
|
|
block:str
|
|
j:int
|
|
|
|
for root_path in self.__root_projects_paths:
|
|
if data["file"] and data["file"].startswith(root_path):
|
|
data["file"] = data["file"][len(root_path):]
|
|
|
|
for j, block in enumerate(trace_format_stack()[:-2] + lines):
|
|
if block:
|
|
data["lines"] += "\n " + str(j) + " - " + RE.NEW_LINE.split(block.strip())[0]
|
|
|
|
data["end"] = Common.string_variables(self.__exception_format, data)
|
|
|
|
message and self.print("exception", message, data, i + 2) |