Merge d5224bbdc89a3c0bb20cabd84ad3f52d2b717856 into e8c482e94e6f85bc8dc70ace0dc834bcf0f6b71b

This commit is contained in:
kvid 2024-10-14 19:26:04 +02:00 committed by GitHub
commit 49b2bede8c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 26 additions and 10 deletions

View File

@ -113,6 +113,21 @@ class Image:
if self.width:
self.height = self.width / aspect_ratio(self.src)
@classmethod
def create(cls, input: Union[None, dict, str, List[Union[dict, str]]]):
"""Create class instance(s) from alternative YAML input types"""
if input in (None, "", []):
return None
if isinstance(input, list):
return [cls.create(entry) for entry in input]
if isinstance(input, str):
input = {"src": input}
if isinstance(input, dict):
return cls(**input)
raise TypeError(
f"Expected None, dict, str, or list as Image input, but got {type(input)}"
)
@dataclass
class AdditionalComponent:
@ -165,8 +180,7 @@ class Connector:
additional_components: List[AdditionalComponent] = field(default_factory=list)
def __post_init__(self) -> None:
if isinstance(self.image, dict):
self.image = Image(**self.image)
self.image = Image.create(self.image)
self.ports_left = False
self.ports_right = False
@ -274,8 +288,7 @@ class Cable:
additional_components: List[AdditionalComponent] = field(default_factory=list)
def __post_init__(self) -> None:
if isinstance(self.image, dict):
self.image = Image(**self.image)
self.image = Image.create(self.image)
if isinstance(self.gauge, str): # gauge and unit specified
try:

View File

@ -34,9 +34,8 @@ from wireviz.wv_colors import get_color_hex, translate_color
from wireviz.wv_gv_html import (
html_bgcolor,
html_bgcolor_attr,
html_caption,
html_colorbar,
html_image,
html_image_rows,
html_line_breaks,
nested_html_table,
remove_links,
@ -203,8 +202,7 @@ class Harness:
translate_color(connector.color, self.options.color_mode) if connector.color else None,
html_colorbar(connector.color)],
'<!-- connector table -->' if connector.style != 'simple' else None,
[html_image(connector.image)],
[html_caption(connector.image)]]
*html_image_rows(connector.image)]
# fmt: on
rows.extend(get_additional_component_table(self, connector))
@ -326,8 +324,7 @@ class Harness:
translate_color(cable.color, self.options.color_mode) if cable.color else None,
html_colorbar(cable.color)],
'<!-- wire table -->',
[html_image(cable.image)],
[html_caption(cable.image)]]
*html_image_rows(cable.image)]
# fmt: on
rows.extend(get_additional_component_table(self, cable))

View File

@ -64,6 +64,12 @@ def html_colorbar(color: Color) -> str:
return html_bgcolor(color, ' width="4"') if color else None
def html_image_rows(image):
from wireviz.wv_bom import make_list
return sum([[[html_image(i)], [html_caption(i)]] for i in make_list(image)], [])
def html_image(image):
from wireviz.DataClasses import Image