Skip to content

Commit

Permalink
sync: version 1.8.33
Browse files Browse the repository at this point in the history
warning for incorrect shortcut key
  • Loading branch information
RF-Tar-Railt committed Nov 21, 2024
1 parent 1fce043 commit 1adc693
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 7 deletions.
16 changes: 16 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,22 @@
- Sistana (及其上游库)仅支持 Python 3.9 及以上版本。
- Sistana 对各种特性的兼容性尚未完全测试。

## 1.8.33

### 改进

- 若快捷指令的 key 以 `^` 为前缀,则其会被替换为命令前缀之一,并发出警告

### 修复

- 修复解析快捷命令时处理换行的行为与正常行为不一致的问题

## 1.8.32

### 修复

- 修复 Args 读取到的字符串参数属于某个选项/子命令的名字时的错误行为

## 1.8.31

### 改进
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 @@ -39,4 +39,4 @@
from .shortcut import ShortcutArgs as ShortcutArgs
from .manager import command_manager as command_manager

__version__ = "1.8.31"
__version__ = "1.8.33"
18 changes: 15 additions & 3 deletions src/arclet/alconna/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import contextlib
import re
import shelve
import warnings
import weakref
from copy import copy
from datetime import datetime
Expand Down Expand Up @@ -200,24 +201,35 @@ def add_shortcut(self, target: Alconna, key: str | TPattern, source: ShortcutArg
_key = key.pattern
_flags = key.flags
humanize = source.pop("humanized", None)
command = source.pop("command", str(target.command))
if _key.startswith("^"):
warnings.warn(
"Shortcut Key should not start with '^', otherwise it will ignore the prefix automatically.",
UserWarning,
stacklevel=3
)
_key = _key[1:]
if target.prefixes and (_pf := next(filter(lambda x: isinstance(x, str), target.prefixes), None)):
command = f"{_pf}{command}"
source["prefix"] = False
if source.get("prefix", False) and target.prefixes:
out = []
for prefix in target.prefixes:
_shortcut[1][f"{re.escape(prefix)}{_key}"] = InnerShortcutArgs(
**{**source, "command": argv.converter(prefix + source.get("command", str(target.command)))},
**{**source, "command": argv.converter(prefix + command)},
flags=_flags,
)
out.append(
i18n.require("shortcut.add_success").format(shortcut=f"{prefix}{_key}", target=target.path)
)
_shortcut[0][humanize or _key] = InnerShortcutArgs(
**{**source, "command": argv.converter(source.get("command", str(target.command))), "prefixes": target.prefixes},
**{**source, "command": argv.converter(command), "prefixes": target.prefixes},
flags=_flags,
)
target.formatter.update_shortcut(target)
return "\n".join(out)
_shortcut[0][humanize or _key] = _shortcut[1][_key] = InnerShortcutArgs(
**{**source, "command": argv.converter(source.get("command", str(target.command)))},
**{**source, "command": argv.converter(command)},
flags=_flags,
)
target.formatter.update_shortcut(target)
Expand Down
11 changes: 8 additions & 3 deletions tests/core_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -442,13 +442,13 @@ def test_shortcut():
assert not alc16_1.parse("echo 123 456").matched
res6 = alc16_1.parse("echo1 123 456 789")
assert res6.header_match.origin == "echo1"
assert res6["content"] == "print('123 456 789')"
assert res6["content"] == "print('123 456 789')"
res7 = alc16_1.parse([123])
assert not res7.matched
res8 = alc16_1.parse("echo '123'")
assert res8["content"] == "print('123')"
assert res8["content"] == "print('123')"
assert not alc16_1.parse("echo").matched
assert alc16_1.parse("echo1")["content"] == "print('')"
assert alc16_1.parse("echo1")["content"] == "print('')"

alc16_2 = Alconna(["/", "."], "core16_2", Args.foo(bool))
alc16_2.shortcut("test", {"command": "/core16_2 True"})
Expand Down Expand Up @@ -556,6 +556,11 @@ def wrapper2(slot, content):
assert res.query("r") == 100
assert res.query("e") == 36

alc16_15 = Alconna(["/"], "core16_15", Args["bar", str])
with pytest.warns(UserWarning):
alc16_15.shortcut("^test", {"args": ["abc"]})
assert alc16_15.parse("test").bar == "abc"


def test_help():
alc17 = Alconna(
Expand Down

0 comments on commit 1adc693

Please sign in to comment.