aboutsummaryrefslogtreecommitdiff
path: root/src/ee/bom
diff options
context:
space:
mode:
Diffstat (limited to 'src/ee/bom')
-rw-r--r--src/ee/bom/__init__.py0
-rw-r--r--src/ee/bom/doit.py95
2 files changed, 95 insertions, 0 deletions
diff --git a/src/ee/bom/__init__.py b/src/ee/bom/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/src/ee/bom/__init__.py
diff --git a/src/ee/bom/doit.py b/src/ee/bom/doit.py
new file mode 100644
index 0000000..d7407d8
--- /dev/null
+++ b/src/ee/bom/doit.py
@@ -0,0 +1,95 @@
+from typing import Mapping
+
+from configclass import Config
+
+from ee.ds import DataSetManager
+
+_dsm = None # type: DataSetManager
+
+_data_sets = {}
+
+_config_template = Config({
+})
+
+_config = None # type: Mapping[str, str]
+
+
+def init(data_set_manager: DataSetManager, **kwargs):
+ global _config
+ _config = _config_template.make(kwargs)
+
+ global _dsm
+ _dsm = data_set_manager
+
+
+class BomComponent(object):
+ def __init__(self, ref, mpn):
+ self.ref = ref
+ self.mpn = mpn
+
+ def to_object(self, ds):
+ return ds.create_object("bom-component", self.ref).\
+ set("ref", self.ref).\
+ set("mpn", self.mpn)
+
+
+class MpnBomComponent(object):
+ def __init__(self, ref, mpn):
+ self.ref = ref
+ self.mpn = mpn
+ self.count = 1
+
+ def to_object(self, ds):
+ return ds.create_object("mpn-bom-component", self.ref).\
+ set("ref", self.ref).\
+ set("mpn", self.mpn)
+
+
+def task_bom():
+ out_data_set = "bom"
+ in_data_sets = _data_sets[task_bom]
+
+ def action(count=1):
+ with _dsm.create_ro(in_data_sets) as in_ds:
+ with _dsm.create_rw(out_data_set, clean=True) as output:
+ components = [o for o in in_ds.items() if o.object_type.name == "component"]
+
+ bom_components = {}
+ mpn_bom_components = {}
+
+ for c in components:
+ ref = c.get("ref")
+ mpn = c.get("mpn")
+
+ if not ref:
+ raise Exception("Missing ref")
+
+ if not mpn:
+ output.create_object("message", "bom-{}".format(ref)). \
+ set("level", "error"). \
+ set("message", "Missing required field 'mpn'")
+ continue
+
+ if ref in bom_components:
+ raise Exception("Duplicate ref '{}'".format("ref"))
+
+ bom_components[ref] = BomComponent(ref, mpn)
+
+ mpn_bom_component = mpn_bom_components[ref]
+ if not mpn_bom_component:
+ mpn_bom_component = MpnBomComponent(ref, mpn)
+ mpn_bom_components[ref] = mpn_bom_component
+
+ mpn_bom_component.count += 1
+
+ [c.to_object(output) for c in bom_components.values()]
+ [c.to_object(output) for c in mpn_bom_components.values()]
+
+ return {
+ "file_dep": [_dsm.cookie_for_ds(ds) for ds in in_data_sets],
+ "actions": [action],
+ "targets": [_dsm.cookie_for_ds(out_data_set)],
+ }
+
+
+_data_sets[task_bom] = ["components"]