Apply suggestions from code review by @kvid

Co-authored-by: kvid <kvid@users.noreply.github.com>
This commit is contained in:
Daniel Rojas 2020-12-13 13:00:17 +01:00 committed by GitHub
parent 6a42a30523
commit 0a59e97d29
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 5 additions and 6 deletions

View File

@ -43,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):
def __post_init__(self) -> None:
if self.fixedsize is None:
# Default True if any dimension specified unless self.scale also is specified.

View File

@ -342,7 +342,7 @@ class Harness:
return data.read()
@property
def svg(self):
def svg(self) -> str:
graph = self.create_graph()
return embed_svg_images(graph.pipe(format='svg').decode('utf-8'), Path.cwd())

View File

@ -8,7 +8,7 @@ from typing import Union
mime_subtype_replacements = {'jpg': 'jpeg', 'tif': 'tiff'}
def embed_svg_images(svg_in: str, base_path: Path):
def embed_svg_images(svg_in: str, base_path: Union[str, Path] = Path.cwd()) -> str:
# first, find any image references in SVG data, and cache the respective base64-encoded image
images_b64 = {} # cache of base64-encoded images
re_xlink=re.compile(r'xlink:href="(?P<URL>.*?)"', re.IGNORECASE)
@ -16,7 +16,6 @@ def embed_svg_images(svg_in: str, base_path: Path):
imgurl = xlink.group('URL')
if not imgurl in images_b64: # only encode/cache every unique URL once
imgurl_abs = (Path(base_path) / imgurl).resolve()
images_b64[imgurl] = base64.b64encode(imgurl_abs.read_bytes()).decode('utf-8')
# second, replace links with the base64-encoded data
svg_out = svg_in
@ -26,14 +25,14 @@ def embed_svg_images(svg_in: str, base_path: Path):
return svg_out
def get_mime_subtype(filename: Union[str, Path]):
def get_mime_subtype(filename: Union[str, Path]) -> str:
mime_subtype = Path(filename).suffix.lstrip('.').lower()
if mime_subtype in mime_subtype_replacements:
mime_subtype = mime_subtype_replacements[mime_subtype]
return mime_subtype
def embed_svg_images_file(filename_in: Union[str, Path], overwrite: bool = True):
def embed_svg_images_file(filename_in: Union[str, Path], overwrite: bool = True) -> None:
filename_in = Path(filename_in).resolve()
filename_out = filename_in.with_suffix('.b64.svg')
filename_out.write_text(embed_svg_images(filename_in.read_text(), filename_in.parent))