cli-def 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- cli_def/__init__.py +16 -0
- cli_def/backend/__init__.py +2 -0
- cli_def/backend/argparse/__init__.py +10 -0
- cli_def/backend/argparse/argparse_builder.py +238 -0
- cli_def/backend/click/__init__.py +13 -0
- cli_def/backend/click/click_binder.py +46 -0
- cli_def/backend/click/click_builder.py +182 -0
- cli_def/backend/protocols.py +22 -0
- cli_def/demo/__init__.py +2 -0
- cli_def/demo/handlers.py +125 -0
- cli_def/demo/profiles/advanced.toml +39 -0
- cli_def/demo/profiles/beginner.toml +21 -0
- cli_def/models/__init__.py +17 -0
- cli_def/models/abstract_node.py +87 -0
- cli_def/models/argument_def.py +84 -0
- cli_def/models/cli_def.py +28 -0
- cli_def/models/command_def.py +49 -0
- cli_def/models/mult_def.py +81 -0
- cli_def/models/schema/cli_schema.py +24 -0
- cli_def/ops/__init__.py +16 -0
- cli_def/ops/dumper.py +216 -0
- cli_def/ops/loader.py +56 -0
- cli_def/ops/utils/__init__.py +3 -0
- cli_def/ops/utils/pretty_renderer.py +543 -0
- cli_def/ops/utils/renderer.py +199 -0
- cli_def/ops/utils/renderer_ops.py +169 -0
- cli_def/parsers/__init__.py +10 -0
- cli_def/parsers/parser.py +101 -0
- cli_def/resources/cli_def.toml +126 -0
- cli_def/runtime/__init__.py +22 -0
- cli_def/runtime/_internal/__init__.py +2 -0
- cli_def/runtime/_internal/scan_runner.py +28 -0
- cli_def/runtime/context.py +12 -0
- cli_def/runtime/dispatcher.py +197 -0
- cli_def/runtime/event.py +35 -0
- cli_def/runtime/handler_support.py +75 -0
- cli_def/runtime/result.py +117 -0
- cli_def/runtime/runner.py +178 -0
- cli_def/runtime/scanner.py +143 -0
- cli_def/runtime/session.py +286 -0
- cli_def/runtime/utils.py +54 -0
- cli_def/script/__init__.py +2 -0
- cli_def/script/common.py +29 -0
- cli_def/script/handlers/__init__.py +14 -0
- cli_def/script/handlers/dummy.py +43 -0
- cli_def/script/handlers/dummy2.py +22 -0
- cli_def/script/handlers/dump.py +50 -0
- cli_def/script/handlers/repl.py +70 -0
- cli_def/script/handlers/run.py +70 -0
- cli_def/script/handlers/scan.py +179 -0
- cli_def/script/handlers/search.py +116 -0
- cli_def/script/main.py +32 -0
- cli_def-0.1.0.dist-info/METADATA +400 -0
- cli_def-0.1.0.dist-info/RECORD +58 -0
- cli_def-0.1.0.dist-info/WHEEL +5 -0
- cli_def-0.1.0.dist-info/entry_points.txt +2 -0
- cli_def-0.1.0.dist-info/licenses/LICENSE +21 -0
- cli_def-0.1.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
# cli_def/ops/utils/renderer.py
|
|
2
|
+
from __future__ import annotations
|
|
3
|
+
|
|
4
|
+
from typing import Protocol, Callable, Any, Sequence, Union
|
|
5
|
+
from dataclasses import dataclass, field
|
|
6
|
+
from enum import Enum, auto
|
|
7
|
+
|
|
8
|
+
import wcwidth
|
|
9
|
+
from wcwidth import wcswidth
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
@dataclass
|
|
13
|
+
class Style:
|
|
14
|
+
DEFAULT_GAP = " "
|
|
15
|
+
DEFAULT_V_SEPARATOR = "|"
|
|
16
|
+
DEFAULT_H_SEPARATOR = "-"
|
|
17
|
+
DEFAULT_TRUNCATE_SYMBOL = "…"
|
|
18
|
+
|
|
19
|
+
h_align: str|None = None # right, left, center
|
|
20
|
+
bold: bool|None = None
|
|
21
|
+
italic: bool|None = None
|
|
22
|
+
underline: bool|None = None
|
|
23
|
+
fg_color: str|None = None
|
|
24
|
+
bg_color: str|None = None
|
|
25
|
+
|
|
26
|
+
prefix: str|None = None
|
|
27
|
+
suffix: str|None = None
|
|
28
|
+
|
|
29
|
+
v_separator: str|None = None # default "|"
|
|
30
|
+
h_separator: str|None = None # default "-"
|
|
31
|
+
gap_to_next: str|None = None # default " "
|
|
32
|
+
|
|
33
|
+
wrap_width: int|None = None
|
|
34
|
+
truncate_width: int|None = None
|
|
35
|
+
truncate_symbol: str|None = None # default "…"
|
|
36
|
+
truncate_mode: str|None = None # begin, middle, end
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def make_display_text(self, text: str) -> str:
|
|
40
|
+
if self.prefix:
|
|
41
|
+
text = self.prefix + text
|
|
42
|
+
if self.suffix:
|
|
43
|
+
text = text + self.suffix
|
|
44
|
+
if self.truncate_width and len(text) > self.truncate_width:
|
|
45
|
+
symbol = self.truncate_symbol or self.DEFAULT_TRUNCATE_SYMBOL
|
|
46
|
+
if self.truncate_mode == "begin":
|
|
47
|
+
text = symbol + text[len(text) - self.truncate_width:]
|
|
48
|
+
elif self.truncate_mode == "middle":
|
|
49
|
+
len_take = self.truncate_width // 2
|
|
50
|
+
text = text[:len_take] + symbol + text[-len_take:]
|
|
51
|
+
else: # truncate_mode "end"
|
|
52
|
+
text = text[:self.truncate_width] + symbol
|
|
53
|
+
return text
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
def merge(self, other: Style|None) -> Style:
|
|
57
|
+
if other is None:
|
|
58
|
+
return self
|
|
59
|
+
return Style(
|
|
60
|
+
h_align= self.h_align or other.h_align,
|
|
61
|
+
bold= self.bold or other.bold,
|
|
62
|
+
italic= self.italic or other.italic,
|
|
63
|
+
underline= self.underline or other.underline,
|
|
64
|
+
fg_color= self.fg_color or other.fg_color,
|
|
65
|
+
bg_color= self.bg_color or other.bg_color,
|
|
66
|
+
prefix= self.prefix or other.prefix,
|
|
67
|
+
suffix= self.suffix or other.suffix,
|
|
68
|
+
v_separator= self.v_separator or other.v_separator,
|
|
69
|
+
h_separator= self.h_separator or other.h_separator,
|
|
70
|
+
gap_to_next= self.gap_to_next or other.gap_to_next,
|
|
71
|
+
wrap_width= self.wrap_width or other.wrap_width,
|
|
72
|
+
truncate_width= self.truncate_width or other.truncate_width,
|
|
73
|
+
truncate_symbol= self.truncate_symbol or other.truncate_symbol,
|
|
74
|
+
truncate_mode= self.truncate_mode or other.truncate_mode,
|
|
75
|
+
)
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
class ConditinalStyle:
|
|
79
|
+
cond: Callable[[Any], bool]
|
|
80
|
+
style: Style
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
class ColumnType(Enum):
|
|
84
|
+
NORMAL = auto()
|
|
85
|
+
INDEX1 = auto()
|
|
86
|
+
INDEX0 = auto()
|
|
87
|
+
SEPARATOR = auto()
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
@dataclass
|
|
91
|
+
class Column:
|
|
92
|
+
key: str
|
|
93
|
+
col_type: ColumnType = ColumnType.NORMAL
|
|
94
|
+
default_style: Style|None = None
|
|
95
|
+
|
|
96
|
+
def __post_init__(self):
|
|
97
|
+
if self.default_style is None:
|
|
98
|
+
self.default_style = Style()
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
ColumnOrKey = Union[
|
|
102
|
+
Column,
|
|
103
|
+
str
|
|
104
|
+
]
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
@dataclass
|
|
108
|
+
class Cell:
|
|
109
|
+
raw_value: Any
|
|
110
|
+
style: Style|None = None
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
CellOrValue = Union[
|
|
114
|
+
Cell,
|
|
115
|
+
Any
|
|
116
|
+
]
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
class RowType(Enum):
|
|
120
|
+
NORMAL = auto()
|
|
121
|
+
TITLE = auto()
|
|
122
|
+
SUBTITLE = auto()
|
|
123
|
+
HEADER = auto()
|
|
124
|
+
HEADER_KEY = auto()
|
|
125
|
+
FOOTER = auto()
|
|
126
|
+
SEPARATOR = auto()
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
@dataclass
|
|
130
|
+
class RowRecord:
|
|
131
|
+
cell_mapping: dict[str, CellOrValue]|None = None
|
|
132
|
+
row_type: RowType = RowType.NORMAL
|
|
133
|
+
default_style: Style|None = None
|
|
134
|
+
source: Any|None = None
|
|
135
|
+
|
|
136
|
+
def get_raw_value(self, key: str, default: Any|None = None) -> Any|None:
|
|
137
|
+
if self.cell_mapping is None:
|
|
138
|
+
return None
|
|
139
|
+
cellOrValue = self.cell_mapping.get(key, default)
|
|
140
|
+
if isinstance(cellOrValue, Cell):
|
|
141
|
+
return cellOrValue.raw_value
|
|
142
|
+
return cellOrValue
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+
@dataclass
|
|
146
|
+
class Table:
|
|
147
|
+
column_mapping: dict[str, Column]
|
|
148
|
+
row_records: list[RowRecord]
|
|
149
|
+
display_column_keys: list[str]
|
|
150
|
+
header_mapping: dict[str, CellOrValue]|None = None
|
|
151
|
+
footer_mapping: dict[str, CellOrValue]|None = None
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
@dataclass
|
|
155
|
+
class RendererContext:
|
|
156
|
+
extra: dict[str, Any] = field(default_factory=dict)
|
|
157
|
+
|
|
158
|
+
|
|
159
|
+
class RenderedObject(Protocol):
|
|
160
|
+
pass
|
|
161
|
+
|
|
162
|
+
|
|
163
|
+
class RendererProtocol(Protocol):
|
|
164
|
+
def render_value(self, raw_value: Any, style: Style|None = None) -> RenderedObject:
|
|
165
|
+
...
|
|
166
|
+
def render_text(self, display_text: str, style: Style|None = None) -> RenderedObject:
|
|
167
|
+
...
|
|
168
|
+
def render_table(self, table: Table) -> Sequence[RenderedObject]:
|
|
169
|
+
...
|
|
170
|
+
|
|
171
|
+
|
|
172
|
+
|
|
173
|
+
# abstract Renderer class
|
|
174
|
+
class BaseRenderer(RendererProtocol):
|
|
175
|
+
|
|
176
|
+
def __init__(self, *,
|
|
177
|
+
ctx: RendererContext|None = None,
|
|
178
|
+
value_formatter: Callable[[Any], str]|None = None,
|
|
179
|
+
):
|
|
180
|
+
self.renderer_context: RendererContext = ctx or RendererContext()
|
|
181
|
+
self.value_formatter: Callable[[Any], str]| None = value_formatter
|
|
182
|
+
|
|
183
|
+
|
|
184
|
+
def make_display_text(self, raw_value: Any, style: Style|None = None):
|
|
185
|
+
if self.value_formatter:
|
|
186
|
+
display_text = self.value_formatter(raw_value)
|
|
187
|
+
else:
|
|
188
|
+
display_text = str(raw_value)
|
|
189
|
+
if style:
|
|
190
|
+
return style.make_display_text(display_text)
|
|
191
|
+
return display_text
|
|
192
|
+
|
|
193
|
+
|
|
194
|
+
def calc_width(self, display_text: str) -> int:
|
|
195
|
+
return max(wcswidth(display_text), 0) # treat wcswidhth returns < 0
|
|
196
|
+
|
|
197
|
+
|
|
198
|
+
def wrap(self, display_text: str, width: int) -> list[str]:
|
|
199
|
+
return wcwidth.wrap(display_text, width)
|
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
# cli_def/ops/utils/renderer_ops.py
|
|
2
|
+
from __future__ import annotations
|
|
3
|
+
|
|
4
|
+
from typing import Mapping, Any, Sequence, Callable, Iterable, Union
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
from .renderer import (
|
|
8
|
+
Style,
|
|
9
|
+
Column,
|
|
10
|
+
ColumnType,
|
|
11
|
+
RowType,
|
|
12
|
+
RowRecord,
|
|
13
|
+
Table,
|
|
14
|
+
CellOrValue,
|
|
15
|
+
ColumnOrKey,
|
|
16
|
+
)
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
class TableBuilder:
|
|
20
|
+
|
|
21
|
+
@classmethod
|
|
22
|
+
def from_row_records(
|
|
23
|
+
cls,
|
|
24
|
+
columns: Iterable[ColumnOrKey],
|
|
25
|
+
row_records: Iterable[RowRecord],
|
|
26
|
+
*,
|
|
27
|
+
display_column_keys: Iterable[str]|None = None,
|
|
28
|
+
headers: Iterable[CellOrValue]|None = None,
|
|
29
|
+
footers: Iterable[CellOrValue]|None = None,
|
|
30
|
+
) -> Table:
|
|
31
|
+
|
|
32
|
+
columns = cls.normalize_columns(columns)
|
|
33
|
+
column_mapping = {
|
|
34
|
+
col.key: col for col in columns
|
|
35
|
+
}
|
|
36
|
+
if display_column_keys is None:
|
|
37
|
+
display_column_keys = [col.key for col in columns]
|
|
38
|
+
else:
|
|
39
|
+
display_column_keys = list(display_column_keys)
|
|
40
|
+
|
|
41
|
+
if headers:
|
|
42
|
+
header_mapping = {
|
|
43
|
+
col.key: v for col, v in zip(columns, headers)
|
|
44
|
+
}
|
|
45
|
+
else:
|
|
46
|
+
header_mapping = None
|
|
47
|
+
|
|
48
|
+
if footers:
|
|
49
|
+
footer_mapping = {
|
|
50
|
+
col.key: v for col, v in zip(columns, footers)
|
|
51
|
+
}
|
|
52
|
+
else:
|
|
53
|
+
footer_mapping = None
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
result = Table(
|
|
57
|
+
column_mapping=column_mapping,
|
|
58
|
+
row_records=list(row_records),
|
|
59
|
+
display_column_keys=display_column_keys,
|
|
60
|
+
header_mapping=header_mapping,
|
|
61
|
+
footer_mapping=footer_mapping,
|
|
62
|
+
)
|
|
63
|
+
return result
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
@classmethod
|
|
67
|
+
def from_columns_and_values(
|
|
68
|
+
cls,
|
|
69
|
+
columns: Iterable[ColumnOrKey],
|
|
70
|
+
valuess: Iterable[Iterable[CellOrValue]],
|
|
71
|
+
*,
|
|
72
|
+
display_column_keys: Iterable[str]|None = None,
|
|
73
|
+
headers: Iterable[CellOrValue]|None = None,
|
|
74
|
+
footers: Iterable[CellOrValue]|None = None,
|
|
75
|
+
) -> Table:
|
|
76
|
+
columns = cls.normalize_columns(columns)
|
|
77
|
+
column_mapping = {
|
|
78
|
+
col.key: col for col in columns
|
|
79
|
+
}
|
|
80
|
+
if display_column_keys is None:
|
|
81
|
+
display_column_keys = [col.key for col in columns]
|
|
82
|
+
else:
|
|
83
|
+
display_column_keys = list(display_column_keys)
|
|
84
|
+
|
|
85
|
+
if headers:
|
|
86
|
+
header_mapping = {
|
|
87
|
+
col.key: v for col, v in zip(columns, headers)
|
|
88
|
+
}
|
|
89
|
+
else:
|
|
90
|
+
header_mapping = None
|
|
91
|
+
|
|
92
|
+
if footers:
|
|
93
|
+
footer_mapping = {
|
|
94
|
+
col.key: v for col, v in zip(columns, footers)
|
|
95
|
+
}
|
|
96
|
+
else:
|
|
97
|
+
footer_mapping = None
|
|
98
|
+
|
|
99
|
+
normal_column_keys = [col.key for col in columns if col.col_type == ColumnType.NORMAL]
|
|
100
|
+
rows: list[RowRecord] = []
|
|
101
|
+
for values in valuess:
|
|
102
|
+
row = cls.make_row(normal_column_keys, values)
|
|
103
|
+
rows.append(row)
|
|
104
|
+
|
|
105
|
+
result = Table(
|
|
106
|
+
column_mapping=column_mapping,
|
|
107
|
+
row_records=rows,
|
|
108
|
+
display_column_keys=display_column_keys,
|
|
109
|
+
header_mapping=header_mapping,
|
|
110
|
+
footer_mapping=footer_mapping,
|
|
111
|
+
)
|
|
112
|
+
return result
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
@classmethod
|
|
116
|
+
def normalize_columns(cls, columns: Iterable[ColumnOrKey]) -> Sequence[Column]:
|
|
117
|
+
normalized: list[Column] = []
|
|
118
|
+
for col in columns:
|
|
119
|
+
if isinstance(col, Column):
|
|
120
|
+
normalized.append(col)
|
|
121
|
+
else:
|
|
122
|
+
normalized.append(
|
|
123
|
+
cls.make_column(col)
|
|
124
|
+
)
|
|
125
|
+
return normalized
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
@classmethod
|
|
129
|
+
def make_column(cls, col_key: str, default_style: Style|None = None) -> Column:
|
|
130
|
+
if col_key == "#":
|
|
131
|
+
default_style = Style(h_align="right").merge(default_style)
|
|
132
|
+
return Column(col_key, col_type=ColumnType.INDEX1, default_style=default_style)
|
|
133
|
+
if col_key == "#0":
|
|
134
|
+
default_style = Style(h_align="right").merge(default_style)
|
|
135
|
+
return Column(col_key, col_type=ColumnType.INDEX0, default_style=default_style)
|
|
136
|
+
if col_key == "|":
|
|
137
|
+
return Column(col_key, col_type=ColumnType.SEPARATOR, default_style=default_style)
|
|
138
|
+
return Column(col_key, col_type=ColumnType.NORMAL, default_style=default_style)
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
@classmethod
|
|
142
|
+
def make_row(
|
|
143
|
+
cls,
|
|
144
|
+
col_keys: Iterable[str],
|
|
145
|
+
values: Iterable[CellOrValue],
|
|
146
|
+
*,
|
|
147
|
+
fillValue: CellOrValue|None = None,
|
|
148
|
+
default_style: Style|None = None,
|
|
149
|
+
) -> RowRecord:
|
|
150
|
+
|
|
151
|
+
col_keys = list(col_keys)
|
|
152
|
+
values = list(values)
|
|
153
|
+
|
|
154
|
+
assert len(values) > 0
|
|
155
|
+
if values[0] == "---":
|
|
156
|
+
return RowRecord(row_type=RowType.SEPARATOR, default_style=default_style)
|
|
157
|
+
|
|
158
|
+
cell_mapping: dict[str, CellOrValue] = {}
|
|
159
|
+
for i, col_key in enumerate(col_keys):
|
|
160
|
+
if i < len(values):
|
|
161
|
+
cellOrValue = values[i]
|
|
162
|
+
else:
|
|
163
|
+
cellOrValue = fillValue
|
|
164
|
+
cell_mapping[col_key] = cellOrValue
|
|
165
|
+
return RowRecord(
|
|
166
|
+
cell_mapping=cell_mapping,
|
|
167
|
+
default_style=default_style
|
|
168
|
+
)
|
|
169
|
+
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
# cli_def/parsers/parser.py
|
|
2
|
+
from __future__ import annotations
|
|
3
|
+
|
|
4
|
+
from typing import Mapping, Any, Iterable
|
|
5
|
+
import tomllib
|
|
6
|
+
from pathlib import Path
|
|
7
|
+
import importlib
|
|
8
|
+
import logging
|
|
9
|
+
|
|
10
|
+
from ..models import (
|
|
11
|
+
CliDef,
|
|
12
|
+
CommandDef,
|
|
13
|
+
ArgumentDef,
|
|
14
|
+
MultDef,
|
|
15
|
+
)
|
|
16
|
+
|
|
17
|
+
# --------------------------------------------------------------------------------
|
|
18
|
+
# CliDefParser class
|
|
19
|
+
# --------------------------------------------------------------------------------
|
|
20
|
+
class CliDefParser:
|
|
21
|
+
|
|
22
|
+
def parse_from_toml(self, path_to_toml_file: str|Path) -> CliDef | None:
|
|
23
|
+
mapping = {}
|
|
24
|
+
with open(path_to_toml_file, "rb") as file:
|
|
25
|
+
mapping = tomllib.load(file)
|
|
26
|
+
if "cli" in mapping:
|
|
27
|
+
return self.parse_from_mapping(mapping)
|
|
28
|
+
return None
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def parse_from_toml_text(self, toml_text: str) -> CliDef | None:
|
|
32
|
+
mapping = {}
|
|
33
|
+
mapping.update(tomllib.loads(toml_text))
|
|
34
|
+
if "cli" in mapping:
|
|
35
|
+
return self.parse_from_mapping(mapping)
|
|
36
|
+
return None
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def parse_from_mapping(self, mapping: Mapping[str, Any]) -> CliDef | None:
|
|
40
|
+
cliDef = CliDef()
|
|
41
|
+
argDefs:Iterable[ArgumentDef] = []
|
|
42
|
+
commandDefs:Iterable[CommandDef] = []
|
|
43
|
+
for key, value in mapping.get("cli", {}).items():
|
|
44
|
+
if key == "args":
|
|
45
|
+
argDefs = self.parse_cli_argument_defs(key, value)
|
|
46
|
+
elif hasattr(cliDef, key):
|
|
47
|
+
setattr(cliDef, key, value)
|
|
48
|
+
elif isinstance(value, Mapping):
|
|
49
|
+
commandDef = self.parse_cli_command_def(key, value)
|
|
50
|
+
if commandDef:
|
|
51
|
+
commandDefs.append(commandDef)
|
|
52
|
+
else:
|
|
53
|
+
cliDef.extra_defs[key] = value
|
|
54
|
+
|
|
55
|
+
for argDef in argDefs:
|
|
56
|
+
argDef.parent = cliDef
|
|
57
|
+
cliDef.arguments = list(argDefs)
|
|
58
|
+
for cmdDef in commandDefs:
|
|
59
|
+
cmdDef.parent = cliDef
|
|
60
|
+
cliDef.commands = commandDefs
|
|
61
|
+
return cliDef
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def parse_cli_argument_defs(self, key: str, mappings: Iterable[Mapping[str, Any]]) -> Iterable[ArgumentDef]:
|
|
65
|
+
argDefs = []
|
|
66
|
+
for mapping in mappings:
|
|
67
|
+
argDef = ArgumentDef.from_mapping(mapping)
|
|
68
|
+
if argDef.key:
|
|
69
|
+
argDefs.append(argDef)
|
|
70
|
+
else:
|
|
71
|
+
logging.warning(f"key not found {argDefs}")
|
|
72
|
+
return argDefs
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
def parse_cli_command_def(self, key: str, mapping: Mapping[str, Any]) -> CommandDef:
|
|
76
|
+
commandDef = CommandDef(
|
|
77
|
+
key=key,
|
|
78
|
+
is_template=key.startswith("_"), # special rule
|
|
79
|
+
)
|
|
80
|
+
for key, value in mapping.items():
|
|
81
|
+
argDefs = []
|
|
82
|
+
if key == "args":
|
|
83
|
+
argDefs = self.parse_cli_argument_defs(key, value)
|
|
84
|
+
if argDefs:
|
|
85
|
+
for argDef in argDefs:
|
|
86
|
+
argDef.parent = commandDef
|
|
87
|
+
commandDef.arguments = list(argDefs)
|
|
88
|
+
elif hasattr(commandDef, key):
|
|
89
|
+
setattr(commandDef, key, value)
|
|
90
|
+
elif isinstance(value, Mapping):
|
|
91
|
+
# parse as subcommands
|
|
92
|
+
if commandDef.subcommands is None:
|
|
93
|
+
commandDef.subcommands = []
|
|
94
|
+
subcommandDef = self.parse_cli_command_def(key, value)
|
|
95
|
+
subcommandDef.parent = commandDef
|
|
96
|
+
commandDef.subcommands.append(subcommandDef)
|
|
97
|
+
else:
|
|
98
|
+
commandDef.extra_defs[key] = value
|
|
99
|
+
|
|
100
|
+
return commandDef
|
|
101
|
+
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
# cli_def/resource/cli_def.toml
|
|
2
|
+
|
|
3
|
+
[cli]
|
|
4
|
+
key = "cli-def"
|
|
5
|
+
help = "CliDef Script"
|
|
6
|
+
|
|
7
|
+
[cli._early] # for early parsing
|
|
8
|
+
args = [
|
|
9
|
+
{key="verbose", option="--verbose", aliases=["-v"], is_flag=true, default=false},
|
|
10
|
+
{key="debug", option="--debug", aliases=["-d"], is_flag=true, default=false},
|
|
11
|
+
{key="backend", option="--backend", type="str", choices=["argparse", "click"]},
|
|
12
|
+
]
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
# --------------------------------------------------------------------------------
|
|
16
|
+
# demo command
|
|
17
|
+
# --------------------------------------------------------------------------------
|
|
18
|
+
[cli.demo]
|
|
19
|
+
help = "Demo"
|
|
20
|
+
args = [
|
|
21
|
+
{key="profile", mult="?", type="str"}
|
|
22
|
+
]
|
|
23
|
+
entrypoint = "cli_def.demo.handlers:run_demo"
|
|
24
|
+
|
|
25
|
+
# --------------------------------------------------------------------------------
|
|
26
|
+
# dump command
|
|
27
|
+
# --------------------------------------------------------------------------------
|
|
28
|
+
[cli.dump]
|
|
29
|
+
help = "Dump cli-def AST"
|
|
30
|
+
args = [
|
|
31
|
+
{key="cli_def_file", mult="1", type="str"},
|
|
32
|
+
#{key="check_entrypoints", option="--check", is_flag=true, default=false},
|
|
33
|
+
]
|
|
34
|
+
entrypoint = "cli_def.script.handlers.dump:run_dump"
|
|
35
|
+
|
|
36
|
+
# --------------------------------------------------------------------------------
|
|
37
|
+
# run command
|
|
38
|
+
# --------------------------------------------------------------------------------
|
|
39
|
+
[cli.run]
|
|
40
|
+
help = "Run CLI"
|
|
41
|
+
args = [
|
|
42
|
+
{key="cli_def_file", mult="1", type="str"},
|
|
43
|
+
{key="no_ctx_propagate", option="--no-ctx-propagate", is_flag=true, default=false},
|
|
44
|
+
]
|
|
45
|
+
entrypoint = "cli_def.script.handlers.run:run_run"
|
|
46
|
+
|
|
47
|
+
# --------------------------------------------------------------------------------
|
|
48
|
+
# repl command
|
|
49
|
+
# --------------------------------------------------------------------------------
|
|
50
|
+
[cli.repl]
|
|
51
|
+
help = "Interactive Mode"
|
|
52
|
+
args = [
|
|
53
|
+
{key="cli_def_file", option="--file", mult="?", type="str"},
|
|
54
|
+
{key="no_ctx_propagate", option="--no-ctx-propagate", is_flag=true, default=false},
|
|
55
|
+
]
|
|
56
|
+
entrypoint = "cli_def.script.handlers.repl:run_repl"
|
|
57
|
+
|
|
58
|
+
# --------------------------------------------------------------------------------
|
|
59
|
+
# scan command
|
|
60
|
+
# --------------------------------------------------------------------------------
|
|
61
|
+
[cli.scan]
|
|
62
|
+
help = "Scan late-binding handlers"
|
|
63
|
+
entrypoint = "cli_def.script.handlers.scan:run_scan"
|
|
64
|
+
|
|
65
|
+
[[cli.scan.args]]
|
|
66
|
+
key = "package"
|
|
67
|
+
mult = "?"
|
|
68
|
+
type = "str"
|
|
69
|
+
|
|
70
|
+
[[cli.scan.args]]
|
|
71
|
+
key = "show_early"
|
|
72
|
+
option = "--show-early"
|
|
73
|
+
is_flag = true
|
|
74
|
+
default = false
|
|
75
|
+
help = "Show not only late-bindings but early-bindings"
|
|
76
|
+
|
|
77
|
+
[[cli.scan.args]]
|
|
78
|
+
key = "recursive"
|
|
79
|
+
option = "--recursive"
|
|
80
|
+
aliases = ["-R"]
|
|
81
|
+
is_flag = true
|
|
82
|
+
default = false
|
|
83
|
+
help = "Recursive scan in the specified package"
|
|
84
|
+
|
|
85
|
+
[[cli.scan.args]]
|
|
86
|
+
key = "no_subprocess"
|
|
87
|
+
option = "--no_subprocess"
|
|
88
|
+
aliases = ["-N"]
|
|
89
|
+
is_flag = true
|
|
90
|
+
default = false
|
|
91
|
+
help = "Package loding will be done in current process instead of subprocess"
|
|
92
|
+
|
|
93
|
+
[[cli.scan.args]]
|
|
94
|
+
key = "check_toml_path"
|
|
95
|
+
option = "--check"
|
|
96
|
+
mult = "1"
|
|
97
|
+
type = "str"
|
|
98
|
+
help = "Check whether each handler is bound to the entrypoint or not in TOML"
|
|
99
|
+
|
|
100
|
+
# --------------------------------------------------------------------------------
|
|
101
|
+
# search command
|
|
102
|
+
# --------------------------------------------------------------------------------
|
|
103
|
+
[cli.search]
|
|
104
|
+
help = "Search cli-def TOMLs in specified directory"
|
|
105
|
+
entrypoint = "cli_def.script.handlers.search:run_search"
|
|
106
|
+
|
|
107
|
+
[[cli.search.args]]
|
|
108
|
+
key = "directory_to_search"
|
|
109
|
+
mult = "1"
|
|
110
|
+
type = "str"
|
|
111
|
+
|
|
112
|
+
[[cli.search.args]]
|
|
113
|
+
key = "recursive"
|
|
114
|
+
option = "--recursive"
|
|
115
|
+
aliases = ["-R"]
|
|
116
|
+
is_flag = true
|
|
117
|
+
default = false
|
|
118
|
+
help = "Recursive search in the specified directory"
|
|
119
|
+
|
|
120
|
+
[[cli.search.args]]
|
|
121
|
+
key = "dump_all"
|
|
122
|
+
option = "--dump-all"
|
|
123
|
+
aliases = ["-D"]
|
|
124
|
+
is_flag = true
|
|
125
|
+
default = false
|
|
126
|
+
help = "dump each cli-def"
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
# cli_def/runtime/init.py
|
|
2
|
+
# package marker
|
|
3
|
+
|
|
4
|
+
from .runner import CliRunner
|
|
5
|
+
from .dispatcher import CliDispatcher
|
|
6
|
+
from .event import CliEvent
|
|
7
|
+
from .session import CliSession
|
|
8
|
+
from .context import CliRuntimeContext
|
|
9
|
+
from .handler_support import cli_def_handler # decorator support
|
|
10
|
+
from .result import CliHandlerResult, CliResult, CliHandlerResultKind
|
|
11
|
+
|
|
12
|
+
__all__ = [
|
|
13
|
+
"CliRunner",
|
|
14
|
+
"CliDispatcher",
|
|
15
|
+
"CliEvent",
|
|
16
|
+
"CliSession",
|
|
17
|
+
"CliRuntimeContext",
|
|
18
|
+
"cli_def_handler",
|
|
19
|
+
"CliHandlerResult",
|
|
20
|
+
"CliHandlerResultKind",
|
|
21
|
+
"CliResult",
|
|
22
|
+
]
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
# cli_def/runtime/_internal/scan_runner.py
|
|
2
|
+
from __future__ import annotations
|
|
3
|
+
import sys
|
|
4
|
+
import json
|
|
5
|
+
|
|
6
|
+
from ..scanner import CliHandlerScanner
|
|
7
|
+
|
|
8
|
+
# --------------------------------------------------------------------------------
|
|
9
|
+
# scanner support prog exected with subprocess
|
|
10
|
+
# --------------------------------------------------------------------------------
|
|
11
|
+
def main():
|
|
12
|
+
package = sys.argv[1]
|
|
13
|
+
recursive = "--recursive" in sys.argv
|
|
14
|
+
|
|
15
|
+
scanner = CliHandlerScanner()
|
|
16
|
+
result = scanner.scan(
|
|
17
|
+
package,
|
|
18
|
+
no_subprocess=True,
|
|
19
|
+
recursive=recursive,
|
|
20
|
+
)
|
|
21
|
+
if result:
|
|
22
|
+
digest = result.to_digest()
|
|
23
|
+
print(json.dumps(digest))
|
|
24
|
+
return 0
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
if __name__ == "__main__":
|
|
28
|
+
main()
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
# cli_def/runtime/context.py
|
|
2
|
+
from __future__ import annotations
|
|
3
|
+
from typing import Optional, Any
|
|
4
|
+
from dataclasses import dataclass, field
|
|
5
|
+
|
|
6
|
+
@dataclass
|
|
7
|
+
class CliRuntimeContext:
|
|
8
|
+
debug: bool = False
|
|
9
|
+
verbose: bool = False
|
|
10
|
+
lang: str = "en"
|
|
11
|
+
backend: Optional[str] = None # "argparse" or "click"
|
|
12
|
+
extra: dict[str, Any] = field(default_factory=dict)
|