aboutsummaryrefslogtreecommitdiff
path: root/src/ee/bom/doit.py
diff options
context:
space:
mode:
Diffstat (limited to 'src/ee/bom/doit.py')
-rw-r--r--src/ee/bom/doit.py71
1 files changed, 50 insertions, 21 deletions
diff --git a/src/ee/bom/doit.py b/src/ee/bom/doit.py
index bbb9241..521b8dd 100644
--- a/src/ee/bom/doit.py
+++ b/src/ee/bom/doit.py
@@ -5,7 +5,7 @@ from typing import Mapping, Union
from configclass import Config
from namedlist import namedlist
-from ee.ds import DataSetManager, DataSet
+from ee.ds import DataSetManager, DataSet, create_message
logger = logging.getLogger(__name__)
@@ -19,15 +19,20 @@ def change_data_sets_for_task(task, _callable):
def output_data_set_for_task(task):
- return _data_sets[task][0]
+ try:
+ return _data_sets[task][0]
+ except KeyError:
+ raise KeyError("No such task registered in this module: {}".format(task))
def input_data_sets_for_task(task):
- return _data_sets[task][1]
+ try:
+ return _data_sets[task][1]
+ except KeyError:
+ raise KeyError("No such task registered in this module: {}".format(task))
-_config_template = Config({
-})
+_config_template = Config({})
_config = None # type: Mapping[str, str]
@@ -79,9 +84,7 @@ def task_bom():
raise Exception("Missing ref")
if not mpn:
- output.create_object("message", "bom-{}".format(ref)). \
- set("level", "error"). \
- set("message", "Missing required field 'mpn'")
+ create_message(output, "Missing required field 'mpn'", "error")
continue
if ref in bom_components:
@@ -101,7 +104,7 @@ def task_bom():
_data_sets[task_bom] = ["bom", ["components"]]
-def order_csv(count: int, style: str, output_file: Path, data_sets):
+def order_csv(count: int, style: str, output_file: Path, data_sets) -> DataSet:
ds = _dsm.load_data_sets(data_sets)
out = DataSet()
@@ -114,26 +117,48 @@ def order_csv(count: int, style: str, output_file: Path, data_sets):
for c in [o for o in ds.items() if o.object_type.name == "bom-component"]:
ref = c.get("ref")
mpn = c.get("mpn")
- digikey_pn = c.get("digikey-pn")
+
+ dk_part = None
if style == "digikey":
- dpn = digikey_pn.strip() if digikey_pn else ""
+ 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]
+
+ # 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)
+
+ if len(dk_parts) == 0:
+ create_message(out, "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.".
+ format(ref, mpn), "error")
+ continue
+ else:
+ dk_part = next(iter(dk_parts))
- # TODO: implement part resolution
- # if len(dpn) == 0:
- # raise Exception("Missing digikey-pn for ref={}".format(ref))
+ dk_part = dk_part.strip()
+
+ if len(dk_part) == 0:
+ raise Exception("Missing digikey part number for ref={}".format(ref))
if mpn in parts:
part = parts[mpn]
- if part.digikey_pn != digikey_pn:
- raise Exception("Bad data, inconsistent digikey-pn for mpn '{}'. Original digikey-pn='{}', new "
- "digikey-pn='{}'".format(mpn, part.digikey_pn, digikey_pn))
+ if dk_part:
+ if part.digikey_pn != dk_part:
+ raise Exception("Bad data, inconsistent digikey-pn for mpn '{}'. First digikey part number='{}', "
+ "new digikey part number='{}'".format(mpn, part.digikey_pn, dk_part))
part.cnt += 1
part.refs.append(ref)
else:
- parts[mpn] = Part(mpn=mpn, cnt=1, refs=[ref], digikey_pn=digikey_pn)
+ parts[mpn] = Part(mpn=mpn, cnt=1, refs=[ref], digikey_pn=dk_part)
mpn_field = "MPN"
count_field = "Count"
@@ -163,10 +188,14 @@ def order_csv(count: int, style: str, output_file: Path, data_sets):
include_extra_fields=include_extra_fields)
-def create_task_order_csv(*, style: str = None, output_file: Union[str, Path], data_sets, count: int = 1):
+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)
+
return {
"name": "order-{}".format(count) if not style else "order-{}-{}".format(style, count),
- "actions": [(order_csv, [count, style, Path(output_file), data_sets])],
+ "actions": [action],
"file_dep": [_dsm.cookie_for_ds(ds) for ds in data_sets],
- "targets": [output_file],
+ "targets": [_dsm.cookie_for_ds(out_data_set), output_file],
}