diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/ee/fact/__init__.py | 92 | ||||
-rw-r--r-- | src/ee/kicad/doit.py | 63 |
2 files changed, 76 insertions, 79 deletions
diff --git a/src/ee/fact/__init__.py b/src/ee/fact/__init__.py index 5c6d789..00035a6 100644 --- a/src/ee/fact/__init__.py +++ b/src/ee/fact/__init__.py @@ -1,49 +1,67 @@ -class Fact(object): - def __init__(self, kind): - self._kind = kind - self._domains = {} +from typing import Optional +import os.path +import configparser - @property - def kind() -> str: - return self._kind +class ObjectDescriptor(object): + def __init__(self): + self._keys = [] - def attr(self, domain: str, key: str): + def index_of(self, key, create: bool = False) -> int: try: - return self._domains[domain][key] - except KeyError: - pass + return self._keys.index(key) + except ValueError as e: + if not create: + raise e - def set(self, domain: str, key: str, value: str): - _kv(domain)[key] = value + self._keys.append(key) + return len(self._keys) - 1 - def _kv(self, domain: str): - try: - kv = self._domains[domain] - except KeyError: - kv = {} - self._domains[domain] = kv + @property + def keys(self): + return self._keys + +class Object(object): + def __init__(self, key, descriptor): + self._key = key + self._descriptor = descriptor + self._data = [] + + @property + def key(self): + return self._key + + def set(self, key: str, value: str): + idx = self._descriptor.index_of(key, create=True) + self._data.insert(idx, value) - return kv + def get(self, key: str) -> Optional[str]: + return self._data[self._descriptor.index_of(key)] - def view(self, domain: str): - return FactKv(self, domain, self._kv(domain)) +class ObjectSet(object): + def __init__(self, meta = dict()): + self._objects = {} + self._meta = meta + self._descriptor = ObjectDescriptor() - def write(self, output): - import configparser - ini = configparser.ConfigParser(interpolation = None) - for domain, kv in self._domains.items(): - ini.add_section(domain) - for key, value in kv.items(): - ini.set(domain, key, value) + def create_object(self, key: str): + o = Object(key, self._descriptor) + self._objects[key] = o + return o - ini.write(output) + def read(self, path): + print("Reading objects from {}".format(path)) + pass + def write(self, path): + print("Writing {} objects".format(len(self._objects))) -class FactKv(object): - def __init__(self, fact, domain, kv): - self._fact = fact - self._domain = domain - self._kv = kv + for o in self._objects.values(): + ini = configparser.ConfigParser(interpolation = None) + ini.add_section("values") + for key in sorted(self._descriptor.keys): + value = o.get(key) + if value: + ini.set("values", key, value) - def set(self, key, value): - self._kv[key] = value + with open(os.path.join(path, "{}.ini".format(o.key)), "w") as f: + ini.write(f) diff --git a/src/ee/kicad/doit.py b/src/ee/kicad/doit.py index 44abc61..a1405d4 100644 --- a/src/ee/kicad/doit.py +++ b/src/ee/kicad/doit.py @@ -9,7 +9,7 @@ class KicadDoitTasks(object): "kicad_pcb": None, "gerber_dir": None, "gerber_zip": None, - "components_dir": None, + "sch_object_set_dir": None, }) def __init__(self, *args, **kwargs): @@ -57,68 +57,47 @@ class KicadDoitTasks(object): "file_dep": [kicad_pcb], }) - components_dir = self.config["components_dir"] - if sch and components_dir: - tasks.append(task_sch_to_component_files(sch, components_dir)) + 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)) for t in tasks: yield t - -def task_sch_to_component_files(sch, components_dir): +def task_create_kicad_sch_objects(sch, object_set_dir): def action(): import ee.kicad from ee.kicad.model import Component, ComponentField import csv import configparser - from ee.fact import Fact + import ee.fact as fact schematics = ee.kicad.read_schematics(sch) cs = [c for c in schematics.components] - os.makedirs(components_dir, exist_ok=True) + os.makedirs(object_set_dir, exist_ok=True) # TODO: remove directory? + oset = fact.ObjectSet() + oset.read(object_set_dir) for c in cs: - path = os.path.join(components_dir, "{}.ini".format(c.timestamp)) - ini = configparser.ConfigParser(interpolation = None) - if os.path.isfile(path) and ini.read(path) != [path]: - print("Could not load component file: {}".format(path)) - return False -# if ini.has_section("kicad-schematic"): -# ini.remove_section("kicad-schematic") -# ini.add_section("kicad-schematic") -# ini.set("kicad-schematic", "ref", c.ref) -# ini.set("kicad-schematic", "ref-type", c.ref_type) - fact = Fact("schematic-symbol") - ks = fact.view("kicad-schematic") - ks.set("ref", c.ref) - ks.set("ref-type", c.ref_type) + o = oset.create_object(c.timestamp) + o.set("ref", c.ref) + o.set("ref-type", c.ref_type) if c.has_ref_num: -# ini.set("kicad-schematic", "ref-num", str(c.ref_num)) - ks.set("ref-num", str(c.ref_num)) -# ini.set("kicad-schematic", "value", c.value) - ks.set("value", c.value) + o.set("ref-num", str(c.ref_num)) + o.set("value", c.value) if c.footprint: -# ini.set("kicad-schematic", "footprint", c.footprint) - ks.set("footprint", c.footprint) - - fields = [f for f in c.fields if f.value and f.name not in ComponentField.names] + o.set("footprint", c.footprint) - if fields: -# if ini.has_section("kicad-fields"): -# ini.remove_section("kicad-fields") -# ini.add_section("kicad-fields") - kf = fact.kv("kicad-fields") - for f in fields: - kf.set(f.name, str(f.value)) + for f in c.fields: + if f.value and f.name not in ComponentField.names: + o.set(f.name, str(f.value)) - with open(path, "w", newline="") as f: -# ini.write(f) - fact.write(f) + oset.write(object_set_dir) - component_files_cookie = "{}/components.cookie".format(components_dir) + component_files_cookie = "{}/kicad-sch.cookie".format(object_set_dir) gen_cookie = "date > %(targets)s" return { - "name": "sch_to_component_files", + "name": "create-kicad-sch-objects", "file_dep": [sch], "actions": [action, gen_cookie], "targets": [component_files_cookie], |