From d3e99abaa8421c6849f326562799d615b39a3e01 Mon Sep 17 00:00:00 2001
From: Daniel Rojas <github@danielrojas.net>
Date: Sat, 2 Oct 2021 13:31:20 +0200
Subject: [PATCH] Only output requested file types (closes #60)

---
 src/wireviz/Harness.py | 34 ++++++++++++++++++++++++++--------
 src/wireviz/wireviz.py |  4 ++--
 src/wireviz/wv_cli.py  |  4 ++--
 3 files changed, 30 insertions(+), 12 deletions(-)

diff --git a/src/wireviz/Harness.py b/src/wireviz/Harness.py
index 5ef25307..09ea3b36 100644
--- a/src/wireviz/Harness.py
+++ b/src/wireviz/Harness.py
@@ -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:
diff --git a/src/wireviz/wireviz.py b/src/wireviz/wireviz.py
index 8cef2242..f78f8e47 100755
--- a/src/wireviz/wireviz.py
+++ b/src/wireviz/wireviz.py
@@ -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
 
@@ -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 = []
diff --git a/src/wireviz/wv_cli.py b/src/wireviz/wv_cli.py
index 4bd0eba5..17bd4069 100644
--- a/src/wireviz/wv_cli.py
+++ b/src/wireviz/wv_cli.py
@@ -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()])
@@ -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()