Coverage for /opt/cloud/slavespace/usr1/096471637100f3de0fcfc01072822a80/ut/src/asys/view/table.py: 98%
59 statements
« prev ^ index » next coverage.py v7.14.1, created at 2026-08-21 15:37 +0800
« prev ^ index » next coverage.py v7.14.1, created at 2026-08-21 15:37 +0800
1#!/usr/bin/env python3
2# -*- coding: utf-8 -*-
3# ----------------------------------------------------------------------------
4# Copyright (c) 2025 Huawei Technologies Co., Ltd.
5#
6# Licensed under the Apache License, Version 2.0 (the "License");
7# you may not use this file except in compliance with the License.
8# You may obtain a copy of the License at
9#
10# http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS,
14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15# See the License for the specific language governing permissions and
16# limitations under the License.
17# ----------------------------------------------------------------------------
19from common.const import MAX_CHAR_LINE
21__all__ = ["generate_report"]
23VERTICAL = " | "
24ADD_SUB = " +-"
25SUB_ADD = "-+ "
28def write_header(table_string, table_header, column_widths):
29 # Table header data
30 for header in table_header:
31 # Align the cells in each column print header
32 table_string += VERTICAL
33 for i, cell in enumerate(header):
34 # Align the cells in each column
35 table_string += str(cell).ljust(column_widths[i]) + VERTICAL
36 table_string += "\n"
37 return table_string
40def write_data(table_string, data_value, column_widths, split_line):
41 # Align the each column print data
42 for row in data_value:
43 table_string += VERTICAL
44 for i, cell in enumerate(row):
45 # Align the cells in each column print data
46 table_string += str(cell).ljust(column_widths[i]) + VERTICAL
47 table_string += "\n"
48 if split_line:
49 table_string += ADD_SUB
50 for i, _ in enumerate(column_widths):
51 # Align the cells in each column
52 table_string += str().ljust(column_widths[i], '-') + SUB_ADD
53 table_string += "\n"
54 return table_string
57def generate_report(table_header, table_data, split_line=False):
58 """
59 Generate a formatted table string.
61 Args:
62 table_header (list): The header of the table [[1, 2, 3]].
63 table_data (dict): The data of the table
64 {
65 'none': [
66 [1, 2, 3],
67 [4, 5, 6]
68 ],
69 'test': [
70 [1, 2, 3],
71 [4, 5, 6]
72 ]
73 }.
74 split_line (bool): Indicates whether to display the data split line.
75 Returns:
76 str: The formatted table string.
77 """
78 # Calculate the width of each column
79 table_row = list()
80 table_row += table_header
81 for data_key, data_value in table_data.items():
82 key_list = ["0" for _ in range(len(table_row[0]))]
83 key_list[0] = data_key
84 table_row.append(key_list)
85 table_row += data_value
86 column_widths = [max(len(str(row[i])) for row in table_row) for i in range(len(table_row[0]))]
87 # Create the formatted table string
88 table_string = "\n"
89 table_string += ADD_SUB
90 for i, _ in enumerate(column_widths):
91 if column_widths[i] > MAX_CHAR_LINE:
92 column_widths[i] = MAX_CHAR_LINE
93 column_widths[i] += 5
94 # Align the cells in each column
95 table_string += str().ljust(column_widths[i], '-') + SUB_ADD
96 table_string += "\n"
98 table_string = write_header(table_string, table_header, column_widths)
100 # Splitting line between header and data
101 table_string += " +="
102 for i, _ in enumerate(column_widths):
103 # Align the cells in each column
104 table_string += str().ljust(column_widths[i], '=') + "=+ "
105 table_string += "\n"
106 # Table content data
107 for data_key, data_value in table_data.items():
108 if data_key != "none":
109 table_string += ADD_SUB
110 for i, _ in enumerate(column_widths):
111 # Align the cells in each column
112 table_string += str("--" + data_key if i == 0 else "").ljust(column_widths[i], '-') + SUB_ADD
113 table_string += "\n"
114 table_string = write_data(table_string, data_value, column_widths, split_line)
116 if not split_line:
117 table_string += ADD_SUB
118 for i, _ in enumerate(column_widths):
119 # Align the cells in each column
120 table_string += str().ljust(column_widths[i], '-') + SUB_ADD
121 table_string += "\n"
122 return table_string.replace("-+ -", "-+--").replace("=+ =", "=+==")