aboutsummaryrefslogtreecommitdiff
path: root/src/ee/tools
diff options
context:
space:
mode:
Diffstat (limited to 'src/ee/tools')
-rw-r--r--src/ee/tools/__init__.py18
-rw-r--r--src/ee/tools/digikey_download_facts.py33
2 files changed, 36 insertions, 15 deletions
diff --git a/src/ee/tools/__init__.py b/src/ee/tools/__init__.py
index 893ee31..136bdf1 100644
--- a/src/ee/tools/__init__.py
+++ b/src/ee/tools/__init__.py
@@ -1,4 +1,5 @@
import os.path
+from colors import color
def _mkdir_and_open(path):
@@ -19,3 +20,20 @@ def mk_parents(path: str):
if not os.path.isdir(dirname):
os.mkdir(dirname)
+
+
+class Log(object):
+ def __init__(self):
+ pass
+
+ def warn(self, msg):
+ print(color(msg, "orange"))
+
+ def info(self, msg):
+ print(color(msg, "orange"))
+
+ def debug(self, msg):
+ print(color(msg, "orange"))
+
+
+log = Log()
diff --git a/src/ee/tools/digikey_download_facts.py b/src/ee/tools/digikey_download_facts.py
index 950623f..c947b67 100644
--- a/src/ee/tools/digikey_download_facts.py
+++ b/src/ee/tools/digikey_download_facts.py
@@ -2,11 +2,9 @@ import argparse
from itertools import *
from functools import total_ordering
-from colors import color
-
import ee.digikey as dk
from ee.digikey import SearchResponseTypes, DigikeyProduct
-from ee.tools import mk_parents
+from ee.tools import log
@total_ordering
@@ -24,6 +22,7 @@ class Query(object):
def __hash__(self):
return hash((self.is_mpn, self.query))
+
parser = argparse.ArgumentParser(description="Download facts about parts from Digi-Key")
parser.add_argument("--out",
@@ -47,7 +46,7 @@ parser.add_argument("--force",
args = parser.parse_args()
digikey = dk.Digikey()
-client = dk.DigikeyClient(digikey, on_download=lambda s: print(color(s, 'grey')))
+client = dk.DigikeyClient(digikey, on_download=log.debug)
repo = dk.DigikeyRepository(digikey, args.out)
@@ -62,6 +61,7 @@ if 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")
@@ -75,43 +75,46 @@ parts = sorted(set(parts))
for q in parts:
p = q.query
- if repo.has_product(mpn = p if q.is_mpn else None, dpn = p if not q.is_mpn else None) and not args.force:
- print(color("Already have facts for {}".format(p), "white"))
+ if repo.has_product(mpn=p if q.is_mpn else None, dpn=p if not q.is_mpn else None) and not args.force:
+ log.info("Already have facts for {}".format(p))
continue
- print(color("Searching for {}".format(p), "white"))
+ log.info("Searching for {}".format(p))
response = client.search(p)
todos = []
if response.response_type == SearchResponseTypes.SINGLE:
p = response.products[0]
- print(color("Direct match mpn/dpn: {}/{}".format(p.mpn, p.part_number), "white"))
+ log.info("Direct match mpn/dpn: {}/{}".format(p.mpn, p.part_number))
on_product(p)
elif response.response_type == SearchResponseTypes.MANY:
- hits = [(mpn, list(products)) for mpn, products in groupby(sorted(response.products, key=lambda p: p.mpn), lambda p: p.mpn)]
+ hits = [(mpn, list(products)) for mpn, products in
+ groupby(sorted(response.products, key=lambda p: p.mpn), lambda p: p.mpn)]
if len(hits) == 1:
(mpn, products) = hits[0]
part_number = products[0].part_number
- print(color("Got many results, but they all point to the same part: {}. Will use {} for downloading attributes.".format(mpn, ", ".join([p.part_number for p in products])), "white"))
+ product_strings = ", ".join([p.part_number for p in products])
+ log.info("Got many results, but they all point to the same part: {}. "
+ "Will use {} for downloading attributes.".format(mpn, product_strings))
todos.append(part_number)
else:
for k, g in hits:
- print(color("Got many results with many parts: {}: {}".format(k, list(g)), "white"))
+ log.info("Got many results with many parts: {}: {}".format(k, list(g)))
on_product(list(g)[0])
elif response.response_type == SearchResponseTypes.TOO_MANY:
- print(color("Too many results ({}), select a category first".format(response.count), 'red'))
+ log.warn("Too many results ({}), select a category first".format(response.count))
elif response.response_type == SearchResponseTypes.NO_MATCHES:
- print(color("Part not found", "orange"))
+ log.warn("Part not found")
for part_number in todos:
response = client.search(part_number)
if response.response_type == SearchResponseTypes.SINGLE:
p = response.products[0]
- print(color("Downloaded {}".format(p.mpn), "white"))
+ log.info("Downloaded {}".format(p.mpn))
on_product(p)
else:
- print("Got response of type {} when really expecting a single product.".format(response.response_type))
+ log.warn("Got response of type {} when really expecting a single product.".format(response.response_type))