Move image attributes into Image dataclass to fix change requests
@formatc1702 requested changes in his review of #153: - Lowercase attribute values - Nested image attributes - Avoid sending the whole node object as argument to helper functions - Simplify html_size_attr() - Move the resources folder
This commit is contained in:
parent
3d7f027a4e
commit
204379a125
@ -7,8 +7,9 @@ connectors:
|
|||||||
pins: [T, R, S]
|
pins: [T, R, S]
|
||||||
pinlabels: [Dot, Dash, Ground]
|
pinlabels: [Dot, Dash, Ground]
|
||||||
show_pincount: false
|
show_pincount: false
|
||||||
image: ../resources/stereo-phone-plug-TRS.png
|
image:
|
||||||
caption: Tip, Ring, and Sleeve
|
src: resources/stereo-phone-plug-TRS.png
|
||||||
|
caption: Tip, Ring, and Sleeve
|
||||||
|
|
||||||
cables:
|
cables:
|
||||||
W1:
|
W1:
|
||||||
@ -18,8 +19,9 @@ cables:
|
|||||||
color_code: DIN
|
color_code: DIN
|
||||||
wirecount: 3
|
wirecount: 3
|
||||||
shield: SN
|
shield: SN
|
||||||
image: ../resources/cable-WH+BN+GN+shield.png
|
image:
|
||||||
caption: Cross-section
|
src: resources/cable-WH+BN+GN+shield.png
|
||||||
|
caption: Cross-section
|
||||||
|
|
||||||
connections:
|
connections:
|
||||||
-
|
-
|
||||||
|
|||||||
|
Before Width: | Height: | Size: 4.3 KiB After Width: | Height: | Size: 4.3 KiB |
|
Before Width: | Height: | Size: 1.9 KiB After Width: | Height: | Size: 1.9 KiB |
@ -7,6 +7,16 @@ from wireviz.wv_helper import int2tuple
|
|||||||
from wireviz import wv_colors
|
from wireviz import wv_colors
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class Image:
|
||||||
|
src: str
|
||||||
|
scale: str = "false"
|
||||||
|
width: Optional[int] = None
|
||||||
|
height: Optional[int] = None
|
||||||
|
fixedsize: bool = False
|
||||||
|
caption: Optional[str] = None
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class Connector:
|
class Connector:
|
||||||
name: str
|
name: str
|
||||||
@ -18,10 +28,7 @@ class Connector:
|
|||||||
type: Optional[str] = None
|
type: Optional[str] = None
|
||||||
subtype: Optional[str] = None
|
subtype: Optional[str] = None
|
||||||
pincount: Optional[int] = None
|
pincount: Optional[int] = None
|
||||||
image: Optional[str] = None
|
image: Optional[Image] = None
|
||||||
image_scale: Optional[str] = "FALSE"
|
|
||||||
image_size: List[Any] = field(default_factory=list)
|
|
||||||
caption: Optional[str] = None
|
|
||||||
notes: Optional[str] = None
|
notes: Optional[str] = None
|
||||||
pinlabels: List[Any] = field(default_factory=list)
|
pinlabels: List[Any] = field(default_factory=list)
|
||||||
pins: List[Any] = field(default_factory=list)
|
pins: List[Any] = field(default_factory=list)
|
||||||
@ -33,6 +40,10 @@ class Connector:
|
|||||||
loops: List[Any] = field(default_factory=list)
|
loops: List[Any] = field(default_factory=list)
|
||||||
|
|
||||||
def __post_init__(self):
|
def __post_init__(self):
|
||||||
|
|
||||||
|
if isinstance(self.image, dict):
|
||||||
|
self.image = Image(**self.image)
|
||||||
|
|
||||||
self.ports_left = False
|
self.ports_left = False
|
||||||
self.ports_right = False
|
self.ports_right = False
|
||||||
self.visible_pins = {}
|
self.visible_pins = {}
|
||||||
@ -95,10 +106,7 @@ class Cable:
|
|||||||
color: Optional[str] = None
|
color: Optional[str] = None
|
||||||
wirecount: Optional[int] = None
|
wirecount: Optional[int] = None
|
||||||
shield: bool = False
|
shield: bool = False
|
||||||
image: Optional[str] = None
|
image: Optional[Image] = None
|
||||||
image_scale: Optional[str] = "FALSE"
|
|
||||||
image_size: List[Any] = field(default_factory=list)
|
|
||||||
caption: Optional[str] = None
|
|
||||||
notes: Optional[str] = None
|
notes: Optional[str] = None
|
||||||
colors: List[Any] = field(default_factory=list)
|
colors: List[Any] = field(default_factory=list)
|
||||||
color_code: Optional[str] = None
|
color_code: Optional[str] = None
|
||||||
@ -107,6 +115,9 @@ class Cable:
|
|||||||
|
|
||||||
def __post_init__(self):
|
def __post_init__(self):
|
||||||
|
|
||||||
|
if isinstance(self.image, dict):
|
||||||
|
self.image = Image(**self.image)
|
||||||
|
|
||||||
if isinstance(self.gauge, str): # gauge and unit specified
|
if isinstance(self.gauge, str): # gauge and unit specified
|
||||||
try:
|
try:
|
||||||
g, u = self.gauge.split(' ')
|
g, u = self.gauge.split(' ')
|
||||||
|
|||||||
@ -98,8 +98,8 @@ class Harness:
|
|||||||
f'{connector.pincount}-pin' if connector.show_pincount else None,
|
f'{connector.pincount}-pin' if connector.show_pincount else None,
|
||||||
connector.color, '<!-- colorbar -->' if connector.color else None],
|
connector.color, '<!-- colorbar -->' if connector.color else None],
|
||||||
'<!-- connector table -->' if connector.style != 'simple' else None,
|
'<!-- connector table -->' if connector.style != 'simple' else None,
|
||||||
[html_image(connector)],
|
[html_image(connector.image)],
|
||||||
[html_caption(connector)],
|
[html_caption(connector.image)],
|
||||||
[html_line_breaks(connector.notes)]]
|
[html_line_breaks(connector.notes)]]
|
||||||
html.extend(nested_html_table(rows))
|
html.extend(nested_html_table(rows))
|
||||||
|
|
||||||
@ -175,8 +175,8 @@ class Harness:
|
|||||||
f'{cable.length} m' if cable.length > 0 else None,
|
f'{cable.length} m' if cable.length > 0 else None,
|
||||||
cable.color, '<!-- colorbar -->' if cable.color else None],
|
cable.color, '<!-- colorbar -->' if cable.color else None],
|
||||||
'<!-- wire table -->',
|
'<!-- wire table -->',
|
||||||
[html_image(cable)],
|
[html_image(cable.image)],
|
||||||
[html_caption(cable)],
|
[html_caption(cable.image)],
|
||||||
[html_line_breaks(cable.notes)]]
|
[html_line_breaks(cable.notes)]]
|
||||||
html.extend(nested_html_table(rows))
|
html.extend(nested_html_table(rows))
|
||||||
|
|
||||||
|
|||||||
@ -55,13 +55,12 @@ def nested_html_table(rows):
|
|||||||
html.append('</table>')
|
html.append('</table>')
|
||||||
return html
|
return html
|
||||||
|
|
||||||
def html_image(node):
|
def html_image(image):
|
||||||
if not node.image:
|
if not image:
|
||||||
return None
|
return None
|
||||||
size = html_size_attr(node.image_size)
|
|
||||||
# The leading attributes belong to the preceeding tag. See where used below.
|
# The leading attributes belong to the preceeding tag. See where used below.
|
||||||
html = f'{"".join(size)}><img scale="{node.image_scale}" src="{node.image}"/>'
|
html = f'{html_size_attr(image)}><img scale="{image.scale}" src="{image.src}"/>'
|
||||||
if size[2]: # fixedsize
|
if image.fixedsize:
|
||||||
# Close the preceeding tag and enclose the image cell in a table without
|
# Close the preceeding tag and enclose the image cell in a table without
|
||||||
# borders to avoid narrow borders when the fixed width < the node width.
|
# borders to avoid narrow borders when the fixed width < the node width.
|
||||||
html = f'''>
|
html = f'''>
|
||||||
@ -69,23 +68,16 @@ def html_image(node):
|
|||||||
<td{html}</td>
|
<td{html}</td>
|
||||||
</tr></table>
|
</tr></table>
|
||||||
'''
|
'''
|
||||||
return f'''<tdX{' sides="TLR"' if node.caption else ''}{html}'''
|
return f'''<tdX{' sides="TLR"' if image.caption else ''}{html}'''
|
||||||
|
|
||||||
def html_caption(node):
|
def html_caption(image):
|
||||||
return f'''<tdX{' sides="LRB"' if node.image else ''}>{html_line_breaks(node.caption)}''' if node.caption else None
|
return f'<tdX sides="BLR">{html_line_breaks(image.caption)}' if image and image.caption else None
|
||||||
|
|
||||||
def html_size_attr(size):
|
def html_size_attr(image):
|
||||||
# Return Graphviz HTML attributes to specify minimum size of a TABLE or TD object
|
# Return Graphviz HTML attributes to specify minimum or fixed size of a TABLE or TD object
|
||||||
# size: List of values where only these are used:
|
return ((f' width="{image.width}"' if image.width else '')
|
||||||
# - First value is minimum width of object in points, an int value in the range 1-65535. (Default 0 = none)
|
+ (f' height="{image.height}"' if image.height else '')
|
||||||
# - Second value is minimum height of the object in points, an int value in the range 1-65535. (Default 0 = none)
|
+ ( ' fixedsize="true"' if image.fixedsize else '')) if image else ''
|
||||||
# - Third value is a boolean where TRUE specify that the first two values are the fixed image cell size.
|
|
||||||
# (Default FALSE) Other string values, e.g. FIXED are also interpreted as TRUE.
|
|
||||||
if not size or not isinstance(size, list):
|
|
||||||
return [''] * 3
|
|
||||||
return [f' width="{size[0]}"' if len(size) > 0 and size[0] else '',
|
|
||||||
f' height="{size[1]}"' if len(size) > 1 and size[1] else '',
|
|
||||||
' fixedsize="TRUE"' if len(size) > 2 and size[2] else '']
|
|
||||||
|
|
||||||
|
|
||||||
def expand(yaml_data):
|
def expand(yaml_data):
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user