From 6a08988ca937f007598fc0da8dfe8059c520d7e1 Mon Sep 17 00:00:00 2001 From: Daniel Rojas Date: Sat, 2 Oct 2021 14:41:26 +0200 Subject: [PATCH] Implement image path resolver --- src/wireviz/wireviz.py | 19 ++++++++++--------- src/wireviz/wv_cli.py | 6 +++++- src/wireviz/wv_helper.py | 2 +- 3 files changed, 16 insertions(+), 11 deletions(-) diff --git a/src/wireviz/wireviz.py b/src/wireviz/wireviz.py index 37d5a5c..c8bf2c3 100755 --- a/src/wireviz/wireviz.py +++ b/src/wireviz/wireviz.py @@ -3,7 +3,7 @@ from pathlib import Path import sys -from typing import Any, Dict, Tuple +from typing import Any, Dict, List, Tuple import yaml @@ -15,7 +15,7 @@ from wireviz.Harness import Harness from wireviz.wv_helper import expand, get_single_key_and_value, is_arrow, open_file_read -def parse_text(yaml_str: str, file_out: (str, Path) = None, return_types: (None, str, Tuple[str]) = ('gv','html','png','svg','tsv')) -> Any: +def parse_text(yaml_str: str, file_out: (str, Path) = None, return_types: (None, str, Tuple[str]) = ('gv','html','png','svg','tsv'), image_paths: List = []) -> Any: """ Parses a YAML input string and does the high-level harness conversion @@ -30,9 +30,9 @@ 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 ) + return parse(yaml_data=yaml_data, file_out=file_out, return_types=return_types, image_paths=image_paths) -def parse(yaml_data: Dict, file_out: (str, Path) = None, return_types: (None, str, Tuple[str]) = ('gv','html','png','svg','tsv')) -> Any: +def parse(yaml_data: Dict, file_out: (str, Path) = None, return_types: (None, str, Tuple[str]) = ('gv','html','png','svg','tsv'), image_paths: List = []) -> Any: """ Parses a YAML dictionary and does the high-level harness conversion @@ -79,9 +79,10 @@ def parse(yaml_data: Dict, file_out: (str, Path) = None, return_types: (None, st # The Image dataclass might need to open an image file with a relative path. image = attribs.get('image') if isinstance(image, dict): - image['gv_dir'] = Path(file_out if file_out else '').parent # Inject context - - # store component templates only; do not generate instances yet + image['gv_dir'] = Path(file_out if file_out else '').parent # Inject context # TODO: remove + image_path = image['src'] + if image_path and not Path(image_path).is_absolute(): # resolve relative image path + image['src'] = smart_file_resolve(image_path, image_paths) if sec == 'connectors': template_connectors[key] = attribs elif sec == 'cables': @@ -298,7 +299,7 @@ def parse(yaml_data: Dict, file_out: (str, Path) = None, return_types: (None, st def parse_file(yaml_file: str, file_out: (str, Path) = None) -> None: yaml_file = Path(yaml_file) with open_file_read(yaml_file) as file: - yaml_input = file.read() + yaml_str = file.read() if file_out: file_out = Path(file_out) @@ -306,7 +307,7 @@ def parse_file(yaml_file: str, file_out: (str, Path) = None) -> None: file_out = yaml_file.parent / yaml_file.stem file_out = file_out.resolve() - parse(yaml_input, file_out=file_out) + parse_text(yaml_str, file_out=file_out, image_paths=[Path(yaml_file).parent]) if __name__ == '__main__': print('When running from the command line, please use wv_cli.py instead.') diff --git a/src/wireviz/wv_cli.py b/src/wireviz/wv_cli.py index 6f173de..c9e2a3e 100644 --- a/src/wireviz/wv_cli.py +++ b/src/wireviz/wv_cli.py @@ -49,6 +49,7 @@ def wireviz(file, format, prepend, output_file, version): return_types = tuple(sorted(set(return_types))) return_types_str = f'[{"|".join(return_types)}]' if len(return_types) > 1 else return_types[0] + image_paths = [] # check prepend file if prepend: prepend = Path(prepend) @@ -58,8 +59,10 @@ def wireviz(file, format, prepend, output_file, version): with open_file_read(prepend) as file_handle: prepend_input = file_handle.read() + '\n' + prepend_dir = prepend.parent else: prepend_input = '' + prepend_dir = None # run WireVIz on each input file for file in filepaths: @@ -74,10 +77,11 @@ def wireviz(file, format, prepend, output_file, version): with open_file_read(file) as file_handle: yaml_input = file_handle.read() + file_dir = file.parent yaml_input = prepend_input + yaml_input - wv.parse_text(yaml_input, file_out=file_out, return_types=return_types) + wv.parse_text(yaml_input, file_out=file_out, return_types=return_types, image_paths=[file_dir, prepend_dir]) print() diff --git a/src/wireviz/wv_helper.py b/src/wireviz/wv_helper.py index 423e974..01bccaa 100644 --- a/src/wireviz/wv_helper.py +++ b/src/wireviz/wv_helper.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- -from typing import List +from typing import Dict, List from pathlib import Path import re