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):
self.columns_data = []
self.columns_labels = []
class TableRenderer(ABC):
@abstractmethod
def render_table(self, table: Table):
pass
class MarkdownTableRenderer():
@staticmethod
def get_md_table_separator_line(columns_width):
sep_line = '|' sep_line = '|'
for col_width in columns_width: for col_width in columns_width:
sep_line += '-' * col_width sep_line += '-' * col_width
sep_line += '|' sep_line += '|'
return sep_line return sep_line
def render_table(self, table: Table):
def render_table_md(columns_data, labels): assert len(table.column_labels) == len(table.columns_data)
assert len(labels) == len(columns_data) num_cols = len(table.columns_data)
num_cols = len(columns_data) num_rows = len(table.columns_data[0])
num_rows = len(columns_data[0])
cols_width = [0 for col in range(num_cols)] cols_width = [0 for col in range(num_cols)]
for icol 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): for irow in range(num_rows):
cols_width[icol] = max(cols_width[icol], len(columns_data[icol][irow])) cols_width[icol] = max(cols_width[icol], len(table.columns_data[icol][irow]))
sep_line = get_md_table_separator_line([col_width + 2 for col_width in cols_width]) 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]) line_format = "|" + ''.join([" %%%ds |" % col_width for col_width in cols_width])
print(sep_line) print(sep_line)
print(line_format % tuple(labels)) print(line_format % tuple(table.column_labels))
print(sep_line) print(sep_line)
for irow in range(num_rows): for irow in range(num_rows):
row = [] row = []
for icol in range(num_cols): for icol in range(num_cols):
row.append(columns_data[icol][irow]) row.append(table.columns_data[icol][irow])
print(line_format % tuple(row)) print(line_format % tuple(row))
print(sep_line) print(sep_line)
def render_table_wiki(columns_data, labels): class WikiTableRenderer():
assert len(labels) == len(columns_data)
num_cols = len(columns_data) def render_table(self, table: Table):
num_rows = len(columns_data[0]) 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)] cols_width = [0 for col in range(num_cols)]
for icol 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): for irow in range(num_rows):
cols_width[icol] = max(cols_width[icol], len(columns_data[icol][irow])) cols_width[icol] = max(cols_width[icol], len(table.columns_data[icol][irow]))
cols_width = [col_width + 1 for col_width in cols_width] cols_width = [col_width + 1 for col_width in cols_width]
header_line_format = "^" + ''.join([" %%%ds ^" % col_width 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]) line_format = "|" + ''.join([" %%%ds |" % col_width for col_width in cols_width])
print(header_line_format % tuple(labels)) print(header_line_format % tuple(table.column_labels))
for irow in range(num_rows): for irow in range(num_rows):
row = [] row = []
for icol in range(num_cols): for icol in range(num_cols):
row.append(columns_data[icol][irow]) row.append(table.columns_data[icol][irow])
print(line_format % tuple(row)) 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