Skip to content

Commit

Permalink
Only output requested file types (closes #60)
Browse files Browse the repository at this point in the history
  • Loading branch information
formatc1702 committed Oct 16, 2021
1 parent 77f668e commit d3e99ab
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 12 deletions.
34 changes: 26 additions & 8 deletions src/wireviz/Harness.py
Original file line number Diff line number Diff line change
Expand Up @@ -464,19 +464,37 @@ def svg(self):
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:
Expand Down
4 changes: 2 additions & 2 deletions src/wireviz/wireviz.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
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
Expand Down Expand Up @@ -260,7 +260,7 @@ def alternate_type(): # flip between connector and cable/arrow
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 = []
Expand Down
4 changes: 2 additions & 2 deletions src/wireviz/wv_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
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()])
Expand Down Expand Up @@ -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()

Expand Down

0 comments on commit d3e99ab

Please sign in to comment.