Fix smart file resolver for images

This commit is contained in:
Daniel Rojas 2021-10-15 14:58:21 +02:00
parent b1fa2b9de9
commit 3a181f6f95
2 changed files with 4 additions and 6 deletions

View File

@ -69,7 +69,6 @@ class Tweak:
@dataclass
class Image:
gv_dir: InitVar[Path] # Directory of .gv file injected as context during parsing
# Attributes of the image object <img>:
src: str
scale: Optional[ImageScale] = None
@ -82,7 +81,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.
@ -98,10 +97,10 @@ class Image:
# 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

View File

@ -12,7 +12,7 @@ if __name__ == '__main__':
from wireviz.DataClasses import Metadata, Options, Tweak
from wireviz.Harness import Harness
from wireviz.wv_helper import expand, get_single_key_and_value, is_arrow, open_file_read
from wireviz.wv_helper import expand, get_single_key_and_value, is_arrow, open_file_read, smart_file_resolve
def parse_text(yaml_str: str, file_out: (str, Path) = None, return_types: (None, str, Tuple[str]) = ('html','png','svg','tsv'), image_paths: List = []) -> Any:
@ -79,7 +79,6 @@ 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 # 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)