xiii-cli-core 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.
@@ -0,0 +1,196 @@
1
+ from difflib import get_close_matches
2
+ from typing import Annotated, Any
3
+
4
+ from cyclopts import App, Parameter
5
+
6
+ from xiii_cli_core.client import is_binary_content_type
7
+ from xiii_cli_core.config import Product
8
+ from xiii_cli_core.direct import load_manifest
9
+ from xiii_cli_core.errors import UsageError
10
+ from xiii_cli_core.render import render_json
11
+
12
+
13
+ def register_describe_command(app: App, product: Product) -> None:
14
+ """`describe` answers from the packaged manifest, without a token or network."""
15
+
16
+ @app.command
17
+ def describe(
18
+ module: str | None = None,
19
+ command: str | None = None,
20
+ /,
21
+ *,
22
+ json_output: Annotated[
23
+ bool,
24
+ Parameter(
25
+ name="--json",
26
+ negative=(),
27
+ help="Accepted for symmetry; describe always prints JSON.",
28
+ ),
29
+ ] = False,
30
+ ) -> None:
31
+ """Print a command's parameters, request schema and an example as JSON.
32
+
33
+ Without arguments it lists the modules, with a module its commands.
34
+ """
35
+ modules = {entry["name"]: entry for entry in load_manifest(product)["modules"]}
36
+ if module is None:
37
+ render_json(
38
+ {
39
+ "modules": [
40
+ {
41
+ "name": name,
42
+ "commands": [
43
+ operation["name"] for operation in entry["operations"]
44
+ ],
45
+ }
46
+ for name, entry in modules.items()
47
+ ]
48
+ }
49
+ )
50
+ return
51
+ operations = {
52
+ operation["name"]: operation
53
+ for operation in _pick(modules, module, "module")["operations"]
54
+ }
55
+ if command is None:
56
+ render_json(
57
+ {
58
+ "module": module,
59
+ "commands": [
60
+ {
61
+ "name": name,
62
+ "summary": operation["summary"],
63
+ "method": operation["method"],
64
+ "dangerous": operation["dangerous"],
65
+ }
66
+ for name, operation in operations.items()
67
+ ],
68
+ }
69
+ )
70
+ return
71
+ operation = _pick(operations, command, f"{module} command")
72
+ render_json(describe_operation(product, module, operation))
73
+
74
+
75
+ def describe_operation(
76
+ product: Product, module: str, operation: dict[str, Any]
77
+ ) -> dict[str, Any]:
78
+ body = operation.get("body")
79
+ arguments = [
80
+ {
81
+ "name": parameter["argument"],
82
+ "required": True,
83
+ "description": parameter.get("description"),
84
+ "schema": parameter["schema"],
85
+ }
86
+ for parameter in operation["parameters"]
87
+ if parameter["location"] == "path"
88
+ ]
89
+ options = [
90
+ {
91
+ "option": f"--{parameter['option']}",
92
+ "in": parameter["location"],
93
+ "required": parameter["required"],
94
+ "description": parameter.get("description"),
95
+ "schema": parameter["schema"],
96
+ }
97
+ for parameter in operation["parameters"]
98
+ if parameter["location"] != "path"
99
+ ]
100
+ if isinstance(body, dict):
101
+ options.extend(
102
+ {
103
+ "option": f"--{field['option']}",
104
+ "in": "body",
105
+ "required": field["required"],
106
+ "description": field.get("description"),
107
+ "schema": field["schema"],
108
+ }
109
+ for field in body["properties"]
110
+ if not field.get("sensitive")
111
+ )
112
+ return {
113
+ "command": f"{product.name} {module} {operation['name']}",
114
+ "operation_id": operation["operation_id"],
115
+ "summary": operation["summary"],
116
+ "description": operation.get("description"),
117
+ "method": operation["method"],
118
+ "path": operation["path"],
119
+ "dangerous": operation["dangerous"],
120
+ "arguments": arguments,
121
+ "options": options,
122
+ "body": _body(body),
123
+ "all_pages": bool(operation.get("pagination")),
124
+ "response_content_types": operation["response_content_types"],
125
+ "example": _example(product, module, operation),
126
+ }
127
+
128
+
129
+ def _body(body: Any) -> dict[str, Any] | None:
130
+ if not isinstance(body, dict):
131
+ return None
132
+ return {
133
+ "content_type": body["content_type"],
134
+ "required": body["required"],
135
+ "accepts_input_file": body["content_type"] == "application/json",
136
+ "required_fields": [
137
+ field["name"] for field in body["properties"] if field["required"]
138
+ ],
139
+ "sensitive_fields": [
140
+ field["name"] for field in body["properties"] if field.get("sensitive")
141
+ ],
142
+ "schema": body.get("schema", {}),
143
+ }
144
+
145
+
146
+ def _example(product: Product, module: str, operation: dict[str, Any]) -> str:
147
+ """One command line that satisfies every required input."""
148
+ parts = [product.name, module, operation["name"]]
149
+ parts.extend(
150
+ f"<{parameter['option']}>"
151
+ for parameter in operation["parameters"]
152
+ if parameter["location"] == "path"
153
+ )
154
+ parts.extend(
155
+ _example_option(parameter)
156
+ for parameter in operation["parameters"]
157
+ if parameter["location"] != "path" and parameter["required"]
158
+ )
159
+ body = operation.get("body")
160
+ if isinstance(body, dict):
161
+ fields = body["properties"]
162
+ needs_input = (not body["structured"] and body["required"]) or any(
163
+ field["required"] and field.get("sensitive") for field in fields
164
+ )
165
+ if needs_input:
166
+ parts.append("--input body.json")
167
+ else:
168
+ parts.extend(
169
+ _example_option(field) for field in fields if field["required"]
170
+ )
171
+ if operation["dangerous"]:
172
+ parts.append("--yes")
173
+ binary = any(
174
+ is_binary_content_type(content_type)
175
+ for content_type in operation["response_content_types"]
176
+ )
177
+ parts.append("--output <file> --json" if binary else "--json")
178
+ return " ".join(parts)
179
+
180
+
181
+ def _example_option(field: dict[str, Any]) -> str:
182
+ schema = field["schema"]
183
+ option = f"--{field['option']}"
184
+ if schema.get("kind") == "boolean":
185
+ return option
186
+ if schema.get("kind") == "enum":
187
+ return f"{option} {schema['values'][0]}"
188
+ return f"{option} <{field['option']}>"
189
+
190
+
191
+ def _pick(choices: dict[str, Any], name: str, label: str) -> Any:
192
+ if name in choices:
193
+ return choices[name]
194
+ suggestions = get_close_matches(name, list(choices), n=3)
195
+ hint = f"; did you mean {', '.join(suggestions)}?" if suggestions else ""
196
+ raise UsageError(f"Unknown {label} `{name}`{hint}")