aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorTrygve Laugstøl <trygvis@inamo.no>2017-09-12 08:51:45 +0200
committerTrygve Laugstøl <trygvis@inamo.no>2017-09-12 08:51:45 +0200
commitf81cd8de1bc679786579f14c3da28285789b38e1 (patch)
tree9b7cd6b811e68667f9765c54a345650604749536 /src
parent7f267c4aabd0a0fc5487bdf962e3c16857137c0c (diff)
downloadee-python-f81cd8de1bc679786579f14c3da28285789b38e1.tar.gz
ee-python-f81cd8de1bc679786579f14c3da28285789b38e1.tar.bz2
ee-python-f81cd8de1bc679786579f14c3da28285789b38e1.tar.xz
ee-python-f81cd8de1bc679786579f14c3da28285789b38e1.zip
o Implementing kicad-make-bom as a tool.
Diffstat (limited to 'src')
-rw-r--r--src/ee/tools/__init__.py21
-rw-r--r--src/ee/tools/digikey_download_facts.py7
-rw-r--r--src/ee/tools/kicad_make_bom.py37
3 files changed, 60 insertions, 5 deletions
diff --git a/src/ee/tools/__init__.py b/src/ee/tools/__init__.py
index e69de29..893ee31 100644
--- a/src/ee/tools/__init__.py
+++ b/src/ee/tools/__init__.py
@@ -0,0 +1,21 @@
+import os.path
+
+
+def _mkdir_and_open(path):
+ dirname = os.path.dirname(path)
+
+ if len(dirname) > 0:
+ if not os.path.isdir(dirname):
+ os.mkdir(dirname)
+
+ return open(path, "w")
+
+
+def mk_parents(path: str):
+ dirname = os.path.dirname(path)
+
+ if len(dirname) == 0:
+ return
+
+ if not os.path.isdir(dirname):
+ os.mkdir(dirname)
diff --git a/src/ee/tools/digikey_download_facts.py b/src/ee/tools/digikey_download_facts.py
index 08a8029..c97ba9d 100644
--- a/src/ee/tools/digikey_download_facts.py
+++ b/src/ee/tools/digikey_download_facts.py
@@ -7,6 +7,7 @@ from colors import color
import ee.digikey as dk
from ee.digikey import SearchResponseTypes, DigikeyProduct
+from ee.tools import mk_parents
parser = argparse.ArgumentParser(description="Download facts about parts from Digi-Key")
@@ -41,11 +42,7 @@ def on_product(product: DigikeyProduct):
y = product.to_yaml()
filename = mpn_to_path(product.mpn)
- dirname = os.path.dirname(filename)
-
- if not os.path.isdir(dirname):
- os.mkdir(dirname)
-
+ mk_parents(filename)
with open(filename, "w") as f:
yaml.dump(y, f, encoding="utf-8", allow_unicode=True)
diff --git a/src/ee/tools/kicad_make_bom.py b/src/ee/tools/kicad_make_bom.py
new file mode 100644
index 0000000..518cf75
--- /dev/null
+++ b/src/ee/tools/kicad_make_bom.py
@@ -0,0 +1,37 @@
+import argparse
+import ee.kicad as kicad
+from ee.tools import mk_parents
+from xml.etree import ElementTree
+from xml.dom import minidom
+
+parser = argparse.ArgumentParser(description="Create a bom XML file from a KiCAD schematic")
+
+parser.add_argument("--sch",
+ required=True,
+ metavar="SCH",
+ help="The schematic to read")
+
+parser.add_argument("--out",
+ metavar="FILE",
+ help="The output file")
+
+parser.add_argument("--pretty",
+ action='store_true',
+ help="Pretty print the XML")
+
+args = parser.parse_args()
+
+sch = kicad.read_schematic(args.sch)
+
+bom = kicad.to_bom(sch)
+xml = ElementTree.tostring(bom, encoding='unicode')
+
+if args.pretty:
+ xml = minidom.parseString(xml).toprettyxml(indent=" ")
+
+if args.out:
+ mk_parents(args.out)
+ with open(args.out, "w") as f:
+ f.write(xml)
+else:
+ print(xml)