Skip to content

Commit 3782bf1

Browse files
Improve output formatting; add result fields
1 parent 5239b95 commit 3782bf1

5 files changed

Lines changed: 285 additions & 85 deletions

File tree

docs/static/overlay.css

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,52 +22,65 @@ td {
2222
width: 150px !important;
2323
}
2424

25-
2625
/*
2726
Custom formatting for actions
2827
*/
29-
3028
.action-subheading {
3129
margin-bottom: 4px !important;
30+
font-weight: 700 !important;
3231
}
3332

3433
.action-subheading-description {
3534
font-size: 90% !important;
3635
margin-bottom: 12px !important;
3736
}
3837

39-
.action-argument-section-header {
38+
.action-section-header {
4039
color: #606060 !important;
4140
font-weight: 700 !important;
4241
padding-top: 10px !important;
4342
margin-bottom: 12px !important;
4443
font-size: 95% !important;
4544
}
4645

47-
.action-argument-required {
48-
color: #CD5C5C !important;
49-
font-weight: bolder !important;
46+
.action-table-field-name,
47+
.action-table-field-optional,
48+
.action-table-field-required,
49+
.action-table-field-type {
50+
font-size: 85% !important;
51+
font-family: SFMono-Regular,Menlo,Monaco,Consolas,Liberation Mono,Courier New,Courier,monospace !important;
52+
}
53+
54+
.action-table-field-required {
55+
color: #E74C3C !important;
5056
}
5157

5258
/*
5359
Formatting for generated tables
5460
*/
5561

56-
/* Prevent word wrapping on `Name` fields */
62+
/* Prevent word wrapping on `Name` and `Example` fields */
5763
.action-argument-section-table > tbody tr > td:nth-child(1) *,
5864
.action-parameter-table > tbody tr > td:nth-child(1) *,
59-
.action-filterable-field-table > tbody tr > td:nth-child(1) * {
65+
.action-filterable-field-table > tbody tr > td:nth-child(1) *,
66+
.action-attribute-section-table > tbody tr > td:nth-child(1) *,
67+
.action-attribute-section-table > tbody tr > td:nth-child(3) * {
6068
word-wrap: unset !important;
6169
white-space: nowrap !important;
6270
}
6371

64-
/* Center-align `Required` and `Type` columns */
72+
/* Center-align `Required`, and `Type` columns */
6573
.action-argument-section-table > tbody tr > td:nth-child(2),
6674
.action-argument-section-table > thead tr > th:nth-child(2),
6775
.action-argument-section-table > tbody tr > td:nth-child(3),
6876
.action-argument-section-table > thead tr > th:nth-child(3),
77+
78+
.action-attribute-section-table > tbody tr > td:nth-child(2),
79+
.action-attribute-section-table > thead tr > th:nth-child(2),
80+
6981
.action-parameter-table > tbody tr > td:nth-child(2),
7082
.action-parameter-table > thead tr > th:nth-child(2),
83+
7184
.action-filterable-field-table > tbody tr > td:nth-child(2),
7285
.action-filterable-field-table > thead tr > th:nth-child(2) {
7386
text-align: center !important;

linodecli/baked/request.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,9 @@ def __init__(
105105
#: Whether null is an acceptable value for this attribute
106106
self.nullable = schema.nullable
107107

108+
#: An example value for this attribute
109+
self.example = schema.example
110+
108111
# handle the type for list values if this is an array
109112
if self.datatype == "array" and schema.items:
110113
self.item_type = schema.items.type

linodecli/baked/response.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
Converting the processed OpenAPI Responses into something the CLI can work with
33
"""
44

5+
from typing import Any, Dict
6+
57
from openapi3.paths import MediaType
68

79
from linodecli.baked.parsing import process_arg_description
@@ -80,11 +82,17 @@ def __init__(self, name, schema, prefix=None, nested_list_depth=0):
8082
#: How we should associate values of this attribute to output colors
8183
self.color_map = schema.extensions.get("linode-cli-color")
8284

85+
#: An example value for this attribute.
86+
self.example = schema.example
87+
8388
#: The type for items in this attribute, if this attribute is a list
8489
self.item_type = None
8590
if schema.type == "array":
8691
self.item_type = schema.items.type
8792

93+
if schema.items.example:
94+
self.example = schema.items.example
95+
8896
@property
8997
def path(self):
9098
"""
@@ -288,3 +296,62 @@ def _fix_nested_list(self, json):
288296
cobj[path_parts[0]] = item
289297
result.append(cobj)
290298
return result
299+
300+
def construct_sample(self) -> Dict[str, Any]:
301+
"""
302+
Reconstructs a sample API response from this response object.
303+
304+
:returns: The reconstructed API response sample.
305+
"""
306+
result = {}
307+
308+
# TODO: Make this whole function less bad
309+
normal_attrs = iter(
310+
attr for attr in self.attrs if attr.datatype != "array"
311+
)
312+
array_attrs = sorted(
313+
iter(attr for attr in self.attrs if attr.datatype == "array"),
314+
key=lambda attr: attr.path,
315+
)
316+
317+
# First, create any necessary structures for list and object values
318+
for attr in array_attrs:
319+
segments = attr.path.split(".")
320+
321+
current = result
322+
for segment in segments[:-1]:
323+
if segment not in current:
324+
current[segment] = {}
325+
326+
current = current[segment]
327+
328+
current[segments[-1]] = []
329+
if attr.example:
330+
current[segments[-1]] = (
331+
attr.example
332+
if isinstance(attr.example, list)
333+
else [attr.example]
334+
)
335+
336+
# Second, populate the rest of the attributes
337+
for attr in normal_attrs:
338+
if attr.example is None:
339+
continue
340+
341+
segments = attr.path.split(".")
342+
343+
current = result
344+
for segment in segments[:-1]:
345+
if segment not in current:
346+
current[segment] = {}
347+
348+
current = current[segment]
349+
350+
if isinstance(current, list):
351+
if len(current) < 1:
352+
current.append({})
353+
current = current[0]
354+
355+
current[segments[-1]] = attr.example
356+
357+
return result

0 commit comments

Comments
 (0)