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