From c21707980e3706bd1df4d3736748b933b6be6846 Mon Sep 17 00:00:00 2001 From: KV Date: Tue, 18 Aug 2020 18:10:45 +0200 Subject: [PATCH] Test image.width and image.height without using 'is not None' - Simplification requested by @formatc1702 in review of #153 - Inspection of size_html_cell() in Graphviz source code https://gitlab.com/graphviz/graphviz/-/blob/master/lib/common/htmltable.c#L1210-1221 supports that a zero value is treated as not specified. --- src/wireviz/DataClasses.py | 18 ++++++++++-------- src/wireviz/wv_helper.py | 4 ++-- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/src/wireviz/DataClasses.py b/src/wireviz/DataClasses.py index 622912a..bde12f5 100644 --- a/src/wireviz/DataClasses.py +++ b/src/wireviz/DataClasses.py @@ -16,24 +16,26 @@ class Image: width: Optional[int] = None height: Optional[int] = None fixedsize: Optional[bool] = None - # Contents of the cell just below the image cell: + # Contents of the text cell just below the image cell: caption: Optional[str] = None # See also HTML doc at https://graphviz.org/doc/info/shapes.html#html def __post_init__(self): if self.scale is None: - self.scale = "false" if self.width is None and self.height is None \ - else "both" if self.width is not None and self.height is not None \ + self.scale = "false" if not self.width and not self.height \ + else "both" if self.width and self.height \ else "true" # When only one dimension is specified. if self.fixedsize is None: - self.fixedsize = self.width is not None or self.height is not None + self.fixedsize = self.width or self.height - if self.width is None and self.height is not None: - self.width = self.height # Assuming 1:1 aspect ratio for now. - elif self.height is None and self.width is not None: - self.height = self.width # Assuming 1:1 aspect ratio for now. + if self.height: + if not self.width: + self.width = self.height # Assuming 1:1 aspect ratio for now. + else: + if self.width: + self.height = self.width # Assuming 1:1 aspect ratio for now. @dataclass diff --git a/src/wireviz/wv_helper.py b/src/wireviz/wv_helper.py index 0bc8139..33936ed 100644 --- a/src/wireviz/wv_helper.py +++ b/src/wireviz/wv_helper.py @@ -75,8 +75,8 @@ def html_caption(image): def html_size_attr(image): # Return Graphviz HTML attributes to specify minimum or fixed size of a TABLE or TD object - return ((f' width="{image.width}"' if image.width is not None else '') - + (f' height="{image.height}"' if image.height is not None else '') + return ((f' width="{image.width}"' if image.width else '') + + (f' height="{image.height}"' if image.height else '') + ( ' fixedsize="true"' if image.fixedsize else '')) if image else ''