From de9e6739543fd6a589b80184e5452a6a5cdb1196 Mon Sep 17 00:00:00 2001
From: Trygve Laugstøl <trygvis@inamo.no>
Date: Sat, 28 Jul 2018 21:59:29 +0200
Subject: o Dumping MpnBomComponent, wtf. o Renaming bom.csv to order.csv.

---
 src/ee/kicad/doit.py | 125 ++++++++++++++++++++++++++-------------------------
 1 file changed, 64 insertions(+), 61 deletions(-)

(limited to 'src/ee/kicad')

diff --git a/src/ee/kicad/doit.py b/src/ee/kicad/doit.py
index 30ca40d..bfbf0a1 100644
--- a/src/ee/kicad/doit.py
+++ b/src/ee/kicad/doit.py
@@ -26,24 +26,24 @@ _data_sets = {}
 
 
 def change_data_sets_for_task(task, _callable):
-    _data_sets[task] = _callable(_data_sets[task])
+    _data_sets[task][1] = _callable(_data_sets[task][1])
 
 
-def init(data_set_manager: DataSetManager, **kwargs):
-    global _config
-    _config = _config_template.make(kwargs)
+def output_data_set_for_task(task):
+    return _data_sets[task][0]
+
 
-    ee_logger = logging.getLogger("ee")
-    formatter = logging.Formatter("%(levelname)s: %(message)s")
-    ch = logging.StreamHandler()
-    ch.setFormatter(formatter)
-    ee_logger.addHandler(ch)
+def input_data_sets_for_task(task):
+    return _data_sets[task][1]
 
-    ee_logger.setLevel(logging.DEBUG)
 
+def init(data_set_manager: DataSetManager, **kwargs):
     global _dsm
     _dsm = data_set_manager
 
+    global _config
+    _config = _config_template.make(kwargs)
+
 
 def task_kicad_gerber():
     kicad_pcb = _config["kicad_pcb"]
@@ -84,8 +84,7 @@ def task_kicad_gerber():
 
 
 def task_kicad_sch_to_data_set():
-    out_data_set = "kicad-sch"
-    in_data_sets = _data_sets[task_kicad_sch_to_data_set]
+    out_data_set, in_data_sets = _data_sets[task_kicad_sch_to_data_set]
 
     sch = _config["sch"]
 
@@ -106,7 +105,7 @@ def task_kicad_sch_to_data_set():
 
                 for f in c.fields:
                     if f.value and f.name not in ComponentField.names:
-                        o.set(f.name, str(f.value))
+                        o.set("field-{}".format(f.name), str(f.value))
 
     return {
         "file_dep": [Path(sch)] + [_dsm.cookie_for_ds(ds) for ds in in_data_sets],
@@ -115,14 +114,13 @@ def task_kicad_sch_to_data_set():
     }
 
 
-_data_sets[task_kicad_sch_to_data_set] = []
+_data_sets[task_kicad_sch_to_data_set] = ["kicad-sch", []]
 
 
 def task_kicad_pcb_to_data_set():
     kicad_pcb = _config["kicad_pcb"]
 
-    out_data_set = "kicad-pcb"
-    in_data_sets = _data_sets[task_kicad_pcb_to_data_set]
+    out_data_set, in_data_sets = _data_sets[task_kicad_pcb_to_data_set]
 
     def action():
         from ee.kicad.pcb import KicadPcb, Module
@@ -154,71 +152,76 @@ def task_kicad_pcb_to_data_set():
     }
 
 
-_data_sets[task_kicad_pcb_to_data_set] = []
+_data_sets[task_kicad_pcb_to_data_set] = ["kicad-pcb", []]
 
 
 def task_kicad_create_component_data_set():
-    out_data_set = "components"
-    in_data_sets = _data_sets[task_kicad_create_component_data_set]
+    out_data_set, in_data_sets = _data_sets[task_kicad_create_component_data_set]
 
     def action():
         logger.info("in_data_sets={}, out_data_set={}".format(in_data_sets, out_data_set))
 
-        with _dsm.create_ro(in_data_sets) as in_ds:
-            # for o in in_ds.items():
-            #     logger.info("item: {}/{}".format(o.object_type.name, o.key))
+        in_ds = _dsm.load_data_sets(in_data_sets)
+        # for o in in_ds.items():
+        #     logger.info("item: {}/{}".format(o.object_type.name, o.key))
+
+        def map_footprint(footprint):
+            for o in in_ds.items():
+                if not o.object_type.name == "kicad-footprint-mapping":
+                    continue
 
-            def map_footprint(footprint):
-                for o in in_ds.items():
-                    if not o.object_type.name == "kicad-footprint-mapping":
-                        continue
+                common = o.get("common")
+                if common:
+                    return common
 
-                    common = o.get("common")
-                    if common:
-                        return common
+            return footprint
 
-                return footprint
+        with _dsm.create_rw(out_data_set, clean=True) as output:
+            kicad_sch = [o for o in in_ds.items() if o.object_type.name == "kicad-schematic-component"]
 
-            with _dsm.create_rw(out_data_set, clean=True) as output:
-                kicad_sch = [o for o in in_ds.items() if o.object_type.name == "kicad-schematic-component"]
+            logger.info("processing {} kicad-sch".format(len(kicad_sch)))
 
-                logger.info("processing {} kicad-sch".format(len(kicad_sch)))
+            ignored_ref_types = {"#PWR", }
 
-                ignored_ref_types = {"#PWR", }
+            for sch in kicad_sch:
+                ref = sch.get("ref")
+                ref_num = sch.get("ref-num")
+                if not ref or not ref_num:
+                    logger.debug("Missing ref or ref-num")
+                    continue
 
-                for sch in kicad_sch:
-                    ref = sch.get("ref")
-                    ref_num = sch.get("ref-num")
-                    if not ref or not ref_num:
-                        logger.debug("Missing ref or ref-num")
-                        continue
+                ref_type = sch.get("ref-type")
+                if not ref_type:
+                    logger.debug("Missing ref-type")
+                    continue
+
+                if ref_type in ignored_ref_types:
+                    continue
 
-                    ref_type = sch.get("ref-type")
-                    if not ref_type or ref_type in ignored_ref_types:
-                        logger.debug("Missing ref-type or bad ref-type: ref={}, ref-type={}".format(ref, ref_type))
-                        continue
+                c = output.create_object("component", ref)
+                c.set("ref", ref)
+                c.set("ref-num", ref_num)
+                c.set("ref-type", ref_type)
 
-                    c = output.create_object("component", ref)
-                    c.set("ref", ref)
-                    c.set("ref-num", ref_num)
-                    c.set("ref-type", ref_type)
+                fp = sch.get("footprint")
+                if fp:
+                    fp = map_footprint(fp)
+                    c.set("footprint", fp)
 
-                    fp = sch.get("footprint")
-                    if fp:
-                        fp = map_footprint(fp)
-                        c.set("footprint", fp)
+                c.set("mpn", sch.get("field-mpn"))
+                c.set("distributor", sch.get("field-distributor"))
 
-                    def pcb_match(o):
-                        return o.object_type.name == "kicad-pcb-component" and \
-                               o.get("ref") == ref
+                def pcb_match(o):
+                    return o.object_type.name == "kicad-pcb-component" and \
+                           o.get("ref") == ref
 
-                    pcb = [o for o in in_ds.items() if pcb_match(o)]
+                pcb = [o for o in in_ds.items() if pcb_match(o)]
 
-                    if not pcb:
-                        logger.info("Could not find PCB component for {}".format(ref))
+                if not pcb:
+                    logger.info("Could not find PCB component for {}".format(ref))
 
-                        # TODO: check that the SCH and PCB footprint are the same
-                        # c.set("footprint", pcb.)
+                    # TODO: check that the SCH and PCB footprint are the same
+                    # c.set("footprint", pcb.)
 
     return {
         "file_dep": [_dsm.cookie_for_ds(ds) for ds in in_data_sets],
@@ -227,7 +230,7 @@ def task_kicad_create_component_data_set():
     }
 
 
-_data_sets[task_kicad_create_component_data_set] = ["kicad-sch", "kicad-pcb"]
+_data_sets[task_kicad_create_component_data_set] = ["components", ["kicad-sch", "kicad-pcb"]]
 
 __all__ = [
     init.__name__,
-- 
cgit v1.2.3