aboutsummaryrefslogtreecommitdiff
path: root/src/ee
diff options
context:
space:
mode:
authorTrygve Laugstøl <trygvis@inamo.no>2017-08-08 08:40:47 +0200
committerTrygve Laugstøl <trygvis@inamo.no>2017-08-08 08:40:47 +0200
commit21ed642fa528cc732f8d682266111be64c1ae711 (patch)
tree7bafd65aef3eff2a05b3815d29fc3efb0c6a1d34 /src/ee
parent4c0acd35552f213effcea1df275242f3a4476ae3 (diff)
downloadee-python-21ed642fa528cc732f8d682266111be64c1ae711.tar.gz
ee-python-21ed642fa528cc732f8d682266111be64c1ae711.tar.bz2
ee-python-21ed642fa528cc732f8d682266111be64c1ae711.tar.xz
ee-python-21ed642fa528cc732f8d682266111be64c1ae711.zip
o KiCAD BOM parser.
Diffstat (limited to 'src/ee')
-rw-r--r--src/ee/kicad/__init__.py0
-rw-r--r--src/ee/kicad/bom/__init__.py58
-rw-r--r--src/ee/kicad/bom/io.py44
3 files changed, 102 insertions, 0 deletions
diff --git a/src/ee/kicad/__init__.py b/src/ee/kicad/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/src/ee/kicad/__init__.py
diff --git a/src/ee/kicad/bom/__init__.py b/src/ee/kicad/bom/__init__.py
new file mode 100644
index 0000000..1808357
--- /dev/null
+++ b/src/ee/kicad/bom/__init__.py
@@ -0,0 +1,58 @@
+__all__ = [
+ 'Part',
+ 'Library',
+ 'Bom',
+ 'Comp',
+]
+
+class Part:
+ def __init__(self, name):
+ self.name = name
+
+class Library:
+ def __init__(self, name):
+ self.name = name
+ self.parts = {}
+
+ def add_part(self, part):
+ try:
+ return self.parts[part]
+ except KeyError:
+ p = Part(part)
+ self.parts[part] = p
+ return p
+
+class Bom:
+ def __init__(self):
+ self.libraries = {}
+ self.components = {}
+
+ def add_component(self, component):
+ self.components[component.ref] = component
+
+ def get_component(self, name):
+ return self.components[name]
+
+ def get_components(self):
+ return self.components
+
+ def find_library(self, name):
+ try:
+ return self.libraries[name]
+ except KeyError:
+ lib = Library(name)
+ self.libraries[name] = lib
+ return lib
+
+class Comp:
+ def __init__(self, ref, value, library, part, footprint):
+ self.ref = ref
+ self.value = value
+ self.footprint = footprint
+ self.library = library
+ self.part = part
+ self.fields = {}
+
+ def add_field(self, key, value):
+ self.fields[key] = value
+
diff --git a/src/ee/kicad/bom/io.py b/src/ee/kicad/bom/io.py
new file mode 100644
index 0000000..c7eef44
--- /dev/null
+++ b/src/ee/kicad/bom/io.py
@@ -0,0 +1,44 @@
+import xml.etree.ElementTree as ElementTree
+from ee.kicad.bom import *
+
+def read_bom(path):
+ def child_text(e, child_tag):
+ child = e.find(child_tag)
+
+ return child.text if child is not None else None
+
+ def add_comp(b, comp):
+ ref = comp.get("ref")
+ value = child_text(comp, "value")
+ footprint = child_text(comp, "footprint")
+ libsource = comp.find("libsource")
+ l, p = (None, None)
+ if libsource is not None:
+ lib = libsource.get("lib")
+ part = libsource.get("part")
+ if lib is not None and part is not None:
+ l = b.find_library(lib)
+ p = l.add_part(part)
+
+ c = Comp(ref, value, l, p, footprint)
+
+ fields = comp.find("fields")
+ if fields is not None:
+ for f in fields.findall("field"):
+ key = f.get("name")
+ value = f.text
+ c.add_field(key, value)
+
+ b.add_component(c)
+ return c
+
+ with open(path) as f:
+ tree = ElementTree.parse(path)
+ root = tree.getroot()
+
+ b = Bom()
+ comp = root.find("components").findall("comp")
+ for c in comp:
+ add_comp(b, c)
+
+ return b