From 2c9386bf9f1d465907903e7da79663b5b7e30ace Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Mon, 16 Jul 2018 11:36:29 +0200 Subject: wip --- src/ee/fact/__init__.py | 92 +++++++++++++++++++++--------------- src/ee/kicad/doit.py | 63 ++++++++---------------- test/doit/schematics/schematic-1.sch | 77 ++++++++++++++++++++++++++++++ test/doit/test_doit.py | 35 ++++++++++++++ 4 files changed, 188 insertions(+), 79 deletions(-) create mode 100644 test/doit/schematics/schematic-1.sch create mode 100644 test/doit/test_doit.py 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], diff --git a/test/doit/schematics/schematic-1.sch b/test/doit/schematics/schematic-1.sch new file mode 100644 index 0000000..a805e83 --- /dev/null +++ b/test/doit/schematics/schematic-1.sch @@ -0,0 +1,77 @@ +EESchema Schematic File Version 4 +EELAYER 26 0 +EELAYER END +$Descr A4 11693 8268 +encoding utf-8 +Sheet 1 1 +Title "" +Date "" +Rev "" +Comp "" +Comment1 "" +Comment2 "" +Comment3 "" +Comment4 "" +$EndDescr +$Comp +L Device:R R? +U 1 1 5B431328 +P 4400 2600 +F 0 "R?" V 4193 2600 50 0000 C CNN +F 1 "10k" V 4284 2600 50 0000 C CNN +F 2 "" V 4330 2600 50 0001 C CNN +F 3 "~" H 4400 2600 50 0001 C CNN + 1 4400 2600 + 0 1 1 0 +$EndComp +$Comp +L Device:Battery_Cell BT? +U 1 1 5B431438 +P 3700 2900 +F 0 "BT?" H 3818 2996 50 0000 L CNN +F 1 "9V" H 3818 2905 50 0000 L CNN +F 2 "" V 3700 2960 50 0001 C CNN +F 3 "~" V 3700 2960 50 0001 C CNN + 1 3700 2900 + 1 0 0 -1 +$EndComp +$Comp +L Device:C C? +U 1 1 5B4314AB +P 5000 2850 +F 0 "C?" H 5115 2896 50 0000 L CNN +F 1 "1u" H 5115 2805 50 0000 L CNN +F 2 "" H 5038 2700 50 0001 C CNN +F 3 "~" H 5000 2850 50 0001 C CNN + 1 5000 2850 + 1 0 0 -1 +$EndComp +Wire Wire Line + 3700 3000 3700 3100 +Wire Wire Line + 5000 3100 5000 3000 +$Comp +L power:GND #PWR? +U 1 1 5B4315FE +P 3700 3200 +F 0 "#PWR?" H 3700 2950 50 0001 C CNN +F 1 "GND" H 3705 3027 50 0000 C CNN +F 2 "" H 3700 3200 50 0001 C CNN +F 3 "" H 3700 3200 50 0001 C CNN + 1 3700 3200 + 1 0 0 -1 +$EndComp +Wire Wire Line + 3700 3200 3700 3100 +Connection ~ 3700 3100 +Wire Wire Line + 3700 2700 3700 2600 +Wire Wire Line + 5000 2600 5000 2700 +Wire Wire Line + 3700 2600 4250 2600 +Wire Wire Line + 3700 3100 5000 3100 +Wire Wire Line + 4550 2600 5000 2600 +$EndSCHEMATC diff --git a/test/doit/test_doit.py b/test/doit/test_doit.py new file mode 100644 index 0000000..61c5074 --- /dev/null +++ b/test/doit/test_doit.py @@ -0,0 +1,35 @@ +import pytest +import os.path +import os + +filedir = os.path.dirname(os.path.abspath(__file__)) +schematics_dir = os.path.join(filedir, "schematics") + +def find_task(tasks, name: str): + t = next((t for t in tasks if t["name"] == name), None) + assert(t is not None) + return t + +def exec_task(task): + targets = task["targets"] + for a in task["actions"]: + if isinstance(a, str): + cmd = a % dict(targets=" ".join(targets)) + ret = os.system(cmd) + assert(ret == 0) + else: + a() + +def test_doit(tmpdir): + from ee.kicad.doit import KicadDoitTasks + + args = dict( + sch=os.path.join(schematics_dir, "schematic-1.sch"), + sch_object_set_dir=os.path.join(tmpdir, "kicad-sch") + ) + tasks = list(KicadDoitTasks(**args).tasks()) + assert(len(tasks) == 1) + create_kicad_sch_objects = find_task(tasks, "create-kicad-sch-objects") + exec_task(create_kicad_sch_objects) + +# thirdparty/olinuxino/HARDWARE/A64-OLinuXino/A64-OLinuXino_Rev_C/A64-OlinuXino_Rev_C.sch -- cgit v1.2.3