diff options
author | Trygve Laugstøl <trygvis@inamo.no> | 2017-09-03 11:21:17 +0200 |
---|---|---|
committer | Trygve Laugstøl <trygvis@inamo.no> | 2017-09-03 11:21:17 +0200 |
commit | d59fb211556cd9b5a2bc028c5cf8a37b891cbfb3 (patch) | |
tree | 000b6bde185b8ff79efff97a3a46e0628bac92d7 /src/ee/tools | |
parent | c895e6c051cfda77a22b31367cf5c0bbedce4249 (diff) | |
download | ee-python-d59fb211556cd9b5a2bc028c5cf8a37b891cbfb3.tar.gz ee-python-d59fb211556cd9b5a2bc028c5cf8a37b891cbfb3.tar.bz2 ee-python-d59fb211556cd9b5a2bc028c5cf8a37b891cbfb3.tar.xz ee-python-d59fb211556cd9b5a2bc028c5cf8a37b891cbfb3.zip |
o Adding tools to download facts about parts from Digi-Key.
Diffstat (limited to 'src/ee/tools')
-rw-r--r-- | src/ee/tools/digikey_download_facts.py | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/src/ee/tools/digikey_download_facts.py b/src/ee/tools/digikey_download_facts.py new file mode 100644 index 0000000..f21f171 --- /dev/null +++ b/src/ee/tools/digikey_download_facts.py @@ -0,0 +1,59 @@ +from colors import color +import argparse +import sys +import ee.digikey as dk +import pandas +from itertools import * +import yaml +import os.path + +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", + dest="out", + action="store", + help="A directory to store fact files") + +parser.add_argument("--force", + dest="force", + action="store", + help="Always download fact even if there is a local file") + +args = parser.parse_args() + +digikey = dk.Digikey() +client = dk.DigikeyClient(digikey, on_download=lambda s: print(color(s, 'grey'))) + +def mpn_to_path(mpn): + return "{}/{}.yaml".format(args.out, mpn) + +def on_product(p): + y = p.to_yaml() + with open(mpn_to_path(p.mpn), "w") as f: + yaml.dump(y, f, encoding="utf-8", allow_unicode=True) + +for p in args.parts: + print(color("Searching for {}".format(p), "white")) + path = mpn_to_path(p) + + if os.path.isfile(path) and not args.force: + continue + + response = client.search(p) + + if not response: + print(color("Part not found", "orange")) + elif isinstance(response, dk.DigikeyProduct): + print(color("Found {}".format(response.mpn))) + on_product(response) + else: + for k, g in groupby(sorted(response.products), lambda p: p.mpn): + print(color("Found {}".format(k), "white")) + on_product(list(g)[0]) |