Skip to content

Commit

Permalink
🐛 version 1.8.3
Browse files Browse the repository at this point in the history
fix shortcut regex wrapper
  • Loading branch information
RF-Tar-Railt committed Mar 1, 2024
1 parent 9636e7d commit b7fff91
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 22 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# 更新日志

## Alconna 1.8.3

### 修复

- 修复 `shortcut.wrapper` 的处理逻辑

## Alconna 1.8.2

### 修复
Expand Down
2 changes: 1 addition & 1 deletion src/arclet/alconna/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
11 changes: 7 additions & 4 deletions src/arclet/alconna/_internal/_analyser.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,17 +298,21 @@ 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"))
if self.command.meta.raise_exception:
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)
Expand Down Expand Up @@ -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:
Expand Down
5 changes: 3 additions & 2 deletions src/arclet/alconna/_internal/_argv.py
Original file line number Diff line number Diff line change
Expand Up @@ -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: 自身
Expand All @@ -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)
Expand Down
18 changes: 4 additions & 14 deletions src/arclet/alconna/_internal/_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -661,39 +661,29 @@ 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):
if index >= len(groups):
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
Expand Down
2 changes: 1 addition & 1 deletion src/arclet/alconna/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit b7fff91

Please sign in to comment.