From bc4d5dfe10a59e1131867502cf4ea6bc2fa6d316 Mon Sep 17 00:00:00 2001 From: RF-Tar-Railt <3165388245@qq.com> Date: Tue, 2 Jan 2024 15:44:29 +0800 Subject: [PATCH] :bookmark: version 0.35.1 --- src/nonebot_plugin_alconna/__init__.py | 2 +- src/nonebot_plugin_alconna/uniseg/__init__.py | 2 +- src/nonebot_plugin_alconna/uniseg/template.py | 80 +++++++++---------- tests/test_uniseg.py | 4 +- 4 files changed, 45 insertions(+), 43 deletions(-) diff --git a/src/nonebot_plugin_alconna/__init__.py b/src/nonebot_plugin_alconna/__init__.py index cc9d8f1..a187f48 100644 --- a/src/nonebot_plugin_alconna/__init__.py +++ b/src/nonebot_plugin_alconna/__init__.py @@ -97,7 +97,7 @@ from .consts import ALCONNA_EXEC_RESULT as ALCONNA_EXEC_RESULT from .extension import add_global_extension as add_global_extension -__version__ = "0.35.0" +__version__ = "0.35.1" __plugin_meta__ = PluginMetadata( name="Alconna 插件", diff --git a/src/nonebot_plugin_alconna/uniseg/__init__.py b/src/nonebot_plugin_alconna/uniseg/__init__.py index f112489..b3c897f 100644 --- a/src/nonebot_plugin_alconna/uniseg/__init__.py +++ b/src/nonebot_plugin_alconna/uniseg/__init__.py @@ -33,7 +33,7 @@ from .params import UniversalMessage as UniversalMessage from .params import UniversalSegment as UniversalSegment -__version__ = "0.35.0" +__version__ = "0.35.1" __plugin_meta__ = PluginMetadata( name="Universal Segment 插件", diff --git a/src/nonebot_plugin_alconna/uniseg/template.py b/src/nonebot_plugin_alconna/uniseg/template.py index 2637451..03532cc 100644 --- a/src/nonebot_plugin_alconna/uniseg/template.py +++ b/src/nonebot_plugin_alconna/uniseg/template.py @@ -1,6 +1,5 @@ import re import functools -import _string # type: ignore from string import Formatter from typing_extensions import TypeAlias from typing import ( @@ -19,6 +18,7 @@ cast, ) +import _string # type: ignore from tarina.tools import gen_subclass from .segment import Segment @@ -33,6 +33,43 @@ _PATTERN = re.compile("(" + "|".join(_MAPPING.keys()) + r")\((.*)\)$") +def _eval(route: str, obj: Any): + res = obj + parts = re.split(r"\.|(\[.+\])|(\(.*\))", route) + for part in parts[1:]: + if not part: + continue + if part.startswith("_"): + raise ValueError(route) + if part.startswith("[") and part.endswith("]"): + item = part[1:-1] + if item[0] in ("'", '"') and item[-1] in ("'", '"'): + res = res[item[1:-1]] + elif ":" in item: + res = res[slice(*(int(x) if x else None for x in item.split(":")))] + else: + res = res[int(item)] + elif part.startswith("(") and part.endswith(")"): + item = part[1:-1] + if not item: + res = res() + else: + _parts = item.split(",") + _args = [] + _kwargs = {} + for part in _parts: + part = part.strip() + if re.match(".+=.+", part): + k, v = part.split("=") + _kwargs[k] = v + else: + _args.append(part) + res = res(*_args, **_kwargs) + else: + res = getattr(res, part) + return res + + class UniMessageTemplate(Formatter): """通用消息模板格式化实现类。 @@ -103,43 +140,6 @@ def _format(self, args: Sequence[Any], kwargs: Mapping[str, Any]): # f"not all arguments converted during string formatting: " f"{set(kwargs) - set(keys)}" # ) - def _getattr(self, route: str, obj: Any): - res = obj - parts = re.split(r"\.|(\[.+\])|(\(.*\))", route) - for part in parts[1:]: - if not part: - continue - if part.startswith("_"): - raise ValueError(route) - if part.startswith("[") and part.endswith("]"): - item = part[1:-1] - if item[0] in ("'", '"') and item[-1] in ("'", '"'): - res = res[item[1:-1]] - elif ":" in item: - res = res[slice(*map(lambda x: int(x) if x else None, item.split(':')))] - else: - res = res[int(item)] - elif part.startswith("(") and part.endswith(")"): - item = part[1:-1] - if not item: - res = res() - else: - _parts = item.split(",") - _args = [] - _kwargs = {} - for part in _parts: - part = part.strip() - if re.match(".+=.+", part): - k, v = part.split("=") - _kwargs[k] = v - else: - _args.append(part) - res = res(*_args, **_kwargs) - else: - res = getattr(res, part) - return res - - def _vformat( self, format_string: str, @@ -164,14 +164,14 @@ def _vformat( for part in parts: part = part.strip() if part.startswith("$") and (key := part.split(".")[0]) in kwargs: - _args.append(self._getattr(part[1:], kwargs[key])) + _args.append(_eval(part[1:], kwargs[key])) elif re.match(".+=.+", part): k, v = part.split("=") if v in kwargs: _kwargs[k] = kwargs[v] used_args.add(v) elif v.startswith("$") and (key := v.split(".")[0]) in kwargs: - _kwargs[k] = self._getattr(v[1:], kwargs[key]) + _kwargs[k] = _eval(v[1:], kwargs[key]) else: _kwargs[k] = v elif part in kwargs: diff --git a/tests/test_uniseg.py b/tests/test_uniseg.py index 1d04d85..cbc6b76 100644 --- a/tests/test_uniseg.py +++ b/tests/test_uniseg.py @@ -50,7 +50,9 @@ async def test_unimsg_template(app: App): @matcher.handle() async def handle(): - await matcher.finish(UniMessage.template("{:Reply($message_id)}{:At(user, $event.get_user_id()[1:])}")) + await matcher.finish( + UniMessage.template("{:Reply($message_id)}{:At(user, $event.get_user_id()[1:])}") + ) async with app.test_matcher(matcher) as ctx: adapter = get_adapter(Adapter)