Skip to content

Commit

Permalink
🔥 remove _header.py
Browse files Browse the repository at this point in the history
  • Loading branch information
RF-Tar-Railt committed Oct 16, 2024
1 parent cc2f4d8 commit b394526
Show file tree
Hide file tree
Showing 11 changed files with 105 additions and 299 deletions.
9 changes: 4 additions & 5 deletions devtool.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@
from typing import Any, Literal

from arclet.alconna._internal._analyser import Analyser, default_compiler
from arclet.alconna._internal._handlers import HEAD_HANDLES
from arclet.alconna._internal._handlers import analyse_header as alh
from arclet.alconna._internal._handlers import analyse_args as ala
from arclet.alconna._internal._handlers import analyse_option as alo
from arclet.alconna._internal._header import Header
from arclet.alconna.args import Args
from arclet.alconna.argv import Argv
from arclet.alconna.base import Option, Subcommand
from arclet.alconna.base import Option, Subcommand, Header
from arclet.alconna.config import Namespace
from arclet.alconna.typing import CommandMeta, DataCollection

Expand Down Expand Up @@ -70,12 +69,12 @@ def analyse_header(
**kwargs
):
meta = CommandMeta(keep_crlf=False, fuzzy_match=False, raise_exception=raise_exception, context_style=context_style)
argv: Argv[DataCollection] = Argv(meta, dev_space, separators=(sep,))
argv: Argv[DataCollection] = Argv(meta, dev_space, separators=sep)
command_header = Header.generate(command_name, headers, compact=compact)
try:
argv.enter(kwargs)
argv.build(command)
return HEAD_HANDLES[command_header.flag](command_header, argv)
return alh(command_header, argv)
except Exception as e:
if raise_exception:
traceback.print_exception(AnalyseError, e, e.__traceback__)
Expand Down
11 changes: 2 additions & 9 deletions src/arclet/alconna/_internal/_analyser.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
from ..output import output_manager
from ..typing import TDC
from ._handlers import (
HEAD_HANDLES,
analyse_header,
analyse_args,
analyse_param,
handle_completion,
Expand All @@ -34,7 +34,6 @@
handle_shortcut,
prompt,
)
from ._header import Header
from ._util import levenshtein

if TYPE_CHECKING:
Expand Down Expand Up @@ -207,10 +206,6 @@ class Analyser(SubAnalyser):

command: Alconna
"""命令实例"""
command_header: Header
"""命令头部"""
header_handler: Callable[[Header, Argv], HeadResult]
"""头部处理器"""

def __init__(self, alconna: Alconna, compiler: TCompile | None = None):
"""初始化解析器
Expand All @@ -224,8 +219,6 @@ def __init__(self, alconna: Alconna, compiler: TCompile | None = None):

def compile(self, param_ids: set[str]):
self.extra_allow = not self.command.meta.strict or not self.command.namespace_config.strict
self.command_header = Header.generate(self.command.command, self.command.prefixes, self.command.meta.compact)
self.header_handler = HEAD_HANDLES[self.command_header.flag]
self._compiler(self, param_ids)
return self

Expand All @@ -241,7 +234,7 @@ def process(self, argv: Argv[TDC]) -> Exception | None:
"""
if not self.header_result:
try:
self.header_result = self.header_handler(self.command_header, argv)
self.header_result = analyse_header(self.command._header, argv)
except InvalidHeader as e:
return e
except RuntimeError:
Expand Down
63 changes: 10 additions & 53 deletions src/arclet/alconna/_internal/_handlers.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
from __future__ import annotations

import re
from typing import TYPE_CHECKING, Any, Callable, Iterable
from typing_extensions import NoReturn
from typing import TYPE_CHECKING, Any, Iterable

from nepattern import ANY, STRING, AnyString, BasePattern, TPattern
from nepattern import ANY, STRING, AnyString, BasePattern
from tarina import Empty, lang, safe_eval, split_once

from ..action import Action
from ..args import Arg, Args
from ..base import Option, Subcommand
from ..base import Option, Subcommand, Header
from ..completion import Prompt, comp_ctx
from ..config import config
from ..exceptions import (
Expand All @@ -24,7 +23,7 @@
from ..model import HeadResult, OptionResult
from ..output import output_manager
from ..typing import KWBool, MultiKeyWordVar, MultiVar, _AllParamPattern, _StrMulti
from ._header import Header

from ._util import levenshtein

if TYPE_CHECKING:
Expand Down Expand Up @@ -470,66 +469,24 @@ def analyse_param(analyser: SubAnalyser, argv: Argv, seps: str | None = None):
return False


def _header_handle0(header: "Header[set[str], TPattern]", argv: Argv):
def analyse_header(header: "Header", argv: Argv):
content = header.content
head_text, _str = argv.next()
if _str:
if head_text in content:
return HeadResult(head_text, head_text, True, fixes=header.mapping)
return HeadResult(head_text, head_text, True)
if header.compact and (mat := header.compact_pattern.match(head_text)):
argv.rollback(head_text[len(mat[0]):], replace=True)
return HeadResult(mat[0], mat[0], True, mat.groupdict(), header.mapping)
return HeadResult(mat[0], mat[0], True)
may_cmd, _m_str = argv.next()
if _m_str:
cmd = f"{head_text}{argv.separators[0]}{may_cmd}"
if cmd in content:
return HeadResult(cmd, cmd, True, fixes=header.mapping)
return HeadResult(cmd, cmd, True)
if header.compact and (mat := header.compact_pattern.match(cmd)):
argv.rollback(cmd[len(mat[0]):], replace=True)
return HeadResult(mat[0], mat[0], True, mat.groupdict(), header.mapping)
_after_analyse_header(argv, head_text, may_cmd, _str, _m_str)


def _header_handle1(header: "Header[TPattern, TPattern]", argv: Argv):
content = header.content
head_text, _str = argv.next()
if _str:
if mat := content.fullmatch(head_text):
return HeadResult(head_text, head_text, True, mat.groupdict(), header.mapping)
if header.compact and (mat := header.compact_pattern.match(head_text)):
argv.rollback(head_text[len(mat[0]):], replace=True)
return HeadResult(mat[0], mat[0], True, mat.groupdict(), header.mapping)
may_cmd, _m_str = argv.next()
if _m_str:
cmd = f"{head_text}{argv.separators[0]}{may_cmd}"
if mat := content.fullmatch(cmd):
return HeadResult(cmd, cmd, True, mat.groupdict(), header.mapping)
if header.compact and (mat := header.compact_pattern.match(cmd)):
argv.rollback(cmd[len(mat[0]):], replace=True)
return HeadResult(mat[0], mat[0], True, mat.groupdict(), header.mapping)
_after_analyse_header(argv, head_text, may_cmd, _str, _m_str)


def _header_handle2(header: "Header[BasePattern, BasePattern]", argv: Argv):
head_text, _str = argv.next()
if (val := header.content.validate(head_text)).success:
return HeadResult(head_text, val._value, True, fixes=header.mapping)
if header.compact and (val := header.compact_pattern.validate(head_text)).success:
if _str:
argv.rollback(head_text[len(str(val._value)):], replace=True)
return HeadResult(val.value, val._value, True, fixes=header.mapping)
may_cmd, _m_str = argv.next()
_after_analyse_header(argv, head_text, may_cmd, _str, _m_str)


HEAD_HANDLES: dict[int, Callable[[Header, Argv], HeadResult]] = {
0: _header_handle0,
1: _header_handle1,
2: _header_handle2,
}


def _after_analyse_header(argv: Argv, head_text: Any, may_cmd: Any, _str: bool, _m_str: bool) -> NoReturn:
return HeadResult(mat[0], mat[0], True)
# _after_analyse_header
if _str:
argv.rollback(may_cmd)
raise InvalidHeader(lang.require("header", "error").format(target=head_text), head_text)
Expand Down
135 changes: 0 additions & 135 deletions src/arclet/alconna/_internal/_header.py

This file was deleted.

10 changes: 0 additions & 10 deletions src/arclet/alconna/arparma.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,21 +164,11 @@ def _clr(self):
for k in ks:
delattr(self, k)

@property
def header(self) -> dict[str, Any]:
"""返回可能解析到的命令头中的组信息"""
return self.header_match.groups

@property
def head_matched(self):
"""返回命令头是否匹配"""
return self.header_match.matched

@property
def header_result(self):
"""返回命令头匹配结果"""
return self.header_match.result

@property
def non_component(self) -> bool:
"""返回是否没有解析到任何组件"""
Expand Down
Loading

0 comments on commit b394526

Please sign in to comment.