Move qty_multiplier functions into Connector and cable dataclasses
Also rename long_name to description
This commit is contained in:
parent
3faaff6ebc
commit
20622e0346
@ -10,6 +10,7 @@ from wireviz import wv_colors
|
|||||||
ConnectorMultiplier = str # = Literal['pincount', 'populated']
|
ConnectorMultiplier = str # = Literal['pincount', 'populated']
|
||||||
CableMultiplier = str # = Literal['wirecount', 'terminations', 'length', 'total_length']
|
CableMultiplier = str # = Literal['wirecount', 'terminations', 'length', 'total_length']
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class Image:
|
class Image:
|
||||||
gv_dir: InitVar[Path] # Directory of .gv file injected as context during parsing
|
gv_dir: InitVar[Path] # Directory of .gv file injected as context during parsing
|
||||||
@ -56,7 +57,7 @@ class AdditionalComponent:
|
|||||||
unit: Optional[str] = None
|
unit: Optional[str] = None
|
||||||
qty_multiplier: Union[ConnectorMultiplier, CableMultiplier, None] = None
|
qty_multiplier: Union[ConnectorMultiplier, CableMultiplier, None] = None
|
||||||
|
|
||||||
def long_name(self) -> str:
|
def description(self) -> str:
|
||||||
name_subtype = f', {self.subtype}' if self.subtype else ''
|
name_subtype = f', {self.subtype}' if self.subtype else ''
|
||||||
return f'{self.type.capitalize()}{name_subtype}'
|
return f'{self.type.capitalize()}{name_subtype}'
|
||||||
|
|
||||||
@ -140,6 +141,16 @@ class Connector:
|
|||||||
def activate_pin(self, pin):
|
def activate_pin(self, pin):
|
||||||
self.visible_pins[pin] = True
|
self.visible_pins[pin] = True
|
||||||
|
|
||||||
|
def get_qty_multiplier(self, qty_multiplier: Optional[ConnectorMultiplier]) -> int:
|
||||||
|
if not qty_multiplier:
|
||||||
|
return 1
|
||||||
|
elif qty_multiplier == 'pincount':
|
||||||
|
return self.pincount
|
||||||
|
elif qty_multiplier == 'populated':
|
||||||
|
return sum(self.visible_pins.values())
|
||||||
|
else:
|
||||||
|
raise ValueError(f'invalid qty multiplier parameter for connector {qty_multiplier}')
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class Cable:
|
class Cable:
|
||||||
@ -235,6 +246,20 @@ class Cable:
|
|||||||
# self.connections.append((from_name, from_pin[i], via_pin[i], to_name, to_pin[i]))
|
# self.connections.append((from_name, from_pin[i], via_pin[i], to_name, to_pin[i]))
|
||||||
self.connections.append(Connection(from_name, from_pin[i], via_pin[i], to_name, to_pin[i]))
|
self.connections.append(Connection(from_name, from_pin[i], via_pin[i], to_name, to_pin[i]))
|
||||||
|
|
||||||
|
def get_qty_multiplier(self, qty_multiplier: Optional[CableMultiplier]) -> float:
|
||||||
|
if not qty_multiplier:
|
||||||
|
return 1
|
||||||
|
elif qty_multiplier == 'wirecount':
|
||||||
|
return self.wirecount
|
||||||
|
elif qty_multiplier == 'terminations':
|
||||||
|
return len(self.connections)
|
||||||
|
elif qty_multiplier == 'length':
|
||||||
|
return self.length
|
||||||
|
elif qty_multiplier == 'total_length':
|
||||||
|
return self.length * self.wirecount
|
||||||
|
else:
|
||||||
|
raise ValueError(f'invalid qty multiplier parameter for cable {qty_multiplier}')
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class Connection:
|
class Connection:
|
||||||
|
|||||||
@ -9,8 +9,7 @@ from wireviz.wv_helper import awg_equiv, mm2_equiv, tuplelist2tsv, \
|
|||||||
nested_html_table, flatten2d, index_if_list, html_line_breaks, \
|
nested_html_table, flatten2d, index_if_list, html_line_breaks, \
|
||||||
graphviz_line_breaks, remove_line_breaks, open_file_read, open_file_write, \
|
graphviz_line_breaks, remove_line_breaks, open_file_read, open_file_write, \
|
||||||
html_colorbar, html_image, html_caption, manufacturer_info_field, \
|
html_colorbar, html_image, html_caption, manufacturer_info_field, \
|
||||||
component_table_entry, \
|
component_table_entry
|
||||||
calculate_qty_multiplier_connector, calculate_qty_multiplier_cable
|
|
||||||
from collections import Counter
|
from collections import Counter
|
||||||
from typing import List
|
from typing import List
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
@ -107,12 +106,12 @@ class Harness:
|
|||||||
if connector.additional_components:
|
if connector.additional_components:
|
||||||
rows.append(["Additional components"])
|
rows.append(["Additional components"])
|
||||||
for extra in connector.additional_components:
|
for extra in connector.additional_components:
|
||||||
qty = extra.qty * calculate_qty_multiplier_connector(extra.qty_multiplier, connector)
|
qty = extra.qty * connector.get_qty_multiplier(extra.qty_multiplier)
|
||||||
if(self.mini_bom_mode):
|
if(self.mini_bom_mode):
|
||||||
id = self.get_bom_index(extra.long_name(), extra.unit, extra.manufacturer, extra.mpn, extra.pn)
|
id = self.get_bom_index(extra.description(), extra.unit, extra.manufacturer, extra.mpn, extra.pn)
|
||||||
rows.append(component_table_entry(f'{id} ({extra.type.capitalize()})', qty, extra.unit))
|
rows.append(component_table_entry(f'{id} ({extra.type.capitalize()})', qty, extra.unit))
|
||||||
else:
|
else:
|
||||||
rows.append(component_table_entry(extra.long_name(), qty, extra.unit, extra.pn, extra.manufacturer, extra.mpn))
|
rows.append(component_table_entry(extra.description(), qty, extra.unit, extra.pn, extra.manufacturer, extra.mpn))
|
||||||
rows.append([html_line_breaks(connector.notes)])
|
rows.append([html_line_breaks(connector.notes)])
|
||||||
html.extend(nested_html_table(rows))
|
html.extend(nested_html_table(rows))
|
||||||
|
|
||||||
@ -190,12 +189,12 @@ class Harness:
|
|||||||
if cable.additional_components:
|
if cable.additional_components:
|
||||||
rows.append(["Additional components"])
|
rows.append(["Additional components"])
|
||||||
for extra in cable.additional_components:
|
for extra in cable.additional_components:
|
||||||
qty = extra.qty * calculate_qty_multiplier_cable(extra.qty_multiplier, cable)
|
qty = extra.qty * cable.get_qty_multiplier(extra.qty_multiplier)
|
||||||
if(self.mini_bom_mode):
|
if(self.mini_bom_mode):
|
||||||
id = self.get_bom_index(extra.long_name(), extra.unit, extra.manufacturer, extra.mpn, extra.pn)
|
id = self.get_bom_index(extra.description(), extra.unit, extra.manufacturer, extra.mpn, extra.pn)
|
||||||
rows.append(component_table_entry(f'{id} ({extra.type.capitalize()})', qty, extra.unit))
|
rows.append(component_table_entry(f'{id} ({extra.type.capitalize()})', qty, extra.unit))
|
||||||
else:
|
else:
|
||||||
rows.append(component_table_entry(extra.long_name(), qty, extra.unit, extra.pn, extra.manufacturer, extra.mpn))
|
rows.append(component_table_entry(extra.description(), qty, extra.unit, extra.pn, extra.manufacturer, extra.mpn))
|
||||||
rows.append([html_line_breaks(cable.notes)])
|
rows.append([html_line_breaks(cable.notes)])
|
||||||
html.extend(nested_html_table(rows))
|
html.extend(nested_html_table(rows))
|
||||||
|
|
||||||
@ -371,10 +370,10 @@ class Harness:
|
|||||||
bom_items.append(item)
|
bom_items.append(item)
|
||||||
|
|
||||||
for part in connector.additional_components:
|
for part in connector.additional_components:
|
||||||
qty = part.qty * calculate_qty_multiplier_connector(part.qty_multiplier, connector)
|
qty = part.qty * connector.get_qty_multiplier(part.qty_multiplier)
|
||||||
bom_items.append(
|
bom_items.append(
|
||||||
{
|
{
|
||||||
'item': part.long_name(),
|
'item': part.description(),
|
||||||
'qty': qty,
|
'qty': qty,
|
||||||
'unit': part.unit,
|
'unit': part.unit,
|
||||||
'manufacturer': part.manufacturer,
|
'manufacturer': part.manufacturer,
|
||||||
@ -409,10 +408,10 @@ class Harness:
|
|||||||
bom_items.append(item)
|
bom_items.append(item)
|
||||||
|
|
||||||
for part in cable.additional_components:
|
for part in cable.additional_components:
|
||||||
qty = part.qty * calculate_qty_multiplier_cable(part.qty_multiplier, cable)
|
qty = part.qty * cable.get_qty_multiplier(part.qty_multiplier)
|
||||||
bom_items.append(
|
bom_items.append(
|
||||||
{
|
{
|
||||||
'item': part.long_name(),
|
'item': part.description(),
|
||||||
'qty': qty,
|
'qty': qty,
|
||||||
'unit': part.unit,
|
'unit': part.unit,
|
||||||
'manufacturer': part.manufacturer,
|
'manufacturer': part.manufacturer,
|
||||||
|
|||||||
@ -200,31 +200,3 @@ def component_table_entry(type, qty, unit=None, pn=None, manufacturer=None, mpn=
|
|||||||
output = html_line_breaks(output)
|
output = html_line_breaks(output)
|
||||||
# format the above output as left aligned text in a single visable cell
|
# format the above output as left aligned text in a single visable cell
|
||||||
return f'<table border="0" cellspacing="0" cellpadding="3" cellborder="1"><tr><td align="left" balign="left">{output}</td></tr></table>'
|
return f'<table border="0" cellspacing="0" cellpadding="3" cellborder="1"><tr><td align="left" balign="left">{output}</td></tr></table>'
|
||||||
|
|
||||||
|
|
||||||
def calculate_qty_multiplier_connector(qty_multiplier, connector):
|
|
||||||
if not qty_multiplier:
|
|
||||||
return 1
|
|
||||||
|
|
||||||
if qty_multiplier == 'pincount':
|
|
||||||
return connector.pincount
|
|
||||||
elif qty_multiplier == 'populated':
|
|
||||||
return sum(connector.visible_pins.values())
|
|
||||||
else:
|
|
||||||
raise ValueError(f'invalid qty multiplier parameter for connector {qty_multiplier}')
|
|
||||||
|
|
||||||
|
|
||||||
def calculate_qty_multiplier_cable(qty_multiplier, cable):
|
|
||||||
if not qty_multiplier:
|
|
||||||
return 1
|
|
||||||
|
|
||||||
if qty_multiplier == 'wirecount':
|
|
||||||
return cable.wirecount
|
|
||||||
elif qty_multiplier == 'terminations':
|
|
||||||
return len(cable.connections)
|
|
||||||
elif qty_multiplier == 'length':
|
|
||||||
return cable.length
|
|
||||||
elif qty_multiplier == 'total_length':
|
|
||||||
return cable.length * cable.wirecount
|
|
||||||
else:
|
|
||||||
raise ValueError(f'invalid qty multiplier parameter for cable {qty_multiplier}')
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user