Implement sorting of BOM sections

This commit is contained in:
Daniel Rojas 2020-06-14 21:31:02 +02:00
parent 5a601f73d0
commit 6785417c5a

View File

@ -207,18 +207,22 @@ class Harness:
def bom(self): def bom(self):
bom = [] bom = []
bom_connectors = []
bom_cables = []
# connectors # connectors
types = Counter([(v.type, v.subtype, v.pincount) for v in self.connectors.values()]) types = Counter([(v.type, v.subtype, v.pincount) for v in self.connectors.values()])
for type in types: for type in types:
items = {k: v for k, v in self.connectors.items() if (v.type, v.subtype, v.pincount) == type} items = {k: v for k, v in self.connectors.items() if (v.type, v.subtype, v.pincount) == type}
shared = next(iter(items.values()))
designators = list(items.keys()) designators = list(items.keys())
designators.sort() designators.sort()
shared = next(iter(items.values()))
name = '{type}{subtype}{pincount}'.format(type = shared.type, name = '{type}{subtype}{pincount}'.format(type = shared.type,
subtype = ', {}'.format(shared.subtype) if shared.subtype else '', subtype = ', {}'.format(shared.subtype) if shared.subtype else '',
pincount = ', {} pins'.format(shared.pincount) if shared.category != 'ferrule' else '') pincount = ', {} pins'.format(shared.pincount) if shared.category != 'ferrule' else '')
item = {'item': name, 'qty': len(designators), 'unit': '', 'designators': designators} item = {'item': name, 'qty': len(designators), 'unit': '', 'designators': designators if shared.category != 'ferrule' else ''}
bom.append(item) bom_connectors.append(item)
bom_connectors = sorted(bom_connectors, key=lambda k: k['item']) # https://stackoverflow.com/a/73050
bom.extend(bom_connectors)
# cables # cables
types = Counter([(v.category, v.gauge, v.gauge_unit, v.wirecount, v.shield) for v in self.cables.values()]) types = Counter([(v.category, v.gauge, v.gauge_unit, v.wirecount, v.shield) for v in self.cables.values()])
for type in types: for type in types:
@ -232,7 +236,7 @@ class Harness:
gauge = ' {} {}'.format(shared.gauge, shared.gauge_unit) if shared.gauge else '', gauge = ' {} {}'.format(shared.gauge, shared.gauge_unit) if shared.gauge else '',
shield = ' shielded' if shared.shield else '') shield = ' shielded' if shared.shield else '')
item = {'item': name, 'qty': round(total_length, 3), 'unit': 'm', 'designators': designators} item = {'item': name, 'qty': round(total_length, 3), 'unit': 'm', 'designators': designators}
bom.append(item) bom_cables.append(item)
# bundles (ignores wirecount) # bundles (ignores wirecount)
wirelist = [] wirelist = []
# list all cables again, since bundles are represented as wires internally, with the category='bundle' set # list all cables again, since bundles are represented as wires internally, with the category='bundle' set
@ -260,14 +264,16 @@ class Harness:
total_length = sum(i['length'] for i in items) total_length = sum(i['length'] for i in items)
name = 'Wire {} {} {}'.format(shared['gauge'], shared['gauge_unit'], shared['color']) name = 'Wire {} {} {}'.format(shared['gauge'], shared['gauge_unit'], shared['color'])
item = {'item': name, 'qty': round(total_length, 3), 'unit': 'm', 'designators': designators} item = {'item': name, 'qty': round(total_length, 3), 'unit': 'm', 'designators': designators}
bom.append(item) bom_cables.append(item)
bom_cables = sorted(bom_cables, key=lambda k: k['item']) # https://stackoverflow.com/a/73050
bom.extend(bom_cables)
return bom return bom
def bom_list(self): def bom_list(self):
bom = self.bom() bom = self.bom()
keys = ['item', 'qty', 'unit', 'designators'] keys = ['item', 'qty', 'unit', 'designators']
bom_list = [] bom_list = []
bom_list.append(k.capitalize() for k in keys) # create header row with keys bom_list.append([k.capitalize() for k in keys]) # create header row with keys
for item in bom: for item in bom:
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
for i, subitem in enumerate(item_list): for i, subitem in enumerate(item_list):