added tsv table renderer

for easy import in excel

also added the ability to choose a file as output
This commit is contained in:
Guillaume Raffy 2023-10-06 16:03:38 +02:00
parent 66058120b2
commit 16268289ba
1 changed files with 33 additions and 20 deletions

View File

@ -1,8 +1,9 @@
from typing import List
from typing import List, IO
from cocluto.inventory import Inventory
from cocluto.SimpaDbUtil import SqlDatabaseReader, SqlFile
from urllib.parse import urlparse
from abc import ABC, abstractmethod
from sys import stdout
import re
@ -18,11 +19,25 @@ class Table():
class TableRenderer(ABC):
@abstractmethod
def render_table(self, table: Table):
def render_table(self, table: Table, file: IO):
pass
class MarkdownTableRenderer():
class TsvTableRenderer(TableRenderer):
def render_table(self, table: Table, file: IO):
assert len(table.column_labels) == len(table.columns_data)
num_cols = len(table.columns_data)
num_rows = len(table.columns_data[0])
file.write('#%s\n' % '\t'.join(table.column_labels))
for irow in range(num_rows):
row = []
for icol in range(num_cols):
row.append(table.columns_data[icol][irow])
file.write('%s\n' % '\t'.join(row))
class MarkdownTableRenderer(TableRenderer):
@staticmethod
def get_md_table_separator_line(columns_width):
@ -32,7 +47,7 @@ class MarkdownTableRenderer():
sep_line += '|'
return sep_line
def render_table(self, table: Table):
def render_table(self, table: Table, file: IO):
assert len(table.column_labels) == len(table.columns_data)
num_cols = len(table.columns_data)
num_rows = len(table.columns_data[0])
@ -43,21 +58,21 @@ class MarkdownTableRenderer():
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)
file.write('%s\n' % sep_line)
file.write('%s\n' % (line_format % tuple(table.column_labels)))
file.write('%s\n' % sep_line)
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))
print(sep_line)
file.write('%s\n' % (line_format % tuple(row)))
file.write('%s\n' % sep_line)
class WikiTableRenderer():
class WikiTableRenderer(TableRenderer):
def render_table(self, table: Table):
def render_table(self, table: Table, file: IO):
assert len(table.column_labels) == len(table.columns_data)
num_cols = len(table.columns_data)
num_rows = len(table.columns_data[0])
@ -69,16 +84,16 @@ class WikiTableRenderer():
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))
file.write('%s\n' % (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))
file.write('%s\n' % (line_format % tuple(row)))
def print_machine_table(machines_fqdn: List[str], inventory: Inventory):
def print_machine_table(machines_fqdn: List[str], inventory: Inventory, out_file: IO, table_renderer: Table):
table = Table()
machines_name = []
@ -99,11 +114,7 @@ def print_machine_table(machines_fqdn: List[str], inventory: Inventory):
]
table.columns_data = [machines_name, machines_spec_id, machines_serial_number]
table_renderer = MarkdownTableRenderer()
table_renderer.render_table(table)
table_renderer = WikiTableRenderer()
table_renderer.render_table(table)
table_renderer.render_table(table, out_file)
# get the list of machines hosted at the dsi
@ -146,7 +157,9 @@ def main():
machines_fqdn = get_hosted_machines_fqdn(inventory)
print('ipr machines hosted at the dsi (%d):' % len(machines_fqdn))
print_machine_table(machines_fqdn, inventory)
table_renderer = MarkdownTableRenderer()
print_machine_table(machines_fqdn, inventory, stdout, table_renderer)
if __name__ == '__main__':