From e3530702f24e8f73caed2d5794fadcc4bf815cc7 Mon Sep 17 00:00:00 2001 From: Daniel Rojas Date: Sat, 16 Oct 2021 13:30:56 +0200 Subject: [PATCH] Allow specifying output directory and file name separately --- src/wireviz/wireviz.py | 43 ++++++++++++++++++++++++++++-------------- src/wireviz/wv_cli.py | 24 +++++++++++++++++------ 2 files changed, 47 insertions(+), 20 deletions(-) diff --git a/src/wireviz/wireviz.py b/src/wireviz/wireviz.py index 636e57a..5ce6250 100755 --- a/src/wireviz/wireviz.py +++ b/src/wireviz/wireviz.py @@ -24,14 +24,18 @@ from wireviz.wv_helper import ( def parse( inp: (str, Path, Dict), output_formats: (None, str, Tuple[str]) = None, - output_path: (str, Path) = None, + output_dir: (str, Path) = None, + output_name: str = None, return_types: (None, str, Tuple[str]) = None, image_paths: List = [], ) -> Any: - yaml_data, yaml_path = get_yaml_data_and_path(inp) - if output_formats: # need to write data to file, determine output path - output_path = get_output_path(yaml_path, output_path) + yaml_data, yaml_file = get_yaml_data_and_path(inp) + if output_formats: + # need to write data to file, determine output directory and filename + output_dir = get_output_dir(yaml_file, output_dir) + output_name = get_output_name(yaml_file, output_name) + output_file = output_dir / output_name # define variables ========================================================= # containers for parsed component data and connection sets @@ -297,7 +301,7 @@ def parse( harness.add_bom_item(line) if output_formats: - harness.output(filename=file_out, fmt=output_formats, view=False) + harness.output(filename=output_file, fmt=output_formats, view=False) if return_types: returns = [] @@ -341,15 +345,26 @@ def get_yaml_data_and_path(inp: (str, Path, Dict)) -> (Dict, Path): return yaml_data, yaml_path -def get_output_path(input_path: Path, default_output_path: Path) -> Path: - if default_output_path: # user-specified output path - output_path = Path(default_output_path) - else: # auto-determine appropriate output path - if input_path: # input comes from a file; place output in same directory - output_path = input_path.parent - else: # input comes from str or dict, fall back to cwd - output_path = Path.cwd() - return output_path.resolve() +def get_output_dir(input_file: Path, default_output_dir: Path) -> Path: + if default_output_dir: # user-specified output directory + output_dir = Path(default_output_dir) + else: # auto-determine appropriate output directory + if input_file: # input comes from a file; place output in same directory + output_dir = input_file.parent + else: # input comes from str or Dict; fall back to cwd + output_dir = Path.cwd() + return output_dir.resolve() + + +def get_output_name(input_file: Path, default_output_name: Path) -> str: + if default_output_name: # user-specified output name + output_name = default_output_name + else: # auto-determine appropriate output name + if input_file: # input comes from a file; use same file stem + output_name = input_file.stem + else: # input comes from str or Dict; no fallback available + raise Exception("No output file name provided") + return output_name def main(): diff --git a/src/wireviz/wv_cli.py b/src/wireviz/wv_cli.py index 472c089..6632b90 100644 --- a/src/wireviz/wv_cli.py +++ b/src/wireviz/wv_cli.py @@ -47,10 +47,17 @@ epilog += ", ".join([f"{key} ({value.upper()})" for key, value in format_codes.i ) @click.option( "-o", - "--output-file", + "--output-dir", default=None, type=Path, - help="File name (without extension) to use for output, if different from input file name.", + help="Directory to use for output files, if different from input file directory.", +) +@click.option( + "-O", + "--output-name", + default=None, + type=str, + help="File name (without extension) to use for output files, if different from input file name.", ) @click.option( "-V", @@ -59,7 +66,7 @@ epilog += ", ".join([f"{key} ({value.upper()})" for key, value in format_codes.i default=False, help=f"Output {APP_NAME} version and exit.", ) -def wireviz(file, format, prepend, output_file, version): +def wireviz(file, format, prepend, output_dir, output_name, version): """ Parses the provided FILE and generates the specified outputs. """ @@ -111,10 +118,14 @@ def wireviz(file, format, prepend, output_file, version): if not file.exists(): raise Exception(f"File does not exist:\n{file}") - file_out = file.with_suffix("") if not output_file else output_file + # file_out = file.with_suffix("") if not output_file else output_file + _output_dir = file.parent if not output_dir else output_dir + _output_name = file.stem if not output_name else output_name print("Input file: ", file) - print("Output file: ", f"{file_out}.{output_formats_str}") + print( + "Output file: ", f"{Path(_output_dir / _output_name)}.{output_formats_str}" + ) with open_file_read(file) as file_handle: yaml_input = file_handle.read() @@ -124,8 +135,9 @@ def wireviz(file, format, prepend, output_file, version): wv.parse( yaml_input, - file_out=file_out, output_formats=output_formats, + output_dir=_output_dir, + output_name=_output_name, image_paths=[file_dir, prepend_dir], )