Skip to content

Commit

Permalink
🎨 format core.py
Browse files Browse the repository at this point in the history
  • Loading branch information
RF-Tar-Railt committed May 10, 2023
1 parent 7ffd7e2 commit 92d741b
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 66 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
- `set_default``arg`, `option`, `subcommand` 合并为 `path`, 原参数仍可用
- `Arg``name` 的后缀标识符不强制需要 `';'` 作为分割, 即 `foo;?``foo?` 等价
- `Argv.preprocessor``Argv.filter_out` 现在接收 type 而不是 str
- 移除 `Alconna.reset_behaviors`

### 修复:

Expand Down
22 changes: 9 additions & 13 deletions src/arclet/alconna/arparma.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,19 +190,15 @@ def execute(self, behaviors: list[ArparmaBehavior] | None = None) -> Self:
Returns:
Self: 返回自身
"""
if behaviors := (self.source.behaviors + (behaviors or [])):
exc_behaviors = []
for behavior in behaviors:
exc_behaviors.extend(requirement_handler(behavior))
for b in exc_behaviors:
b.before_operate(self)
for b in exc_behaviors:
try:
b.operate(self)
except BehaveCancelled:
continue
except OutBoundsBehave as e:
return self.fail(e)
for b in behaviors:
b.before_operate(self)
for b in behaviors:
try:
b.operate(self)
except BehaveCancelled:
continue
except OutBoundsBehave as e:
return self.fail(e)
return self

def call(self, target: Callable[..., T], **additional) -> T:
Expand Down
91 changes: 38 additions & 53 deletions src/arclet/alconna/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import sys
from dataclasses import dataclass, field
from functools import partial, reduce
from functools import partial
from pathlib import Path
from typing import Any, Callable, Generic, Sequence, TypeVar, overload

Expand All @@ -12,7 +12,7 @@

from ._internal._analyser import Analyser, TCompile
from .args import Arg, Args
from .arparma import Arparma, ArparmaBehavior
from .arparma import Arparma, ArparmaBehavior, requirement_handler
from .base import Option, Subcommand
from .config import Namespace, config
from .duplication import Duplication
Expand All @@ -26,6 +26,25 @@
TDC1 = TypeVar("TDC1", bound=DataCollection[Any])


def add_builtin_options(options: list[Option | Subcommand], ns: Namespace) -> None:
options.append(
Option("|".join(ns.builtin_option_name['help']), help_text=lang.require("builtin", "option_help")),
)
options.append(
Option(
"|".join(ns.builtin_option_name['shortcut']),
Args["delete;?", "delete"]["name", str]["command", str, "_"],
help_text=lang.require("builtin", "option_shortcut")
)
)
options.append(
Option(
"|".join(ns.builtin_option_name['completion']),
help_text=lang.require("builtin", "option_completion")
)
)


@dataclass(init=True, unsafe_hash=True)
class ArparmaExecutor(Generic[T]):
"""Arparma 执行器
Expand Down Expand Up @@ -113,11 +132,11 @@ def __init__(
"""
if not namespace:
ns_config = config.default_namespace
elif isinstance(namespace, Namespace):
ns_config = config.namespaces.setdefault(namespace.name, namespace)
else:
elif isinstance(namespace, str):
ns_config = config.namespaces.setdefault(namespace, Namespace(namespace))
self.prefixes = next(filter(lambda x: isinstance(x, list), args + (ns_config.prefixes.copy(),))) # type: ignore
else:
ns_config = namespace
self.prefixes = next(filter(lambda x: isinstance(x, list), args), ns_config.prefixes.copy()) # type: ignore
try:
self.command = next(filter(lambda x: not isinstance(x, (list, Option, Subcommand, Args, Arg)), args))
except StopIteration:
Expand All @@ -135,33 +154,20 @@ def __init__(
self.meta.raise_exception = self.meta.raise_exception or ns_config.raise_exception
self.meta.compact = self.meta.compact or ns_config.compact
options = [i for i in args if isinstance(i, (Option, Subcommand))]
options.append(
Option("|".join(ns_config.builtin_option_name['help']), help_text=lang.require("builtin", "option_help")),
)
options.append(
Option(
"|".join(ns_config.builtin_option_name['shortcut']),
Args["delete;?", "delete"]["name", str]["command", str, "_"],
help_text=lang.require("builtin", "option_shortcut")
)
)
options.append(
Option(
"|".join(ns_config.builtin_option_name['completion']),
help_text=lang.require("builtin", "option_completion")
)
)
name = f"{self.command or self.prefixes[0]}".replace(command_manager.sign, "") # type: ignore
add_builtin_options(options, ns_config)
name = f"{self.command or self.prefixes[0]}" # type: ignore
self.path = f"{self.namespace}::{name}"
_args = Args()
for i in filter(lambda x: isinstance(x, (Args, Arg)), args):
_args << i
super().__init__(
"ALCONNA::",
reduce(lambda x, y: x + y, [Args()] + [i for i in args if isinstance(i, (Arg, Args))]), # type: ignore
*options,
dest=name,
separators=separators or ns_config.separators,
_args, *options, dest=name, separators=separators or ns_config.separators,
)
self.name = name
self.behaviors = behaviors or []
self.behaviors = []
for behavior in behaviors or []:
self.behaviors.extend(requirement_handler(behavior))
command_manager.register(self)
self._executors: list[ArparmaExecutor] = []
self.union = set()
Expand All @@ -184,33 +190,14 @@ def reset_namespace(self, namespace: Namespace | str, header: bool = True) -> Se
self.path = f"{self.namespace}::{self.name}"
if header:
self.prefixes = namespace.prefixes.copy()
self.options[-3] = Option(
"|".join(namespace.builtin_option_name['help']), help_text=lang.require("builtin", "option_help")
)
self.options[-2] = Option(
"|".join(namespace.builtin_option_name['shortcut']),
Args["delete;?", "delete"]["name", str]["command", str, "_"],
help_text=lang.require("builtin", "option_shortcut")
)
self.options[-1] = Option(
"|".join(namespace.builtin_option_name['completion']),
help_text=lang.require("builtin", "option_completion")
)
self.options = self.options[:-3]
add_builtin_options(self.options, namespace)
self.meta.fuzzy_match = namespace.fuzzy_match or self.meta.fuzzy_match
self.meta.raise_exception = namespace.raise_exception or self.meta.raise_exception
self._hash = self._calc_hash()
command_manager.register(self)
return self

def reset_behaviors(self, behaviors: list[ArparmaBehavior]) -> Self:
"""重新设置解析行为器
Args:
behaviors (list[ArparmaBehavior]): 解析行为器
"""
self.behaviors[1:] = behaviors
return self

def get_help(self) -> str:
"""返回该命令的帮助信息"""
return self.formatter.format_node()
Expand Down Expand Up @@ -298,9 +285,7 @@ def parse(self, message: TDC) -> Arparma[TDC]:
def parse(self, message, *, duplication: type[T_Duplication]) -> T_Duplication:
...

def parse(
self, message: TDC, *, duplication: type[T_Duplication] | None = None
) -> Arparma[TDC] | T_Duplication:
def parse(self, message: TDC, *, duplication: type[T_Duplication] | None = None) -> Arparma[TDC] | T_Duplication:
"""命令分析功能, 传入字符串或消息链, 返回一个特定的数据集合类
Args:
Expand All @@ -318,7 +303,7 @@ def parse(
raise e
return Arparma(self.path, message, False, error_info=e)
if arp.matched:
arp = arp.execute()
arp = arp.execute(self.behaviors)
if self._executors:
for ext in self._executors:
arp.call(ext.target)
Expand Down

0 comments on commit 92d741b

Please sign in to comment.