Add more robust routines for BOM file output
This commit is contained in:
parent
a632dc6cb8
commit
c2d96e8e4c
@ -3,9 +3,9 @@
|
|||||||
|
|
||||||
from wireviz.DataClasses import Connector, Cable
|
from wireviz.DataClasses import Connector, Cable
|
||||||
from graphviz import Graph
|
from graphviz import Graph
|
||||||
from wireviz import wv_colors, wv_helper
|
from wireviz import wv_colors, wv_helper, bom_helper
|
||||||
from wireviz.wv_colors import get_color_hex
|
from wireviz.wv_colors import get_color_hex
|
||||||
from wireviz.wv_helper import awg_equiv, mm2_equiv, tuplelist2tsv, \
|
from wireviz.wv_helper import awg_equiv, mm2_equiv, \
|
||||||
nested_html_table, flatten2d, index_if_list, html_line_breaks, \
|
nested_html_table, flatten2d, index_if_list, html_line_breaks, \
|
||||||
graphviz_line_breaks, remove_line_breaks, open_file_read, open_file_write
|
graphviz_line_breaks, remove_line_breaks, open_file_read, open_file_write
|
||||||
from collections import Counter
|
from collections import Counter
|
||||||
@ -297,8 +297,8 @@ class Harness:
|
|||||||
graph.save(filename=f'{filename}.gv')
|
graph.save(filename=f'{filename}.gv')
|
||||||
# bom output
|
# bom output
|
||||||
bom_list = self.bom_list()
|
bom_list = self.bom_list()
|
||||||
with open_file_write(f'{filename}.bom.tsv') as file:
|
# todo: support user choices of BOM format (probably also graphviz outputs, html outputs)
|
||||||
file.write(tuplelist2tsv(bom_list))
|
bom_helper.generate_bom_outputs(filename,bom_list,bom_helper.WIREVIZ_TSV, bom_helper.EXCEL_CSV)
|
||||||
# HTML output
|
# HTML output
|
||||||
with open_file_write(f'{filename}.html') as file:
|
with open_file_write(f'{filename}.html') as file:
|
||||||
file.write('<!DOCTYPE html>\n')
|
file.write('<!DOCTYPE html>\n')
|
||||||
|
|||||||
42
src/wireviz/bom_helper.py
Normal file
42
src/wireviz/bom_helper.py
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
|
import csv
|
||||||
|
from wireviz import wv_helper
|
||||||
|
from wireviz.wv_helper import open_file_write
|
||||||
|
|
||||||
|
EXCEL_CSV = csv.excel
|
||||||
|
EXCEL_TSV = csv.excel_tab
|
||||||
|
UNIX_CSV = csv.unix_dialect
|
||||||
|
WIREVIZ_TSV = type('Wireviz BOM', (csv.Dialect, object), dict(
|
||||||
|
delimiter='\t',
|
||||||
|
doublequote=True,
|
||||||
|
escapechar=None,
|
||||||
|
lineterminator='\n',
|
||||||
|
quoting=0,
|
||||||
|
skipinitialspace=False,
|
||||||
|
strict=False,
|
||||||
|
quotechar='"'
|
||||||
|
))
|
||||||
|
csv.register_dialect('Wireviz BOM', WIREVIZ_TSV)
|
||||||
|
|
||||||
|
_csv_formats = { EXCEL_CSV, UNIX_CSV }
|
||||||
|
_tsv_formats = { EXCEL_TSV, WIREVIZ_TSV }
|
||||||
|
|
||||||
|
_csv_ext = '.bom.csv'
|
||||||
|
_tsv_ext = '.bom.tsv'
|
||||||
|
|
||||||
|
def generate_bom_outputs(base_filename, bomdata, *argv):
|
||||||
|
expanded_csv_names = len(_csv_formats.intersection(set(argv))) > 1
|
||||||
|
expanded_tsv_names = len(_tsv_formats.intersection(set(argv))) > 1
|
||||||
|
for fmt in argv:
|
||||||
|
if fmt in _csv_formats:
|
||||||
|
file = csv.writer(open_file_write(base_filename + ("_" + fmt.__name__ if expanded_csv_names else "") + _csv_ext, fmt.lineterminator), fmt)
|
||||||
|
|
||||||
|
elif fmt in _tsv_formats:
|
||||||
|
file = csv.writer(open_file_write(base_filename + ("_"+fmt.__name__ if expanded_tsv_names else "") + _tsv_ext, fmt.lineterminator), fmt)
|
||||||
|
else:
|
||||||
|
raise KeyError("Unknown BOM Format Specified")
|
||||||
|
file.writerows(wv_helper.flatten2d(bomdata))
|
||||||
|
|
||||||
|
# TODO: Possibly refactor other BOM output operations, such as HTML, into here?
|
||||||
@ -74,10 +74,11 @@ def build_tutorials():
|
|||||||
|
|
||||||
file.write(f'\n\n')
|
file.write(f'\n\n')
|
||||||
|
|
||||||
file.write(f'[Bill of Materials](tutorial{outfile_name}.bom.tsv)\n\n\n')
|
file.write(f'[Bill of Materials - TSV](tutorial{outfile_name}.bom.tsv)\n\n')
|
||||||
|
file.write(f'[Bill of Materials - CSV](tutorial{outfile_name}.bom.csv)\n\n\n')
|
||||||
|
|
||||||
def clean_examples():
|
def clean_examples():
|
||||||
generated_extensions = ['.gv', '.png', '.svg', '.html', '.bom.tsv']
|
generated_extensions = ['.gv', '.png', '.svg', '.html', '.bom.tsv', '.bom.csv']
|
||||||
|
|
||||||
for filepath in [examples_path, demos_path, tutorials_path]:
|
for filepath in [examples_path, demos_path, tutorials_path]:
|
||||||
print(filepath)
|
print(filepath)
|
||||||
|
|||||||
@ -116,5 +116,5 @@ def open_file_read(filename):
|
|||||||
# TODO: Intelligently determine encoding
|
# TODO: Intelligently determine encoding
|
||||||
return open(filename, 'r', encoding='UTF-8')
|
return open(filename, 'r', encoding='UTF-8')
|
||||||
|
|
||||||
def open_file_write(filename):
|
def open_file_write(filename, newline='\n'):
|
||||||
return open(filename, 'w', encoding='UTF-8')
|
return open(filename, 'w', encoding='UTF-8', newline=newline)
|
||||||
Loading…
x
Reference in New Issue
Block a user