aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTrygve Laugstøl <trygvis@inamo.no>2017-09-24 09:46:21 +0200
committerTrygve Laugstøl <trygvis@inamo.no>2017-09-24 09:46:21 +0200
commit930e3d61917bd4bf162142db64301691afdf9539 (patch)
treef2e06b6aef12026a5f99742ca78f178f63f5037b
parent33a03ca8cb65202509bd009832010f8f587ace47 (diff)
downloadee-python-930e3d61917bd4bf162142db64301691afdf9539.tar.gz
ee-python-930e3d61917bd4bf162142db64301691afdf9539.tar.bz2
ee-python-930e3d61917bd4bf162142db64301691afdf9539.tar.xz
ee-python-930e3d61917bd4bf162142db64301691afdf9539.zip
o Let digikey-download-facts read .sch files directly.
-rw-r--r--src/ee/digikey/__init__.py22
-rw-r--r--src/ee/kicad/__init__.py3
-rw-r--r--src/ee/kicad/model.py9
-rw-r--r--src/ee/kicad/to_bom.py8
-rw-r--r--src/ee/tools/digikey_download_facts.py42
-rw-r--r--src/ee/tools/kicad_make_bom.py2
-rw-r--r--test/test_read_schematic.py2
7 files changed, 65 insertions, 23 deletions
diff --git a/src/ee/digikey/__init__.py b/src/ee/digikey/__init__.py
index 64d8943..af5977b 100644
--- a/src/ee/digikey/__init__.py
+++ b/src/ee/digikey/__init__.py
@@ -1,8 +1,12 @@
import enum
from typing import List, Optional
+from ee.tools import mk_parents
import re
import requests
+import os
+import os.path
+import yaml
from cachecontrol import CacheControl
from cachecontrol.caches.file_cache import FileCache
from cachecontrol.heuristics import ExpiresAfter
@@ -266,3 +270,21 @@ class DigikeyClient(object):
return res
else:
return DigikeySearchResponse(1, SearchResponseTypes.NO_MATCHES)
+
+class DigikeyRepository(object):
+ def __init__(self, path):
+ self._path = path
+
+ def mpn_to_path(self, mpn):
+ return "{}/{}.yaml".format(self._path, mpn)
+
+ def has_product(self, mpn):
+ filename = self.mpn_to_path(mpn)
+ return os.path.isfile(filename)
+
+ def save(self, product: DigikeyProduct):
+ y = product.to_yaml()
+ filename = self.mpn_to_path(product.mpn)
+ mk_parents(filename)
+ with open(filename, "w") as f:
+ yaml.dump(y, f, encoding="utf-8", allow_unicode=True)
diff --git a/src/ee/kicad/__init__.py b/src/ee/kicad/__init__.py
index adf8ddd..5187a9c 100644
--- a/src/ee/kicad/__init__.py
+++ b/src/ee/kicad/__init__.py
@@ -2,7 +2,7 @@ from typing import Any
from ee import EeException
from ee.kicad.read_schematic import read_schematic
-from ee.kicad.to_bom import to_bom
+from ee.kicad.to_bom import to_bom, to_bom_xml
from .model import *
__all__ = [
@@ -12,6 +12,7 @@ __all__ = [
"Schematic",
"read_schematic",
"to_bom",
+ "to_bom_xml",
"to_pandas",
]
diff --git a/src/ee/kicad/model.py b/src/ee/kicad/model.py
index f112e72..b230f94 100644
--- a/src/ee/kicad/model.py
+++ b/src/ee/kicad/model.py
@@ -105,6 +105,10 @@ class Component(object):
return self.ref_type == "#PWR"
@property
+ def is_flg(self) -> bool:
+ return self.ref_type == "#FLG"
+
+ @property
def value(self):
return self._fields[1].value
@@ -120,6 +124,11 @@ class Component(object):
def named_fields(self):
return [f for f in self._fields if f.name]
+ def get_field(self, name) -> ComponentField:
+ for f in self.fields:
+ if name.lower() == f.name.lower():
+ return f
+
class Sheet(object):
def __init__(self):
diff --git a/src/ee/kicad/to_bom.py b/src/ee/kicad/to_bom.py
index d684486..ecab211 100644
--- a/src/ee/kicad/to_bom.py
+++ b/src/ee/kicad/to_bom.py
@@ -34,7 +34,11 @@ def comp(c: Component) -> Element:
return comp
-def to_bom(schematic: Schematic) -> Element:
+def to_bom(schematic: Schematic) -> Component:
+ return [c for c in sorted(schematic.components) if c.ref_type != "#PWR" and c.ref_type != "#FLG"]
+
+
+def to_bom_xml(schematic: Schematic) -> Element:
root = Element("export")
root.attrib["version"] = "D"
design = Element("design")
@@ -42,6 +46,6 @@ def to_bom(schematic: Schematic) -> Element:
components = Element("components")
root.append(components)
- [components.append(comp(c)) for c in sorted(schematic.components) if c.ref_type != "#PWR" and c.ref_type != "#FLG"]
+ [components.append(comp(c)) for c in to_bom(schematic)]
return root
diff --git a/src/ee/tools/digikey_download_facts.py b/src/ee/tools/digikey_download_facts.py
index c97ba9d..0550c5f 100644
--- a/src/ee/tools/digikey_download_facts.py
+++ b/src/ee/tools/digikey_download_facts.py
@@ -1,8 +1,6 @@
import argparse
from itertools import *
-import os.path
-import yaml
from colors import color
import ee.digikey as dk
@@ -11,11 +9,6 @@ from ee.tools import mk_parents
parser = argparse.ArgumentParser(description="Download facts about parts from Digi-Key")
-parser.add_argument("parts",
- metavar="PART",
- nargs="+",
- help="The parts to download fact for")
-
parser.add_argument("--out",
required=True,
metavar="OUTPUT_DIRECTORY",
@@ -23,6 +16,12 @@ parser.add_argument("--out",
action="store",
help="A directory to store fact files")
+parser.add_argument("--part",
+ nargs="+",
+ help="the parts to download fact for")
+
+parser.add_argument("--sch")
+
parser.add_argument("--force",
dest="force",
action="store",
@@ -32,26 +31,33 @@ args = parser.parse_args()
digikey = dk.Digikey()
client = dk.DigikeyClient(digikey, on_download=lambda s: print(color(s, 'grey')))
+repo = dk.DigikeyRepository(args.out)
-def mpn_to_path(mpn):
- return "{}/{}.yaml".format(args.out, mpn)
+def on_product(product: DigikeyProduct):
+ repo.save(product)
-def on_product(product: DigikeyProduct):
- y = product.to_yaml()
+parts = []
- filename = mpn_to_path(product.mpn)
- mk_parents(filename)
- with open(filename, "w") as f:
- yaml.dump(y, f, encoding="utf-8", allow_unicode=True)
+if args.part:
+ parts.append(args.part)
+if args.sch:
+ from ee.kicad import read_schematic, to_bom
+ sch = read_schematic(args.sch)
+ for c in to_bom(sch):
+ digikey = c.get_field("digikey")
+ if digikey:
+ parts.append(digikey.value)
+ mpn = c.get_field("mpn")
+ if mpn:
+ parts.append(mpn.value)
-for p in args.parts:
+for p in parts:
print(color("Searching for {}".format(p), "white"))
- path = mpn_to_path(p)
- if os.path.isfile(path) and not args.force:
+ if repo.has_product(p) and not args.force:
continue
response = client.search(p)
diff --git a/src/ee/tools/kicad_make_bom.py b/src/ee/tools/kicad_make_bom.py
index 518cf75..7d25799 100644
--- a/src/ee/tools/kicad_make_bom.py
+++ b/src/ee/tools/kicad_make_bom.py
@@ -23,7 +23,7 @@ args = parser.parse_args()
sch = kicad.read_schematic(args.sch)
-bom = kicad.to_bom(sch)
+bom = kicad.to_bom_xml(sch)
xml = ElementTree.tostring(bom, encoding='unicode')
if args.pretty:
diff --git a/test/test_read_schematic.py b/test/test_read_schematic.py
index 3943e9a..1af7002 100644
--- a/test/test_read_schematic.py
+++ b/test/test_read_schematic.py
@@ -24,7 +24,7 @@ def dump_bom(sch):
from xml.dom import minidom
print("---")
- bom = kicad.to_bom(sch)
+ bom = kicad.to_bom_xml(sch)
s = ElementTree.tostring(bom, encoding='unicode')
print(s)
xmlstr = minidom.parseString(s).toprettyxml(indent=" ")