refactored code into object oriented

will ease adding csv renderer

work related to https://bugzilla.ipr.univ-rennes1.fr/show_bug.cgi?id=3368
This commit is contained in:
Guillaume Raffy 2023-10-06 14:27:49 +02:00
parent 60291a9b76
commit 66058120b2
1 changed files with 77 additions and 50 deletions

View File

@ -2,64 +2,85 @@ from typing import List
from cocluto.inventory import Inventory
from cocluto.SimpaDbUtil import SqlDatabaseReader, SqlFile
from urllib.parse import urlparse
from abc import ABC, abstractmethod
import re
class Table():
columns_data: List[List[str]]
column_labels: List[str]
def get_md_table_separator_line(columns_width):
sep_line = '|'
for col_width in columns_width:
sep_line += '-' * col_width
sep_line += '|'
return sep_line
def __init__(self):
self.columns_data = []
self.columns_labels = []
def render_table_md(columns_data, labels):
assert len(labels) == len(columns_data)
num_cols = len(columns_data)
num_rows = len(columns_data[0])
cols_width = [0 for col in range(num_cols)]
for icol in range(num_cols):
cols_width[icol] = max(cols_width[icol], len(labels[icol]))
for irow in range(num_rows):
cols_width[icol] = max(cols_width[icol], len(columns_data[icol][irow]))
sep_line = get_md_table_separator_line([col_width + 2 for col_width in cols_width])
line_format = "|" + ''.join([" %%%ds |" % col_width for col_width in cols_width])
print(sep_line)
print(line_format % tuple(labels))
print(sep_line)
for irow in range(num_rows):
row = []
class TableRenderer(ABC):
@abstractmethod
def render_table(self, table: Table):
pass
class MarkdownTableRenderer():
@staticmethod
def get_md_table_separator_line(columns_width):
sep_line = '|'
for col_width in columns_width:
sep_line += '-' * col_width
sep_line += '|'
return sep_line
def render_table(self, table: Table):
assert len(table.column_labels) == len(table.columns_data)
num_cols = len(table.columns_data)
num_rows = len(table.columns_data[0])
cols_width = [0 for col in range(num_cols)]
for icol in range(num_cols):
row.append(columns_data[icol][irow])
print(line_format % tuple(row))
print(sep_line)
def render_table_wiki(columns_data, labels):
assert len(labels) == len(columns_data)
num_cols = len(columns_data)
num_rows = len(columns_data[0])
cols_width = [0 for col in range(num_cols)]
for icol in range(num_cols):
cols_width[icol] = max(cols_width[icol], len(labels[icol]))
cols_width[icol] = max(cols_width[icol], len(table.column_labels[icol]))
for irow in range(num_rows):
cols_width[icol] = max(cols_width[icol], len(table.columns_data[icol][irow]))
sep_line = MarkdownTableRenderer.get_md_table_separator_line([col_width + 2 for col_width in cols_width])
line_format = "|" + ''.join([" %%%ds |" % col_width for col_width in cols_width])
print(sep_line)
print(line_format % tuple(table.column_labels))
print(sep_line)
for irow in range(num_rows):
cols_width[icol] = max(cols_width[icol], len(columns_data[icol][irow]))
cols_width = [col_width + 1 for col_width in cols_width]
header_line_format = "^" + ''.join([" %%%ds ^" % col_width for col_width in cols_width])
line_format = "|" + ''.join([" %%%ds |" % col_width for col_width in cols_width])
print(header_line_format % tuple(labels))
for irow in range(num_rows):
row = []
for icol in range(num_cols):
row.append(columns_data[icol][irow])
row = []
for icol in range(num_cols):
row.append(table.columns_data[icol][irow])
print(line_format % tuple(row))
print(line_format % tuple(row))
print(sep_line)
class WikiTableRenderer():
def render_table(self, table: Table):
assert len(table.column_labels) == len(table.columns_data)
num_cols = len(table.columns_data)
num_rows = len(table.columns_data[0])
cols_width = [0 for col in range(num_cols)]
for icol in range(num_cols):
cols_width[icol] = max(cols_width[icol], len(table.column_labels[icol]))
for irow in range(num_rows):
cols_width[icol] = max(cols_width[icol], len(table.columns_data[icol][irow]))
cols_width = [col_width + 1 for col_width in cols_width]
header_line_format = "^" + ''.join([" %%%ds ^" % col_width for col_width in cols_width])
line_format = "|" + ''.join([" %%%ds |" % col_width for col_width in cols_width])
print(header_line_format % tuple(table.column_labels))
for irow in range(num_rows):
row = []
for icol in range(num_cols):
row.append(table.columns_data[icol][irow])
print(line_format % tuple(row))
def print_machine_table(machines_fqdn: List[str], inventory: Inventory):
table = Table()
machines_name = []
machines_spec_id = []
machines_serial_number = []
@ -71,12 +92,18 @@ def print_machine_table(machines_fqdn: List[str], inventory: Inventory):
machines_name.append(machine_name)
machines_spec_id.append(machine_spec_id)
machines_serial_number.append(serial_number)
name_label = 'Nom'
model_label = 'Modèle'
serial_label = 'Numéro de Série'
table.column_labels = [
'Nom',
'Modèle',
'Numéro de Série'
]
table.columns_data = [machines_name, machines_spec_id, machines_serial_number]
render_table_md([machines_name, machines_spec_id, machines_serial_number], [name_label, model_label, serial_label])
render_table_wiki([machines_name, machines_spec_id, machines_serial_number], [name_label, model_label, serial_label])
table_renderer = MarkdownTableRenderer()
table_renderer.render_table(table)
table_renderer = WikiTableRenderer()
table_renderer.render_table(table)
# get the list of machines hosted at the dsi