Skip to content

Commit beae082

Browse files
committed
Introduce --pretty option to pretty print result models
By default, the models will be printed as JSON object. Using --pretty the result will be pretty printed, for example, the tables will be printed as tables or trees as table tree. Add json encoder for virtual table lines and headers Signed-off-by: Bernd Hufmann <bernd.hufmann@ericsson.com>
1 parent 0be1820 commit beae082

File tree

5 files changed

+103
-9
lines changed

5 files changed

+103
-9
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,8 @@ optional arguments:
178178
The output ID
179179
--json-file JSON_FILE
180180
JSON file with parameter
181+
--pretty
182+
Pretty print result (if supported) else plain JSON
181183
```
182184

183185
Examples:

tsp/entry_model.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,15 @@ def __init__(self, params, model_type=ModelType.XY_TREE):
6060

6161
def __repr__(self) -> str:
6262
return 'EntryModel({})'.format(', '.join(str(entry) for entry in self.entries))
63+
64+
def to_json(self):
65+
return json.dumps(self, cls=EntryModelEncoder, indent=4)
6366

6467
class EntryModelEncoder(json.JSONEncoder):
6568
def default(self, obj):
6669
if isinstance(obj, EntryModel):
6770
return {
68-
'headers': [EntryHeaderEncoder().default(header) for header in obj.headers],
69-
'entries': [TimeGraphEntryEncoder().default(entry) if isinstance(entry, TimeGraphEntry) else EntryEncoder().default(entry) for entry in obj.entries]
71+
HEADER_KEY: [EntryHeaderEncoder().default(header) for header in obj.headers],
72+
ENTRIES_KEY: [TimeGraphEntryEncoder().default(entry) if isinstance(entry, TimeGraphEntry) else EntryEncoder().default(entry) for entry in obj.entries]
7073
}
7174
return super().default(obj)

tsp/virtual_table_header_model.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222

2323
"""Virtual table header model file."""
2424

25+
import json
26+
2527
COLUMN_ID_KEY = "id"
2628
COLUMN_NAME_KEY = "name"
2729
COLUMN_DESCRIPTION_KEY = "description"
@@ -49,6 +51,17 @@ def print(self):
4951
for column in self.columns:
5052
column.print()
5153

54+
def to_json(self):
55+
return json.dumps(self, cls=VirtualTableHeaderModelEncoder, indent=4)
56+
57+
class VirtualTableHeaderModelEncoder(json.JSONEncoder):
58+
def default(self, obj):
59+
if isinstance(obj, VirtualTableHeaderModel):
60+
# result = {}
61+
# result['model'] = [ VirtualTableHeaderColumnModelEncoder().default(column) for column in obj.columns ]
62+
return [ VirtualTableHeaderColumnModelEncoder().default(column) for column in obj.columns ]
63+
return super().default(obj)
64+
5265
class VirtualTableHeaderColumnModel:
5366
'''
5467
Virtual table header column model that will be returned by the server
@@ -87,4 +100,18 @@ def print(self):
87100
print(" name: " + str(self.name))
88101
print(" description: " + str(self.description))
89102
print(" type: " + str(self.type))
90-
print("-" * 50)
103+
print("-" * 50)
104+
105+
def to_json(self):
106+
return json.dumps(self, cls=VirtualTableHeaderColumnModelEncoder, indent=4)
107+
108+
class VirtualTableHeaderColumnModelEncoder(json.JSONEncoder):
109+
def default(self, obj):
110+
if isinstance(obj, VirtualTableHeaderColumnModel):
111+
result = {}
112+
result[COLUMN_ID_KEY] = obj.id
113+
result[COLUMN_NAME_KEY] = obj.name
114+
result[COLUMN_DESCRIPTION_KEY] = obj.description
115+
result[COLUMN_TYPE_KEY] = obj.type
116+
return result
117+
return super().default(obj)

tsp/virtual_table_model.py

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
"""VirtualTableModel class file."""
2424

25+
import json
2526
from tsp.virtual_table_tag import VirtualTableTag
2627

2728
SIZE_KEY = "size"
@@ -71,6 +72,9 @@ def __init__(self, params):
7172
self.lines.append(VirtualTableLine(line))
7273
del params[LINES_KEY]
7374

75+
def to_json(self):
76+
return json.dumps(self, cls=VirtualTableModelEncoder, indent=4)
77+
7478
def print(self):
7579
print("VirtualTableModel:")
7680
print(f" size: {self.size}")
@@ -81,6 +85,17 @@ def print(self):
8185
for i, line in enumerate(self.lines):
8286
line.print()
8387

88+
class VirtualTableModelEncoder(json.JSONEncoder):
89+
def default(self, obj):
90+
if isinstance(obj, VirtualTableModel):
91+
result = {}
92+
result[SIZE_KEY] = obj.size
93+
result[LOW_INDEX_KEY] = obj.low_index
94+
result[COLUMN_IDS_KEY] = obj.column_ids
95+
result[LINES_KEY] = [ VirtualTableLineEncoder().default(line) for line in obj.lines ]
96+
return result
97+
return super().default(obj)
98+
8499
class VirtualTableLine:
85100
'''
86101
Virtual table line that will be returned by the server
@@ -122,6 +137,9 @@ def __init__(self, params):
122137
def has_tag(self, tag):
123138
return bool(self.tags & tag)
124139

140+
def to_json(self):
141+
return json.dumps(self, cls=VirtualTableLineEncoder, indent=4)
142+
125143
def print(self):
126144

127145
print(f" index: {self.index}")
@@ -138,6 +156,16 @@ def print(self):
138156
cell.print()
139157
print(f" {'-' * 30}")
140158

159+
class VirtualTableLineEncoder(json.JSONEncoder):
160+
def default(self, obj):
161+
if isinstance(obj, VirtualTableLine):
162+
result = {}
163+
result[TABLE_LINE_INDEX_KEY] = obj.index
164+
result[TAGS_KEY] = obj.tags.value
165+
result[TABLE_LINE_CELLS_KEY] = [ VirtualTableLineCellEncoder().default(cell) for cell in obj.cells ]
166+
return result
167+
return super().default(obj)
168+
141169
class VirtualTableLineCell:
142170
'''
143171
Virtual table line cell that will be returned by the server
@@ -180,4 +208,16 @@ def print(self):
180208
tags_str = " | ".join(active_tags)
181209

182210
print(f" \"tags\": \"{tags_str}\"")
183-
print(f" {'-' * 10}")
211+
print(f" {'-' * 10}")
212+
213+
def to_json(self):
214+
return json.dumps(self, cls=VirtualTableLineCell, indent=4)
215+
216+
class VirtualTableLineCellEncoder(json.JSONEncoder):
217+
def default(self, obj):
218+
if isinstance(obj, VirtualTableLineCell):
219+
return {
220+
TABLE_LINE_CELL_CONTENT_KEY: obj.content,
221+
TAGS_KEY: obj.tags.value
222+
}
223+
return super().default(obj)

tsp_cli_client

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,18 @@ def __get_tree(uuid, outputid, treetype):
8686
if tree is None:
8787
print("Tree had no model; retry?")
8888
sys.exit(1)
89+
90+
print('Successfully fetched tree')
91+
print('-------------------------')
8992

90-
tree_model = TreeModel(tree.entries, tree.headers)
91-
tree_model.print()
93+
if (options.pretty):
94+
tree_model = TreeModel(tree.entries, tree.headers)
95+
tree_model.print()
96+
sys.exit(0)
97+
98+
print(tree.to_json())
9299
sys.exit(0)
100+
93101
else:
94102
sys.exit(1)
95103
else:
@@ -221,6 +229,8 @@ if __name__ == "__main__":
221229
parser.add_argument("--delete-output", dest="delete_output", help="Delete derived output", metavar="DERIVED_OUTPUT_ID")
222230
parser.add_argument("--output-id", dest="output_id", help="The output ID")
223231
parser.add_argument("--json-file", dest="json_file", help="JSON file with parameter")
232+
parser.add_argument("--pretty", action='store_true', dest="pretty", help="Pretty print result (if supported) else plain JSON")
233+
224234

225235
argcomplete.autocomplete(parser)
226236
options = parser.parse_args()
@@ -432,8 +442,15 @@ if __name__ == "__main__":
432442
options.uuid, options.get_virtual_table_columns)
433443

434444
if response.status_code == 200:
445+
print('Successfully fetched virtual table columns')
446+
print('------------------------------------------')
447+
435448
model = response.model.model
436-
model.print()
449+
if (options.pretty):
450+
model.print()
451+
sys.exit(0)
452+
453+
print(model.to_json())
437454
sys.exit(0)
438455
else:
439456
sys.exit(1)
@@ -482,8 +499,13 @@ if __name__ == "__main__":
482499
if response.status_code == 200:
483500
headers = columns_response.model.model.columns
484501

485-
table_model = TableModel(response.model.model, headers)
486-
table_model.print()
502+
if (options.pretty):
503+
table_model = TableModel(response.model.model, headers)
504+
table_model.print()
505+
sys.exit(0)
506+
507+
print(response.model.model.to_json())
508+
sys.exit(0)
487509

488510
# python_object = json.loads(response.status_text)
489511
# print(json.dumps(python_object, indent=4))

0 commit comments

Comments
 (0)