Split file output logic and object return logic

Use `output_formats` parameter to specify which *files* to output to disk,
Use `return_types` parameter to specify which objects to return to a calling Python script
This commit is contained in:
Daniel Rojas 2021-10-15 16:36:49 +02:00
parent 3dae1cbca2
commit fb2aae5515
2 changed files with 12 additions and 10 deletions

View File

@ -15,12 +15,13 @@ from wireviz.Harness import Harness
from wireviz.wv_helper import expand, get_single_key_and_value, is_arrow, open_file_read, smart_file_resolve
def parse_text(yaml_str: str, file_out: (str, Path) = None, return_types: (None, str, Tuple[str]) = ('html','png','svg','tsv'), image_paths: List = []) -> Any:
def parse_text(yaml_str: str, file_out: (str, Path) = None, output_formats: (None, str, Tuple[str]) = ('html','png','svg','tsv'), return_types: (None, str, Tuple[str]) = None, image_paths: List = []) -> Any:
"""
Parses a YAML input string and does the high-level harness conversion
:param yaml_input: a string containing the YAML input data
:param file_out:
:param output_formats:
: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
@ -30,14 +31,15 @@ def parse_text(yaml_str: str, file_out: (str, Path) = None, return_types: (None,
- "harness" - will return the `Harness` instance
"""
yaml_data = yaml.safe_load(yaml_str)
return parse(yaml_data=yaml_data, file_out=file_out, return_types=return_types, image_paths=image_paths)
return parse(yaml_data=yaml_data, file_out=file_out, output_formats=output_formats, return_types=return_types, image_paths=image_paths)
def parse(yaml_data: Dict, file_out: (str, Path) = None, return_types: (None, str, Tuple[str]) = ('html','png','svg','tsv'), image_paths: List = []) -> Any:
def parse(yaml_data: Dict, file_out: (str, Path) = None, output_formats: (None, str, Tuple[str]) = ('html','png','svg','tsv'), return_types: (None, str, Tuple[str]) = None, image_paths: List = []) -> Any:
"""
Parses a YAML dictionary and does the high-level harness conversion
:param yaml_data: a dictionary containing the YAML data
:param file_out:
:param output_formats:
: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
@ -273,7 +275,7 @@ def parse(yaml_data: Dict, 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=return_types, view=False)
harness.output(filename=file_out, fmt=output_formats, view=False)
if return_types is not None:
returns = []

View File

@ -42,14 +42,14 @@ def wireviz(file, format, prepend, output_file, version):
filepaths = list(file)
# determine output formats
return_types = []
output_formats = []
for code in format:
if code in format_codes:
return_types.append(format_codes[code])
output_formats.append(format_codes[code])
else:
raise Exception(f'Unknown output format: {code}')
return_types = tuple(sorted(set(return_types)))
return_types_str = f'[{"|".join(return_types)}]' if len(return_types) > 1 else return_types[0]
output_formats = tuple(sorted(set(output_formats)))
output_formats_str = f'[{"|".join(output_formats)}]' if len(output_formats) > 1 else output_formats[0]
image_paths = []
# check prepend file
@ -75,7 +75,7 @@ def wireviz(file, format, prepend, output_file, version):
file_out = file.with_suffix('') if not output_file else output_file
print('Input file: ', file)
print('Output file: ', f'{file_out}.{return_types_str}')
print('Output file: ', f'{file_out}.{output_formats_str}')
with open_file_read(file) as file_handle:
yaml_input = file_handle.read()
@ -83,7 +83,7 @@ def wireviz(file, format, prepend, output_file, version):
yaml_input = prepend_input + yaml_input
wv.parse_text(yaml_input, file_out=file_out, return_types=return_types, image_paths=[file_dir, prepend_dir])
wv.parse_text(yaml_input, file_out=file_out, output_formats=output_formats, image_paths=[file_dir, prepend_dir])
print()