Add ability to export PNG data directly to other programs. (#55)

This commit is contained in:
Jason 2020-07-04 11:03:04 -04:00 committed by GitHub
parent 8b067e5873
commit ebf1e5a6f2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 38 additions and 2 deletions

View File

@ -255,6 +255,15 @@ class Harness:
return dot
@property
def png(self):
from io import BytesIO
graph = self.create_graph()
data = BytesIO()
data.write(graph.pipe(format='png'))
data.seek(0)
return data.read()
def output(self, filename, directory='_output', view=False, cleanup=True, fmt='pdf', gen_bom=False):
# graphical output
graph = self.create_graph()

View File

@ -4,6 +4,7 @@
import argparse
import os
import sys
from typing import Tuple
import yaml
@ -14,7 +15,19 @@ if __name__ == '__main__':
from wireviz.Harness import Harness
def parse(yaml_input, file_out=None, generate_bom=False):
def parse(yaml_input, file_out=None, generate_bom=False, return_types: (None, str, Tuple[str]) = None):
"""
Parses yaml input string and does the high-level harness conversion
:param yaml_input: a string containing the yaml input data
:param file_out:
:param generate_bom:
:param return_types: if None, then returns None; if the value is a string, then a
corresponding data format will be returned; if the value is a tuple of strings,
then for every valid format in the `return_types` tuple, another return type
will be generated and returned in the same order; currently supports only 'png',
but could easily support other types
"""
yaml_data = yaml.safe_load(yaml_input)
@ -180,8 +193,22 @@ def parse(yaml_input, file_out=None, generate_bom=False):
else:
raise Exception('Wrong number of connection parameters')
if file_out is not None:
harness.output(filename=file_out, fmt=('png', 'svg'), gen_bom=generate_bom, view=False)
if return_types is not None:
# gather the harness data and return it as a single data type
if isinstance(return_types, str):
if return_types.lower() == 'png':
return harness.png
else:
# gather the harness data and return it as a series of tuples
returns = []
if 'png' in return_types:
returns.append(harness.png)
return tuple(returns)
def parse_file(yaml_file, file_out=None, generate_bom=False):
with open(yaml_file, 'r') as file: