From b7fff918a587f5151eba65be3312cda66cb4553a Mon Sep 17 00:00:00 2001 From: rf_tar_railt <3165388245@qq.com> Date: Fri, 1 Mar 2024 18:25:32 +0800 Subject: [PATCH] :bug: version 1.8.3 fix shortcut regex wrapper --- CHANGELOG.md | 6 ++++++ src/arclet/alconna/__init__.py | 2 +- src/arclet/alconna/_internal/_analyser.py | 11 +++++++---- src/arclet/alconna/_internal/_argv.py | 5 +++-- src/arclet/alconna/_internal/_handlers.py | 18 ++++-------------- src/arclet/alconna/typing.py | 2 +- 6 files changed, 22 insertions(+), 22 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 935f46fa..1ccfb77c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # 更新日志 +## Alconna 1.8.3 + +### 修复 + +- 修复 `shortcut.wrapper` 的处理逻辑 + ## Alconna 1.8.2 ### 修复 diff --git a/src/arclet/alconna/__init__.py b/src/arclet/alconna/__init__.py index dfaf2ff1..fe1961d1 100644 --- a/src/arclet/alconna/__init__.py +++ b/src/arclet/alconna/__init__.py @@ -50,7 +50,7 @@ from .typing import UnpackVar as UnpackVar from .typing import Up as Up -__version__ = "1.8.2" +__version__ = "1.8.3" # backward compatibility AnyOne = ANY diff --git a/src/arclet/alconna/_internal/_analyser.py b/src/arclet/alconna/_internal/_analyser.py index ec4ad816..5728ffc2 100644 --- a/src/arclet/alconna/_internal/_analyser.py +++ b/src/arclet/alconna/_internal/_analyser.py @@ -298,7 +298,7 @@ def shortcut( if self.command.meta.raise_exception: raise exc return self.export(argv, True, exc) - argv.addon(short.args) + argv.addon(short.args, merge_str=False) data = _handle_shortcut_data(argv, data) if not data and argv.raw_data and any(isinstance(i, str) and bool(re.search(r"\{%(\d+)|\*(.*?)\}", i)) for i in argv.raw_data): exc = ArgumentMissing(lang.require("analyser", "param_missing")) @@ -306,9 +306,13 @@ def shortcut( raise exc return self.export(argv, True, exc) argv.bak_data = argv.raw_data.copy() - argv.addon(data) + argv.addon(data, merge_str=False) if reg: - argv.raw_data = _handle_shortcut_reg(argv, reg.groups(), reg.groupdict(), short.wrapper) + data = _handle_shortcut_reg(argv, reg.groups(), reg.groupdict(), short.wrapper) + argv.raw_data.clear() + argv.ndata = 0 + argv.current_index = 0 + argv.addon(data) argv.bak_data = argv.raw_data.copy() if argv.message_cache: argv.token = argv.generate_token(argv.raw_data) @@ -350,7 +354,6 @@ def process(self, argv: Argv[TDC]) -> Arparma[TDC]: argv.context[SHORTCUT_REST] = rest argv.context[SHORTCUT_REGEX_MATCH] = mat self.reset() - argv.reset() return self.shortcut(argv, rest, short, mat) except FuzzyMatchSuccess as Fuzzy: diff --git a/src/arclet/alconna/_internal/_argv.py b/src/arclet/alconna/_internal/_argv.py index 5bdf5198..6c6369c7 100644 --- a/src/arclet/alconna/_internal/_argv.py +++ b/src/arclet/alconna/_internal/_argv.py @@ -142,11 +142,12 @@ def build(self, data: TDC) -> Self: self.token = self.generate_token(raw_data) return self - def addon(self, data: Iterable[str | Any]) -> Self: + def addon(self, data: Iterable[str | Any], merge_str: bool = True) -> Self: """添加命令元素 Args: data (Iterable[str | Any]): 命令元素 + merge_str (bool, optional): 是否合并前后字符串 Returns: Self: 自身 @@ -158,7 +159,7 @@ def addon(self, data: Iterable[str | Any]) -> Self: d = res if isinstance(d, str) and not (d := d.strip()): continue - if isinstance(d, str) and i > 0 and isinstance(self.raw_data[-1], str): + if merge_str and isinstance(d, str) and i > 0 and isinstance(self.raw_data[-1], str): self.raw_data[-1] += f"{self.separators[0]}{d}" else: self.raw_data.append(d) diff --git a/src/arclet/alconna/_internal/_handlers.py b/src/arclet/alconna/_internal/_handlers.py index 75340bb8..1e15cfca 100644 --- a/src/arclet/alconna/_internal/_handlers.py +++ b/src/arclet/alconna/_internal/_handlers.py @@ -661,18 +661,14 @@ def _handle_shortcut_reg(argv: Argv, groups: tuple[str, ...], gdict: dict[str, s if index >= len(groups): continue slot = groups[index] - if slot is None: - continue - data.append(wrapper(index, slot) or slot) + data.append(wrapper(index, slot)) continue if mat := KEY_REG_SLOT.fullmatch(unit): key = mat[1] if key not in gdict: continue slot = gdict[key] - if slot is None: - continue - data.append(wrapper(key, slot) or slot) + data.append(wrapper(key, slot)) continue if mat := INDEX_REG_SLOT.findall(unit): for index in map(int, mat): @@ -680,20 +676,14 @@ def _handle_shortcut_reg(argv: Argv, groups: tuple[str, ...], gdict: dict[str, s unit = unit.replace(f"{{{index}}}", "") continue slot = groups[index] - if slot is None: - unit = unit.replace(f"{{{index}}}", "") - continue - unit = unit.replace(f"{{{index}}}", str(wrapper(index, slot) or slot)) + unit = unit.replace(f"{{{index}}}", str(wrapper(index, slot) or "")) if mat := KEY_REG_SLOT.findall(unit): for key in mat: if key not in gdict: unit = unit.replace(f"{{{key}}}", "") continue slot = gdict[key] - if slot is None: - unit = unit.replace(f"{{{key}}}", "") - continue - unit = unit.replace(f"{{{key}}}", str(wrapper(key, slot) or slot)) + unit = unit.replace(f"{{{key}}}", str(wrapper(key, slot) or "")) if unit: data.append(unescape(unit)) return data diff --git a/src/arclet/alconna/typing.py b/src/arclet/alconna/typing.py index 1d058a58..1815a326 100644 --- a/src/arclet/alconna/typing.py +++ b/src/arclet/alconna/typing.py @@ -25,7 +25,7 @@ class ShortcutRegWrapper(Protocol): - def __call__(self, slot: int | str, content: str) -> Any: ... + def __call__(self, slot: int | str, content: str | None) -> Any: ... class ShortcutArgs(TypedDict):