-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathdevtool.py
136 lines (118 loc) · 4.56 KB
/
devtool.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
from __future__ import annotations
import traceback
from collections import namedtuple
from typing import Any, Literal
from arclet.alconna.ingedia._analyser import Analyser
from arclet.alconna.ingedia._handlers import analyse_header as alh
from arclet.alconna.ingedia._handlers import analyse_args as ala
from arclet.alconna.ingedia._handlers import analyse_option as alo
from arclet.alconna.args import ARGS_PARAM, handle_args
from arclet.alconna.ingedia._argv import Argv
from arclet.alconna.base import Option, Subcommand, Header, Config
from arclet.alconna.config import Namespace
from arclet.alconna.utils import DataCollection
class AnalyseError(Exception):
"""分析时发生错误"""
dev_space = Namespace("devtool", Config(enable_message_cache=False))
class _DummyAnalyser(Analyser):
filter_out = []
class _DummyALC:
options = []
config = namedtuple("Config", ["keep_crlf", "fuzzy_match", "raise_exception"])(False, False, True)
namespace_config = dev_space
def __new__(cls, *args, **kwargs):
cls.command = cls._DummyALC() # type: ignore
cls.compile_params = {}
cls.compact_params = []
cls.default_opt_result = {}
return super().__new__(cls)
def analyse_args(
args: ARGS_PARAM,
command: list[str | Any],
raise_exception: bool = True,
context_style: Literal["bracket", "parentheses"] | None = None,
**kwargs,
):
conf = Config(keep_crlf=False, fuzzy_match=False, raise_exception=raise_exception, context_style=context_style)
argv: Argv[DataCollection] = Argv(conf, dev_space)
try:
argv.enter(kwargs)
argv.build(["test"] + command)
argv.next()
return ala(argv, handle_args(args))
except Exception as e:
if raise_exception:
traceback.print_exception(AnalyseError, e, e.__traceback__)
return {}
def analyse_header(
headers: list[str | Any] | list[tuple[Any, str]],
command_name: str,
command: list[str | Any],
sep: str = " ",
compact: bool = False,
raise_exception: bool = True,
context_style: Literal["bracket", "parentheses"] | None = None,
**kwargs,
):
conf = Config(keep_crlf=False, fuzzy_match=False, raise_exception=raise_exception, context_style=context_style)
argv: Argv[DataCollection] = Argv(conf, dev_space, separators=sep)
command_header = Header.generate(command_name, headers, compact=compact)
try:
argv.enter(kwargs)
argv.build(command)
return alh(command_header, argv)
except Exception as e:
if raise_exception:
traceback.print_exception(AnalyseError, e, e.__traceback__)
return
def analyse_option(
option: Option,
command: list[str | Any],
raise_exception: bool = True,
context_style: Literal["bracket", "parentheses"] | None = None,
**kwargs,
):
conf = Config(keep_crlf=False, fuzzy_match=False, raise_exception=raise_exception, context_style=context_style)
argv: Argv[DataCollection] = Argv(conf, dev_space)
_analyser = _DummyAnalyser.__new__(_DummyAnalyser)
_analyser.reset()
_analyser.command.separators = " "
_analyser.need_main_args = False
_analyser.command.options.append(option)
_analyser.compile()
argv.stack_params.base = _analyser.compile_params
_analyser.command.options.clear()
try:
argv.enter(kwargs)
argv.build(command)
alo(_analyser, argv, option, False)
return _analyser.options_result[option.dest]
except Exception as e:
if raise_exception:
traceback.print_exception(AnalyseError, e, e.__traceback__)
return
def analyse_subcommand(
subcommand: Subcommand,
command: list[str | Any],
raise_exception: bool = True,
context_style: Literal["bracket", "parentheses"] | None = None,
**kwargs,
):
conf = Config(keep_crlf=False, fuzzy_match=False, raise_exception=raise_exception, context_style=context_style)
argv: Argv[DataCollection] = Argv(conf, dev_space)
_analyser = _DummyAnalyser.__new__(_DummyAnalyser)
_analyser.reset()
_analyser.command.separators = " "
_analyser.need_main_args = False
_analyser.command.options.append(subcommand)
_analyser.compile()
argv.stack_params.base = _analyser.compile_params
_analyser.command.options.clear()
try:
argv.enter(kwargs)
argv.build(command)
return _analyser.compile_params[subcommand.name].process(argv, False).result() # type: ignore
except Exception as e:
if raise_exception:
traceback.print_exception(AnalyseError, e, e.__traceback__)
return