Only output requested file types (closes #60)
This commit is contained in:
parent
77f668e553
commit
d3e99abaa8
@ -464,19 +464,37 @@ class Harness:
|
||||
data.seek(0)
|
||||
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
|
||||
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:
|
||||
graph.format = f
|
||||
graph.render(filename=filename, view=view, cleanup=cleanup)
|
||||
graph.save(filename=f'{filename}.gv')
|
||||
# bom output
|
||||
if f in ('png', 'svg', 'html'):
|
||||
if f == 'html': # if HTML format is specified,
|
||||
f = 'svg' # generate SVG for embedding into HTML
|
||||
# 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())
|
||||
with open_file_write(f'{filename}.bom.tsv') as file:
|
||||
file.write(tuplelist2tsv(bomlist))
|
||||
if 'tsv' in fmt:
|
||||
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
|
||||
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):
|
||||
if not self._bom:
|
||||
|
||||
@ -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
|
||||
|
||||
|
||||
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
|
||||
|
||||
@ -260,7 +260,7 @@ def parse(yaml_input: str, file_out: (str, Path) = None, return_types: (None, st
|
||||
harness.add_bom_item(line)
|
||||
|
||||
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:
|
||||
returns = []
|
||||
|
||||
@ -8,7 +8,7 @@ from wireviz import APP_NAME, __version__
|
||||
import wireviz.wireviz as wv
|
||||
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 += ', '.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
|
||||
|
||||
wv.parse(yaml_input, file_out=file_out)
|
||||
wv.parse(yaml_input, file_out=file_out, return_types=return_types)
|
||||
|
||||
print()
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user