Skip to content

Commit 0be1820

Browse files
committed
Print virtual table lines as table using panda data frame
Signed-off-by: Bernd Hufmann <bernd.hufmann@ericsson.com>
1 parent cd3f493 commit 0be1820

File tree

2 files changed

+86
-2
lines changed

2 files changed

+86
-2
lines changed

table_model.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
2+
# The MIT License (MIT)
3+
#
4+
# Copyright (C) 2025 - Ericsson
5+
#
6+
# Permission is hereby granted, free of charge, to any person obtaining a copy
7+
# of this software and associated documentation files (the "Software"), to deal
8+
# in the Software without restriction, including without limitation the rights
9+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
# copies of the Software, and to permit persons to whom the Software is
11+
# furnished to do so, subject to the following conditions:
12+
#
13+
# The above copyright notice and this permission notice shall be included in
14+
# all copies or substantial portions of the Software.
15+
#
16+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
# SOFTWARE.
23+
24+
"""TableModel class file."""
25+
26+
from tabulate import tabulate
27+
28+
import pandas as pd
29+
30+
31+
# pylint: disable=too-few-public-methods
32+
class TableModel:
33+
"""TreeModel class implementation."""
34+
35+
def __init__(self, model, headers=None):
36+
self._headers = headers
37+
self._model = model
38+
39+
def print(self):
40+
"""Render this tree model."""
41+
frame = {}
42+
data = []
43+
low_index = self._model.low_index
44+
for line in self._model.lines:
45+
row = []
46+
row.append(low_index)
47+
low_index += 1
48+
for cell in line.cells:
49+
row.append(cell.content)
50+
data.append(row)
51+
52+
headers = []
53+
headers.append("Index")
54+
55+
if self._headers is not None:
56+
for col_id in self._model.column_ids:
57+
headers.append(self._headers[col_id].name)
58+
59+
# for header in self._headers:
60+
# headers.append(header.name)
61+
if len(headers) == len(data[0]):
62+
frame = pd.DataFrame(data, columns=headers)
63+
else:
64+
frame = pd.DataFrame(data)
65+
else:
66+
frame = pd.DataFrame(data)
67+
# print(frame.to_string())
68+
#frame.to_csv('output.csv', index=False, sep='\t')
69+
print(tabulate(frame.values, headers, tablefmt="fancy_grid"))
70+

tsp_cli_client

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import requests
3838

3939
from termcolor import colored
4040
from tree_model import TreeModel
41+
from table_model import TableModel
4142
from tsp.tsp_client import TspClient
4243

4344
TRACE_MISSING = "Trace UUID is missing"
@@ -471,8 +472,21 @@ if __name__ == "__main__":
471472

472473
response = tsp_client.fetch_virtual_table_lines(options.uuid, options.get_virtual_table_lines, parameters)
473474
if response.status_code == 200:
474-
model = response.model.model
475-
model.print()
475+
print('Successfully fetched virtual table lines')
476+
print('----------------------------------------')
477+
478+
columns_response = tsp_client.fetch_virtual_table_columns(
479+
options.uuid, options.get_virtual_table_lines)
480+
481+
headers = {}
482+
if response.status_code == 200:
483+
headers = columns_response.model.model.columns
484+
485+
table_model = TableModel(response.model.model, headers)
486+
table_model.print()
487+
488+
# python_object = json.loads(response.status_text)
489+
# print(json.dumps(python_object, indent=4))
476490
sys.exit(0)
477491
else:
478492
sys.exit(1)

0 commit comments

Comments
 (0)