summaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
author简律纯 <i@jyunko.cn>2023-12-18 02:43:15 +0800
committer简律纯 <i@jyunko.cn>2023-12-18 02:43:15 +0800
commit573a6dbd224e31beb8aa02f9f8e1a0072da90a2f (patch)
tree744a8492095853a5cd763c780d9620a3f9bbb508
parent5ba9f3557d6359c8041c9928e9b2f567286a4f74 (diff)
downloadinfini-573a6dbd224e31beb8aa02f9f8e1a0072da90a2f.tar.gz
infini-573a6dbd224e31beb8aa02f9f8e1a0072da90a2f.zip
feat(handler): 实现泛型,实现 `self.stop()` `self.skip()` 方法
-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: