Skip to content

Commit

Permalink
Make Image.gv_dir an InitVar
Browse files Browse the repository at this point in the history
It is only used during initialization in the __post_init__() function
and does need to be a proper attribute.
https://docs.python.org/3/library/dataclasses.html#init-only-variables
  • Loading branch information
kvid committed Aug 29, 2020
1 parent 902c3f8 commit 545ce9e
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/wireviz/DataClasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
# -*- coding: utf-8 -*-

from typing import Optional, List, Any, Union
from dataclasses import dataclass, field
from dataclasses import dataclass, field, InitVar
from pathlib import Path
from wireviz.wv_helper import int2tuple, aspect_ratio
from wireviz import wv_colors


@dataclass
class Image:
gv_dir: Path # Directory of .gv file injected as context during parsing
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 @@ -22,7 +22,7 @@ class Image:
caption: Optional[str] = None
# See also HTML doc at https://graphviz.org/doc/info/shapes.html#html

def __post_init__(self):
def __post_init__(self, gv_dir):

if self.fixedsize is None:
# Avoid default True if the user specify a self.scale value.
Expand All @@ -38,10 +38,10 @@ def __post_init__(self):
# because Graphviz requires both when fixedsize=True.
if self.height:
if not self.width:
self.width = self.height * aspect_ratio(self.gv_dir.joinpath(self.src))
self.width = self.height * aspect_ratio(gv_dir.joinpath(self.src))
else:
if self.width:
self.height = self.width / aspect_ratio(self.gv_dir.joinpath(self.src))
self.height = self.width / aspect_ratio(gv_dir.joinpath(self.src))


@dataclass
Expand Down

0 comments on commit 545ce9e

Please sign in to comment.