Skip to content

Commit

Permalink
wv_dataclasses: force cast where needed
Browse files Browse the repository at this point in the history
  • Loading branch information
laurierloi committed Nov 28, 2023
1 parent 066ffaf commit b17df57
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion src/wireviz/wv_dataclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,12 @@ def __post_init__(self):
self.type = MultilineHypertext.to(self.type)
self.subtype = MultilineHypertext.to(self.subtype)

if self.qty_multiplier:
try:
self.qty_multiplier = int(self.qty_multiplier)
except:
pass

if isinstance(self.pn, list):
raise RuntimeError(f"PN ({self.pn}) should not be a list")

Expand Down Expand Up @@ -267,14 +273,17 @@ def __post_init__(self) -> None:
self._ports_left_set = False
self._ports_right_set = False

if self.pincount is not None:
self.pincount = int(self.pincount)

if self.style == "simple":
if self.pincount and self.pincount > 1:
raise Exception(
"Connectors with style set to simple may only have one pin"
)
self.pincount = 1

if not self.pincount:
if self.pincount is None:
self.pincount = max(
len(self.pins), len(self.pinlabels), len(self.pincolors)
)
Expand All @@ -291,6 +300,14 @@ def __post_init__(self) -> None:
if len(self.pins) != len(set(self.pins)):
raise Exception("Pins are not unique")

# convert all pins which can be to int
def to_int_pin(pin):
try:
return int(pin)
except ValueError:
return pin
self.pins = [to_int_pin(p) for p in self.pins]

# all checks have passed
pin_tuples = zip_longest(
self.pins,
Expand All @@ -315,6 +332,7 @@ def __post_init__(self) -> None:
# hide pincount for simple (1 pin) connectors by default
self.show_pincount = self.style != "simple"

self.loops = [[to_int_pin(p) for p in loop] for loop in self.loops]
for loop in self.loops:
# TODO: check that pins to connect actually exist
# TODO: allow using pin labels in addition to pin numbers,
Expand Down Expand Up @@ -688,6 +706,7 @@ def __post_init__(self) -> None:
self.amount = self.length # for BOM

if self.wirecount: # number of wires explicitly defined
self.wirecount = int(self.wirecount)
if self.colors: # use custom color palette (partly or looped if needed)
self.colors = [
self.colors[i % len(self.colors)] for i in range(self.wirecount)
Expand Down

0 comments on commit b17df57

Please sign in to comment.