aboutsummaryrefslogtreecommitdiff
path: root/src/ee/bom
diff options
context:
space:
mode:
Diffstat (limited to 'src/ee/bom')
-rw-r--r--src/ee/bom/doit.py62
1 files changed, 37 insertions, 25 deletions
diff --git a/src/ee/bom/doit.py b/src/ee/bom/doit.py
index 521b8dd..7c8f63a 100644
--- a/src/ee/bom/doit.py
+++ b/src/ee/bom/doit.py
@@ -84,7 +84,7 @@ def task_bom():
raise Exception("Missing ref")
if not mpn:
- create_message(output, "Missing required field 'mpn'", "error")
+ create_message(output, "Missing required field 'mpn' on component ref={}".format(ref), "error")
continue
if ref in bom_components:
@@ -104,61 +104,73 @@ def task_bom():
_data_sets[task_bom] = ["bom", ["components"]]
-def order_csv(count: int, style: str, output_file: Path, data_sets) -> DataSet:
+def order_csv(count: int, style: str, output_file: Path, out_ds: DataSet, data_sets):
ds = _dsm.load_data_sets(data_sets)
- out = DataSet()
+ csv_ds = DataSet()
parts = {}
# noinspection PyPep8Naming
Part = namedlist("Part", "mpn, cnt, refs, digikey_pn")
+ digikey_parts = [o for o in ds.items() if
+ o.object_type.name == "component-to-part-mapping" and
+ o.get("seller") == "digikey"]
+
for c in [o for o in ds.items() if o.object_type.name == "bom-component"]:
ref = c.get("ref")
mpn = c.get("mpn")
- dk_part = None
+ digikey_pn = None
if style == "digikey":
- dk_parts = [o.get_req("part-number") for o in ds.items() if
- o.object_type.name == "component-to-part-mapping" and
- o.has("seller", "ref", "part-number") and
- o.get("seller") == "digikey" and
- o.get("ref") == ref]
+
+ digikey_pns = []
+ for o in (o for o in digikey_parts if o.get("ref") == ref):
+
+ t = o.values("part-number", strip=True, required=True)
+
+ if not t:
+ create_message(out_ds, "Missing required field value for field part-number, object key={}".
+ format(o.key), level="error")
+
+ part_number = t[0]
+
+ digikey_pns.append(part_number)
# The actual DK part should depend on the price breaks for the different packagings, but we don't have that
# info here. Luckily the DK store proposes cheaper packagings when ordering so that is something.
- dk_parts = set(dk_parts)
+ digikey_pns = set(digikey_pns)
- if len(dk_parts) == 0:
- create_message(out, "No part for component: ref={}, mpn={}".format(ref, mpn), "error")
+ if len(digikey_pns) == 0:
+ create_message(out_ds, "No part for component: ref={}, mpn={}".format(ref, mpn), "error")
continue
- elif len(dk_parts) > 1:
- create_message(out, "Multiple parts for component: ref={}, mpn={}. Don't know which one to select.".
+ elif len(digikey_pns) > 1:
+ create_message(out_ds, "Multiple parts for component: ref={}, mpn={}. Don't know which one to select.".
format(ref, mpn), "error")
continue
else:
- dk_part = next(iter(dk_parts))
+ digikey_pn = next(iter(digikey_pns))
- dk_part = dk_part.strip()
+ digikey_pn = digikey_pn.strip()
- if len(dk_part) == 0:
+ if len(digikey_pn) == 0:
raise Exception("Missing digikey part number for ref={}".format(ref))
if mpn in parts:
part = parts[mpn]
- if dk_part:
- if part.digikey_pn != dk_part:
+ if digikey_pn:
+ if part.digikey_pn != digikey_pn:
raise Exception("Bad data, inconsistent digikey-pn for mpn '{}'. First digikey part number='{}', "
- "new digikey part number='{}'".format(mpn, part.digikey_pn, dk_part))
+ "new digikey part number='{}'".format(mpn, part.digikey_pn, digikey_pn))
part.cnt += 1
part.refs.append(ref)
else:
- parts[mpn] = Part(mpn=mpn, cnt=1, refs=[ref], digikey_pn=dk_part)
+ parts[mpn] = Part(mpn=mpn, cnt=1, refs=[ref], digikey_pn=digikey_pn)
mpn_field = "MPN"
count_field = "Count"
@@ -169,7 +181,7 @@ def order_csv(count: int, style: str, output_file: Path, data_sets) -> DataSet:
refs_field = "Customer Reference"
for part in sorted(parts.values(), key=lambda p: p.mpn):
- o = out.create_object("row", part.mpn). \
+ o = csv_ds.create_object("row", part.mpn). \
set(mpn_field, part.mpn). \
set(count_field, part.cnt * count). \
set(refs_field, ",".join(part.refs))
@@ -184,14 +196,14 @@ def order_csv(count: int, style: str, output_file: Path, data_sets) -> DataSet:
fields = ["Digi-Key Part Number", refs_field, count_field, mpn_field]
include_extra_fields = False
- _dsm.store_csv(output_file, out, "row", order_by=mpn_field, fields=fields,
+ _dsm.store_csv(output_file, csv_ds, "row", order_by=mpn_field, fields=fields,
include_extra_fields=include_extra_fields)
def create_task_order_csv(*, style: str = None, output_file: Union[str, Path], out_data_set, data_sets, count: int = 1):
def action():
- with _dsm.create_rw(out_data_set, clean=True):
- order_csv(count, style, Path(output_file), data_sets)
+ with _dsm.create_rw(out_data_set, clean=True) as out:
+ order_csv(count, style, Path(output_file), out, data_sets)
return {
"name": "order-{}".format(count) if not style else "order-{}-{}".format(style, count),