Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion arclet/entari/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,4 @@
WS = WebsocketsInfo
WH = WebhookInfo

__version__ = "0.18.5"
__version__ = "0.18.6"
29 changes: 17 additions & 12 deletions arclet/entari/event/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@

from ..const import (
ITEM_ACCOUNT,
ITEM_GUILD,
ITEM_MESSAGE_CONTENT,
ITEM_MESSAGE_ORIGIN,
ITEM_MESSAGE_REPLY,
Expand Down Expand Up @@ -74,8 +73,6 @@ def __init__(self, key: str | None = None, cls: Callable[..., T] | None = None,

def __set_name__(self, owner: type["SatoriEvent"], name: str):
self.key = self.key or name
if name not in ("id", "timestamp"):
owner._attrs.add(name)

def __get__(self, instance: "SatoriEvent", owner: type["SatoriEvent"]):
if self.internal and instance._origin._data:
Expand Down Expand Up @@ -114,9 +111,24 @@ def attr(*args, internal: bool = False) -> Any:
return Attr(key, cls, internal)


SATORI_NAMES = {
"login",
"argv",
"button",
"channel",
"guild",
"member",
"message",
"operator",
"role",
"user",
"referrer",
"emoji",
}


class SatoriEvent:
type: ClassVar[str]
_attrs: ClassVar[set[str]] = set()
_origin: OriginEvent
account: Account

Expand All @@ -135,9 +147,6 @@ class SatoriEvent:
referrer: dict | None = attr()
emoji: EmojiObject | None = attr()

def __init_subclass__(cls, **kwargs):
cls._attrs = set()

def __init__(self, account: Account, origin: OriginEvent):
self.account = account
self._origin = origin
Expand All @@ -146,7 +155,7 @@ async def gather(self, context: Contexts):
context[ITEM_ACCOUNT] = self.account
context[ITEM_ORIGIN_EVENT] = self._origin

for name in self.__class__._attrs:
for name in SATORI_NAMES:
value = getattr(self, name)
if value is not None:
context[ITEM_MESSAGE_ORIGIN if name == "message" else f"${name}"] = value
Expand Down Expand Up @@ -188,10 +197,6 @@ class FriendRequestEvent(FriendEvent):
class GuildEvent(NoticeEvent):
guild: Guild = attr()

async def gather(self, context: Contexts):
await super().gather(context)
context[ITEM_GUILD] = self.guild


class GuildAddedEvent(GuildEvent):
type = EventType.GUILD_ADDED
Expand Down
49 changes: 39 additions & 10 deletions arclet/entari/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import logging
import sys
import traceback
from enum import Enum
from pathlib import Path
from types import TracebackType
from typing import TYPE_CHECKING, cast
Expand Down Expand Up @@ -183,15 +184,32 @@ def _hidden_upsteam(record: Record):
logger.configure(patcher=_hidden_upsteam)


def loguru_exc_callback(cls: type[BaseException], val: BaseException, tb: TracebackType | None, *_, **__):
"""loguru 异常回调
class _Sentinel(Enum):
SENTINEL = "SENTINEL"


def _parse_value_tb(exc, value, tb):
if (value is _Sentinel.SENTINEL) != (tb is _Sentinel.SENTINEL):
raise ValueError("Both or neither of value and tb must be given")
if value is tb is _Sentinel.SENTINEL:
if exc is not None:
if isinstance(exc, BaseException):
return exc, exc.__traceback__
raise TypeError(f"Exception expected for value, {type(exc).__name__} found")
else:
return None, None
return value, tb


_print_exception = traceback.print_exception

Args:
cls (Type[Exception]): 异常类
val (Exception): 异常的实际值
tb (TracebackType): 回溯消息
"""
log.error.opt(exception=(cls, val, tb)).error("Exception:")

def loguru_print_exception(exc: type[BaseException] | BaseException | None, /, value: BaseException | None | _Sentinel = _Sentinel.SENTINEL, tb: TracebackType | None | _Sentinel = _Sentinel.SENTINEL, *args, colorize: bool = True, **kwargs):
value, tb = _parse_value_tb(exc, value, tb)
if colorize:
log.error.opt(exception=(type(value), value, tb), colors=colorize).error("Exception:")
else:
_print_exception(exc, value, tb, *args, **kwargs)


def loguru_print_trace(te: Trace):
Expand All @@ -216,9 +234,20 @@ class FakeTraceback:
log.error.opt(exception=(te.exc_type, te.exc_value, cast(TracebackType, FakeTraceback()))).error("Exception:")


traceback.print_exception = loguru_exc_callback
def loguru_excepthook(cls: type[BaseException], val: BaseException, tb: TracebackType | None):
"""loguru 异常回调

Args:
cls (Type[Exception]): 异常类
val (Exception): 异常的实际值
tb (TracebackType): 回溯消息
"""
log.error.opt(exception=(cls, val, tb)).error("Exception:")


traceback.print_exception = loguru_print_exception
ExceptionHandler.print_trace = loguru_print_trace
sys.excepthook = loguru_exc_callback
sys.excepthook = loguru_excepthook


def apply_log_save(
Expand Down