diff options
| -rw-r--r-- | README.md | 23 | ||||
| -rw-r--r-- | pyproject.toml | 2 | ||||
| -rw-r--r-- | src/infini/consts/templates.py | 7 | ||||
| -rw-r--r-- | src/infini/event.py | 16 | ||||
| -rw-r--r-- | src/infini/handler.py | 4 | ||||
| -rw-r--r-- | src/infini/logging.py | 2 | ||||
| -rw-r--r-- | src/infini/matcher.py | 18 |
7 files changed, 35 insertions, 37 deletions
@@ -41,25 +41,16 @@ 2. 创建规则包实例 - 创建`cli.py`并写入以下内容: - - ```python - import infini - - client = infini.Cli() - client.parse_args() - ``` - - 打开终端并执行: + 确保你的`infini`正确安装后,打开终端并执行: ``` shell - python cli.py --new --path MyRule + python -m infini new MyRule ``` - 你可以在生成的 `MyRule\rule.py` 创建一个或者多个 `rule` 实例并继承 `Rule` 基类, 通过编写合适的相关方法与类注册规则包实现规则的自定义。 + 你可以在生成的 `MyRule\rule.py` 创建一个或者多个继承 `Handler` 基类的实例, 通过编写合适的相关方法与类注册规则包实现规则的自定义。 ``` python - from infini import Rule, Result, Dice + from infini import Handler, Result class MyRule(Rule): """自设规则包""" @@ -70,12 +61,12 @@ def __init__(self) -> None: """初始化你的规则包""" - def check(self, dice: Dice) -> Result: - """声明规则包检定方式""" + def process(self, **kwargs) -> Result: + """声明规则包运行方式""" return Result("event1", True) ``` - `check`函数应当返回一个`Result`对象,它应当包含一个消息事件名(例如示例中的`event1`),该消息事件名应当在 `MyRule\event.py` 中被注册。消息事件的动态内容通过`{name}`的方式声明并通过`name="内容"`的方式实现。 + `process`函数应当返回一个`Result`对象,它应当包含一个消息事件名(例如示例中的`event1`),该消息事件名应当在 `MyRule\event.py` 中被注册。消息事件的动态内容通过`{name}`的方式声明并通过`name="内容"`的方式实现。 3. 合理修改你的 `config.toml` 配置文件,完成注册! diff --git a/pyproject.toml b/pyproject.toml index 6084eb00..da3c0b59 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,7 +1,7 @@ [project] name = "infini" version = "1.0.4" -description = "The `Core` of `HydroRoll`, the `Loader` of your rules packages." +description = "Infini 核心标准输入输出模块" authors = [{ name = "简律纯", email = "i@jyunko.cn" }, { name = "苏向夜", email = "fu050409@163.com" }] dependencies = [ "mkdocs>=1.5.3", diff --git a/src/infini/consts/templates.py b/src/infini/consts/templates.py index c3b979bd..a662814c 100644 --- a/src/infini/consts/templates.py +++ b/src/infini/consts/templates.py @@ -28,5 +28,10 @@ TEST = """from infini.matcher import matcher, MatcherEvent def test(): event = MatcherEvent("MyRule") - print(matcher.run(event)) + try: + matcher.run(event) + except Exception as error: + return [error] + finally: + return [] """ diff --git a/src/infini/event.py b/src/infini/event.py index f456500a..a83c4042 100644 --- a/src/infini/event.py +++ b/src/infini/event.py @@ -40,4 +40,20 @@ class MessageEvent(metaclass=ABCMeta): events.regist(cls.name, cls.output) +class MatcherEvent: + """Matcher 事件""" + + name: str + string: str + kwargs: dict + + def __init__(self, event_name: str, string: str | None = None, **kwargs): + self.name = event_name + self.string = string or "" + self.kwargs = kwargs + + def __repr__(self) -> str: + return f"<MatcherEvent [{self.name}]>" + + events = Events() diff --git a/src/infini/handler.py b/src/infini/handler.py index 8f50fc8c..06d5c5fa 100644 --- a/src/infini/handler.py +++ b/src/infini/handler.py @@ -1,5 +1,5 @@ from abc import ABCMeta, abstractmethod -from .exceptions import HydroError +from .event import MatcherEvent from .typing import Dict __all__ = ["Result", "Handler"] @@ -28,7 +28,7 @@ class Handler: handlers.regist(cls.name, cls()) @abstractmethod - def process(self, **kwargs) -> Result: + def process(self, event: MatcherEvent) -> Result: raise NotImplementedError diff --git a/src/infini/logging.py b/src/infini/logging.py index 1346c7f7..72736ef7 100644 --- a/src/infini/logging.py +++ b/src/infini/logging.py @@ -12,7 +12,7 @@ from .settings import DEBUG __all__ = ["logger", "error_or_exception"] logger = multilogger( - name="HydroRoll", payload="Core", level="DEBUG" if DEBUG else "INFO" + name="Infini", payload="Core", level="DEBUG" if DEBUG else "INFO" ) current_path = Path(__file__).resolve().parent LOG_PATH = current_path / "logs" diff --git a/src/infini/matcher.py b/src/infini/matcher.py index d7cfa336..7fc5b88a 100644 --- a/src/infini/matcher.py +++ b/src/infini/matcher.py @@ -1,22 +1,8 @@ -from .event import Events, events +from .event import MatcherEvent, Events, events from .handler import Handlers, Handler, handlers from .exceptions import UnknownMatcherEvent -class MatcherEvent: - """Matcher 事件""" - - name: str - kwargs: dict - - def __init__(self, name: str, **kwargs): - self.name = name - self.kwargs = kwargs - - def __repr__(self) -> str: - return f"<MatcherEvent [{self.name}]>" - - class Matcher: """事件处理单元""" @@ -36,7 +22,7 @@ class Matcher: raise UnknownMatcherEvent(f"未知的规则包: {name}") def run(self, event: MatcherEvent) -> str: - result = self.match(event.name).process(**event.kwargs) + result = self.match(event.name).process(event) return self.events.process( result.event, **result.kwargs if result.kwargs else event.kwargs ) |
