Skip to content

Commit

Permalink
🐛 version 1.8.11
Browse files Browse the repository at this point in the history
fix bracket header and shortcut args with seps
  • Loading branch information
RF-Tar-Railt committed Apr 26, 2024
1 parent 70f246d commit 06984e1
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 8 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
# 更新日志

## Alconna 1.8.11

### 改进

- 快捷指令处理数据时会尝试把带分隔符的字符串用引号包裹

### 修复

- 修复 `Bracket Header` 的正则表达式丢失问题

## Alconna 1.8.10

### 改进
Expand Down
8 changes: 4 additions & 4 deletions pdm.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/arclet/alconna/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
from .typing import UnpackVar as UnpackVar
from .typing import Up as Up

__version__ = "1.8.10"
__version__ = "1.8.11"

# backward compatibility
AnyOne = ANY
2 changes: 2 additions & 0 deletions src/arclet/alconna/_internal/_argv.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,8 @@ def rollback(self, data: str | Any, replace: bool = False):
return
if self._sep:
_current_data = self.raw_data[self.current_index]
if self._sep[0] in data and data[0] not in ("'", '"'):
data = f"\'{data}\'"
self.raw_data[self.current_index] = f"{data}{self._sep[0]}{_current_data}"
return
if self.current_index >= 1:
Expand Down
8 changes: 7 additions & 1 deletion src/arclet/alconna/_internal/_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -707,7 +707,13 @@ def _handle_shortcut_data(argv: Argv, data: list):
argv.raw_data[i + offset] = unescape(unit.replace(f"{{*{mat[1]}}}", "".join(map(str, extend))))
data.clear()
break
return [unit for i, unit in enumerate(data) if i not in record]

def recover_quote(_unit: str):
if any(_unit.count(sep) for sep in argv.separators) and not (_unit[0] in ('"', "'") and _unit[0] == _unit[-1]):
return f'"{_unit}"'
return _unit

return [recover_quote(unit) for i, unit in enumerate(data) if i not in record]


INDEX_REG_SLOT = re.compile(r"\{(\d+)\}")
Expand Down
18 changes: 16 additions & 2 deletions src/arclet/alconna/_internal/_header.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,19 @@ def prefixed(pat: BasePattern):
return new_pat


regex_patterns = {
"str": r".+",
"int": r"\-?\d+",
"float": r"\-?\d+\.?\d*",
"number": r"\-?\d+(?:\.\d*)?",
"bool": "(?i:True|False)",
"list": r"\[.+?\]",
"tuple": r"\(.+?\)",
"set": r"\{.+?\}",
"dict": r"\{.+?\}",
}


def handle_bracket(name: str, mapping: dict):
"""处理字符串中的括号对并转为正则表达式"""
pattern_map = all_patterns()
Expand All @@ -37,10 +50,11 @@ def handle_bracket(name: str, mapping: dict):
parts[i] = f"(?P<{res[0]}>.+)"
elif not res[0]:
pat = pattern_map.get(res[1], res[1])
parts[i] = str(pat.pattern if isinstance(pat, BasePattern) else pat)
parts[i] = regex_patterns.get(res[1], str(pat.pattern if isinstance(pat, BasePattern) else pat))
elif res[1] in pattern_map:
mapping[res[0]] = pattern_map[res[1]]
parts[i] = f"(?P<{res[0]}>{pattern_map[res[1]].pattern})"
pat = regex_patterns.get(res[1], str(pattern_map[res[1]].pattern))
parts[i] = f"(?P<{res[0]}>{pat})"
else:
parts[i] = f"(?P<{res[0]}>{res[1]})"
return unescape("".join(parts)), True
Expand Down
5 changes: 5 additions & 0 deletions tests/core_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,11 @@ def wrapper(slot, content):
assert alc16_9.parse("test123").bar == "123"
assert not alc16_9.parse("test").matched

alc16_10 = Alconna("core16_10", Args["bar", str]["baz", int])
alc16_10.shortcut("/qux", {"command": "core16_10"})

assert alc16_10.parse('/qux "abc def.zip" 123').bar == "abc def.zip"


def test_help():
from arclet.alconna import output_manager
Expand Down

0 comments on commit 06984e1

Please sign in to comment.