From 911f3eec3e2c901627399d62049af42d33154b6a Mon Sep 17 00:00:00 2001 From: Daniel Rojas Date: Sat, 24 Oct 2020 21:01:02 +0200 Subject: [PATCH] Resolve image paths correctly --- src/wireviz/DataClasses.py | 7 +++---- src/wireviz/wireviz.py | 15 ++++++++------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/wireviz/DataClasses.py b/src/wireviz/DataClasses.py index a9ce303f..a7ff9c59 100644 --- a/src/wireviz/DataClasses.py +++ b/src/wireviz/DataClasses.py @@ -31,7 +31,6 @@ @dataclass class Image: - gv_dir: InitVar[Path] # Directory of .gv file injected as context during parsing # Attributes of the image object : src: str scale: Optional[ImageScale] = None @@ -43,7 +42,7 @@ class Image: caption: Optional[MultilineHypertext] = None # See also HTML doc at https://graphviz.org/doc/info/shapes.html#html - def __post_init__(self, gv_dir): + def __post_init__(self): if self.fixedsize is None: # Default True if any dimension specified unless self.scale also is specified. @@ -59,10 +58,10 @@ def __post_init__(self, gv_dir): # because Graphviz requires both when fixedsize=True. if self.height: if not self.width: - self.width = self.height * aspect_ratio(gv_dir.joinpath(self.src)) + self.width = self.height * aspect_ratio(self.src) else: if self.width: - self.height = self.width / aspect_ratio(gv_dir.joinpath(self.src)) + self.height = self.width / aspect_ratio(self.src) @dataclass diff --git a/src/wireviz/wireviz.py b/src/wireviz/wireviz.py index 9ecef5a0..3c8d0a82 100755 --- a/src/wireviz/wireviz.py +++ b/src/wireviz/wireviz.py @@ -17,7 +17,7 @@ from wireviz.wv_helper import expand, open_file_read -def parse(yaml_input: str, file_out: (str, Path) = None, return_types: (None, str, Tuple[str]) = None) -> Any: +def parse(yaml_input: str, file_in: (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 @@ -44,10 +44,11 @@ def parse(yaml_input: str, file_out: (str, Path) = None, return_types: (None, st if len(yaml_data[sec]) > 0: if ty == dict: for key, attribs in yaml_data[sec].items(): - # 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 + if attribs.get('image'): + image_path = attribs['image']['src'] + if not Path(image_path).is_absolute(): # resolve relative image path + image_path = (Path(file_in).parent / image_path).resolve() + attribs['image']['src'] = image_path if sec == 'connectors': if not attribs.get('autogenerate', False): @@ -209,7 +210,7 @@ def parse_file(yaml_file: str, file_out: (str, Path) = None) -> None: file_out = fn file_out = os.path.abspath(file_out) - parse(yaml_input, file_out=file_out) + parse(yaml_input, file_in=Path(yaml_file).resolve(), file_out=file_out) def parse_cmdline(): @@ -251,7 +252,7 @@ def main(): file_out = args.output_file file_out = os.path.abspath(file_out) - parse(yaml_input, file_out=file_out) + parse(yaml_input, file_in=Path(args.input_file).resolve(), file_out=file_out) if __name__ == '__main__':