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.
Files changed (58) hide show
  1. cli_def/__init__.py +16 -0
  2. cli_def/backend/__init__.py +2 -0
  3. cli_def/backend/argparse/__init__.py +10 -0
  4. cli_def/backend/argparse/argparse_builder.py +238 -0
  5. cli_def/backend/click/__init__.py +13 -0
  6. cli_def/backend/click/click_binder.py +46 -0
  7. cli_def/backend/click/click_builder.py +182 -0
  8. cli_def/backend/protocols.py +22 -0
  9. cli_def/demo/__init__.py +2 -0
  10. cli_def/demo/handlers.py +125 -0
  11. cli_def/demo/profiles/advanced.toml +39 -0
  12. cli_def/demo/profiles/beginner.toml +21 -0
  13. cli_def/models/__init__.py +17 -0
  14. cli_def/models/abstract_node.py +87 -0
  15. cli_def/models/argument_def.py +84 -0
  16. cli_def/models/cli_def.py +28 -0
  17. cli_def/models/command_def.py +49 -0
  18. cli_def/models/mult_def.py +81 -0
  19. cli_def/models/schema/cli_schema.py +24 -0
  20. cli_def/ops/__init__.py +16 -0
  21. cli_def/ops/dumper.py +216 -0
  22. cli_def/ops/loader.py +56 -0
  23. cli_def/ops/utils/__init__.py +3 -0
  24. cli_def/ops/utils/pretty_renderer.py +543 -0
  25. cli_def/ops/utils/renderer.py +199 -0
  26. cli_def/ops/utils/renderer_ops.py +169 -0
  27. cli_def/parsers/__init__.py +10 -0
  28. cli_def/parsers/parser.py +101 -0
  29. cli_def/resources/cli_def.toml +126 -0
  30. cli_def/runtime/__init__.py +22 -0
  31. cli_def/runtime/_internal/__init__.py +2 -0
  32. cli_def/runtime/_internal/scan_runner.py +28 -0
  33. cli_def/runtime/context.py +12 -0
  34. cli_def/runtime/dispatcher.py +197 -0
  35. cli_def/runtime/event.py +35 -0
  36. cli_def/runtime/handler_support.py +75 -0
  37. cli_def/runtime/result.py +117 -0
  38. cli_def/runtime/runner.py +178 -0
  39. cli_def/runtime/scanner.py +143 -0
  40. cli_def/runtime/session.py +286 -0
  41. cli_def/runtime/utils.py +54 -0
  42. cli_def/script/__init__.py +2 -0
  43. cli_def/script/common.py +29 -0
  44. cli_def/script/handlers/__init__.py +14 -0
  45. cli_def/script/handlers/dummy.py +43 -0
  46. cli_def/script/handlers/dummy2.py +22 -0
  47. cli_def/script/handlers/dump.py +50 -0
  48. cli_def/script/handlers/repl.py +70 -0
  49. cli_def/script/handlers/run.py +70 -0
  50. cli_def/script/handlers/scan.py +179 -0
  51. cli_def/script/handlers/search.py +116 -0
  52. cli_def/script/main.py +32 -0
  53. cli_def-0.1.0.dist-info/METADATA +400 -0
  54. cli_def-0.1.0.dist-info/RECORD +58 -0
  55. cli_def-0.1.0.dist-info/WHEEL +5 -0
  56. cli_def-0.1.0.dist-info/entry_points.txt +2 -0
  57. cli_def-0.1.0.dist-info/licenses/LICENSE +21 -0
  58. cli_def-0.1.0.dist-info/top_level.txt +1 -0
@@ -0,0 +1,70 @@
1
+ # cli_def/script/handlers/repl.py
2
+ from __future__ import annotations
3
+ from typing import Sequence, Any
4
+ import logging
5
+ import copy
6
+
7
+
8
+ from ...runtime import (
9
+ CliEvent,
10
+ CliSession,
11
+ CliHandlerResult,
12
+ )
13
+ from ...ops import (
14
+ load_cli_def_path,
15
+ CliDefDumper,
16
+ )
17
+
18
+ from ...runtime import cli_def_handler, CliHandlerResult
19
+
20
+ from ..common import load_builtin_cli_def, print_handler
21
+
22
+
23
+
24
+ # --------------------------------------------------------------------------------
25
+ #
26
+ # repl command handler
27
+ #
28
+ # --------------------------------------------------------------------------------
29
+ @cli_def_handler("/cli-def/repl", description="builtin repl command handler", late_binding=True)
30
+ def run_repl(event: CliEvent):
31
+
32
+ logging.info("=== repl command ===")
33
+
34
+ cli_def_file = event.params.get("cli_def_file")
35
+ if cli_def_file is None:
36
+ logging.info("load builtin cli_def")
37
+ cli_def = load_builtin_cli_def()
38
+ else:
39
+ logging.info(f"try to load: {cli_def_file} ")
40
+ cli_def = load_cli_def_path(cli_def_file)
41
+ if cli_def is None:
42
+ return CliHandlerResult.make_error(
43
+ event,
44
+ f"cli_def could not load: {cli_def_file or "builtin"}",
45
+ )
46
+
47
+ assert event.ctx is not None
48
+ if event.ctx.debug or event.ctx.verbose:
49
+ CliDefDumper.dump_pretty(cli_def)
50
+ no_ctx_propagate = event.params.get("no_ctx_propagate")
51
+ if not no_ctx_propagate:
52
+ child_ctx = copy.deepcopy(event.ctx)
53
+ else:
54
+ child_ctx = None
55
+
56
+ print("Type 'help' to list commands, 'exit' to exit")
57
+ session = CliSession(
58
+ cli_def,
59
+ fallback_handler=print_handler,
60
+ cli_def_file=cli_def_file,
61
+ ctx=child_ctx,
62
+ )
63
+ session.repl()
64
+
65
+ return CliHandlerResult.make_result(
66
+ event,
67
+ "run_repl",
68
+ data=session.result_store.all_data()
69
+ )
70
+
@@ -0,0 +1,70 @@
1
+ # cli_def/script/handlers/run.py
2
+ from __future__ import annotations
3
+ from typing import Sequence, Any
4
+ import logging
5
+ import copy
6
+
7
+
8
+ from ...runtime import (
9
+ CliEvent,
10
+ CliRunner,
11
+ CliHandlerResult,
12
+ )
13
+ from ...ops import (
14
+ load_cli_def_path,
15
+ CliDefDumper,
16
+ )
17
+
18
+ from ...runtime import cli_def_handler, CliHandlerResult
19
+
20
+ from ..common import load_builtin_cli_def, print_handler
21
+
22
+
23
+
24
+
25
+ # --------------------------------------------------------------------------------
26
+ #
27
+ # run command handler
28
+ #
29
+ # --------------------------------------------------------------------------------
30
+ @cli_def_handler("/cli-def/run", description="builin run command handler", late_binding=True)
31
+ def run_run(event: CliEvent):
32
+ logging.info("=== run command ===")
33
+ cli_def_file = event.params.get("cli_def_file")
34
+ assert cli_def_file is not None
35
+ cli_def = load_cli_def_path(cli_def_file)
36
+ if cli_def is None:
37
+ return CliHandlerResult.make_error(event, f"cli_def file could not load: {cli_def_file}")
38
+ if cli_def is None:
39
+ return CliHandlerResult.make_error(
40
+ event,
41
+ f"cli_def could not load: {cli_def_file}",
42
+ )
43
+
44
+ assert event.ctx is not None
45
+ if event.ctx.debug or event.ctx.verbose:
46
+ CliDefDumper.dump_pretty(cli_def)
47
+
48
+ no_ctx_propagate = event.params.get("no_ctx_propagate")
49
+ if not no_ctx_propagate:
50
+ child_ctx = copy.deepcopy(event.ctx)
51
+ else:
52
+ child_ctx = None
53
+
54
+ print(f"[run] forwarding args: {event.extra_args}, no_ctx_propagate: {no_ctx_propagate}")
55
+ runner = CliRunner(
56
+ cli_def,
57
+ fallback_handler=print_handler,
58
+ default_ctx=child_ctx,
59
+ )
60
+
61
+ result = runner.run(
62
+ argv=event.extra_args if event.extra_args else [],
63
+ )
64
+
65
+ return CliHandlerResult.make_result(
66
+ event,
67
+ "run_run",
68
+ data=result.all_data(),
69
+ )
70
+
@@ -0,0 +1,179 @@
1
+ # cli_def/script/handlers/scan.py
2
+ from __future__ import annotations
3
+ from typing import Sequence, Any, Mapping
4
+ import logging
5
+ import copy
6
+ from pathlib import Path
7
+
8
+ from importlib import resources
9
+
10
+ from ...models import (
11
+ CliDef,
12
+ CommandDef,
13
+ )
14
+ from ...parsers import CliDefParser
15
+ from ...runtime import (
16
+ CliEvent,
17
+ CliHandlerResult,
18
+ )
19
+ from ...ops import (
20
+ load_cli_def_path,
21
+ CliDefDumper,
22
+ )
23
+ from ...ops.utils.renderer import (
24
+ Style,
25
+ Column,
26
+ ColumnType,
27
+ RowRecord,
28
+ RowType,
29
+ Cell,
30
+ Table,
31
+ )
32
+ from ...ops.utils.pretty_renderer import PrettyRenderer
33
+ from ...ops.utils.renderer_ops import TableBuilder
34
+
35
+ from ...runtime import cli_def_handler, CliHandlerResult
36
+ from ...runtime.scanner import CliHandlerScanner
37
+
38
+
39
+
40
+ # --------------------------------------------------------------------------------
41
+ #
42
+ # scan command handler
43
+ #
44
+ # --------------------------------------------------------------------------------
45
+ @cli_def_handler("/cli-def/scan", description="builtin scan command handler", late_binding=True)
46
+ def run_scan(event: CliEvent):
47
+ logging.info("=== scan command ===")
48
+ package_name = event.params.get("package") or __package__
49
+ no_subprocess = event.params.get("no_subprocess")
50
+ recursive = event.params.get("recursive")
51
+ check_toml = event.params.get("check_toml_path")
52
+
53
+ print(f"package_name: {package_name}")
54
+ assert package_name is not None
55
+
56
+ cli_def = None
57
+ if check_toml:
58
+ cli_def = load_cli_def_path(check_toml)
59
+ if cli_def is None:
60
+ print(f"[WARNING] cli_def file {check_toml} could not load, so will not check.")
61
+
62
+
63
+ scanner = CliHandlerScanner()
64
+ result = scanner.scan(
65
+ package_name,
66
+ no_subprocess=no_subprocess is True,
67
+ recursive=recursive is True,
68
+ )
69
+
70
+ if result is None:
71
+ return CliHandlerResult.make_error(
72
+ event,
73
+ f"package_name not found: {package_name}"
74
+ )
75
+
76
+ digest = result.to_digest()
77
+
78
+ if digest is None or len(digest["catalog_digest"]) == 0:
79
+ print("No handlers found")
80
+ return CliHandlerResult.make_result(event, "No handlers found", digest)
81
+
82
+ assert event.ctx is not None
83
+ if event.ctx.verbose:
84
+ print("=== scan coverage ===")
85
+ for k, v in digest["scan_coverage"].items():
86
+ print(f"{k} : {v}")
87
+ print("=====================")
88
+
89
+ specified_entry_point_set = {
90
+ n.entrypoint for n in cli_def.iter_all_nodes() if isinstance(n, CommandDef) and n.entrypoint
91
+ } if cli_def else None
92
+
93
+ # catalog = digest["catalog_digest"]
94
+ # for key, lst in catalog.items():
95
+ # print(f"{key}:")
96
+ # indent = " "
97
+ # for meta_digest in lst:
98
+ # late_binding = meta_digest.get("late_binding")
99
+ # if not show_all and not late_binding:
100
+ # continue
101
+ # entrypoint = meta_digest.get("entrypoint")
102
+ # desc = meta_digest.get("description")
103
+ # bound = entrypoint in specified_entry_point_set
104
+ # print(indent + f"{"*" if bound else " "}{entrypoint}, desc={desc}, late_binding={late_binding}")
105
+
106
+ pretty_report(event, digest, specified_entry_point_set)
107
+
108
+ return CliHandlerResult.make_result(
109
+ event,
110
+ "run_scan",
111
+ data = digest["catalog_digest"]
112
+ )
113
+
114
+
115
+ def pretty_report(event: CliEvent, digest: Mapping[str, Any], entrypoint_set: set[str]|None):
116
+ show_early = event.params.get("show_early")
117
+ catalog: Mapping[str, Any] = digest["catalog_digest"]
118
+
119
+ check_enabled = entrypoint_set is not None
120
+ entrypoint_set = entrypoint_set or set()
121
+
122
+ # 1) prepare columns
123
+ columns = [
124
+ Column("#", col_type=ColumnType.INDEX1, default_style=Style(h_align="right")),
125
+ Column("defpath"),
126
+ Column("entrypoint"),
127
+ Column("desc"),
128
+ ]
129
+ if check_enabled:
130
+ columns.append(
131
+ Column("is_bound")
132
+ )
133
+ if show_early:
134
+ columns.append(
135
+ Column("binding_type"),
136
+ )
137
+
138
+ column_mapping = {col.key: col for col in columns}
139
+ display_column_keys = list(column_mapping.keys())
140
+
141
+ # 2) prepare rows
142
+ row_records: list[RowRecord] = []
143
+ row_records.append(RowRecord(row_type=RowType.HEADER_KEY))
144
+ row_records.append(RowRecord(row_type=RowType.SEPARATOR))
145
+
146
+ for k in sorted(catalog.keys()):
147
+ lst = catalog[k]
148
+ for meta_digest in lst:
149
+ is_late_binding = meta_digest.get("late_binding")
150
+ if not show_early and not is_late_binding:
151
+ continue
152
+ binding_type = "late" if is_late_binding else "early"
153
+ entrypoint = meta_digest.get("entrypoint")
154
+ is_bound = entrypoint in entrypoint_set
155
+ row_style = Style(fg_color="green") if is_bound else None
156
+ row = RowRecord(
157
+ cell_mapping={
158
+ "defpath": k,
159
+ "entrypoint": entrypoint,
160
+ "desc": meta_digest.get("description"),
161
+ "is_bound": is_bound,
162
+ "binding_type": binding_type,
163
+ }, default_style=row_style
164
+ )
165
+ row_records.append(row)
166
+ row_records.append(RowRecord(row_type=RowType.SEPARATOR))
167
+
168
+ table = Table(
169
+ column_mapping=column_mapping,
170
+ row_records=row_records,
171
+ display_column_keys=display_column_keys,
172
+ )
173
+
174
+ renderer = PrettyRenderer()
175
+ for rendered_line in renderer.render_table(table):
176
+ print(rendered_line)
177
+ #print(renderer.render_text(f"[{len(catalog)} items]", Style()))
178
+
179
+
@@ -0,0 +1,116 @@
1
+ # cli_def/script/handlers/search.py
2
+ from __future__ import annotations
3
+ from typing import Sequence, Any, Mapping
4
+ import logging
5
+ from pathlib import Path
6
+
7
+
8
+ from ...models import (
9
+ CliDef,
10
+ )
11
+ from ...runtime import (
12
+ CliEvent,
13
+ CliHandlerResult,
14
+ )
15
+ from ...ops import (
16
+ load_cli_def_path,
17
+ CliDefDumper,
18
+ )
19
+ from ...ops.utils.renderer import (
20
+ Style,
21
+ Column,
22
+ ColumnType,
23
+ RowRecord,
24
+ RowType,
25
+ Cell,
26
+ Table,
27
+ )
28
+ from ...ops.utils.pretty_renderer import PrettyRenderer
29
+
30
+ from ...runtime import cli_def_handler, CliHandlerResult
31
+ from ...runtime.scanner import CliHandlerScanner
32
+
33
+
34
+
35
+ # --------------------------------------------------------------------------------
36
+ #
37
+ # search command handler
38
+ #
39
+ # --------------------------------------------------------------------------------
40
+ @cli_def_handler("/cli-def/search", description="builtin search command handler", late_binding=True)
41
+ def run_search(event: CliEvent):
42
+ logging.info("=== search command ===")
43
+ dir_to_search = event.params.get("directory_to_search")
44
+ recursive = event.params.get("recursive")
45
+
46
+ assert dir_to_search
47
+ path = Path(dir_to_search)
48
+
49
+ if not path.is_dir():
50
+ msg = f"[Error] directory not found: {dir_to_search}"
51
+ print(msg)
52
+ return CliHandlerResult.make_error(event, msg)
53
+
54
+ toml_files = path.glob(
55
+ "**/*.toml" if recursive else "*.toml"
56
+ )
57
+
58
+ toml_files = list(toml_files)
59
+ logging.info(f"@@@ {len(toml_files)} TOMLs found")
60
+ mapping: dict[Path, CliDef] = {}
61
+ for toml in toml_files:
62
+ cli_def = load_cli_def_path(toml)
63
+ if cli_def is None:
64
+ continue
65
+ mapping[toml] = cli_def
66
+
67
+ pretty_report(event, mapping)
68
+
69
+ return CliHandlerResult.make_result(
70
+ event,
71
+ "run_search",
72
+ data = mapping
73
+ )
74
+
75
+
76
+ def pretty_report(event: CliEvent, mapping: Mapping[Path, CliDef]):
77
+ dump_all = event.params.get("dump_all")
78
+
79
+ columns = [
80
+ Column("#", col_type=ColumnType.INDEX1, default_style=Style(h_align="right")),
81
+ Column("path", default_style=Style()),
82
+ #Column("|", col_type=ColumnType.SEPARATOR),
83
+ Column("cli_key"),
84
+ Column("cli_help"),
85
+ Column("nodes_count", default_style=Style(h_align="right")),
86
+ ]
87
+ column_mapping = {col.key: col for col in columns}
88
+ display_column_keys = list(column_mapping.keys())
89
+
90
+ row_records: list[RowRecord] = []
91
+ row_records.append(RowRecord(row_type=RowType.HEADER_KEY))
92
+ row_records.append(RowRecord(row_type=RowType.SEPARATOR))
93
+ for k, v in mapping.items():
94
+ row = RowRecord(
95
+ cell_mapping={
96
+ "path": Cell(k),
97
+ "cli_key": v.key,
98
+ "cli_help": v.help,
99
+ "nodes_count": len(list(v.iter_all_nodes())),
100
+ }, #default_style=Style(bg_color="yellow")
101
+ )
102
+ row_records.append(row)
103
+ row_records.append(RowRecord(row_type=RowType.SEPARATOR))
104
+
105
+ table = Table(
106
+ column_mapping=column_mapping,
107
+ row_records=row_records,
108
+ display_column_keys=display_column_keys,
109
+ )
110
+
111
+ renderer = PrettyRenderer()
112
+ for rendered_line in renderer.render_table(table):
113
+ print(rendered_line)
114
+ print(renderer.render_text(f"[{len(mapping)} items]", Style()))
115
+
116
+
cli_def/script/main.py ADDED
@@ -0,0 +1,32 @@
1
+ # cli_def/script/main.py
2
+ from __future__ import annotations
3
+ from typing import Sequence
4
+ import logging
5
+ import sys
6
+
7
+ from ..runtime import CliRunner
8
+
9
+ from .common import (
10
+ load_builtin_cli_def,
11
+ print_handler
12
+ )
13
+
14
+
15
+ # --------------------------------------------------------------------------------
16
+ # main
17
+ # --------------------------------------------------------------------------------
18
+
19
+ def main(argv: Sequence[str]|None=None):
20
+ if argv is None:
21
+ argv = sys.argv[1:]
22
+
23
+ cli_def = load_builtin_cli_def()
24
+ assert cli_def is not None
25
+ runner = CliRunner(cli_def, fallback_handler=print_handler)
26
+
27
+ result = runner.run(argv)
28
+
29
+ logging.info(f"@@@ cli_def.sctipt.main:result = {result}")
30
+
31
+ return result.exit_code
32
+