diff options
author | Trygve Laugstøl <trygvis@inamo.no> | 2018-07-23 22:53:17 +0200 |
---|---|---|
committer | Trygve Laugstøl <trygvis@inamo.no> | 2018-07-23 22:53:17 +0200 |
commit | 894ff92c770320e264961bba55bbf0fba16efe56 (patch) | |
tree | 1cd723415aaaf5ad45a7aa32ac77b4e8e5645db9 /src/ee/kicad | |
parent | 3e5819caef56c449606e27ec80f9d563d519a907 (diff) | |
download | ee-python-894ff92c770320e264961bba55bbf0fba16efe56.tar.gz ee-python-894ff92c770320e264961bba55bbf0fba16efe56.tar.bz2 ee-python-894ff92c770320e264961bba55bbf0fba16efe56.tar.xz ee-python-894ff92c770320e264961bba55bbf0fba16efe56.zip |
wip.
o Support for loading CSV files.
Diffstat (limited to 'src/ee/kicad')
-rw-r--r-- | src/ee/kicad/doit.py | 127 |
1 files changed, 98 insertions, 29 deletions
diff --git a/src/ee/kicad/doit.py b/src/ee/kicad/doit.py index 7273076..41e3155 100644 --- a/src/ee/kicad/doit.py +++ b/src/ee/kicad/doit.py @@ -1,5 +1,7 @@ import logging import os.path +from pathlib import Path +from typing import Mapping from configclass import Config @@ -14,27 +16,33 @@ _config_template = Config({ "kicad_pcb": None, "gerber_dir": None, "gerber_zip": None, - "data_set_dir": None, }) -_config = None +_config = None # type: Mapping[str, str] _dsm = None # type: DataSetManager +_data_sets = {} -def init(**kwargs): + +def change_data_sets_for_task(task, callable): + _data_sets[task] = callable(_data_sets[task]) + + +def init(data_set_manager: DataSetManager, **kwargs): global _config _config = _config_template.make(kwargs) + ee_logger = logging.getLogger("ee") formatter = logging.Formatter("%(levelname)s: %(message)s") ch = logging.StreamHandler() ch.setFormatter(formatter) - logger.addHandler(ch) + ee_logger.addHandler(ch) - logger.setLevel(logging.DEBUG) + ee_logger.setLevel(logging.DEBUG) global _dsm - _dsm = DataSetManager(_config["data_set_dir"]) + _dsm = data_set_manager def task_kicad_gerber(): @@ -69,7 +77,6 @@ def task_kicad_gerber(): z.write(p, arcname=p.relative_to(gerber_dir)) return { - "basename": "kicad-gerber", "targets": [gerber_zip], "actions": [mkdir, export_gerber, make_zip], "file_dep": [kicad_pcb], @@ -78,14 +85,14 @@ def task_kicad_gerber(): def task_kicad_sch_to_data_set(): out_data_set = "kicad-sch" - in_data_sets = [] + in_data_sets = _data_sets[task_kicad_sch_to_data_set] sch = _config["sch"] def action(): from ee.kicad.model import ComponentField - with _dsm.create_rw(out_data_set, inputs=in_data_sets) as ds: + with _dsm.create_rw(out_data_set) as ds: schematics = ee.kicad.read_schematics(sch) for c in [c for c in schematics.components]: o = ds.create_object("kicad-schematic-component", c.timestamp) @@ -102,18 +109,20 @@ def task_kicad_sch_to_data_set(): o.set(f.name, str(f.value)) return { - "basename": "kicad-sch-to-data-set", - "file_dep": [sch] + [_dsm.metafile_for_ds(ds) for ds in in_data_sets], + "file_dep": [Path(sch)] + [_dsm.cookie_for_ds(ds) for ds in in_data_sets], "actions": [action], - "targets": [_dsm.metafile_for_ds(out_data_set)], + "targets": [_dsm.cookie_for_ds(out_data_set)], } +_data_sets[task_kicad_sch_to_data_set] = [] + + def task_kicad_pcb_to_data_set(): kicad_pcb = _config["kicad_pcb"] out_data_set = "kicad-pcb" - in_data_sets = [] + in_data_sets = _data_sets[task_kicad_pcb_to_data_set] def action(): from ee.kicad.pcb import KicadPcb, Module @@ -130,7 +139,7 @@ def task_kicad_pcb_to_data_set(): o = ds.create_object("kicad-pcb-component", m.tstamp) ref_text = next((t for t in m.fp_texts if t.kind == "reference"), None) - o.set("reference", ref_text.value) + o.set("ref", ref_text.value) x, y, rot = m.at o.set("placement-x", x) @@ -139,31 +148,91 @@ def task_kicad_pcb_to_data_set(): o.set("layer", m.layer) return { - "basename": "kicad-pcb-to-data-set", - "file_dep": [kicad_pcb] + [_dsm.metafile_for_ds(ds) for ds in in_data_sets], + "file_dep": [Path(kicad_pcb)] + [_dsm.cookie_for_ds(ds) for ds in in_data_sets], "actions": [action], - "targets": [_dsm.metafile_for_ds(out_data_set)], + "targets": [_dsm.cookie_for_ds(out_data_set)], } +_data_sets[task_kicad_pcb_to_data_set] = [] + + def task_kicad_create_component_data_set(): out_data_set = "components" - in_data_sets = ["kicad-sch", "kicad-pcb"] + 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)) + + 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 - def action(targets, *args, **kwargs): - logger.info("targets={}, args={}, kwargs={}".format(targets, args, kwargs)) + return footprint - with _dsm.create_ro(in_data_sets) as input: with _dsm.create_rw(out_data_set) as output: - items = input.items() - logger.info("Got {} objects".format(len(items))) - for o in items: - ot = o.object_type - logger.info("processing {}:{}".format(ot.name, o.key)) + 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))) + + 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 or ref_type == "#PWR": + 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) + + fp = sch.get("footprint") + if fp: + fp = map_footprint(fp) + c.set("footprint", fp) + + 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)] + + 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.) return { - "basename": "kicad-create-component-data-set", - "file_dep": [_dsm.metafile_for_ds(ds) for ds in in_data_sets], + "file_dep": [_dsm.cookie_for_ds(ds) for ds in in_data_sets], "actions": [action], - "targets": [_dsm.metafile_for_ds(out_data_set)], + "targets": [_dsm.cookie_for_ds(out_data_set)], } + + +_data_sets[task_kicad_create_component_data_set] = ["kicad-sch", "kicad-pcb"] + +__all__ = [ + init.__name__, + change_data_sets_for_task.__name__, + + task_kicad_create_component_data_set.__name__, + task_kicad_gerber.__name__, + task_kicad_pcb_to_data_set.__name__, + task_kicad_sch_to_data_set.__name__, +] |