aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTrygve Laugstøl <trygvis@inamo.no>2018-07-16 13:23:36 +0200
committerTrygve Laugstøl <trygvis@inamo.no>2018-07-16 13:23:36 +0200
commit831485bd027eb288f52a16f2078b232553758d3c (patch)
treeec722ff3add026b5c4afa7eea96afad7c32201d9
parent2c9386bf9f1d465907903e7da79663b5b7e30ace (diff)
downloadee-python-831485bd027eb288f52a16f2078b232553758d3c.tar.gz
ee-python-831485bd027eb288f52a16f2078b232553758d3c.tar.bz2
ee-python-831485bd027eb288f52a16f2078b232553758d3c.tar.xz
ee-python-831485bd027eb288f52a16f2078b232553758d3c.zip
wip
-rw-r--r--src/ee/fact/__init__.py34
-rw-r--r--src/ee/kicad/doit.py53
-rw-r--r--test/doit/test_doit.py8
3 files changed, 78 insertions, 17 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")],
}
diff --git a/test/doit/test_doit.py b/test/doit/test_doit.py
index 61c5074..ff480c5 100644
--- a/test/doit/test_doit.py
+++ b/test/doit/test_doit.py
@@ -25,11 +25,15 @@ def test_doit(tmpdir):
args = dict(
sch=os.path.join(schematics_dir, "schematic-1.sch"),
- sch_object_set_dir=os.path.join(tmpdir, "kicad-sch")
+ kicad_sch_object_set_dir=os.path.join(tmpdir, "kicad-sch"),
+ component_object_set_dir=os.path.join(tmpdir, "component"),
)
tasks = list(KicadDoitTasks(**args).tasks())
- assert(len(tasks) == 1)
+ assert(len(tasks) > 1)
create_kicad_sch_objects = find_task(tasks, "create-kicad-sch-objects")
exec_task(create_kicad_sch_objects)
+ kicad_sch_to_component_object = find_task(tasks, "kicad-sch-to-component-object")
+ exec_task(kicad_sch_to_component_object)
+
# thirdparty/olinuxino/HARDWARE/A64-OLinuXino/A64-OLinuXino_Rev_C/A64-OlinuXino_Rev_C.sch