Skip to content

Commit

Permalink
Implement image path resolver
Browse files Browse the repository at this point in the history
  • Loading branch information
17o2 authored and laurierloi committed Jan 19, 2023
1 parent 471a6c3 commit a4a89eb
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 11 deletions.
19 changes: 10 additions & 9 deletions src/wireviz/wireviz.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from pathlib import Path
import sys
from typing import Any, Dict, Tuple
from typing import Any, Dict, List, Tuple

import yaml

Expand All @@ -15,7 +15,7 @@
from wireviz.wv_helper import expand, get_single_key_and_value, is_arrow, open_file_read


def parse_text(yaml_str: str, file_out: (str, Path) = None, return_types: (None, str, Tuple[str]) = ('gv','html','png','svg','tsv')) -> Any:
def parse_text(yaml_str: str, file_out: (str, Path) = None, return_types: (None, str, Tuple[str]) = ('gv','html','png','svg','tsv'), image_paths: List = []) -> Any:
"""
Parses a YAML input string and does the high-level harness conversion
Expand All @@ -30,9 +30,9 @@ def parse_text(yaml_str: str, file_out: (str, Path) = None, return_types: (None,
- "harness" - will return the `Harness` instance
"""
yaml_data = yaml.safe_load(yaml_str)
return parse(yaml_data=yaml_data, file_out=file_out, return_types=return_types )
return parse(yaml_data=yaml_data, file_out=file_out, return_types=return_types, image_paths=image_paths)

def parse(yaml_data: Dict, file_out: (str, Path) = None, return_types: (None, str, Tuple[str]) = ('gv','html','png','svg','tsv')) -> Any:
def parse(yaml_data: Dict, file_out: (str, Path) = None, return_types: (None, str, Tuple[str]) = ('gv','html','png','svg','tsv'), image_paths: List = []) -> Any:
"""
Parses a YAML dictionary and does the high-level harness conversion
Expand Down Expand Up @@ -82,9 +82,10 @@ 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

# store component templates only; do not generate instances yet
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)
if sec == 'connectors':
template_connectors[key] = attribs
elif sec == 'cables':
Expand Down Expand Up @@ -301,15 +302,15 @@ def alternate_type(): # flip between connector and cable/arrow
def parse_file(yaml_file: str, file_out: (str, Path) = None) -> None:
yaml_file = Path(yaml_file)
with open_file_read(yaml_file) as file:
yaml_input = file.read()
yaml_str = file.read()

if file_out:
file_out = Path(file_out)
else:
file_out = yaml_file.parent / yaml_file.stem
file_out = file_out.resolve()

parse(yaml_input, file_out=file_out)
parse_text(yaml_str, file_out=file_out, image_paths=[Path(yaml_file).parent])

if __name__ == '__main__':
print('When running from the command line, please use wv_cli.py instead.')
6 changes: 5 additions & 1 deletion src/wireviz/wv_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ def wireviz(file, format, prepend, output_file, version):
return_types = tuple(sorted(set(return_types)))
return_types_str = f'[{"|".join(return_types)}]' if len(return_types) > 1 else return_types[0]

image_paths = []
# check prepend file
if prepend:
prepend = Path(prepend)
Expand All @@ -58,8 +59,10 @@ def wireviz(file, format, prepend, output_file, version):

with open_file_read(prepend) as file_handle:
prepend_input = file_handle.read() + '\n'
prepend_dir = prepend.parent
else:
prepend_input = ''
prepend_dir = None

# run WireVIz on each input file
for file in filepaths:
Expand All @@ -74,10 +77,11 @@ def wireviz(file, format, prepend, output_file, version):

with open_file_read(file) as file_handle:
yaml_input = file_handle.read()
file_dir = file.parent

yaml_input = prepend_input + yaml_input

wv.parse_text(yaml_input, file_out=file_out, return_types=return_types)
wv.parse_text(yaml_input, file_out=file_out, return_types=return_types, image_paths=[file_dir, prepend_dir])

print()

Expand Down
2 changes: 1 addition & 1 deletion src/wireviz/wv_helper.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-

from typing import List
from typing import Dict, List
from pathlib import Path
import re

Expand Down

0 comments on commit a4a89eb

Please sign in to comment.