aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
-rw-r--r--src/infini/handler.py31
1 files changed, 23 insertions, 8 deletions
diff --git a/src/infini/handler.py b/src/infini/handler.py
index dbec3eaf..da4f84b8 100644
--- a/src/infini/handler.py
+++ b/src/infini/handler.py
@@ -5,10 +5,13 @@
此外,每个规则包业务类还可以定义一个名为 priority 的类属性,用于指定该业务类的优先级。优先级越高,该业务类处理事件的顺序越靠前。
"""
-from abc import ABCMeta, abstractmethod
+from abc import ABC, ABCMeta, abstractmethod
from enum import Enum
-from typing import ClassVar, Optional
-from .event import MatcherEvent, InfiniEvent
+from typing import ClassVar, Generic, NoReturn, Optional, final
+
+from infini.exceptions import SkipException, StopException
+from infini.typing import StateT
+from infini.event import MatcherEvent, InfiniEvent
__all__ = ["Handler", "HandlerLoadType"]
@@ -22,18 +25,30 @@ class HandlerLoadType(Enum):
CLASS = "class"
-class Handler:
+class Handler(ABC, Generic[StateT]):
"""规则包业务基类"""
- name: str
priority: ClassVar[int] = 0
block: ClassVar[bool] = False
-
+
def __init_state__(self) -> Optional[StateT]:
"""初始化规则包状态。"""
- def __init__(self) -> None:
- pass
+ @final
+ @property
+ def name(self) -> str:
+ """规则包名称。"""
+ return self.__class__.__name__
+
+ @final
+ def stop(self) -> NoReturn:
+ """停止当前事件传播。"""
+ raise StopException
+
+ @final
+ def skip(self) -> NoReturn:
+ """跳过自身继续当前事件传播。"""
+ raise SkipException
@abstractmethod
def process(self, event: MatcherEvent) -> InfiniEvent: