Only output requested file types (closes #60)

This commit is contained in:
Daniel Rojas 2021-10-02 13:31:20 +02:00
parent 77f668e553
commit d3e99abaa8
3 changed files with 30 additions and 12 deletions

View File

@ -464,19 +464,37 @@ class Harness:
data.seek(0) data.seek(0)
return data.read() return data.read()
def output(self, filename: (str, Path), view: bool = False, cleanup: bool = True, fmt: tuple = ('pdf', )) -> None: def output(self, filename: (str, Path), view: bool = False, cleanup: bool = True, fmt: tuple = ('gv','html','png','svg','tsv')) -> None:
# graphical output # graphical output
graph = self.create_graph() graph = self.create_graph()
svg_already_exists = Path(f'{filename}.svg').exists() # if SVG already exists, do not delete later
# graphical output
for f in fmt: for f in fmt:
graph.format = f if f in ('png', 'svg', 'html'):
graph.render(filename=filename, view=view, cleanup=cleanup) if f == 'html': # if HTML format is specified,
graph.save(filename=f'{filename}.gv') f = 'svg' # generate SVG for embedding into HTML
# bom output # TODO: prevent rendering SVG twice when both SVG and HTML are specified
graph.format = f
graph.render(filename=filename, view=view, cleanup=cleanup)
# GraphViz output
if 'gv' in fmt:
graph.save(filename=f'{filename}.gv')
# BOM output
bomlist = bom_list(self.bom()) bomlist = bom_list(self.bom())
with open_file_write(f'{filename}.bom.tsv') as file: if 'tsv' in fmt:
file.write(tuplelist2tsv(bomlist)) with open_file_write(f'{filename}.bom.tsv') as file:
file.write(tuplelist2tsv(bomlist))
if 'csv' in fmt:
print('CSV output is not yet supported') # TODO: implement CSV output (preferrably using CSV library)
# HTML output # HTML output
generate_html_output(filename, bomlist, self.metadata, self.options) if 'html' in fmt:
generate_html_output(filename, bomlist, self.metadata, self.options)
# PDF output
if 'pdf' in fmt:
print('PDF output is not yet supported') # TODO: implement PDF output
# delete SVG if not needed
if 'html' in fmt and not 'svg' in fmt and not svg_already_exists:
Path(f'{filename}.svg').unlink()
def bom(self): def bom(self):
if not self._bom: if not self._bom:

View File

@ -15,7 +15,7 @@ from wireviz.Harness import Harness
from wireviz.wv_helper import expand, get_single_key_and_value, is_arrow, open_file_read from wireviz.wv_helper import expand, get_single_key_and_value, is_arrow, open_file_read
def parse(yaml_input: str, file_out: (str, Path) = None, return_types: (None, str, Tuple[str]) = None) -> Any: def parse(yaml_input: str, file_out: (str, Path) = None, return_types: (None, str, Tuple[str]) = ('gv','html','png','svg','tsv')) -> Any:
""" """
Parses yaml input string and does the high-level harness conversion Parses yaml input string and does the high-level harness conversion
@ -260,7 +260,7 @@ def parse(yaml_input: str, file_out: (str, Path) = None, return_types: (None, st
harness.add_bom_item(line) harness.add_bom_item(line)
if file_out is not None: if file_out is not None:
harness.output(filename=file_out, fmt=('png', 'svg'), view=False) harness.output(filename=file_out, fmt=return_types, view=False)
if return_types is not None: if return_types is not None:
returns = [] returns = []

View File

@ -8,7 +8,7 @@ from wireviz import APP_NAME, __version__
import wireviz.wireviz as wv import wireviz.wireviz as wv
from wireviz.wv_helper import open_file_read from wireviz.wv_helper import open_file_read
format_codes = {'p': 'png', 's': 'svg', 't': 'tsv', 'c': 'csv', 'h': 'html', 'P': 'pdf'} format_codes = {'c': 'csv', 'g': 'gv', 'p': 'png', 's': 'svg', 't': 'tsv', 'c': 'csv', 'h': 'html', 'P': 'pdf'}
epilog = 'The -f or --format option accepts a string containing one or more of the following characters to specify which file types to output:\n' epilog = 'The -f or --format option accepts a string containing one or more of the following characters to specify which file types to output:\n'
epilog += ', '.join([f'{key} ({value.upper()})' for key, value in format_codes.items()]) epilog += ', '.join([f'{key} ({value.upper()})' for key, value in format_codes.items()])
@ -74,7 +74,7 @@ def main(file, format, prepend, output_file, version):
yaml_input = prepend_input + yaml_input yaml_input = prepend_input + yaml_input
wv.parse(yaml_input, file_out=file_out) wv.parse(yaml_input, file_out=file_out, return_types=return_types)
print() print()