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 @dataclass
class Image: class Image:
gv_dir: InitVar[Path] # Directory of .gv file injected as context during parsing
# Attributes of the image object <img>: # Attributes of the image object <img>:
src: str src: str
scale: Optional[ImageScale] = None scale: Optional[ImageScale] = None
@ -82,7 +81,7 @@ class Image:
caption: Optional[MultilineHypertext] = None caption: Optional[MultilineHypertext] = None
# See also HTML doc at https://graphviz.org/doc/info/shapes.html#html # 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: if self.fixedsize is None:
# Default True if any dimension specified unless self.scale also is specified. # 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. # because Graphviz requires both when fixedsize=True.
if self.height: if self.height:
if not self.width: if not self.width:
self.width = self.height * aspect_ratio(gv_dir.joinpath(self.src)) self.width = self.height * aspect_ratio(self.src)
else: else:
if self.width: if self.width:
self.height = self.width / aspect_ratio(gv_dir.joinpath(self.src)) self.height = self.width / aspect_ratio(self.src)
@dataclass @dataclass

View File

@ -12,7 +12,7 @@ if __name__ == '__main__':
from wireviz.DataClasses import Metadata, Options, Tweak from wireviz.DataClasses import Metadata, Options, Tweak
from wireviz.Harness import Harness 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: 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. # The Image dataclass might need to open an image file with a relative path.
image = attribs.get('image') image = attribs.get('image')
if isinstance(image, dict): if isinstance(image, dict):
image['gv_dir'] = Path(file_out if file_out else '').parent # Inject context # TODO: remove
image_path = image['src'] image_path = image['src']
if image_path and not Path(image_path).is_absolute(): # resolve relative image path if image_path and not Path(image_path).is_absolute(): # resolve relative image path
image['src'] = smart_file_resolve(image_path, image_paths) image['src'] = smart_file_resolve(image_path, image_paths)