From 1dbdbc7e84a7d107e35c507ee439075d6e469ea6 Mon Sep 17 00:00:00 2001 From: 苏向夜 Date: Thu, 14 Dec 2023 17:42:23 +0800 Subject: :fire: 使用__main__.py替代project.scripts MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pyproject.toml | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) (limited to 'pyproject.toml') diff --git a/pyproject.toml b/pyproject.toml index 20f6c671..6084eb00 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -2,7 +2,7 @@ name = "infini" version = "1.0.4" description = "The `Core` of `HydroRoll`, the `Loader` of your rules packages." -authors = [{ name = "简律纯", email = "i@jyunko.cn" }] +authors = [{ name = "简律纯", email = "i@jyunko.cn" }, { name = "苏向夜", email = "fu050409@163.com" }] dependencies = [ "mkdocs>=1.5.3", "mkdocs-material[imaging]>=9.4.4", @@ -25,16 +25,10 @@ dependencies = [ "pydantic>=2.4.2", "loguru>=0.7.2", ] -requires-python = ">=3.8" +requires-python = ">=3.9" readme = "README.md" license = { text = "MIT" } - -[project.scripts] -infini = "infini.cli:Cli.parse_args" -ifi = "infini.cli:Cli.parse_args" - [build-system] requires = ["pdm-backend"] build-backend = "pdm.backend" - -- cgit v1.2.3-70-g09d2 From c559e35fb99733c60ab97e536aa10fac59766f55 Mon Sep 17 00:00:00 2001 From: 苏向夜 Date: Thu, 14 Dec 2023 18:22:32 +0800 Subject: :sparkles: 新增原始消息传递 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 23 +++++++---------------- pyproject.toml | 2 +- src/infini/consts/templates.py | 7 ++++++- src/infini/event.py | 16 ++++++++++++++++ src/infini/handler.py | 4 ++-- src/infini/logging.py | 2 +- src/infini/matcher.py | 18 ++---------------- 7 files changed, 35 insertions(+), 37 deletions(-) (limited to 'pyproject.toml') diff --git a/README.md b/README.md index 5b69d574..cdb2d7ce 100644 --- a/README.md +++ b/README.md @@ -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"" + + 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"" - - 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 ) -- cgit v1.2.3-70-g09d2