aboutsummaryrefslogtreecommitdiff
path: root/src/ee/tools
diff options
context:
space:
mode:
authorTrygve Laugstøl <trygvis@inamo.no>2017-09-10 12:32:49 +0200
committerTrygve Laugstøl <trygvis@inamo.no>2017-09-10 12:32:49 +0200
commit090a2703703877bd150aae637031c5d7dcba2df4 (patch)
tree3d498820503e1ab3c7186c2839a661a76083550b /src/ee/tools
parentc96bc5755a68d8e317d96be31b330cc0b626c194 (diff)
downloadee-python-090a2703703877bd150aae637031c5d7dcba2df4.tar.gz
ee-python-090a2703703877bd150aae637031c5d7dcba2df4.tar.bz2
ee-python-090a2703703877bd150aae637031c5d7dcba2df4.tar.xz
ee-python-090a2703703877bd150aae637031c5d7dcba2df4.zip
setup.py: Adding install_requires.
digikey: Updating tests. Making sure the directory exist before writing facts. Stop recursing into new searches when a search returns multiple hits. Let the frontends do that.
Diffstat (limited to 'src/ee/tools')
-rw-r--r--src/ee/tools/digikey_download_facts.py30
1 files changed, 24 insertions, 6 deletions
diff --git a/src/ee/tools/digikey_download_facts.py b/src/ee/tools/digikey_download_facts.py
index a1e242b..08a8029 100644
--- a/src/ee/tools/digikey_download_facts.py
+++ b/src/ee/tools/digikey_download_facts.py
@@ -37,9 +37,16 @@ def mpn_to_path(mpn):
return "{}/{}.yaml".format(args.out, mpn)
-def on_product(p: DigikeyProduct):
- y = p.to_yaml()
- with open(mpn_to_path(p.mpn), "w") as f:
+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)
+
+ with open(filename, "w") as f:
yaml.dump(y, f, encoding="utf-8", allow_unicode=True)
@@ -54,11 +61,22 @@ for p in args.parts:
if response.response_type == SearchResponseTypes.SINGLE:
p = response.products[0]
- print(color("Found {}".format(p.mpn), "white"))
+ print(color("Direct match {}".format(p.mpn), "white"))
on_product(p)
elif response.response_type == SearchResponseTypes.MANY:
- for k, g in groupby(sorted(response.products), lambda p: p.mpn):
- print(color("Found {}".format(k), "white"))
+ hits = list(groupby(sorted(response.products), lambda p: p.mpn))
+
+ if len(hits) == 1:
+ (mpn, products) = hits[0]
+ products = list(products)
+
+ if len(products) == 1:
+ print(color("Got many results, but they all point to the same part: {}".format(mpn), "white"))
+ on_product(products[0])
+ continue
+
+ for k, g in hits:
+ print(color("Got many results with many parts: {}: {}".format(k, list(g)), "white"))
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'))