Deduplicate additional components functions
Also refine bom variable names and fix a comple of typos
This commit is contained in:
parent
a450c82e72
commit
cb3e3f44e8
@ -59,8 +59,7 @@ class AdditionalComponent:
|
|||||||
|
|
||||||
@property
|
@property
|
||||||
def description(self) -> str:
|
def description(self) -> str:
|
||||||
name_subtype = f', {self.subtype}' if self.subtype else ''
|
return self.type.capitalize() + (f', {self.subtype}' if self.subtype else '')
|
||||||
return f'{self.type.capitalize()}{name_subtype}'
|
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
|
|||||||
@ -11,7 +11,7 @@ from wireviz.wv_helper import awg_equiv, mm2_equiv, tuplelist2tsv, \
|
|||||||
html_colorbar, html_image, html_caption, manufacturer_info_field, \
|
html_colorbar, html_image, html_caption, manufacturer_info_field, \
|
||||||
component_table_entry
|
component_table_entry
|
||||||
from collections import Counter
|
from collections import Counter
|
||||||
from typing import List
|
from typing import List, Union
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
import re
|
import re
|
||||||
|
|
||||||
@ -103,15 +103,7 @@ class Harness:
|
|||||||
'<!-- connector table -->' if connector.style != 'simple' else None,
|
'<!-- connector table -->' if connector.style != 'simple' else None,
|
||||||
[html_image(connector.image)],
|
[html_image(connector.image)],
|
||||||
[html_caption(connector.image)]]
|
[html_caption(connector.image)]]
|
||||||
if connector.additional_components:
|
rows.extend(self.get_additional_component_table(connector))
|
||||||
rows.append(["Additional components"])
|
|
||||||
for extra in connector.additional_components:
|
|
||||||
qty = extra.qty * connector.get_qty_multiplier(extra.qty_multiplier)
|
|
||||||
if(self.mini_bom_mode):
|
|
||||||
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))
|
|
||||||
else:
|
|
||||||
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))
|
||||||
|
|
||||||
@ -186,15 +178,7 @@ class Harness:
|
|||||||
[html_image(cable.image)],
|
[html_image(cable.image)],
|
||||||
[html_caption(cable.image)]]
|
[html_caption(cable.image)]]
|
||||||
|
|
||||||
if cable.additional_components:
|
rows.extend(self.get_additional_component_table(cable))
|
||||||
rows.append(["Additional components"])
|
|
||||||
for extra in cable.additional_components:
|
|
||||||
qty = extra.qty * cable.get_qty_multiplier(extra.qty_multiplier)
|
|
||||||
if(self.mini_bom_mode):
|
|
||||||
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))
|
|
||||||
else:
|
|
||||||
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))
|
||||||
|
|
||||||
@ -351,99 +335,107 @@ class Harness:
|
|||||||
|
|
||||||
file.write('</body></html>')
|
file.write('</body></html>')
|
||||||
|
|
||||||
|
def get_additional_component_table(self, component: Union[Connector, Cable]) -> List[str]:
|
||||||
|
rows = []
|
||||||
|
if component.additional_components:
|
||||||
|
rows.append(["Additional components"])
|
||||||
|
for extra in component.additional_components:
|
||||||
|
qty = extra.qty * component.get_qty_multiplier(extra.qty_multiplier)
|
||||||
|
if(self.mini_bom_mode):
|
||||||
|
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))
|
||||||
|
else:
|
||||||
|
rows.append(component_table_entry(extra.description, qty, extra.unit, extra.pn, extra.manufacturer, extra.mpn))
|
||||||
|
return(rows)
|
||||||
|
|
||||||
|
def get_additional_component_bom(self, component: Union[Connector, Cable]) -> List[dict]:
|
||||||
|
bom_entries = []
|
||||||
|
for part in component.additional_components:
|
||||||
|
qty = part.qty * component.get_qty_multiplier(part.qty_multiplier)
|
||||||
|
bom_entries.append(
|
||||||
|
{
|
||||||
|
'item': part.description,
|
||||||
|
'qty': qty,
|
||||||
|
'unit': part.unit,
|
||||||
|
'manufacturer': part.manufacturer,
|
||||||
|
'mpn': part.mpn,
|
||||||
|
'pn': part.pn,
|
||||||
|
'designators': component.name if component.show_name else None
|
||||||
|
}
|
||||||
|
)
|
||||||
|
return(bom_entries)
|
||||||
|
|
||||||
def bom(self):
|
def bom(self):
|
||||||
# if the bom has previously been generated then return the generated bom
|
# if the bom has previously been generated then return the generated bom
|
||||||
if self._bom:
|
if self._bom:
|
||||||
return self._bom
|
return self._bom
|
||||||
bom_items = []
|
bom_entries = []
|
||||||
|
|
||||||
# connectors
|
# connectors
|
||||||
for connector in self.connectors.values():
|
for connector in self.connectors.values():
|
||||||
if not connector.ignore_in_bom:
|
if not connector.ignore_in_bom:
|
||||||
conn_type = f', {remove_line_breaks(connector.type)}' if connector.type else ''
|
conn_type = f', {connector.type}' if connector.type else ''
|
||||||
conn_subtype = f', {remove_line_breaks(connector.subtype)}' if connector.subtype else ''
|
conn_subtype = f', {connector.subtype}' if connector.subtype else ''
|
||||||
conn_pincount = f', {connector.pincount} pins' if connector.style != 'simple' else ''
|
conn_pincount = f', {connector.pincount} pins' if connector.style != 'simple' else ''
|
||||||
conn_color = f', {connector.color}' if connector.color else ''
|
conn_color = f', {connector.color}' if connector.color else ''
|
||||||
name = f'Connector{conn_type}{conn_subtype}{conn_pincount}{conn_color}'
|
description = f'Connector{conn_type}{conn_subtype}{conn_pincount}{conn_color}'
|
||||||
item = {'item': name, 'qty': 1, 'unit': '', 'designators': connector.name if connector.show_name else None,
|
entry = {'item': description, 'qty': 1, 'unit': '', 'designators': connector.name if connector.show_name else None,
|
||||||
'manufacturer': remove_line_breaks(connector.manufacturer), 'mpn': remove_line_breaks(connector.mpn), 'pn': connector.pn}
|
'manufacturer': connector.manufacturer, 'mpn': connector.mpn, 'pn': connector.pn}
|
||||||
bom_items.append(item)
|
bom_entries.append(entry)
|
||||||
|
|
||||||
for part in connector.additional_components:
|
# add connectors aditional components to bom
|
||||||
qty = part.qty * connector.get_qty_multiplier(part.qty_multiplier)
|
bom_entries.extend(self.get_additional_component_bom(connector))
|
||||||
bom_items.append(
|
|
||||||
{
|
|
||||||
'item': part.description,
|
|
||||||
'qty': qty,
|
|
||||||
'unit': part.unit,
|
|
||||||
'manufacturer': part.manufacturer,
|
|
||||||
'mpn': part.mpn,
|
|
||||||
'pn': part.pn,
|
|
||||||
'designators': connector.name if connector.show_name else None
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
# cables
|
# cables
|
||||||
# TODO: If category can have other non-empty values than 'bundle', maybe it should be part of item name?
|
# TODO: If category can have other non-empty values than 'bundle', maybe it should be part of item name?
|
||||||
for cable in self.cables.values():
|
for cable in self.cables.values():
|
||||||
if not cable.ignore_in_bom:
|
if not cable.ignore_in_bom:
|
||||||
# create name beilds used by both cables and bundles
|
# create description fields used by both cables and bundles
|
||||||
cable_type = f', {remove_line_breaks(cable.type)}' if cable.type else ''
|
cable_type = f', {cable.type}' if cable.type else ''
|
||||||
gauge_name = f' x {cable.gauge} {cable.gauge_unit}' if cable.gauge else ' wires'
|
cable_gauge = f' x {cable.gauge} {cable.gauge_unit}' if cable.gauge else ' wires'
|
||||||
if cable.category != 'bundle':
|
if cable.category != 'bundle':
|
||||||
# process cable as a single entity
|
# process cable as a single entity
|
||||||
shield_name = ' shielded' if cable.shield else ''
|
cable_shield = ' shielded' if cable.shield else ''
|
||||||
name = f'Cable{cable_type}, {cable.wirecount}{gauge_name}{shield_name}'
|
name = f'Cable{cable_type}, {cable.wirecount}{cable_gauge}{cable_shield}'
|
||||||
item = {'item': name, 'qty': cable.length, 'unit': 'm', 'designators': cable.name,
|
entry = {'item': name, 'qty': cable.length, 'unit': 'm', 'designators': cable.name if cable.show_name else None,
|
||||||
'manufacturer': remove_line_breaks(cable.manufacturer), 'mpn': remove_line_breaks(cable.mpn), 'pn': cable.pn}
|
'manufacturer': cable.manufacturer, 'mpn': cable.mpn, 'pn': cable.pn}
|
||||||
bom_items.append(item)
|
bom_entries.append(entry)
|
||||||
else:
|
else:
|
||||||
# add each wire from the bundle to the bom
|
# add each wire from the bundle to the bom
|
||||||
for index, color in enumerate(cable.colors, 0):
|
for index, color in enumerate(cable.colors):
|
||||||
wire_color = f', {color}' if color else ''
|
wire_color = f', {color}' if color else ''
|
||||||
name = f'Wire{cable_type}{gauge_name}{wire_color}'
|
description = f'Wire{cable_type}{cable_gauge}{wire_color}'
|
||||||
item = {'item': name, 'qty': cable.length, 'unit': 'm', 'designators': cable.name,
|
entry = {'item': description, 'qty': cable.length, 'unit': 'm', 'designators': cable.name if cable.show_name else None,
|
||||||
'manufacturer': remove_line_breaks(index_if_list(cable.manufacturer, index)),
|
'manufacturer': index_if_list(cable.manufacturer, index),
|
||||||
'mpn': remove_line_breaks(index_if_list(cable.mpn, index)), 'pn': index_if_list(cable.pn, index)}
|
'mpn': index_if_list(cable.mpn, index), 'pn': index_if_list(cable.pn, index)}
|
||||||
bom_items.append(item)
|
bom_entries.append(entry)
|
||||||
|
|
||||||
for part in cable.additional_components:
|
# add cable/bundles aditional components to bom
|
||||||
qty = part.qty * cable.get_qty_multiplier(part.qty_multiplier)
|
bom_entries.extend(self.get_additional_component_bom(cable))
|
||||||
bom_items.append(
|
|
||||||
{
|
|
||||||
'item': part.description,
|
|
||||||
'qty': qty,
|
|
||||||
'unit': part.unit,
|
|
||||||
'manufacturer': part.manufacturer,
|
|
||||||
'mpn': part.mpn,
|
|
||||||
'pn': part.pn,
|
|
||||||
'designators': cable.name
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
for item in self.additional_bom_items:
|
for item in self.additional_bom_items:
|
||||||
item = {'item': item.get('description', ''), 'qty': item.get('qty'), 'unit': item.get('unit'), 'designators': item.get('designators'),
|
entry = {'item': item.get('description', ''), 'qty': item.get('qty'), 'unit': item.get('unit'), 'designators': item.get('designators'),
|
||||||
'manufacturer': item.get('manufacturer'), 'mpn': item.get('mpn'), 'pn': item.get('pn')}
|
'manufacturer': item.get('manufacturer'), 'mpn': item.get('mpn'), 'pn': item.get('pn')}
|
||||||
bom_items.append(item)
|
bom_entries.append(entry)
|
||||||
|
|
||||||
# deduplicate bom
|
# deduplicate bom
|
||||||
bom_types_group = lambda bt: (bt['item'], bt['unit'], bt['manufacturer'], bt['mpn'], bt['pn'])
|
bom_types_group = lambda bt: (bt['item'], bt['unit'], bt['manufacturer'], bt['mpn'], bt['pn'])
|
||||||
for group in Counter([bom_types_group(v) for v in bom_items]):
|
for group in Counter([bom_types_group(v) for v in bom_entries]):
|
||||||
items = [v for v in bom_items if bom_types_group(v) == group]
|
group_entries = [v for v in bom_entries if bom_types_group(v) == group]
|
||||||
shared = items[0]
|
shared = group_entries[0]
|
||||||
designators = []
|
designators = []
|
||||||
for item in items:
|
for group_entry in group_entries:
|
||||||
if item.get('designators'):
|
if group_entry.get('designators'):
|
||||||
if isinstance(item['designators'], List):
|
if isinstance(group_entry['designators'], List):
|
||||||
designators.extend(item['designators'])
|
designators.extend(group_entry['designators'])
|
||||||
else:
|
else:
|
||||||
designators.append(item['designators'])
|
designators.append(group_entry['designators'])
|
||||||
designators = list(dict.fromkeys(designators)) # remove duplicates
|
designators = list(dict.fromkeys(designators)) # remove duplicates
|
||||||
designators.sort()
|
designators.sort()
|
||||||
total_qty = sum(i['qty'] for i in items)
|
total_qty = sum(i['qty'] for i in group_entries)
|
||||||
item = {'item': shared['item'], 'qty': round(total_qty, 3), 'unit': shared['unit'], 'designators': designators,
|
entry = {'item': shared['item'], 'qty': round(total_qty, 3), 'unit': shared['unit'], 'designators': designators,
|
||||||
'manufacturer': shared['manufacturer'], 'mpn': shared['mpn'], 'pn': shared['pn']}
|
'manufacturer': shared['manufacturer'], 'mpn': shared['mpn'], 'pn': shared['pn']}
|
||||||
self._bom.append(item)
|
self._bom.append(entry)
|
||||||
|
|
||||||
self._bom = sorted(self._bom, key=lambda k: k['item']) # sort list of dicts by their values (https://stackoverflow.com/a/73050)
|
self._bom = sorted(self._bom, key=lambda k: k['item']) # sort list of dicts by their values (https://stackoverflow.com/a/73050)
|
||||||
# add index
|
# add index
|
||||||
@ -476,5 +468,6 @@ class Harness:
|
|||||||
item_list = [item.get(key, '') for key in keys] # fill missing values with blanks
|
item_list = [item.get(key, '') for key in keys] # fill missing values with blanks
|
||||||
item_list = [', '.join(subitem) if isinstance(subitem, List) else subitem for subitem in item_list] # convert any lists into comma separated strings
|
item_list = [', '.join(subitem) if isinstance(subitem, List) else subitem for subitem in item_list] # convert any lists into comma separated strings
|
||||||
item_list = ['' if subitem is None else subitem for subitem in item_list] # if a field is missing for some (but not all) BOM items
|
item_list = ['' if subitem is None else subitem for subitem in item_list] # if a field is missing for some (but not all) BOM items
|
||||||
|
item_list = [remove_line_breaks(subitem) for subitem in item_list] # remove line breaks if present
|
||||||
bom_list.append(item_list)
|
bom_list.append(item_list)
|
||||||
return bom_list
|
return bom_list
|
||||||
|
|||||||
@ -198,5 +198,5 @@ def component_table_entry(type, qty, unit=None, pn=None, manufacturer=None, mpn=
|
|||||||
if manufacturer_str:
|
if manufacturer_str:
|
||||||
output += manufacturer_str
|
output += manufacturer_str
|
||||||
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 visible 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>'
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user