diff options
| author | 2023-12-18 20:34:07 +0800 | |
|---|---|---|
| committer | 2023-12-18 20:34:07 +0800 | |
| commit | eb4f88dc6d05696fdaa1c7af3b514800dcd83f97 (patch) | |
| tree | 97daacbf229e7268bdef1a8f72f28c752b98572d | |
| parent | aee5f2f8c28c18c04b6f650b97adda44f4a5c05c (diff) | |
| parent | 9ac667f1b923dd55137e346ab8281c6bae0f9713 (diff) | |
| download | infini-dev.tar.gz infini-dev.zip | |
Merge pull request #48 from fu050409/masterdev
🐛 修复一些已知问题
| -rw-r--r-- | src/infini/__main__.py | 4 | ||||
| -rw-r--r-- | src/infini/const/templates.py | 2 | ||||
| -rw-r--r-- | src/infini/exceptions.py | 22 | ||||
| -rw-r--r-- | src/infini/handler.py | 29 | ||||
| -rw-r--r-- | src/infini/register.py | 11 | ||||
| -rw-r--r-- | src/infini/typing.py | 2 | ||||
| -rw-r--r-- | src/infini/utils/cli.py | 2 |
7 files changed, 21 insertions, 51 deletions
diff --git a/src/infini/__main__.py b/src/infini/__main__.py index aa829131..9a8cd58c 100644 --- a/src/infini/__main__.py +++ b/src/infini/__main__.py @@ -39,7 +39,7 @@ def main(): (path / "event.py").write_text(templates.EVENT, encoding="utf-8") (path / "tests.py").write_text(templates.TEST, encoding="utf-8") - logger.success("HydroRoll 规则包模板已创建!") + logger.success("Infini 规则包模板已创建!") if args.operate == "test": exceptions = [] @@ -48,7 +48,7 @@ def main(): logger.info("初始化规则包中...") try: - register.regist(path) + register.register(path) except Exception as error: if args.verbose: logger.exception(error) diff --git a/src/infini/const/templates.py b/src/infini/const/templates.py index 9a450846..d0be9e30 100644 --- a/src/infini/const/templates.py +++ b/src/infini/const/templates.py @@ -7,6 +7,7 @@ from .event import MyEvent class MyHandler(Handler): \"\"\"自设业务函数\"\"\" + name: str = "example_handler" priority: int = 0 # 业务函数权重 def process(self, event: MatcherEvent) -> InfiniEvent: @@ -36,5 +37,4 @@ def test(): except Exception as error: return error return [] - """ diff --git a/src/infini/exceptions.py b/src/infini/exceptions.py index 86144a49..61c93f63 100644 --- a/src/infini/exceptions.py +++ b/src/infini/exceptions.py @@ -5,18 +5,6 @@ """ -class EventException(BaseException): - """事件处理过程中由规则包抛出的异常, 用于控制事件的传播, 会被 Infini 自动捕获并处理。""" - - -class SkipException(EventException): - """跳过当前规则包继续当前事件传播。""" - - -class StopException(EventException): - """停止当前事件传播。""" - - class InfiniException(Exception): """Infini 异常基类""" @@ -30,7 +18,7 @@ class LoadError(InfiniException): class PackageNotFound(LoadError): - """规则包不存在时错误, """ + """规则包不存在时错误""" class EventLoadError(LoadError, RuntimeError): @@ -41,15 +29,15 @@ class HandlerLoadError(LoadError, RuntimeError): """业务函数导入失败""" -class UnknownException(BaseException): - """未知异常基类""" +class EventException(InfiniException): + """事件异常基类""" -class UnknownMatcherEvent(UnknownException): +class UnknownMatcherEvent(EventException): """未知的给入实现""" -class UnknownMessageEvent(UnknownException): +class UnknownMessageEvent(EventException): """未知的给出实现""" diff --git a/src/infini/handler.py b/src/infini/handler.py index da4f84b8..0f72eccf 100644 --- a/src/infini/handler.py +++ b/src/infini/handler.py @@ -5,13 +5,10 @@ 此外,每个规则包业务类还可以定义一个名为 priority 的类属性,用于指定该业务类的优先级。优先级越高,该业务类处理事件的顺序越靠前。 """ -from abc import ABC, ABCMeta, abstractmethod +from abc import ABC, abstractmethod from enum import Enum -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 +from .typing import StateT, ClassVar, Generic +from .event import MatcherEvent, InfiniEvent __all__ = ["Handler", "HandlerLoadType"] @@ -28,28 +25,10 @@ class HandlerLoadType(Enum): class Handler(ABC, Generic[StateT]): """规则包业务基类""" + name: str priority: ClassVar[int] = 0 block: ClassVar[bool] = False - def __init_state__(self) -> Optional[StateT]: - """初始化规则包状态。""" - - @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: raise NotImplementedError diff --git a/src/infini/register.py b/src/infini/register.py index 4596b930..55677152 100644 --- a/src/infini/register.py +++ b/src/infini/register.py @@ -8,7 +8,7 @@ from .exceptions import UnknownMatcherEvent, UnsupportedError from .handler import Handler from .event import InfiniEvent, MessageEvent, WorkflowEvent from .typing import Dict, Type -from .exceptions import LoadError, EventLoadError, HandlerLoadError, UnknownException +from .exceptions import LoadError, EventLoadError, HandlerLoadError import re import sys @@ -70,8 +70,7 @@ class Loader: for handler in handlers: self.handlers[f"{self.name}.{handler.__dict__['name']}"] = handler except Exception as error: - raise HandlerLoadError( - f"规则包[{self.name}]业务函数导入失败: {error}") from error + raise HandlerLoadError(f"规则包[{self.name}]业务函数导入失败: {error}") from error sys.path.remove(str(self.meta_path)) @@ -152,8 +151,10 @@ class Register: self.events = Events() self.handlers = Handlers() - def register(self, meta_path: Path | str | None = None): - _loader = Loader(meta_path or ".") + def register( + self, meta_path: Path | str | None = None, loader: Type[Loader] | None = None + ): + _loader = Loader(meta_path or ".") if not loader else loader(meta_path or ".") _loader.load() self.events.update(_loader.events) self.handlers.update(_loader.handlers) diff --git a/src/infini/typing.py b/src/infini/typing.py index 58d2b80d..0da72385 100644 --- a/src/infini/typing.py +++ b/src/infini/typing.py @@ -7,6 +7,8 @@ from typing import ( Dict as Dict, Any as Any, Type as Type, + ClassVar as ClassVar, + Generic as Generic, TYPE_CHECKING as TYPE_CHECKING, TypeVar as TypeVar, Callable as Callable, diff --git a/src/infini/utils/cli.py b/src/infini/utils/cli.py index 8d83cc52..3d30f839 100644 --- a/src/infini/utils/cli.py +++ b/src/infini/utils/cli.py @@ -25,4 +25,4 @@ def parse_args(argv: list[str] | None = None) -> argparse.Namespace: test_parser.add_argument("path", help="目标位置") test_parser.add_argument("-v", "--verbose", action="store_true", help="异常追踪") - return parser.parse_args(argv or sys.argv[1:]) + return parser.parse_args(sys.argv[1:] or argv or ["--help"]) |
