Resolve image paths correctly

This commit is contained in:
Daniel Rojas 2020-10-24 21:01:02 +02:00
parent 96bd121403
commit a7e75a05e3
2 changed files with 11 additions and 11 deletions

View File

@ -32,7 +32,6 @@ OneOrMoreWires = Union[Wire, Tuple[Wire, ...]] # One or a tuple of wires
@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
@ -44,7 +43,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.
@ -60,10 +59,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

@ -17,7 +17,7 @@ from wireviz.Harness import Harness
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__':