Allow specifying output directory and file name separately

This commit is contained in:
Daniel Rojas 2021-10-16 13:30:56 +02:00
parent d7d7854bce
commit e3530702f2
2 changed files with 47 additions and 20 deletions

View File

@ -24,14 +24,18 @@ from wireviz.wv_helper import (
def parse( def parse(
inp: (str, Path, Dict), inp: (str, Path, Dict),
output_formats: (None, str, Tuple[str]) = None, 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, return_types: (None, str, Tuple[str]) = None,
image_paths: List = [], image_paths: List = [],
) -> Any: ) -> Any:
yaml_data, yaml_path = get_yaml_data_and_path(inp) yaml_data, yaml_file = get_yaml_data_and_path(inp)
if output_formats: # need to write data to file, determine output path if output_formats:
output_path = get_output_path(yaml_path, output_path) # 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 ========================================================= # define variables =========================================================
# containers for parsed component data and connection sets # containers for parsed component data and connection sets
@ -297,7 +301,7 @@ def parse(
harness.add_bom_item(line) harness.add_bom_item(line)
if output_formats: 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: if return_types:
returns = [] returns = []
@ -341,15 +345,26 @@ def get_yaml_data_and_path(inp: (str, Path, Dict)) -> (Dict, Path):
return yaml_data, yaml_path return yaml_data, yaml_path
def get_output_path(input_path: Path, default_output_path: Path) -> Path: def get_output_dir(input_file: Path, default_output_dir: Path) -> Path:
if default_output_path: # user-specified output path if default_output_dir: # user-specified output directory
output_path = Path(default_output_path) output_dir = Path(default_output_dir)
else: # auto-determine appropriate output path else: # auto-determine appropriate output directory
if input_path: # input comes from a file; place output in same directory if input_file: # input comes from a file; place output in same directory
output_path = input_path.parent output_dir = input_file.parent
else: # input comes from str or dict, fall back to cwd else: # input comes from str or Dict; fall back to cwd
output_path = Path.cwd() output_dir = Path.cwd()
return output_path.resolve() 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(): def main():

View File

@ -47,10 +47,17 @@ epilog += ", ".join([f"{key} ({value.upper()})" for key, value in format_codes.i
) )
@click.option( @click.option(
"-o", "-o",
"--output-file", "--output-dir",
default=None, default=None,
type=Path, 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( @click.option(
"-V", "-V",
@ -59,7 +66,7 @@ epilog += ", ".join([f"{key} ({value.upper()})" for key, value in format_codes.i
default=False, default=False,
help=f"Output {APP_NAME} version and exit.", 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. 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(): if not file.exists():
raise Exception(f"File does not exist:\n{file}") 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("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: with open_file_read(file) as file_handle:
yaml_input = file_handle.read() yaml_input = file_handle.read()
@ -124,8 +135,9 @@ def wireviz(file, format, prepend, output_file, version):
wv.parse( wv.parse(
yaml_input, yaml_input,
file_out=file_out,
output_formats=output_formats, output_formats=output_formats,
output_dir=_output_dir,
output_name=_output_name,
image_paths=[file_dir, prepend_dir], image_paths=[file_dir, prepend_dir],
) )