Change parse() function's arguments`

Input to `parse()` used to be `yaml_input` for the actual YAML, and `file_in` for the YAML file path.
THe latter was only used to resolve relative image paths, not for reading the actual file that was passed.
Therefore, `file_in` was changed to `base_path`; this argument receives the *directory* of the original YAML file, which is more intuitive for the actual use case.
This commit is contained in:
Daniel Rojas 2020-12-06 13:30:01 +01:00
parent 2d7770a755
commit 6a42a30523

View File

@ -17,12 +17,13 @@ from wireviz.Harness import Harness
from wireviz.wv_helper import expand, open_file_read from wireviz.wv_helper import expand, open_file_read
def parse(yaml_input: str, file_in: (str, Path) = None, file_out: (str, Path) = None, return_types: (None, str, Tuple[str]) = None) -> Any: def parse(yaml_input: str, base_path: (str, Path) = None, file_out: (str, Path) = None, return_types: (None, str, Tuple[str]) = None) -> Any:
""" """
Parses yaml input string and does the high-level harness conversion Parses yaml input string and does the high-level harness conversion
:param yaml_input: a string containing the yaml input data :param yaml_input: a string containing the yaml input data
:param file_out: :param base_path: base path used to resolve any relative paths to image files
:param file_out: filename of the generated output
:param return_types: if None, then returns None; if the value is a string, then a :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, 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 then for every valid format in the `return_types` tuple, another return type
@ -47,7 +48,7 @@ def parse(yaml_input: str, file_in: (str, Path) = None, file_out: (str, Path) =
if attribs.get('image'): if attribs.get('image'):
image_path = attribs['image']['src'] image_path = attribs['image']['src']
if not Path(image_path).is_absolute(): # resolve relative image path if not Path(image_path).is_absolute(): # resolve relative image path
image_path = (Path(file_in).parent / image_path).resolve() image_path = (Path(base_path) / image_path).resolve()
attribs['image']['src'] = image_path attribs['image']['src'] = image_path
if sec == 'connectors': if sec == 'connectors':
@ -210,7 +211,7 @@ def parse_file(yaml_file: str, file_out: (str, Path) = None) -> None:
file_out = fn file_out = fn
file_out = os.path.abspath(file_out) file_out = os.path.abspath(file_out)
parse(yaml_input, file_in=Path(yaml_file).resolve(), file_out=file_out) parse(yaml_input, base_path=Path(yaml_file).parent, file_out=file_out)
def parse_cmdline(): def parse_cmdline():
@ -252,7 +253,7 @@ def main():
file_out = args.output_file file_out = args.output_file
file_out = os.path.abspath(file_out) file_out = os.path.abspath(file_out)
parse(yaml_input, file_in=Path(args.input_file).resolve(), file_out=file_out) parse(yaml_input, base_path=Path(args.input_file).parent, file_out=file_out)
if __name__ == '__main__': if __name__ == '__main__':