Skip to content

Commit 3277724

Browse files
committed
Add json encoder for virtual table lines and option to print json
... instead of pretty printed table. Introduced --print-json option Also, apply --print-json option for data tree Signed-off-by: Bernd Hufmann <bernd.hufmann@ericsson.com>
1 parent 0be1820 commit 3277724

3 files changed

Lines changed: 53 additions & 1 deletion

File tree

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+
--print-json
182+
Print JSON representation of model instead of pretty-format (for some replies only)
181183
```
182184

183185
Examples:

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: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,10 @@ def __get_tree(uuid, outputid, treetype):
8787
print("Tree had no model; retry?")
8888
sys.exit(1)
8989

90+
if (options.print_raw):
91+
print(tree.to_json())
92+
sys.exit(0)
93+
9094
tree_model = TreeModel(tree.entries, tree.headers)
9195
tree_model.print()
9296
sys.exit(0)
@@ -221,6 +225,8 @@ if __name__ == "__main__":
221225
parser.add_argument("--delete-output", dest="delete_output", help="Delete derived output", metavar="DERIVED_OUTPUT_ID")
222226
parser.add_argument("--output-id", dest="output_id", help="The output ID")
223227
parser.add_argument("--json-file", dest="json_file", help="JSON file with parameter")
228+
parser.add_argument("--print-json", action='store_true', dest="print_json", help="Print JSON representation of model instead of pretty-format (for some replies only)")
229+
224230

225231
argcomplete.autocomplete(parser)
226232
options = parser.parse_args()
@@ -482,6 +488,10 @@ if __name__ == "__main__":
482488
if response.status_code == 200:
483489
headers = columns_response.model.model.columns
484490

491+
if (options.print_raw):
492+
print(response.model.model.to_json())
493+
sys.exit(0)
494+
485495
table_model = TableModel(response.model.model, headers)
486496
table_model.print()
487497

0 commit comments

Comments
 (0)