Skip to content

Commit

Permalink
Split out class AdditionalBomItem from AdditionalComponent
Browse files Browse the repository at this point in the history
#251 (comment)

No output changed for any examples/tutorial/tests input.
  • Loading branch information
kvid committed Oct 28, 2023
1 parent 98e8f7f commit 49556bd
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 27 deletions.
57 changes: 34 additions & 23 deletions src/wireviz/wv_dataclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,9 @@ def __post_init__(self):
partnos = tuple(partnos)
self.partnumbers = PartNumberInfo(*partnos)

self.qty = self.parse_number_and_unit(self.qty, None)
self.amount = self.parse_number_and_unit(self.amount, None)

def parse_number_and_unit(
self,
inp: Optional[Union[NumberAndUnit, float, int, str]],
Expand Down Expand Up @@ -260,21 +263,40 @@ def bom_amount(self) -> NumberAndUnit:
def has_pn_info(self) -> bool:
return any([self.pn, self.manufacturer, self.mpn, self.supplier, self.spn])

@property
def description(self) -> str:
return f"{self.type}{', ' + self.subtype if self.subtype else ''}"


@dataclass
class AdditionalBomItem(Component):
designators: Optional[str] = None

@property
def additional_components(self):
# An additional item may not have further nested additional comonents.
# This property is currently needed for objects in the same list as
# TopLevelGraphicalComponent objects in a Harness method.
return []


@dataclass
class AdditionalComponent(Component):
class GraphicalComponent(Component): # abstract class
bgcolor: Optional[SingleColor] = None

def __post_init__(self):
super().__post_init__()
self.bgcolor = SingleColor(self.bgcolor)


@dataclass
class AdditionalComponent(GraphicalComponent):
qty_multiplier: Union[QtyMultiplierConnector, QtyMultiplierCable, int] = 1
_qty_multiplier_computed: Union[int, float] = 1
designators: Optional[str] = None # used for components definedi in the
# additional_bom_items section within another component
bgcolor: SingleColor = None # ^ same here
note: str = None

def __post_init__(self):
super().__post_init__()
self.bgcolor = SingleColor(self.bgcolor)
self.qty = self.parse_number_and_unit(self.qty, None)
self.amount = self.parse_number_and_unit(self.amount, None)

if isinstance(self.qty_multiplier, float) or isinstance(
self.qty_multiplier, int
Expand All @@ -289,24 +311,10 @@ def __post_init__(self):
else:
raise Exception(f"Unknown qty multiplier: {self.qty_multiplier}")

@property
def additional_components(self):
# an additional component may not have further nested additional comonents
return []

@property
def bom_qty(self):
return self.qty.number * self._qty_multiplier_computed

@property
def description(self) -> str:
return f"{self.type}{', ' + self.subtype if self.subtype else ''}"


@dataclass
class GraphicalComponent(Component): # abstract class, for future use
bgcolor: Optional[SingleColor] = None


@dataclass
class TopLevelGraphicalComponent(GraphicalComponent): # abstract class
Expand Down Expand Up @@ -368,11 +376,15 @@ def unit(self): # for compatibility with BOM hashing
def __post_init__(self) -> None:
super().__post_init__()

self.bgcolor = SingleColor(self.bgcolor)
self.bgcolor_title = SingleColor(self.bgcolor_title)
self.color = MultiColor(self.color)

# connectors do not support custom qty or amount
if self.qty != NumberAndUnit(1, None):
raise Exception("Connector qty != 1 not supported")
if self.amount is not None:
raise Exception("Connector amount not supported")
# TODO: Delete next two assignments if tests above is sufficient. Please verify!
self.qty = NumberAndUnit(1, None)
self.amount = None

Expand Down Expand Up @@ -653,7 +665,6 @@ def _get_correct_element(inp, idx):
def __post_init__(self) -> None:
super().__post_init__()

self.bgcolor = SingleColor(self.bgcolor)
self.bgcolor_title = SingleColor(self.bgcolor_title)
self.color = MultiColor(self.color)

Expand Down
8 changes: 4 additions & 4 deletions src/wireviz/wv_harness.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from wireviz.wv_bom import BomCategory, BomEntry, bom_list, print_bom_table
from wireviz.wv_dataclasses import (
AUTOGENERATED_PREFIX,
AdditionalComponent,
AdditionalBomItem,
Arrow,
ArrowWeight,
Cable,
Expand Down Expand Up @@ -48,7 +48,7 @@ class Harness:
metadata: Metadata
options: Options
tweak: Tweak
additional_bom_items: List[AdditionalComponent] = field(default_factory=list)
additional_bom_items: List[AdditionalBomItem] = field(default_factory=list)

def __post_init__(self):
self.connectors = {}
Expand All @@ -66,7 +66,7 @@ def add_cable(self, designator: str, *args, **kwargs) -> None:
self.cables[designator] = cbl

def add_additional_bom_item(self, item: dict) -> None:
new_item = AdditionalComponent(**item)
new_item = AdditionalBomItem(**item)
self.additional_bom_items.append(new_item)

def add_mate_pin(self, from_name, from_pin, to_name, to_pin, arrow_str) -> None:
Expand Down Expand Up @@ -200,7 +200,7 @@ def _add(hash, qty, designator=None, category=None):
qty=comp.bom_qty,
category=BomCategory.ADDITIONAL_INSIDE,
)
elif isinstance(item, AdditionalComponent):
elif isinstance(item, AdditionalBomItem):
cat = BomCategory.ADDITIONAL_OUTSIDE
_add(
hash=item.bom_hash,
Expand Down

0 comments on commit 49556bd

Please sign in to comment.