Skip to content

Commit

Permalink
Resolve image paths correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
formatc1702 committed Oct 24, 2020
1 parent e2e8bbf commit 0bdf57b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
7 changes: 3 additions & 4 deletions src/wireviz/DataClasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

@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[str] = None # false | true | width | height | both
Expand All @@ -26,7 +25,7 @@ class Image:
caption: Optional[str] = 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.
Expand All @@ -42,10 +41,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
class AdditionalComponent:
Expand Down
15 changes: 8 additions & 7 deletions src/wireviz/wireviz.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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):
Expand Down Expand Up @@ -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():
Expand Down Expand Up @@ -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__':
Expand Down

0 comments on commit 0bdf57b

Please sign in to comment.