diff --git a/arclet/entari/__init__.py b/arclet/entari/__init__.py index 5ddf75c..7e20c5b 100644 --- a/arclet/entari/__init__.py +++ b/arclet/entari/__init__.py @@ -91,4 +91,4 @@ WS = WebsocketsInfo WH = WebhookInfo -__version__ = "0.18.5" +__version__ = "0.18.6" diff --git a/arclet/entari/event/base.py b/arclet/entari/event/base.py index 60261eb..8ed1fe0 100644 --- a/arclet/entari/event/base.py +++ b/arclet/entari/event/base.py @@ -24,7 +24,6 @@ from ..const import ( ITEM_ACCOUNT, - ITEM_GUILD, ITEM_MESSAGE_CONTENT, ITEM_MESSAGE_ORIGIN, ITEM_MESSAGE_REPLY, @@ -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: @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/arclet/entari/logger.py b/arclet/entari/logger.py index 7d38460..0ace305 100644 --- a/arclet/entari/logger.py +++ b/arclet/entari/logger.py @@ -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 @@ -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): @@ -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(