|
| 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 | + |
0 commit comments