Skip to content

Commit

Permalink
Define tuples of BOM columns as common constants
Browse files Browse the repository at this point in the history
  • Loading branch information
kvid committed Apr 6, 2021
1 parent 30a6b13 commit f1cb7e2
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions src/wireviz/wv_bom.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,18 @@
from wireviz.wv_gv_html import html_line_breaks
from wireviz.wv_helper import clean_whitespace

BOM_COLUMNS_ALWAYS = ('id', 'description', 'qty', 'unit', 'designators')
BOM_COLUMNS_OPTIONAL = ('pn', 'manufacturer', 'mpn')
BOM_COLUMNS_IN_KEY = ('description', 'unit') + BOM_COLUMNS_OPTIONAL

BOMKey = Tuple[str, ...]
BOMColumn = str # = Literal['id', 'description', 'qty', 'unit', 'designators', 'pn', 'manufacturer', 'mpn']
BOMColumn = str # = Literal[*BOM_COLUMNS_ALWAYS, *BOM_COLUMNS_OPTIONAL]
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}
part = asdict(part)
return {field: part.get(field) for field in BOM_COLUMNS_OPTIONAL}

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."""
Expand Down Expand Up @@ -50,7 +55,7 @@ def get_additional_component_bom(component: Union[Connector, Cable]) -> List[BOM
def bom_entry_key(entry: BOMEntry) -> BOMKey:
"""Return a tuple of string values from the dict that must be equal to join BOM entries."""
if 'key' not in entry:
entry['key'] = tuple(clean_whitespace(make_str(entry.get(k))) for k in ('description', 'unit', 'manufacturer', 'mpn', 'pn'))
entry['key'] = tuple(clean_whitespace(make_str(entry.get(c))) for c in BOM_COLUMNS_IN_KEY)
return entry['key']

def generate_bom(harness: "Harness") -> List[BOMEntry]:
Expand Down Expand Up @@ -129,8 +134,8 @@ def get_bom_index(bom: List[BOMEntry], target: BOMKey) -> int:

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', '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
keys = list(BOM_COLUMNS_ALWAYS) # Always include this fixed set of BOM columns.
for fieldname in BOM_COLUMNS_OPTIONAL: # Include only those optional BOM columns that are in use.
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
Expand Down

0 comments on commit f1cb7e2

Please sign in to comment.