From 0f3b5e9edf70c92634943b969bc32c043f1f84dc Mon Sep 17 00:00:00 2001 From: KV Date: Sat, 14 Nov 2020 20:19:31 +0100 Subject: [PATCH 01/38] Skip assignment and return expression directly --- src/wireviz/wv_bom.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/wireviz/wv_bom.py b/src/wireviz/wv_bom.py index ac5b071d..3176c2d4 100644 --- a/src/wireviz/wv_bom.py +++ b/src/wireviz/wv_bom.py @@ -115,8 +115,7 @@ def generate_bom(harness): bom = sorted(bom, key=lambda k: k['item']) # sort list of dicts by their values (https://stackoverflow.com/a/73050) # add an incrementing id to each bom item - bom = [{**entry, 'id': index} for index, entry in enumerate(bom, 1)] - return bom + return [{**entry, 'id': index} for index, entry in enumerate(bom, 1)] def get_bom_index(harness, item, unit, manufacturer, mpn, pn): # Remove linebreaks and clean whitespace of values in search From 6525537312b57834e6b50a8e90bf28d1f2331153 Mon Sep 17 00:00:00 2001 From: KV Date: Sat, 14 Nov 2020 20:21:58 +0100 Subject: [PATCH 02/38] Simplify get_bom_index() parameters - Use the actual BOM as first parameter instead of the whole harness. - Use a whole AdditionalComponent as second parameter instead of each attribute separately. --- src/wireviz/wv_bom.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/wireviz/wv_bom.py b/src/wireviz/wv_bom.py index 3176c2d4..84426a5a 100644 --- a/src/wireviz/wv_bom.py +++ b/src/wireviz/wv_bom.py @@ -4,7 +4,7 @@ from typing import List, Union from collections import Counter -from wireviz.DataClasses import Connector, Cable +from wireviz.DataClasses import AdditionalComponent, Connector, Cable from wireviz.wv_gv_html import html_line_breaks from wireviz.wv_helper import clean_whitespace @@ -15,7 +15,7 @@ def get_additional_component_table(harness, component: Union[Connector, Cable]) for extra in component.additional_components: qty = extra.qty * component.get_qty_multiplier(extra.qty_multiplier) if harness.mini_bom_mode: - id = get_bom_index(harness, extra.description, extra.unit, extra.manufacturer, extra.mpn, extra.pn) + id = get_bom_index(harness.bom(), extra) rows.append(component_table_entry(f'#{id} ({extra.type.rstrip()})', qty, extra.unit)) else: rows.append(component_table_entry(extra.description, qty, extra.unit, extra.pn, extra.manufacturer, extra.mpn)) @@ -117,10 +117,10 @@ def generate_bom(harness): # add an incrementing id to each bom item return [{**entry, 'id': index} for index, entry in enumerate(bom, 1)] -def get_bom_index(harness, item, unit, manufacturer, mpn, pn): +def get_bom_index(bom: List[dict], extra: AdditionalComponent) -> int: # Remove linebreaks and clean whitespace of values in search - target = tuple(clean_whitespace(v) for v in (item, unit, manufacturer, mpn, pn)) - for entry in harness.bom(): + target = tuple(clean_whitespace(v) for v in (extra.description, extra.unit, extra.manufacturer, extra.mpn, extra.pn)) + for entry in bom: if (entry['item'], entry['unit'], entry['manufacturer'], entry['mpn'], entry['pn']) == target: return entry['id'] return None From 45b13ef79736c027f8fadfdd10661404908edeb9 Mon Sep 17 00:00:00 2001 From: KV Date: Sat, 14 Nov 2020 20:51:50 +0100 Subject: [PATCH 03/38] Use the same lambda in get_bom_index() as for deduplicating BOM Move the lambda declaration out of the function scope for common access from two different functions. --- src/wireviz/wv_bom.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/wireviz/wv_bom.py b/src/wireviz/wv_bom.py index 84426a5a..d9a1f259 100644 --- a/src/wireviz/wv_bom.py +++ b/src/wireviz/wv_bom.py @@ -36,6 +36,8 @@ def get_additional_component_bom(component: Union[Connector, Cable]) -> List[dic }) return(bom_entries) +bom_types_group = lambda bt: (bt['item'], bt['unit'], bt['manufacturer'], bt['mpn'], bt['pn']) + def generate_bom(harness): from wireviz.Harness import Harness # Local import to avoid circular imports bom_entries = [] @@ -97,7 +99,6 @@ def generate_bom(harness): # deduplicate bom bom = [] - 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_entries]): group_entries = [v for v in bom_entries if bom_types_group(v) == group] designators = [] @@ -121,7 +122,7 @@ def get_bom_index(bom: List[dict], extra: AdditionalComponent) -> int: # Remove linebreaks and clean whitespace of values in search target = tuple(clean_whitespace(v) for v in (extra.description, extra.unit, extra.manufacturer, extra.mpn, extra.pn)) for entry in bom: - if (entry['item'], entry['unit'], entry['manufacturer'], entry['mpn'], entry['pn']) == target: + if bom_types_group(entry) == target: return entry['id'] return None From da453db9f091e8fe5a53a93477ab8e132eb112c2 Mon Sep 17 00:00:00 2001 From: KV Date: Sat, 14 Nov 2020 21:06:22 +0100 Subject: [PATCH 04/38] Convert dataclass object to dict to use the same lambda --- src/wireviz/wv_bom.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/wireviz/wv_bom.py b/src/wireviz/wv_bom.py index d9a1f259..641c434e 100644 --- a/src/wireviz/wv_bom.py +++ b/src/wireviz/wv_bom.py @@ -1,8 +1,9 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- -from typing import List, Union from collections import Counter +from dataclasses import asdict +from typing import List, Union from wireviz.DataClasses import AdditionalComponent, Connector, Cable from wireviz.wv_gv_html import html_line_breaks @@ -120,7 +121,7 @@ def generate_bom(harness): def get_bom_index(bom: List[dict], extra: AdditionalComponent) -> int: # Remove linebreaks and clean whitespace of values in search - target = tuple(clean_whitespace(v) for v in (extra.description, extra.unit, extra.manufacturer, extra.mpn, extra.pn)) + target = tuple(clean_whitespace(v) for v in bom_types_group({**asdict(extra), 'item': extra.description})) for entry in bom: if bom_types_group(entry) == target: return entry['id'] From 347f1e303181796f67766887eb4d29fb45723a3b Mon Sep 17 00:00:00 2001 From: KV Date: Sat, 14 Nov 2020 21:26:15 +0100 Subject: [PATCH 05/38] Redefine the common lambda to an ordinary function --- src/wireviz/wv_bom.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/wireviz/wv_bom.py b/src/wireviz/wv_bom.py index 641c434e..9e451aea 100644 --- a/src/wireviz/wv_bom.py +++ b/src/wireviz/wv_bom.py @@ -3,7 +3,7 @@ from collections import Counter from dataclasses import asdict -from typing import List, Union +from typing import List, Tuple, Union from wireviz.DataClasses import AdditionalComponent, Connector, Cable from wireviz.wv_gv_html import html_line_breaks @@ -37,7 +37,9 @@ def get_additional_component_bom(component: Union[Connector, Cable]) -> List[dic }) return(bom_entries) -bom_types_group = lambda bt: (bt['item'], bt['unit'], bt['manufacturer'], bt['mpn'], bt['pn']) +def bom_types_group(entry: dict) -> Tuple[str, ...]: + """Return a tuple of values from the dict that must be equal to join BOM entries.""" + return tuple(entry.get(key) for key in ('item', 'unit', 'manufacturer', 'mpn', 'pn')) def generate_bom(harness): from wireviz.Harness import Harness # Local import to avoid circular imports From 6378b9654178a716820e35859e9eba1748618d24 Mon Sep 17 00:00:00 2001 From: KV Date: Sat, 14 Nov 2020 21:28:33 +0100 Subject: [PATCH 06/38] Simplify BOM header row logic --- src/wireviz/wv_bom.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wireviz/wv_bom.py b/src/wireviz/wv_bom.py index 9e451aea..e9fc4785 100644 --- a/src/wireviz/wv_bom.py +++ b/src/wireviz/wv_bom.py @@ -140,7 +140,7 @@ def bom_list(bom): "pn": "P/N", "mpn": "MPN" } - bom_list.append([(bom_headings[k] if k in bom_headings else k.capitalize()) for k in keys]) # create header row with keys + bom_list.append([bom_headings.get(k, k.capitalize()) for k in keys]) # create header row with keys for item in bom: 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 From d2f8034961b1915e899c42f1e57ace766b31526e Mon Sep 17 00:00:00 2001 From: KV Date: Sat, 14 Nov 2020 21:37:31 +0100 Subject: [PATCH 07/38] Simplify collecting designators for a joined BOM entry Assign input designators once to a temporary variable for easy reusage. --- src/wireviz/wv_bom.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/wireviz/wv_bom.py b/src/wireviz/wv_bom.py index e9fc4785..c27a2045 100644 --- a/src/wireviz/wv_bom.py +++ b/src/wireviz/wv_bom.py @@ -106,11 +106,8 @@ def generate_bom(harness): group_entries = [v for v in bom_entries if bom_types_group(v) == group] designators = [] for group_entry in group_entries: - if group_entry.get('designators'): - if isinstance(group_entry['designators'], List): - designators.extend(group_entry['designators']) - else: - designators.append(group_entry['designators']) + d = group_entry.get('designators') + designators.extend(d if isinstance(d, List) else [d] if d else []) designators = list(dict.fromkeys(designators)) # remove duplicates designators.sort() total_qty = sum(entry['qty'] for entry in group_entries) From e1d7babf63b14733fb21ab939c899d6ac3e73037 Mon Sep 17 00:00:00 2001 From: KV Date: Sat, 14 Nov 2020 21:43:57 +0100 Subject: [PATCH 08/38] Simplify deduplication and sorting of collected designators --- src/wireviz/wv_bom.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/wireviz/wv_bom.py b/src/wireviz/wv_bom.py index c27a2045..c1789633 100644 --- a/src/wireviz/wv_bom.py +++ b/src/wireviz/wv_bom.py @@ -108,10 +108,8 @@ def generate_bom(harness): for group_entry in group_entries: d = group_entry.get('designators') designators.extend(d if isinstance(d, List) else [d] if d else []) - designators = list(dict.fromkeys(designators)) # remove duplicates - designators.sort() total_qty = sum(entry['qty'] for entry in group_entries) - bom.append({**group_entries[0], 'qty': round(total_qty, 3), 'designators': designators}) + bom.append({**group_entries[0], 'qty': round(total_qty, 3), 'designators': sorted(set(designators))}) bom = sorted(bom, key=lambda k: k['item']) # sort list of dicts by their values (https://stackoverflow.com/a/73050) From 74462cd2250002edaab4e64fe023024de62b4143 Mon Sep 17 00:00:00 2001 From: KV Date: Mon, 16 Nov 2020 19:59:52 +0100 Subject: [PATCH 09/38] Remove parentheses around return expressions https://stackoverflow.com/questions/4978567/should-a-return-statement-have-parentheses --- src/wireviz/wv_bom.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/wireviz/wv_bom.py b/src/wireviz/wv_bom.py index c1789633..03286618 100644 --- a/src/wireviz/wv_bom.py +++ b/src/wireviz/wv_bom.py @@ -20,7 +20,7 @@ def get_additional_component_table(harness, component: Union[Connector, Cable]) rows.append(component_table_entry(f'#{id} ({extra.type.rstrip()})', qty, extra.unit)) else: rows.append(component_table_entry(extra.description, qty, extra.unit, extra.pn, extra.manufacturer, extra.mpn)) - return(rows) + return rows def get_additional_component_bom(component: Union[Connector, Cable]) -> List[dict]: bom_entries = [] @@ -35,7 +35,7 @@ def get_additional_component_bom(component: Union[Connector, Cable]) -> List[dic 'pn': part.pn, 'designators': component.name if component.show_name else None }) - return(bom_entries) + return bom_entries def bom_types_group(entry: dict) -> Tuple[str, ...]: """Return a tuple of values from the dict that must be equal to join BOM entries.""" From 10b1198b77e56d23f59c835fe1f782f69b9bd1a8 Mon Sep 17 00:00:00 2001 From: KV Date: Fri, 27 Nov 2020 00:37:03 +0100 Subject: [PATCH 10/38] Move out code from inner loop into helper functions --- src/wireviz/wv_bom.py | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/src/wireviz/wv_bom.py b/src/wireviz/wv_bom.py index 03286618..223de38c 100644 --- a/src/wireviz/wv_bom.py +++ b/src/wireviz/wv_bom.py @@ -3,7 +3,7 @@ from collections import Counter from dataclasses import asdict -from typing import List, Tuple, Union +from typing import Any, List, Tuple, Union from wireviz.DataClasses import AdditionalComponent, Connector, Cable from wireviz.wv_gv_html import html_line_breaks @@ -106,8 +106,7 @@ def generate_bom(harness): group_entries = [v for v in bom_entries if bom_types_group(v) == group] designators = [] for group_entry in group_entries: - d = group_entry.get('designators') - designators.extend(d if isinstance(d, List) else [d] if d else []) + designators.extend(make_list(group_entry.get('designators'))) total_qty = sum(entry['qty'] for entry in group_entries) bom.append({**group_entries[0], 'qty': round(total_qty, 3), 'designators': sorted(set(designators))}) @@ -129,19 +128,13 @@ def bom_list(bom): for fieldname in ['pn', 'manufacturer', 'mpn']: # these optional BOM columns will only be included if at least one BOM item actually uses them if any(entry.get(fieldname) for entry in bom): keys.append(fieldname) - bom_list = [] # list of staic bom header names, headers not specified here are generated by capitilising the internal name bom_headings = { "pn": "P/N", "mpn": "MPN" } - bom_list.append([bom_headings.get(k, k.capitalize()) for k in keys]) # create header row with keys - for item in bom: - 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 = ['' if subitem is None else subitem for subitem in item_list] # if a field is missing for some (but not all) BOM items - bom_list.append(item_list) - return bom_list + return ([[bom_headings.get(k, k.capitalize()) for k in keys]] + # Create header row with key names + [[make_str(entry.get(k)) for k in keys] for entry in bom]) # Create string list for each entry row def component_table_entry(type, qty, unit=None, pn=None, manufacturer=None, mpn=None): output = f'{qty}' @@ -174,3 +167,11 @@ def manufacturer_info_field(manufacturer, mpn): # Return the value indexed if it is a list, or simply the value otherwise. def index_if_list(value, index): return value[index] if isinstance(value, list) else value + +def make_list(value: Any) -> list: + """Return value if a list, empty list if None, or single element list otherwise.""" + return value if isinstance(value, list) else [] if value is None else [value] + +def make_str(value: Any) -> str: + """Return comma separated elements if a list, empty string if None, or value as a string otherwise.""" + return ', '.join(str(element) for element in make_list(value)) From cdca708da98b1b1511c2257b2af87b4dd40d7a23 Mon Sep 17 00:00:00 2001 From: KV Date: Fri, 27 Nov 2020 01:16:26 +0100 Subject: [PATCH 11/38] Move BOM sorting above grouping to use groupby() - Use one common entry loop to consume iterator only once. - Use same key function for sort() and groupby(), except replace None with empty string when sorting. --- src/wireviz/wv_bom.py | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/src/wireviz/wv_bom.py b/src/wireviz/wv_bom.py index 223de38c..d2546767 100644 --- a/src/wireviz/wv_bom.py +++ b/src/wireviz/wv_bom.py @@ -1,8 +1,8 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- -from collections import Counter from dataclasses import asdict +from itertools import groupby from typing import Any, List, Tuple, Union from wireviz.DataClasses import AdditionalComponent, Connector, Cable @@ -100,17 +100,20 @@ def generate_bom(harness): # remove line breaks if present and cleanup any resulting whitespace issues bom_entries = [{k: clean_whitespace(v) for k, v in entry.items()} for entry in bom_entries] + # Sort entries to prepare grouping on the same key function. + bom_entries.sort(key=lambda entry: tuple(attr or '' for attr in bom_types_group(entry))) + # deduplicate bom bom = [] - for group in Counter([bom_types_group(v) for v in bom_entries]): - group_entries = [v for v in bom_entries if bom_types_group(v) == group] + for _, group in groupby(bom_entries, bom_types_group): + last_entry = None + total_qty = 0 designators = [] - for group_entry in group_entries: + for group_entry in group: designators.extend(make_list(group_entry.get('designators'))) - total_qty = sum(entry['qty'] for entry in group_entries) - bom.append({**group_entries[0], 'qty': round(total_qty, 3), 'designators': sorted(set(designators))}) - - bom = sorted(bom, key=lambda k: k['item']) # sort list of dicts by their values (https://stackoverflow.com/a/73050) + total_qty += group_entry['qty'] + last_entry = group_entry + bom.append({**last_entry, 'qty': round(total_qty, 3), 'designators': sorted(set(designators))}) # add an incrementing id to each bom item return [{**entry, 'id': index} for index, entry in enumerate(bom, 1)] From f13f8a7dd7c2372c8125fdbf9c3c9cf0ad9220a5 Mon Sep 17 00:00:00 2001 From: KV Date: Fri, 27 Nov 2020 21:15:59 +0100 Subject: [PATCH 12/38] Make the BOM grouping function return string tuple for sorting --- src/wireviz/wv_bom.py | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/wireviz/wv_bom.py b/src/wireviz/wv_bom.py index d2546767..d21b96ef 100644 --- a/src/wireviz/wv_bom.py +++ b/src/wireviz/wv_bom.py @@ -38,8 +38,8 @@ def get_additional_component_bom(component: Union[Connector, Cable]) -> List[dic return bom_entries def bom_types_group(entry: dict) -> Tuple[str, ...]: - """Return a tuple of values from the dict that must be equal to join BOM entries.""" - return tuple(entry.get(key) for key in ('item', 'unit', 'manufacturer', 'mpn', 'pn')) + """Return a tuple of string values from the dict that must be equal to join BOM entries.""" + return tuple(make_str(entry.get(key)) for key in ('item', 'unit', 'manufacturer', 'mpn', 'pn')) def generate_bom(harness): from wireviz.Harness import Harness # Local import to avoid circular imports @@ -100,12 +100,9 @@ def generate_bom(harness): # remove line breaks if present and cleanup any resulting whitespace issues bom_entries = [{k: clean_whitespace(v) for k, v in entry.items()} for entry in bom_entries] - # Sort entries to prepare grouping on the same key function. - bom_entries.sort(key=lambda entry: tuple(attr or '' for attr in bom_types_group(entry))) - # deduplicate bom bom = [] - for _, group in groupby(bom_entries, bom_types_group): + for _, group in groupby(sorted(bom_entries, key=bom_types_group), key=bom_types_group): last_entry = None total_qty = 0 designators = [] From 96d393dfb757afc61ffb319c34035d8d2ce7c33d Mon Sep 17 00:00:00 2001 From: KV Date: Fri, 27 Nov 2020 22:20:45 +0100 Subject: [PATCH 13/38] Use a generator expressions and raise exception if failing Seems to be the most popular search alternative: https://stackoverflow.com/questions/8653516/python-list-of-dictionaries-search Raising StopIteration if not found is better than returning None to detect such an internal error more easily. --- src/wireviz/wv_bom.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/wireviz/wv_bom.py b/src/wireviz/wv_bom.py index d21b96ef..b7dda90c 100644 --- a/src/wireviz/wv_bom.py +++ b/src/wireviz/wv_bom.py @@ -116,12 +116,10 @@ def generate_bom(harness): return [{**entry, 'id': index} for index, entry in enumerate(bom, 1)] def get_bom_index(bom: List[dict], extra: AdditionalComponent) -> int: + """Return id of BOM entry or raise StopIteration if not found.""" # Remove linebreaks and clean whitespace of values in search target = tuple(clean_whitespace(v) for v in bom_types_group({**asdict(extra), 'item': extra.description})) - for entry in bom: - if bom_types_group(entry) == target: - return entry['id'] - return None + return next(entry['id'] for entry in bom if bom_types_group(entry) == target) def bom_list(bom): keys = ['id', 'item', 'qty', 'unit', 'designators'] # these BOM columns will always be included From 1d653c44eded9dceebbd81744651d147efd70b3b Mon Sep 17 00:00:00 2001 From: KV Date: Mon, 30 Nov 2020 18:10:24 +0100 Subject: [PATCH 14/38] Replace accumulation loop with sum expressions Make a list from the group iterator for reusage in sum expressions and to pick first group entry. The expected group sizes are very small, so performance loss by creating a temporary list should be neglectable. Alternativly, itertools.tee(group, 3) could be called to triplicate the iterator, but it was not chosen for readability reasons. --- src/wireviz/wv_bom.py | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/src/wireviz/wv_bom.py b/src/wireviz/wv_bom.py index b7dda90c..dbd5f798 100644 --- a/src/wireviz/wv_bom.py +++ b/src/wireviz/wv_bom.py @@ -103,14 +103,10 @@ def generate_bom(harness): # deduplicate bom bom = [] for _, group in groupby(sorted(bom_entries, key=bom_types_group), key=bom_types_group): - last_entry = None - total_qty = 0 - designators = [] - for group_entry in group: - designators.extend(make_list(group_entry.get('designators'))) - total_qty += group_entry['qty'] - last_entry = group_entry - bom.append({**last_entry, 'qty': round(total_qty, 3), 'designators': sorted(set(designators))}) + group_entries = list(group) + designators = sum((make_list(entry.get('designators')) for entry in group_entries), []) + total_qty = sum(entry['qty'] for entry in group_entries) + bom.append({**group_entries[0], 'qty': round(total_qty, 3), 'designators': sorted(set(designators))}) # add an incrementing id to each bom item return [{**entry, 'id': index} for index, entry in enumerate(bom, 1)] From 12e570fdad2e0c50e88c0a671967ae27b673c970 Mon Sep 17 00:00:00 2001 From: KV Date: Mon, 30 Nov 2020 19:31:08 +0100 Subject: [PATCH 15/38] Add function type hints and doc strings --- src/wireviz/wv_bom.py | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/src/wireviz/wv_bom.py b/src/wireviz/wv_bom.py index dbd5f798..66f66454 100644 --- a/src/wireviz/wv_bom.py +++ b/src/wireviz/wv_bom.py @@ -3,13 +3,14 @@ from dataclasses import asdict from itertools import groupby -from typing import Any, List, Tuple, Union +from typing import Any, List, Optional, Tuple, Union from wireviz.DataClasses import AdditionalComponent, Connector, Cable from wireviz.wv_gv_html import html_line_breaks from wireviz.wv_helper import clean_whitespace -def get_additional_component_table(harness, component: Union[Connector, Cable]) -> List[str]: +def get_additional_component_table(harness: "Harness", component: Union[Connector, Cable]) -> List[str]: + """Return a list of diagram node table row strings with additional components.""" rows = [] if component.additional_components: rows.append(["Additional components"]) @@ -23,6 +24,7 @@ def get_additional_component_table(harness, component: Union[Connector, Cable]) return rows def get_additional_component_bom(component: Union[Connector, Cable]) -> List[dict]: + """Return a list of BOM entries with additional components.""" bom_entries = [] for part in component.additional_components: qty = part.qty * component.get_qty_multiplier(part.qty_multiplier) @@ -41,7 +43,8 @@ def bom_types_group(entry: dict) -> Tuple[str, ...]: """Return a tuple of string values from the dict that must be equal to join BOM entries.""" return tuple(make_str(entry.get(key)) for key in ('item', 'unit', 'manufacturer', 'mpn', 'pn')) -def generate_bom(harness): +def generate_bom(harness: "Harness") -> List[dict]: + """Return a list of BOM entries generated from the harness.""" from wireviz.Harness import Harness # Local import to avoid circular imports bom_entries = [] # connectors @@ -117,7 +120,8 @@ def get_bom_index(bom: List[dict], extra: AdditionalComponent) -> int: target = tuple(clean_whitespace(v) for v in bom_types_group({**asdict(extra), 'item': extra.description})) return next(entry['id'] for entry in bom if bom_types_group(entry) == target) -def bom_list(bom): +def bom_list(bom: List[dict]) -> List[List[str]]: + """Return list of BOM rows as lists of column strings with headings in top row.""" keys = ['id', 'item', 'qty', 'unit', 'designators'] # these BOM columns will always be included for fieldname in ['pn', 'manufacturer', 'mpn']: # these optional BOM columns will only be included if at least one BOM item actually uses them if any(entry.get(fieldname) for entry in bom): @@ -130,7 +134,15 @@ def bom_list(bom): return ([[bom_headings.get(k, k.capitalize()) for k in keys]] + # Create header row with key names [[make_str(entry.get(k)) for k in keys] for entry in bom]) # Create string list for each entry row -def component_table_entry(type, qty, unit=None, pn=None, manufacturer=None, mpn=None): +def component_table_entry( + type: str, + qty: Union[int, float], + unit: Optional[str] = None, + pn: Optional[str] = None, + manufacturer: Optional[str] = None, + mpn: Optional[str] = None, + ) -> str: + """Return a diagram node table row string with an additional component.""" output = f'{qty}' if unit: output += f' {unit}' @@ -152,14 +164,15 @@ def component_table_entry(type, qty, unit=None, pn=None, manufacturer=None, mpn= {output} ''' -def manufacturer_info_field(manufacturer, mpn): +def manufacturer_info_field(manufacturer: Optional[str], mpn: Optional[str]) -> Optional[str]: + """Return the manufacturer and/or the mpn in one single string or None otherwise.""" if manufacturer or mpn: return f'{manufacturer if manufacturer else "MPN"}{": " + str(mpn) if mpn else ""}' else: return None -# Return the value indexed if it is a list, or simply the value otherwise. -def index_if_list(value, index): +def index_if_list(value: Any, index: int) -> Any: + """Return the value indexed if it is a list, or simply the value otherwise.""" return value[index] if isinstance(value, list) else value def make_list(value: Any) -> list: From c22c42e72263d8e3634eff577f886f01596ef8bb Mon Sep 17 00:00:00 2001 From: KV Date: Tue, 1 Dec 2020 16:54:32 +0100 Subject: [PATCH 16/38] Add BOMEntry type alias This type alias describes the possible types of keys and values in the dict representing a BOM entry. --- src/wireviz/wv_bom.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/wireviz/wv_bom.py b/src/wireviz/wv_bom.py index 66f66454..985084fe 100644 --- a/src/wireviz/wv_bom.py +++ b/src/wireviz/wv_bom.py @@ -3,12 +3,14 @@ from dataclasses import asdict from itertools import groupby -from typing import Any, List, Optional, Tuple, Union +from typing import Any, Dict, List, Optional, Tuple, Union from wireviz.DataClasses import AdditionalComponent, Connector, Cable from wireviz.wv_gv_html import html_line_breaks from wireviz.wv_helper import clean_whitespace +BOMEntry = Dict[str, Union[str, int, float, List[str], None]] + def get_additional_component_table(harness: "Harness", component: Union[Connector, Cable]) -> List[str]: """Return a list of diagram node table row strings with additional components.""" rows = [] @@ -23,7 +25,7 @@ def get_additional_component_table(harness: "Harness", component: Union[Connecto rows.append(component_table_entry(extra.description, qty, extra.unit, extra.pn, extra.manufacturer, extra.mpn)) return rows -def get_additional_component_bom(component: Union[Connector, Cable]) -> List[dict]: +def get_additional_component_bom(component: Union[Connector, Cable]) -> List[BOMEntry]: """Return a list of BOM entries with additional components.""" bom_entries = [] for part in component.additional_components: @@ -39,11 +41,11 @@ def get_additional_component_bom(component: Union[Connector, Cable]) -> List[dic }) return bom_entries -def bom_types_group(entry: dict) -> Tuple[str, ...]: +def bom_types_group(entry: BOMEntry) -> Tuple[str, ...]: """Return a tuple of string values from the dict that must be equal to join BOM entries.""" return tuple(make_str(entry.get(key)) for key in ('item', 'unit', 'manufacturer', 'mpn', 'pn')) -def generate_bom(harness: "Harness") -> List[dict]: +def generate_bom(harness: "Harness") -> List[BOMEntry]: """Return a list of BOM entries generated from the harness.""" from wireviz.Harness import Harness # Local import to avoid circular imports bom_entries = [] @@ -114,13 +116,13 @@ def generate_bom(harness: "Harness") -> List[dict]: # add an incrementing id to each bom item return [{**entry, 'id': index} for index, entry in enumerate(bom, 1)] -def get_bom_index(bom: List[dict], extra: AdditionalComponent) -> int: +def get_bom_index(bom: List[BOMEntry], extra: AdditionalComponent) -> int: """Return id of BOM entry or raise StopIteration if not found.""" # Remove linebreaks and clean whitespace of values in search target = tuple(clean_whitespace(v) for v in bom_types_group({**asdict(extra), 'item': extra.description})) return next(entry['id'] for entry in bom if bom_types_group(entry) == target) -def bom_list(bom: List[dict]) -> List[List[str]]: +def bom_list(bom: List[BOMEntry]) -> List[List[str]]: """Return list of BOM rows as lists of column strings with headings in top row.""" keys = ['id', 'item', 'qty', 'unit', 'designators'] # these BOM columns will always be included for fieldname in ['pn', 'manufacturer', 'mpn']: # these optional BOM columns will only be included if at least one BOM item actually uses them From d6d0d2a486a7f70121d680a30404d64694b3a19c Mon Sep 17 00:00:00 2001 From: KV Date: Sun, 3 Jan 2021 05:29:30 +0100 Subject: [PATCH 17/38] Rename extra variable to part for consistency --- src/wireviz/wv_bom.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/wireviz/wv_bom.py b/src/wireviz/wv_bom.py index 985084fe..17f090a2 100644 --- a/src/wireviz/wv_bom.py +++ b/src/wireviz/wv_bom.py @@ -16,13 +16,13 @@ def get_additional_component_table(harness: "Harness", component: Union[Connecto 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) + for part in component.additional_components: + qty = part.qty * component.get_qty_multiplier(part.qty_multiplier) if harness.mini_bom_mode: - id = get_bom_index(harness.bom(), extra) - rows.append(component_table_entry(f'#{id} ({extra.type.rstrip()})', qty, extra.unit)) + id = get_bom_index(harness.bom(), part) + rows.append(component_table_entry(f'#{id} ({part.type.rstrip()})', qty, part.unit)) else: - rows.append(component_table_entry(extra.description, qty, extra.unit, extra.pn, extra.manufacturer, extra.mpn)) + rows.append(component_table_entry(part.description, qty, part.unit, part.pn, part.manufacturer, part.mpn)) return rows def get_additional_component_bom(component: Union[Connector, Cable]) -> List[BOMEntry]: @@ -116,10 +116,10 @@ def generate_bom(harness: "Harness") -> List[BOMEntry]: # add an incrementing id to each bom item return [{**entry, 'id': index} for index, entry in enumerate(bom, 1)] -def get_bom_index(bom: List[BOMEntry], extra: AdditionalComponent) -> int: +def get_bom_index(bom: List[BOMEntry], part: AdditionalComponent) -> int: """Return id of BOM entry or raise StopIteration if not found.""" # Remove linebreaks and clean whitespace of values in search - target = tuple(clean_whitespace(v) for v in bom_types_group({**asdict(extra), 'item': extra.description})) + target = tuple(clean_whitespace(v) for v in bom_types_group({**asdict(part), 'item': part.description})) return next(entry['id'] for entry in bom if bom_types_group(entry) == target) def bom_list(bom: List[BOMEntry]) -> List[List[str]]: From d15eeb1f9fefb44d9bd38e2e1f5dbc7cec260f31 Mon Sep 17 00:00:00 2001 From: KV Date: Sun, 3 Jan 2021 05:51:02 +0100 Subject: [PATCH 18/38] Build output string in one big expression Build output string in component_table_entry() as the similar strings in generate_bom(). Repeating a couple of minor if-expressions is small cost to obtain a more compact and readable main expression. --- src/wireviz/wv_bom.py | 23 ++++++++--------------- 1 file changed, 8 insertions(+), 15 deletions(-) diff --git a/src/wireviz/wv_bom.py b/src/wireviz/wv_bom.py index 17f090a2..cb9a76a9 100644 --- a/src/wireviz/wv_bom.py +++ b/src/wireviz/wv_bom.py @@ -145,25 +145,18 @@ def component_table_entry( mpn: Optional[str] = None, ) -> str: """Return a diagram node table row string with an additional component.""" - output = f'{qty}' - if unit: - output += f' {unit}' - output += f' x {type}' - # print an extra line with part and manufacturer information if provided manufacturer_str = manufacturer_info_field(manufacturer, mpn) - if pn or manufacturer_str: - output += '
' - if pn: - output += f'P/N: {pn}' - if manufacturer_str: - output += ', ' - if manufacturer_str: - output += manufacturer_str - output = html_line_breaks(output) + output = (f'{qty}' + + (f' {unit}' if unit else '') + + f' x {type}' + + ('
' if pn or manufacturer_str else '') + + (f'P/N: {pn}' if pn else '') + + (', ' if pn and manufacturer_str else '') + + (manufacturer_str or '')) # format the above output as left aligned text in a single visible cell # indent is set to two to match the indent in the generated html table return f''' - +
{output}{html_line_breaks(output)}
''' def manufacturer_info_field(manufacturer: Optional[str], mpn: Optional[str]) -> Optional[str]: From 2e244981fe047f6b2150dd4bbf4cdfa5b9fee3d1 Mon Sep 17 00:00:00 2001 From: KV Date: Tue, 5 Jan 2021 04:12:05 +0100 Subject: [PATCH 19/38] Move default qty value=1 to BOM deduplication --- src/wireviz/wv_bom.py | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/src/wireviz/wv_bom.py b/src/wireviz/wv_bom.py index cb9a76a9..56cca29a 100644 --- a/src/wireviz/wv_bom.py +++ b/src/wireviz/wv_bom.py @@ -58,7 +58,7 @@ def generate_bom(harness: "Harness") -> List[BOMEntry]: + (f', {connector.pincount} pins' if connector.show_pincount else '') + (f', {connector.color}' if connector.color else '')) bom_entries.append({ - 'item': description, 'qty': 1, 'unit': None, 'designators': connector.name if connector.show_name else None, + 'item': description, 'designators': connector.name if connector.show_name else None, 'manufacturer': connector.manufacturer, 'mpn': connector.mpn, 'pn': connector.pn }) @@ -96,11 +96,8 @@ def generate_bom(harness: "Harness") -> List[BOMEntry]: # add cable/bundles aditional components to bom bom_entries.extend(get_additional_component_bom(cable)) - for item in harness.additional_bom_items: - bom_entries.append({ - 'item': item.get('description', ''), 'qty': item.get('qty', 1), 'unit': item.get('unit'), 'designators': item.get('designators'), - 'manufacturer': item.get('manufacturer'), 'mpn': item.get('mpn'), 'pn': item.get('pn') - }) + # TODO: Simplify this by renaming the 'item' key to 'description' in all BOMEntry dicts. + bom_entries.extend([{k.replace('description', 'item'): v for k, v in entry.items()} for entry in harness.additional_bom_items]) # remove line breaks if present and cleanup any resulting whitespace issues bom_entries = [{k: clean_whitespace(v) for k, v in entry.items()} for entry in bom_entries] @@ -110,7 +107,7 @@ def generate_bom(harness: "Harness") -> List[BOMEntry]: for _, group in groupby(sorted(bom_entries, key=bom_types_group), key=bom_types_group): group_entries = list(group) designators = sum((make_list(entry.get('designators')) for entry in group_entries), []) - total_qty = sum(entry['qty'] for entry in group_entries) + total_qty = sum(entry.get('qty', 1) for entry in group_entries) bom.append({**group_entries[0], 'qty': round(total_qty, 3), 'designators': sorted(set(designators))}) # add an incrementing id to each bom item From 8e7c48d42e1d557efb7e468766650cb8be44f222 Mon Sep 17 00:00:00 2001 From: KV Date: Tue, 5 Jan 2021 04:20:44 +0100 Subject: [PATCH 20/38] Eliminate local variable --- src/wireviz/wv_bom.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/wireviz/wv_bom.py b/src/wireviz/wv_bom.py index 56cca29a..b1acd587 100644 --- a/src/wireviz/wv_bom.py +++ b/src/wireviz/wv_bom.py @@ -29,10 +29,9 @@ def get_additional_component_bom(component: Union[Connector, Cable]) -> List[BOM """Return a list of BOM entries with additional components.""" 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, + 'qty': part.qty * component.get_qty_multiplier(part.qty_multiplier), 'unit': part.unit, 'manufacturer': part.manufacturer, 'mpn': part.mpn, From 30dbd9573b35d2951b1c1e3de9a9d655b804d62c Mon Sep 17 00:00:00 2001 From: KV Date: Wed, 6 Jan 2021 22:53:33 +0100 Subject: [PATCH 21/38] Rename the 'item' key to 'description' in all BOMEntry dicts This way, both BOM and harness.additional_bom_items uses the same set of keys in their dict entries. This was originally suggested in a #115 review, but had too many issues to be implemented then. --- src/wireviz/wv_bom.py | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/src/wireviz/wv_bom.py b/src/wireviz/wv_bom.py index b1acd587..0be35592 100644 --- a/src/wireviz/wv_bom.py +++ b/src/wireviz/wv_bom.py @@ -30,7 +30,7 @@ def get_additional_component_bom(component: Union[Connector, Cable]) -> List[BOM bom_entries = [] for part in component.additional_components: bom_entries.append({ - 'item': part.description, + 'description': part.description, 'qty': part.qty * component.get_qty_multiplier(part.qty_multiplier), 'unit': part.unit, 'manufacturer': part.manufacturer, @@ -42,7 +42,7 @@ def get_additional_component_bom(component: Union[Connector, Cable]) -> List[BOM def bom_types_group(entry: BOMEntry) -> Tuple[str, ...]: """Return a tuple of string values from the dict that must be equal to join BOM entries.""" - return tuple(make_str(entry.get(key)) for key in ('item', 'unit', 'manufacturer', 'mpn', 'pn')) + return tuple(make_str(entry.get(key)) for key in ('description', 'unit', 'manufacturer', 'mpn', 'pn')) def generate_bom(harness: "Harness") -> List[BOMEntry]: """Return a list of BOM entries generated from the harness.""" @@ -57,7 +57,7 @@ def generate_bom(harness: "Harness") -> List[BOMEntry]: + (f', {connector.pincount} pins' if connector.show_pincount else '') + (f', {connector.color}' if connector.color else '')) bom_entries.append({ - 'item': description, 'designators': connector.name if connector.show_name else None, + 'description': description, 'designators': connector.name if connector.show_name else None, 'manufacturer': connector.manufacturer, 'mpn': connector.mpn, 'pn': connector.pn }) @@ -65,7 +65,7 @@ def generate_bom(harness: "Harness") -> List[BOMEntry]: bom_entries.extend(get_additional_component_bom(connector)) # 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 description? for cable in harness.cables.values(): if not cable.ignore_in_bom: if cable.category != 'bundle': @@ -76,7 +76,7 @@ def generate_bom(harness: "Harness") -> List[BOMEntry]: + (f' x {cable.gauge} {cable.gauge_unit}' if cable.gauge else ' wires') + (' shielded' if cable.shield else '')) bom_entries.append({ - 'item': description, 'qty': cable.length, 'unit': cable.length_unit, 'designators': cable.name if cable.show_name else None, + 'description': description, 'qty': cable.length, 'unit': cable.length_unit, 'designators': cable.name if cable.show_name else None, 'manufacturer': cable.manufacturer, 'mpn': cable.mpn, 'pn': cable.pn }) else: @@ -87,7 +87,7 @@ def generate_bom(harness: "Harness") -> List[BOMEntry]: + (f', {cable.gauge} {cable.gauge_unit}' if cable.gauge else '') + (f', {color}' if color else '')) bom_entries.append({ - 'item': description, 'qty': cable.length, 'unit': cable.length_unit, 'designators': cable.name if cable.show_name else None, + 'description': description, 'qty': cable.length, 'unit': cable.length_unit, 'designators': cable.name if cable.show_name else None, 'manufacturer': index_if_list(cable.manufacturer, index), 'mpn': index_if_list(cable.mpn, index), 'pn': index_if_list(cable.pn, index) }) @@ -95,8 +95,8 @@ def generate_bom(harness: "Harness") -> List[BOMEntry]: # add cable/bundles aditional components to bom bom_entries.extend(get_additional_component_bom(cable)) - # TODO: Simplify this by renaming the 'item' key to 'description' in all BOMEntry dicts. - bom_entries.extend([{k.replace('description', 'item'): v for k, v in entry.items()} for entry in harness.additional_bom_items]) + # add harness aditional components to bom directly, as they both are List[BOMEntry] + bom_entries.extend(harness.additional_bom_items) # remove line breaks if present and cleanup any resulting whitespace issues bom_entries = [{k: clean_whitespace(v) for k, v in entry.items()} for entry in bom_entries] @@ -109,23 +109,24 @@ def generate_bom(harness: "Harness") -> List[BOMEntry]: total_qty = sum(entry.get('qty', 1) for entry in group_entries) bom.append({**group_entries[0], 'qty': round(total_qty, 3), 'designators': sorted(set(designators))}) - # add an incrementing id to each bom item + # add an incrementing id to each bom entry return [{**entry, 'id': index} for index, entry in enumerate(bom, 1)] def get_bom_index(bom: List[BOMEntry], part: AdditionalComponent) -> int: """Return id of BOM entry or raise StopIteration if not found.""" # Remove linebreaks and clean whitespace of values in search - target = tuple(clean_whitespace(v) for v in bom_types_group({**asdict(part), 'item': part.description})) + target = tuple(clean_whitespace(v) for v in bom_types_group({**asdict(part), 'description': part.description})) return next(entry['id'] for entry in bom if bom_types_group(entry) == target) def bom_list(bom: List[BOMEntry]) -> List[List[str]]: """Return list of BOM rows as lists of column strings with headings in top row.""" - keys = ['id', 'item', 'qty', 'unit', 'designators'] # these BOM columns will always be included - for fieldname in ['pn', 'manufacturer', 'mpn']: # these optional BOM columns will only be included if at least one BOM item actually uses them + keys = ['id', 'description', 'qty', 'unit', 'designators'] # these BOM columns will always be included + for fieldname in ['pn', 'manufacturer', 'mpn']: # these optional BOM columns will only be included if at least one BOM entry actually uses them if any(entry.get(fieldname) for entry in bom): keys.append(fieldname) # list of staic bom header names, headers not specified here are generated by capitilising the internal name bom_headings = { + "description": "Item", # TODO: Remove this line to use 'Description' in BOM heading. "pn": "P/N", "mpn": "MPN" } From 183a9432c3048189230d072da86a92ec0ccc66b6 Mon Sep 17 00:00:00 2001 From: KV Date: Wed, 6 Jan 2021 23:58:30 +0100 Subject: [PATCH 22/38] Move repeated code into new optional_fields() function --- src/wireviz/wv_bom.py | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/src/wireviz/wv_bom.py b/src/wireviz/wv_bom.py index 0be35592..5ba6e094 100644 --- a/src/wireviz/wv_bom.py +++ b/src/wireviz/wv_bom.py @@ -9,7 +9,12 @@ from wireviz.wv_gv_html import html_line_breaks from wireviz.wv_helper import clean_whitespace -BOMEntry = Dict[str, Union[str, int, float, List[str], None]] +BOMColumn = str # = Literal['id', 'description', 'qty', 'unit', 'designators', 'pn', 'manufacturer', 'mpn'] +BOMEntry = Dict[BOMColumn, Union[str, int, float, List[str], None]] + +def optional_fields(part: Union[Connector, Cable, AdditionalComponent]) -> BOMEntry: + """Return part field values for the optional BOM columns as a dict.""" + return {'pn': part.pn, 'manufacturer': part.manufacturer, 'mpn': part.mpn} def get_additional_component_table(harness: "Harness", component: Union[Connector, Cable]) -> List[str]: """Return a list of diagram node table row strings with additional components.""" @@ -22,7 +27,7 @@ def get_additional_component_table(harness: "Harness", component: Union[Connecto id = get_bom_index(harness.bom(), part) rows.append(component_table_entry(f'#{id} ({part.type.rstrip()})', qty, part.unit)) else: - rows.append(component_table_entry(part.description, qty, part.unit, part.pn, part.manufacturer, part.mpn)) + rows.append(component_table_entry(part.description, qty, part.unit, **optional_fields(part))) return rows def get_additional_component_bom(component: Union[Connector, Cable]) -> List[BOMEntry]: @@ -33,10 +38,8 @@ def get_additional_component_bom(component: Union[Connector, Cable]) -> List[BOM 'description': part.description, 'qty': part.qty * component.get_qty_multiplier(part.qty_multiplier), 'unit': part.unit, - 'manufacturer': part.manufacturer, - 'mpn': part.mpn, - 'pn': part.pn, - 'designators': component.name if component.show_name else None + 'designators': component.name if component.show_name else None, + **optional_fields(part), }) return bom_entries @@ -58,7 +61,7 @@ def generate_bom(harness: "Harness") -> List[BOMEntry]: + (f', {connector.color}' if connector.color else '')) bom_entries.append({ 'description': description, 'designators': connector.name if connector.show_name else None, - 'manufacturer': connector.manufacturer, 'mpn': connector.mpn, 'pn': connector.pn + **optional_fields(connector), }) # add connectors aditional components to bom @@ -77,7 +80,7 @@ def generate_bom(harness: "Harness") -> List[BOMEntry]: + (' shielded' if cable.shield else '')) bom_entries.append({ 'description': description, 'qty': cable.length, 'unit': cable.length_unit, 'designators': cable.name if cable.show_name else None, - 'manufacturer': cable.manufacturer, 'mpn': cable.mpn, 'pn': cable.pn + **optional_fields(cable), }) else: # add each wire from the bundle to the bom @@ -88,8 +91,7 @@ def generate_bom(harness: "Harness") -> List[BOMEntry]: + (f', {color}' if color else '')) bom_entries.append({ 'description': description, 'qty': cable.length, 'unit': cable.length_unit, 'designators': cable.name if cable.show_name else None, - 'manufacturer': index_if_list(cable.manufacturer, index), - 'mpn': index_if_list(cable.mpn, index), 'pn': index_if_list(cable.pn, index) + **{k: index_if_list(v, index) for k, v in optional_fields(cable).items()}, }) # add cable/bundles aditional components to bom From 523d0c659ee58412d8cf11416978eb80e424ec4c Mon Sep 17 00:00:00 2001 From: KV Date: Sat, 9 Jan 2021 18:13:47 +0100 Subject: [PATCH 23/38] Group common function arguments into a dict --- src/wireviz/wv_bom.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/wireviz/wv_bom.py b/src/wireviz/wv_bom.py index 5ba6e094..f71abc11 100644 --- a/src/wireviz/wv_bom.py +++ b/src/wireviz/wv_bom.py @@ -22,12 +22,15 @@ def get_additional_component_table(harness: "Harness", component: Union[Connecto if component.additional_components: rows.append(["Additional components"]) for part in component.additional_components: - qty = part.qty * component.get_qty_multiplier(part.qty_multiplier) + common_args = { + 'qty': part.qty * component.get_qty_multiplier(part.qty_multiplier), + 'unit': part.unit, + } if harness.mini_bom_mode: id = get_bom_index(harness.bom(), part) - rows.append(component_table_entry(f'#{id} ({part.type.rstrip()})', qty, part.unit)) + rows.append(component_table_entry(f'#{id} ({part.type.rstrip()})', **common_args)) else: - rows.append(component_table_entry(part.description, qty, part.unit, **optional_fields(part))) + rows.append(component_table_entry(part.description, **common_args, **optional_fields(part))) return rows def get_additional_component_bom(component: Union[Connector, Cable]) -> List[BOMEntry]: From 8c26c8fbbd6df555f617fb2620384fcb3372c803 Mon Sep 17 00:00:00 2001 From: Daniel Rojas Date: Tue, 23 Mar 2021 10:49:56 +0100 Subject: [PATCH 24/38] New addit. compo. BOM table proof of concept --- src/wireviz/Harness.py | 14 ++++++++------ src/wireviz/wv_bom.py | 40 +++++++++++++++++++++++++++++++--------- 2 files changed, 39 insertions(+), 15 deletions(-) diff --git a/src/wireviz/Harness.py b/src/wireviz/Harness.py index 9d83a3df..4df9c7a0 100644 --- a/src/wireviz/Harness.py +++ b/src/wireviz/Harness.py @@ -24,7 +24,8 @@ class Harness: def __init__(self): self.color_mode = 'SHORT' - self.mini_bom_mode = True + self.show_part_numbers = True # TODO: Make configurable via YAML + self.show_bom_item_numbers = True # TODO: Make configurable via YAML self.connectors = {} self.cables = {} self._bom = [] # Internal Cache for generated bom @@ -114,16 +115,17 @@ def create_graph(self) -> Graph: html = [] rows = [[remove_links(connector.name) if connector.show_name else None], - [f'P/N: {remove_links(connector.pn)}' if connector.pn else None, - html_line_breaks(manufacturer_info_field(connector.manufacturer, connector.mpn))], - [html_line_breaks(connector.type), + ['<BOM Number>' if self.show_bom_item_numbers else None, # TODO: Show actual BOM number + html_line_breaks(connector.type), html_line_breaks(connector.subtype), f'{connector.pincount}-pin' if connector.show_pincount else None, connector.color, html_colorbar(connector.color)], + [f'P/N: {remove_links(connector.pn)}' if connector.pn else None, + html_line_breaks(manufacturer_info_field(connector.manufacturer, connector.mpn))] if self.show_part_numbers else None, '' if connector.style != 'simple' else None, [html_image(connector.image)], [html_caption(connector.image)]] - rows.extend(get_additional_component_table(self, connector)) + rows.append(get_additional_component_table(self, connector)) rows.append([html_line_breaks(connector.notes)]) html.extend(nested_html_table(rows)) @@ -209,7 +211,7 @@ def create_graph(self) -> Graph: [html_image(cable.image)], [html_caption(cable.image)]] - rows.extend(get_additional_component_table(self, cable)) + rows.append(get_additional_component_table(self, cable)) # TODO: reimplement rows.append([html_line_breaks(cable.notes)]) html.extend(nested_html_table(rows)) diff --git a/src/wireviz/wv_bom.py b/src/wireviz/wv_bom.py index f71abc11..85ff0f83 100644 --- a/src/wireviz/wv_bom.py +++ b/src/wireviz/wv_bom.py @@ -20,18 +20,40 @@ def get_additional_component_table(harness: "Harness", component: Union[Connecto """Return a list of diagram node table row strings with additional components.""" rows = [] if component.additional_components: - rows.append(["Additional components"]) + # rows.append(["Additional components"]) for part in component.additional_components: common_args = { 'qty': part.qty * component.get_qty_multiplier(part.qty_multiplier), 'unit': part.unit, } - if harness.mini_bom_mode: - id = get_bom_index(harness.bom(), part) - rows.append(component_table_entry(f'#{id} ({part.type.rstrip()})', **common_args)) - else: - rows.append(component_table_entry(part.description, **common_args, **optional_fields(part))) - return rows + # if True: + # id = get_bom_index(harness.bom(), part) + # rows.append(component_table_entry(f'#{id} ({part.type.rstrip()})', **common_args)) + # else: + # rows.append(component_table_entry(part.description, **common_args, **optional_fields(part))) + id = get_bom_index(harness.bom(), part) + manufacturer_str = manufacturer_info_field(part.manufacturer, part.mpn) + columns = [] + if harness.show_bom_item_numbers: + columns.append(f'
{id}
') + columns.append(f'{part.qty}' + (f' {part.unit}' if part.unit else ' x')) + columns.append(f'{part.type}') + if harness.show_part_numbers: + columns.append(f'P/N: {part.pn}' if part.pn else '') + columns.append(f'{manufacturer_str}' if manufacturer_str else '') + # TODO: Add note column as proposed in #222 + + rowstr = '' + ''.join([f'{col}' for col in columns]) + '' + rows.append(rowstr) + + print(rows) + pre = '' + post = '
' + if len(rows) > 0: + tbl = pre + ''.join(rows) + post + else: + return None + return tbl def get_additional_component_bom(component: Union[Connector, Cable]) -> List[BOMEntry]: """Return a list of BOM entries with additional components.""" @@ -157,9 +179,9 @@ def component_table_entry( + (manufacturer_str or '')) # format the above output as left aligned text in a single visible cell # indent is set to two to match the indent in the generated html table - return f''' + return f''' -
{html_line_breaks(output)}
''' + ''' def manufacturer_info_field(manufacturer: Optional[str], mpn: Optional[str]) -> Optional[str]: """Return the manufacturer and/or the mpn in one single string or None otherwise.""" From e61d14ba43a3a0a64cf99278167e7bac35b1b8e3 Mon Sep 17 00:00:00 2001 From: Daniel Rojas Date: Tue, 23 Mar 2021 11:00:26 +0100 Subject: [PATCH 25/38] Create `bom_bubble()` function --- src/wireviz/Harness.py | 4 ++-- src/wireviz/wv_bom.py | 4 ++-- src/wireviz/wv_gv_html.py | 3 +++ 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/wireviz/Harness.py b/src/wireviz/Harness.py index 4df9c7a0..117e0d9e 100644 --- a/src/wireviz/Harness.py +++ b/src/wireviz/Harness.py @@ -12,7 +12,7 @@ from wireviz.DataClasses import Connector, Cable from wireviz.wv_colors import get_color_hex from wireviz.wv_gv_html import nested_html_table, html_colorbar, html_image, \ - html_caption, remove_links, html_line_breaks + html_caption, remove_links, html_line_breaks, bom_bubble from wireviz.wv_bom import manufacturer_info_field, component_table_entry, \ get_additional_component_table, bom_list, generate_bom from wireviz.wv_html import generate_html_output @@ -115,7 +115,7 @@ def create_graph(self) -> Graph: html = [] rows = [[remove_links(connector.name) if connector.show_name else None], - ['<BOM Number>' if self.show_bom_item_numbers else None, # TODO: Show actual BOM number + [bom_bubble('###') if self.show_bom_item_numbers else None, # TODO: Show actual BOM number html_line_breaks(connector.type), html_line_breaks(connector.subtype), f'{connector.pincount}-pin' if connector.show_pincount else None, diff --git a/src/wireviz/wv_bom.py b/src/wireviz/wv_bom.py index 85ff0f83..461a82e3 100644 --- a/src/wireviz/wv_bom.py +++ b/src/wireviz/wv_bom.py @@ -6,7 +6,7 @@ from typing import Any, Dict, List, Optional, Tuple, Union from wireviz.DataClasses import AdditionalComponent, Connector, Cable -from wireviz.wv_gv_html import html_line_breaks +from wireviz.wv_gv_html import html_line_breaks, bom_bubble from wireviz.wv_helper import clean_whitespace BOMColumn = str # = Literal['id', 'description', 'qty', 'unit', 'designators', 'pn', 'manufacturer', 'mpn'] @@ -35,7 +35,7 @@ def get_additional_component_table(harness: "Harness", component: Union[Connecto manufacturer_str = manufacturer_info_field(part.manufacturer, part.mpn) columns = [] if harness.show_bom_item_numbers: - columns.append(f'
{id}
') + columns.append(bom_bubble(id)) columns.append(f'{part.qty}' + (f' {part.unit}' if part.unit else ' x')) columns.append(f'{part.type}') if harness.show_part_numbers: diff --git a/src/wireviz/wv_gv_html.py b/src/wireviz/wv_gv_html.py index ee5b2e28..87b24962 100644 --- a/src/wireviz/wv_gv_html.py +++ b/src/wireviz/wv_gv_html.py @@ -64,3 +64,6 @@ def html_size_attr(image): def html_line_breaks(inp): return remove_links(inp).replace('\n', '
') if isinstance(inp, str) else inp + +def bom_bubble(inp): + return(f'
{inp}
') From 53fefa8f3cc43404fcc35b507c73e06539818696 Mon Sep 17 00:00:00 2001 From: Daniel Rojas Date: Tue, 23 Mar 2021 11:04:33 +0100 Subject: [PATCH 26/38] Add `note` attribute to additional components --- src/wireviz/DataClasses.py | 1 + src/wireviz/wv_bom.py | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/wireviz/DataClasses.py b/src/wireviz/DataClasses.py index fac6ab58..c56f9848 100644 --- a/src/wireviz/DataClasses.py +++ b/src/wireviz/DataClasses.py @@ -76,6 +76,7 @@ class AdditionalComponent: qty: float = 1 unit: Optional[str] = None qty_multiplier: Union[ConnectorMultiplier, CableMultiplier, None] = None + note: Optional[str] = None @property def description(self) -> str: diff --git a/src/wireviz/wv_bom.py b/src/wireviz/wv_bom.py index 461a82e3..5f61aba3 100644 --- a/src/wireviz/wv_bom.py +++ b/src/wireviz/wv_bom.py @@ -41,7 +41,7 @@ def get_additional_component_table(harness: "Harness", component: Union[Connecto if harness.show_part_numbers: columns.append(f'P/N: {part.pn}' if part.pn else '') columns.append(f'{manufacturer_str}' if manufacturer_str else '') - # TODO: Add note column as proposed in #222 + columns.append(f'{part.note}' if part.note else '') rowstr = '' + ''.join([f'{col}' for col in columns]) + '' rows.append(rowstr) From 0dc02fe8b5091baa3c2bc4340f98ca494e853f35 Mon Sep 17 00:00:00 2001 From: Daniel Rojas Date: Tue, 23 Mar 2021 11:14:49 +0100 Subject: [PATCH 27/38] Correctly determine qty --- src/wireviz/wv_bom.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wireviz/wv_bom.py b/src/wireviz/wv_bom.py index 5f61aba3..990e1538 100644 --- a/src/wireviz/wv_bom.py +++ b/src/wireviz/wv_bom.py @@ -36,7 +36,7 @@ def get_additional_component_table(harness: "Harness", component: Union[Connecto columns = [] if harness.show_bom_item_numbers: columns.append(bom_bubble(id)) - columns.append(f'{part.qty}' + (f' {part.unit}' if part.unit else ' x')) + columns.append(f'{part.qty * component.get_qty_multiplier(part.qty_multiplier)}' + (f' {part.unit}' if part.unit else ' x')) columns.append(f'{part.type}') if harness.show_part_numbers: columns.append(f'P/N: {part.pn}' if part.pn else '') From 9a93d8605845304cdf14cf4f7a1f721b46481d8b Mon Sep 17 00:00:00 2001 From: Daniel Rojas Date: Tue, 23 Mar 2021 11:19:20 +0100 Subject: [PATCH 28/38] remove `component_table_entry()` as separate func. to simplyfy code --- src/wireviz/Harness.py | 2 +- src/wireviz/wv_bom.py | 29 +---------------------------- 2 files changed, 2 insertions(+), 29 deletions(-) diff --git a/src/wireviz/Harness.py b/src/wireviz/Harness.py index 117e0d9e..3ad4ee06 100644 --- a/src/wireviz/Harness.py +++ b/src/wireviz/Harness.py @@ -13,7 +13,7 @@ from wireviz.wv_colors import get_color_hex from wireviz.wv_gv_html import nested_html_table, html_colorbar, html_image, \ html_caption, remove_links, html_line_breaks, bom_bubble -from wireviz.wv_bom import manufacturer_info_field, component_table_entry, \ +from wireviz.wv_bom import manufacturer_info_field, \ get_additional_component_table, bom_list, generate_bom from wireviz.wv_html import generate_html_output from wireviz.wv_helper import awg_equiv, mm2_equiv, tuplelist2tsv, flatten2d, \ diff --git a/src/wireviz/wv_bom.py b/src/wireviz/wv_bom.py index 990e1538..eca24ca5 100644 --- a/src/wireviz/wv_bom.py +++ b/src/wireviz/wv_bom.py @@ -22,10 +22,6 @@ def get_additional_component_table(harness: "Harness", component: Union[Connecto if component.additional_components: # rows.append(["Additional components"]) for part in component.additional_components: - common_args = { - 'qty': part.qty * component.get_qty_multiplier(part.qty_multiplier), - 'unit': part.unit, - } # if True: # id = get_bom_index(harness.bom(), part) # rows.append(component_table_entry(f'#{id} ({part.type.rstrip()})', **common_args)) @@ -43,7 +39,7 @@ def get_additional_component_table(harness: "Harness", component: Union[Connecto columns.append(f'{manufacturer_str}' if manufacturer_str else '') columns.append(f'{part.note}' if part.note else '') - rowstr = '' + ''.join([f'{col}' for col in columns]) + '' + rowstr = '' + ''.join([f'{html_line_breaks(col)}' for col in columns]) + '' rows.append(rowstr) print(rows) @@ -160,29 +156,6 @@ def bom_list(bom: List[BOMEntry]) -> List[List[str]]: return ([[bom_headings.get(k, k.capitalize()) for k in keys]] + # Create header row with key names [[make_str(entry.get(k)) for k in keys] for entry in bom]) # Create string list for each entry row -def component_table_entry( - type: str, - qty: Union[int, float], - unit: Optional[str] = None, - pn: Optional[str] = None, - manufacturer: Optional[str] = None, - mpn: Optional[str] = None, - ) -> str: - """Return a diagram node table row string with an additional component.""" - manufacturer_str = manufacturer_info_field(manufacturer, mpn) - output = (f'{qty}' - + (f' {unit}' if unit else '') - + f' x {type}' - + ('
' if pn or manufacturer_str else '') - + (f'P/N: {pn}' if pn else '') - + (', ' if pn and manufacturer_str else '') - + (manufacturer_str or '')) - # format the above output as left aligned text in a single visible cell - # indent is set to two to match the indent in the generated html table - return f''' - {html_line_breaks(output)} - ''' - def manufacturer_info_field(manufacturer: Optional[str], mpn: Optional[str]) -> Optional[str]: """Return the manufacturer and/or the mpn in one single string or None otherwise.""" if manufacturer or mpn: From 4338b7a1c5b5286e66afd51009cbbc4fd92ab923 Mon Sep 17 00:00:00 2001 From: Daniel Rojas Date: Tue, 23 Mar 2021 11:22:09 +0100 Subject: [PATCH 29/38] Remove header row for additional components --- src/wireviz/wv_bom.py | 1 - 1 file changed, 1 deletion(-) diff --git a/src/wireviz/wv_bom.py b/src/wireviz/wv_bom.py index eca24ca5..93f66760 100644 --- a/src/wireviz/wv_bom.py +++ b/src/wireviz/wv_bom.py @@ -20,7 +20,6 @@ def get_additional_component_table(harness: "Harness", component: Union[Connecto """Return a list of diagram node table row strings with additional components.""" rows = [] if component.additional_components: - # rows.append(["Additional components"]) for part in component.additional_components: # if True: # id = get_bom_index(harness.bom(), part) From 7e168ea77568b7b586e9b0435dd55a1ab68cdfcf Mon Sep 17 00:00:00 2001 From: Daniel Rojas Date: Tue, 23 Mar 2021 11:26:13 +0100 Subject: [PATCH 30/38] Improve GraphViz output, remove debug print --- src/wireviz/wv_bom.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/wireviz/wv_bom.py b/src/wireviz/wv_bom.py index 93f66760..3a2d55c8 100644 --- a/src/wireviz/wv_bom.py +++ b/src/wireviz/wv_bom.py @@ -38,12 +38,11 @@ def get_additional_component_table(harness: "Harness", component: Union[Connecto columns.append(f'{manufacturer_str}' if manufacturer_str else '') columns.append(f'{part.note}' if part.note else '') - rowstr = '' + ''.join([f'{html_line_breaks(col)}' for col in columns]) + '' + rowstr = '\n \n' + ''.join([f' {html_line_breaks(col)}\n' for col in columns]) + ' ' rows.append(rowstr) - print(rows) pre = '' - post = '
' + post = '\n ' if len(rows) > 0: tbl = pre + ''.join(rows) + post else: From f928bc73ca499f3129be274dd9202770ce91e83b Mon Sep 17 00:00:00 2001 From: Daniel Rojas Date: Tue, 23 Mar 2021 11:50:02 +0100 Subject: [PATCH 31/38] Add BOM bubble cell to cables --- src/wireviz/Harness.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/wireviz/Harness.py b/src/wireviz/Harness.py index 3ad4ee06..c575f954 100644 --- a/src/wireviz/Harness.py +++ b/src/wireviz/Harness.py @@ -197,16 +197,17 @@ def create_graph(self) -> Graph: awg_fmt = f' ({mm2_equiv(cable.gauge)} mm\u00B2)' rows = [[remove_links(cable.name) if cable.show_name else None], - [f'P/N: {remove_links(cable.pn)}' if (cable.pn and not isinstance(cable.pn, list)) else None, - html_line_breaks(manufacturer_info_field( - cable.manufacturer if not isinstance(cable.manufacturer, list) else None, - cable.mpn if not isinstance(cable.mpn, list) else None))], - [html_line_breaks(cable.type), + [bom_bubble('###') if self.show_bom_item_numbers else None, # TODO: Show actual BOM number + html_line_breaks(cable.type), f'{cable.wirecount}x' if cable.show_wirecount else None, f'{cable.gauge} {cable.gauge_unit}{awg_fmt}' if cable.gauge else None, '+ S' if cable.shield else None, f'{cable.length} {cable.length_unit}' if cable.length > 0 else None, cable.color, html_colorbar(cable.color)], + [f'P/N: {remove_links(cable.pn)}' if (cable.pn and not isinstance(cable.pn, list)) else None, + html_line_breaks(manufacturer_info_field( + cable.manufacturer if not isinstance(cable.manufacturer, list) else None, + cable.mpn if not isinstance(cable.mpn, list) else None))], '', [html_image(cable.image)], [html_caption(cable.image)]] From aaf88d7601048bdb4ffd40d55e60edf7baa75d1f Mon Sep 17 00:00:00 2001 From: Daniel Rojas Date: Tue, 23 Mar 2021 12:15:03 +0100 Subject: [PATCH 32/38] Remove vertical separators in BOM table --- src/wireviz/wv_bom.py | 10 ++++++++-- test/bomnumbertest.bom.tsv | 9 +++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) create mode 100644 test/bomnumbertest.bom.tsv diff --git a/src/wireviz/wv_bom.py b/src/wireviz/wv_bom.py index 3a2d55c8..2e5cbb2b 100644 --- a/src/wireviz/wv_bom.py +++ b/src/wireviz/wv_bom.py @@ -31,14 +31,20 @@ def get_additional_component_table(harness: "Harness", component: Union[Connecto columns = [] if harness.show_bom_item_numbers: columns.append(bom_bubble(id)) - columns.append(f'{part.qty * component.get_qty_multiplier(part.qty_multiplier)}' + (f' {part.unit}' if part.unit else ' x')) + columns.append(f'{part.qty * component.get_qty_multiplier(part.qty_multiplier)}' + (f' {part.unit}' if part.unit else 'x')) columns.append(f'{part.type}') if harness.show_part_numbers: columns.append(f'P/N: {part.pn}' if part.pn else '') columns.append(f'{manufacturer_str}' if manufacturer_str else '') columns.append(f'{part.note}' if part.note else '') - rowstr = '\n \n' + ''.join([f' {html_line_breaks(col)}\n' for col in columns]) + ' ' + # TODO: Remove empty columns + + rowstr = '\n \n' + for index, column in enumerate(columns): + sides = "tbl" if index == 0 else "tbr" if index == len(columns) -1 else "tb" + rowstr = rowstr + f' {html_line_breaks(column)}\n' + rowstr = rowstr + ' ' rows.append(rowstr) pre = '' diff --git a/test/bomnumbertest.bom.tsv b/test/bomnumbertest.bom.tsv new file mode 100644 index 00000000..fb6b2da2 --- /dev/null +++ b/test/bomnumbertest.bom.tsv @@ -0,0 +1,9 @@ +Id Item Qty Unit Designators P/N Manufacturer MPN +1 Cable, 4 x 0.25 mm² 99 m W1 qwerty uiop +2 Connector, Plug, 4 pins 1 X1 123 ACME ABC +3 Connector, Receptacle, 4 pins 1 X2 234 ACME DEF +4 Crimp 1 X2 876 ACME WVU +5 Crimp 4 X1 987 ACME ZYX +6 Housing 1 X1 345 OTHER +7 Label 1 X1 +8 Sleeving 5 m W1 From 6aa53f6ce450eae22883f3b698b1ebe788f82d9a Mon Sep 17 00:00:00 2001 From: Daniel Rojas Date: Tue, 23 Mar 2021 12:40:56 +0100 Subject: [PATCH 33/38] Remove unused columns in BOM table --- src/wireviz/wv_bom.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/wireviz/wv_bom.py b/src/wireviz/wv_bom.py index 2e5cbb2b..6e48b1a1 100644 --- a/src/wireviz/wv_bom.py +++ b/src/wireviz/wv_bom.py @@ -20,6 +20,7 @@ def get_additional_component_table(harness: "Harness", component: Union[Connecto """Return a list of diagram node table row strings with additional components.""" rows = [] if component.additional_components: + parts = [] for part in component.additional_components: # if True: # id = get_bom_index(harness.bom(), part) @@ -38,11 +39,18 @@ def get_additional_component_table(harness: "Harness", component: Union[Connecto columns.append(f'{manufacturer_str}' if manufacturer_str else '') columns.append(f'{part.note}' if part.note else '') - # TODO: Remove empty columns + parts.append(columns) + # remove unused columns + transp = list(map(list, zip(*parts))) # transpose list + transp = [item for item in transp if any(item)] # remove empty rows (easier) + parts = list(map(list, zip(*transp))) # transpose back + + # generate HTML output + for part in parts: rowstr = '\n \n' - for index, column in enumerate(columns): - sides = "tbl" if index == 0 else "tbr" if index == len(columns) -1 else "tb" + for index, column in enumerate(part): + sides = "tbl" if index == 0 else "tbr" if index == len(part) -1 else "tb" rowstr = rowstr + f' \n' rowstr = rowstr + ' ' rows.append(rowstr) @@ -52,7 +60,7 @@ def get_additional_component_table(harness: "Harness", component: Union[Connecto if len(rows) > 0: tbl = pre + ''.join(rows) + post else: - return None + tbl = '' return tbl def get_additional_component_bom(component: Union[Connector, Cable]) -> List[BOMEntry]: From 09d5496ca4392812797689a9809180b37276d17a Mon Sep 17 00:00:00 2001 From: Daniel Rojas Date: Tue, 23 Mar 2021 13:04:31 +0100 Subject: [PATCH 34/38] Add new `bom_item_number` property to components --- src/wireviz/DataClasses.py | 2 ++ src/wireviz/Harness.py | 4 ++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/wireviz/DataClasses.py b/src/wireviz/DataClasses.py index c56f9848..87dfe6cd 100644 --- a/src/wireviz/DataClasses.py +++ b/src/wireviz/DataClasses.py @@ -105,6 +105,7 @@ class Connector: hide_disconnected_pins: bool = False autogenerate: bool = False loops: List[List[Pin]] = field(default_factory=list) + bom_item_number: Optional[int] = None ignore_in_bom: bool = False additional_components: List[AdditionalComponent] = field(default_factory=list) @@ -189,6 +190,7 @@ class Cable: show_name: bool = True show_wirecount: bool = True show_wirenumbers: Optional[bool] = None + bom_item_number: Optional[int] = None ignore_in_bom: bool = False additional_components: List[AdditionalComponent] = field(default_factory=list) diff --git a/src/wireviz/Harness.py b/src/wireviz/Harness.py index c575f954..14f96b06 100644 --- a/src/wireviz/Harness.py +++ b/src/wireviz/Harness.py @@ -115,7 +115,7 @@ def create_graph(self) -> Graph: html = [] rows = [[remove_links(connector.name) if connector.show_name else None], - [bom_bubble('###') if self.show_bom_item_numbers else None, # TODO: Show actual BOM number + [bom_bubble(connector.bom_item_number) if self.show_bom_item_numbers else None, # TODO: Show actual BOM number html_line_breaks(connector.type), html_line_breaks(connector.subtype), f'{connector.pincount}-pin' if connector.show_pincount else None, @@ -197,7 +197,7 @@ def create_graph(self) -> Graph: awg_fmt = f' ({mm2_equiv(cable.gauge)} mm\u00B2)' rows = [[remove_links(cable.name) if cable.show_name else None], - [bom_bubble('###') if self.show_bom_item_numbers else None, # TODO: Show actual BOM number + [bom_bubble(cable.bom_item_number) if self.show_bom_item_numbers else None, # TODO: Show actual BOM number html_line_breaks(cable.type), f'{cable.wirecount}x' if cable.show_wirecount else None, f'{cable.gauge} {cable.gauge_unit}{awg_fmt}' if cable.gauge else None, From 1f86c97c6725e7f22c8df7d5d1d6363a5b5374d0 Mon Sep 17 00:00:00 2001 From: Daniel Rojas Date: Tue, 23 Mar 2021 15:15:20 +0100 Subject: [PATCH 35/38] Fix rendering bug for empty add.comp. table --- src/wireviz/wv_bom.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wireviz/wv_bom.py b/src/wireviz/wv_bom.py index 6e48b1a1..b0ac6664 100644 --- a/src/wireviz/wv_bom.py +++ b/src/wireviz/wv_bom.py @@ -60,7 +60,7 @@ def get_additional_component_table(harness: "Harness", component: Union[Connecto if len(rows) > 0: tbl = pre + ''.join(rows) + post else: - tbl = '' + tbl = None return tbl def get_additional_component_bom(component: Union[Connector, Cable]) -> List[BOMEntry]: From b4a0ae50b84ab7d23a84247d440382b1944ecdb6 Mon Sep 17 00:00:00 2001 From: Daniel Rojas Date: Tue, 23 Mar 2021 15:57:38 +0100 Subject: [PATCH 36/38] Implement additional parameter table as proposed in #222 --- src/wireviz/DataClasses.py | 4 +++- src/wireviz/Harness.py | 5 ++++- src/wireviz/wv_gv_html.py | 15 ++++++++++++++- 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/src/wireviz/DataClasses.py b/src/wireviz/DataClasses.py index 87dfe6cd..6aa9e662 100644 --- a/src/wireviz/DataClasses.py +++ b/src/wireviz/DataClasses.py @@ -1,7 +1,7 @@ #!/usr/bin/env python3 # -*- coding: utf-8 -*- -from typing import Optional, List, Tuple, Union +from typing import Optional, List, Dict, Tuple, Union from dataclasses import dataclass, field, InitVar from pathlib import Path @@ -94,6 +94,7 @@ class Connector: type: Optional[MultilineHypertext] = None subtype: Optional[MultilineHypertext] = None pincount: Optional[int] = None + additional_parameters: Optional[Dict] = None image: Optional[Image] = None notes: Optional[MultilineHypertext] = None pinlabels: List[Pin] = field(default_factory=list) @@ -182,6 +183,7 @@ class Cable: color: Optional[Color] = None wirecount: Optional[int] = None shield: Union[bool, Color] = False + additional_parameters: Optional[Dict] = None image: Optional[Image] = None notes: Optional[MultilineHypertext] = None colors: List[Colors] = field(default_factory=list) diff --git a/src/wireviz/Harness.py b/src/wireviz/Harness.py index 14f96b06..486dfdd9 100644 --- a/src/wireviz/Harness.py +++ b/src/wireviz/Harness.py @@ -12,7 +12,7 @@ from wireviz.DataClasses import Connector, Cable from wireviz.wv_colors import get_color_hex from wireviz.wv_gv_html import nested_html_table, html_colorbar, html_image, \ - html_caption, remove_links, html_line_breaks, bom_bubble + html_caption, remove_links, html_line_breaks, bom_bubble, nested_html_table_dict from wireviz.wv_bom import manufacturer_info_field, \ get_additional_component_table, bom_list, generate_bom from wireviz.wv_html import generate_html_output @@ -122,11 +122,13 @@ def create_graph(self) -> Graph: connector.color, html_colorbar(connector.color)], [f'P/N: {remove_links(connector.pn)}' if connector.pn else None, html_line_breaks(manufacturer_info_field(connector.manufacturer, connector.mpn))] if self.show_part_numbers else None, + nested_html_table_dict(connector.additional_parameters), '' if connector.style != 'simple' else None, [html_image(connector.image)], [html_caption(connector.image)]] rows.append(get_additional_component_table(self, connector)) rows.append([html_line_breaks(connector.notes)]) + html.extend(nested_html_table(rows)) if connector.style != 'simple': @@ -208,6 +210,7 @@ def create_graph(self) -> Graph: html_line_breaks(manufacturer_info_field( cable.manufacturer if not isinstance(cable.manufacturer, list) else None, cable.mpn if not isinstance(cable.mpn, list) else None))], + nested_html_table_dict(cable.additional_parameters), '', [html_image(cable.image)], [html_caption(cable.image)]] diff --git a/src/wireviz/wv_gv_html.py b/src/wireviz/wv_gv_html.py index 87b24962..a54f0020 100644 --- a/src/wireviz/wv_gv_html.py +++ b/src/wireviz/wv_gv_html.py @@ -1,12 +1,25 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- -from typing import List, Union +from typing import List, Dict, Union import re from wireviz.wv_colors import translate_color from wireviz.wv_helper import remove_links +def nested_html_table_dict(rows): + if isinstance(rows, Dict): + html = [] + html.append('
{html_line_breaks(column)}
') + for (key, value) in rows.items(): + html.append(f' ') + html.append(f' ') + html.append('
{key}{value}
') + out = '\n'.join(html) + else: + out = None + return out + def nested_html_table(rows): # input: list, each item may be scalar or list # output: a parent table with one child table per parent item that is list, and one cell per parent item that is scalar From 37bf5309f033e07b93e978a573378291c5280528 Mon Sep 17 00:00:00 2001 From: Daniel Rojas Date: Tue, 23 Mar 2021 18:02:55 +0100 Subject: [PATCH 37/38] Allow line breaks in additional parameters --- src/wireviz/wv_gv_html.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wireviz/wv_gv_html.py b/src/wireviz/wv_gv_html.py index a54f0020..ea80adaf 100644 --- a/src/wireviz/wv_gv_html.py +++ b/src/wireviz/wv_gv_html.py @@ -13,7 +13,7 @@ def nested_html_table_dict(rows): html.append('') for (key, value) in rows.items(): html.append(f' ') - html.append(f' ') + html.append(f' ') html.append('
{key}{value}
{html_line_breaks(value)}
') out = '\n'.join(html) else: From 9821ca31f77edfc0e0b0793a76ba27cdfd174cf3 Mon Sep 17 00:00:00 2001 From: Daniel Rojas Date: Tue, 23 Mar 2021 18:02:28 +0100 Subject: [PATCH 38/38] Update demo02 to showcase more features zzz --- examples/demo02.yml | 145 ++++++++++++++++++++++++++++++-------------- 1 file changed, 101 insertions(+), 44 deletions(-) diff --git a/examples/demo02.yml b/examples/demo02.yml index 5002e7d2..ca5cadfe 100644 --- a/examples/demo02.yml +++ b/examples/demo02.yml @@ -1,69 +1,126 @@ -templates: # defining templates to be used later on - - &molex_f - type: Molex KK 254 - subtype: female - - &con_i2c - pinlabels: [GND, +5V, SCL, SDA] - - &wire_i2c - category: bundle - gauge: 0.14 mm2 - colors: [BK, RD, YE, GN] - connectors: X1: - <<: *molex_f # copying items from the template - pinlabels: [GND, +5V, SCL, SDA, MISO, MOSI, SCK, N/C] + type: KK 254 + pn: CON-245-8 + manufacturer: Molex + mpn: '0022013087' + subtype: female + pincount: 8 + pinlabels: [Audio L, Audio R, Audio GND, N/C, I2C GND, I2C +5V, SCL, SDA] + additional_parameters: + Sleeving removal: 20 mm + Insulation removal: 2 mm + additional_components: + - + type: Crimp + pn: CRI-254 + manufacturer: Molex + mpn: '008500032' + qty_multiplier: populated + - + type: Label + pn: LAB-444 + note: '"C745-X1"' + notes: | + - Attach to main PCB + - Ensure proper contact + - Clamp down cables after attaching X2: - <<: *molex_f - <<: *con_i2c # it is possible to copy from more than one template + type: 3.5 mm + subtype: jack + color: BK + pins: [T,R,S] + show_pincount: false + pinlabels: [L, R, GND] + image: + src: resources/stereo-phone-plug-TRS.png + caption: Tip, Ring, and Sleeve X3: - <<: *molex_f - <<: *con_i2c + type: KK 254 + subtype: female + pinlabels: [VCC, GND, SCL, SDA] + pincolors: [RD, BK, GN, BU] X4: - <<: *molex_f - pinlabels: [GND, +12V, MISO, MOSI, SCK] - ferrule_crimp: + type: D-Sub + subtype: female + pincount: 9 + pn: CON-D9-F + pinlabels: [GND, +5V, SCL, SDA, N/C, +12V IN, GND, +12V OUT, GND] + additional_components: + - + type: Casing, plastic + pn: CAS-D9 + - + type: Mounting screws, M3 x 8 + qty: 2 + F: style: simple autogenerate: true type: Crimp ferrule - subtype: 0.25 mm² - color: YE + subtype: 0.5 mm² + color: OG cables: W1: - <<: *wire_i2c - length: 0.2 + wirecount: 3 + shield: true + length: 0.5 + gauge: 0.25 mm2 show_equiv: true - W2: - <<: *wire_i2c - length: 0.4 + colors: [WH, RD, BK] + color: GY + additional_components: + - + type: Heatshrink D=5mm + qty: 15 + unit: mm + note: left + - + type: Heatshrink D=5mm + qty: 25 + unit: mm + note: right + W2: &wire_i2c + wirecount: 4 + length: 0.2 + gauge: 0.25 mm2 show_equiv: true + color_code: IEC W3: + <<: *wire_i2c + color_code: DIN + W4: &wire_power category: bundle - gauge: 0.14 mm2 - length: 0.3 - colors: [BK, BU, OG, VT] - show_equiv: true - W4: - gauge: 0.25 mm2 - length: 0.3 - colors: [BK, RD] + show_name: false + wirecount: 2 + colors: [RD, BK] + gauge: 0.5 mm2 show_equiv: true + length: 1.0 + additional_parameters: + Twist rate: 10/m + Twist direction: CCW + W5: + <<: *wire_power connections: - - - X1: [1-4] - - W1: [1-4] - - X2: [1-4] + - X1: [Audio L, Audio R, Audio GND, Audio GND] + - W1: [1-3,s] + - X2: [T,R,S,S] - - - X1: [1-4] + - X1: [I2C GND, I2C +5V, SCL, SDA] - W2: [1-4] - - X3: [1-4] + - X3: [GND, VCC, SCL, SDA] - - - X1: [1,5-7] + - X1: [I2C GND, I2C +5V, SCL, SDA] - W3: [1-4] - - X4: [1,3-5] + - X4: [1,2,3,4] - - - ferrule_crimp + - F - W4: [1,2] - - X4: [1,2] + - X4: [6,7] + - + - X4: [8,9] + - W5: [1,2] + - F