diff options
Diffstat (limited to 'src/ee')
-rw-r--r-- | src/ee/fact/__init__.py | 34 | ||||
-rw-r--r-- | src/ee/kicad/doit.py | 53 |
2 files changed, 72 insertions, 15 deletions
diff --git a/src/ee/fact/__init__.py b/src/ee/fact/__init__.py index 00035a6..659558f 100644 --- a/src/ee/fact/__init__.py +++ b/src/ee/fact/__init__.py @@ -1,4 +1,4 @@ -from typing import Optional +from typing import Optional, Mapping import os.path import configparser @@ -34,6 +34,10 @@ class Object(object): idx = self._descriptor.index_of(key, create=True) self._data.insert(idx, value) + def merge(self, kv: Mapping[str, str]): + for k, v in kv.items(): + self.set(k, v) + def get(self, key: str) -> Optional[str]: return self._data[self._descriptor.index_of(key)] @@ -43,20 +47,46 @@ class ObjectSet(object): self._meta = meta self._descriptor = ObjectDescriptor() + def items(self): + return self._objects.values() + def create_object(self, key: str): + if key in self._objects: + raise ValueError("Object already exists: {}".format(key)) o = Object(key, self._descriptor) self._objects[key] = o return o def read(self, path): + from pathlib import Path print("Reading objects from {}".format(path)) - pass + for p in Path(path).glob("*.ini"): + if p.name == "object-set.ini": + continue + + with open(p, "r") as f: + ini = configparser.ConfigParser(interpolation = None) + ini.read(p) + + key = ini.get("meta", "key") + + o = self.create_object(key) + o.merge({k:v for k, v in ini.items("values")}) + print("Read {} objects".format(len(self._objects))) def write(self, path): print("Writing {} objects".format(len(self._objects))) + ini = configparser.ConfigParser(interpolation = None) + ini.add_section("object-set") + with open(os.path.join(path, "object-set.ini"), "w") as f: + ini.write(f) + for o in self._objects.values(): ini = configparser.ConfigParser(interpolation = None) + ini.add_section("meta") + ini.set("meta", "key", o.key) + ini.add_section("values") for key in sorted(self._descriptor.keys): value = o.get(key) diff --git a/src/ee/kicad/doit.py b/src/ee/kicad/doit.py index a1405d4..cc9936c 100644 --- a/src/ee/kicad/doit.py +++ b/src/ee/kicad/doit.py @@ -9,7 +9,8 @@ class KicadDoitTasks(object): "kicad_pcb": None, "gerber_dir": None, "gerber_zip": None, - "sch_object_set_dir": None, + "kicad_sch_object_set_dir": None, + "component_object_set_dir": None, }) def __init__(self, *args, **kwargs): @@ -57,14 +58,19 @@ class KicadDoitTasks(object): "file_dep": [kicad_pcb], }) - sch_object_set_dir = self.config["sch_object_set_dir"] - if sch and sch_object_set_dir: - tasks.append(task_create_kicad_sch_objects(sch, sch_object_set_dir)) + kicad_sch_object_set_dir = self.config["kicad_sch_object_set_dir"] + component_object_set_dir = self.config["component_object_set_dir"] + + if sch and kicad_sch_object_set_dir: + tasks.append(task_create_kicad_sch_objects(sch, kicad_sch_object_set_dir)) + + if kicad_sch_object_set_dir and component_object_set_dir: + tasks.append(task_kicad_sch_to_component_object(kicad_sch_object_set_dir, component_object_set_dir)) for t in tasks: yield t -def task_create_kicad_sch_objects(sch, object_set_dir): +def task_create_kicad_sch_objects(sch, os_dir, name="create-kicad-sch-objects"): def action(): import ee.kicad from ee.kicad.model import Component, ComponentField @@ -74,10 +80,10 @@ def task_create_kicad_sch_objects(sch, object_set_dir): schematics = ee.kicad.read_schematics(sch) cs = [c for c in schematics.components] - os.makedirs(object_set_dir, exist_ok=True) + os.makedirs(os_dir, exist_ok=True) # TODO: remove directory? oset = fact.ObjectSet() - oset.read(object_set_dir) + oset.read(os_dir) for c in cs: o = oset.create_object(c.timestamp) o.set("ref", c.ref) @@ -92,13 +98,34 @@ def task_create_kicad_sch_objects(sch, object_set_dir): if f.value and f.name not in ComponentField.names: o.set(f.name, str(f.value)) - oset.write(object_set_dir) + oset.write(os_dir) - component_files_cookie = "{}/kicad-sch.cookie".format(object_set_dir) - gen_cookie = "date > %(targets)s" return { - "name": "create-kicad-sch-objects", + "name": name, "file_dep": [sch], - "actions": [action, gen_cookie], - "targets": [component_files_cookie], + "actions": [action], + "targets": [os.path.join(os_dir, "object-set.ini")], + } + +def task_kicad_sch_to_component_object(kicad_sch_os_dir, component_os_dir, name="kicad-sch-to-component-object"): + def action(): + import ee.fact as fact + + kicad_sch_os = fact.ObjectSet() + kicad_sch_os.read(kicad_sch_os_dir) + + os.makedirs(component_os_dir, exist_ok=True) + component_os = fact.ObjectSet() + component_os.read(component_os_dir) + + for o in kicad_sch_os.items(): + print("processing {}".format(o.key)) + + component_os.write(component_os_dir) + + return { + "name": name, + "file_dep": [os.path.join(kicad_sch_os_dir, "object-set.ini")], + "actions": [action], + "targets": [os.path.join(component_os_dir, "object-set.ini")], } |