From 21ed642fa528cc732f8d682266111be64c1ae711 Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Tue, 8 Aug 2017 08:40:47 +0200 Subject: o KiCAD BOM parser. --- src/ee/kicad/bom/io.py | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 src/ee/kicad/bom/io.py (limited to 'src/ee/kicad/bom/io.py') 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 -- cgit v1.2.3