aboutsummaryrefslogtreecommitdiff
path: root/src/ee/digikey
diff options
context:
space:
mode:
authorTrygve Laugstøl <trygvis@inamo.no>2019-05-14 23:36:04 +0200
committerTrygve Laugstøl <trygvis@inamo.no>2019-05-14 23:36:04 +0200
commitcf4c049a28c6718f1363c4e371ca141aaf679db6 (patch)
tree2bbb6c640a8b5529e52a2398029fe4715a225bc7 /src/ee/digikey
parent66e4ca88a41dccdf6bc537929ea2bd3beb2001a3 (diff)
downloadee-python-cf4c049a28c6718f1363c4e371ca141aaf679db6.tar.gz
ee-python-cf4c049a28c6718f1363c4e371ca141aaf679db6.tar.bz2
ee-python-cf4c049a28c6718f1363c4e371ca141aaf679db6.tar.xz
ee-python-cf4c049a28c6718f1363c4e371ca141aaf679db6.zip
digikey-search-parts: Logging output to file.
Diffstat (limited to 'src/ee/digikey')
-rw-r--r--src/ee/digikey/search_parts.py28
1 files changed, 18 insertions, 10 deletions
diff --git a/src/ee/digikey/search_parts.py b/src/ee/digikey/search_parts.py
index 5d6a17a..09f682d 100644
--- a/src/ee/digikey/search_parts.py
+++ b/src/ee/digikey/search_parts.py
@@ -49,7 +49,13 @@ def resolved(supplier, p: DigikeyProduct) -> Part:
return part
-def search_parts(in_path: Path, out_path: Path, cache_dir: Path, store_code, print_result=True):
+def search_parts(in_path: Path, out_path: Path, log_path: Path, cache_dir: Path, store_code):
+ print_result = True
+ with log_path.open("w") as log:
+ run_search_parts(in_path, out_path, log, cache_dir, store_code, print_result)
+
+
+def run_search_parts(in_path: Path, out_path: Path, log, cache_dir: Path, store_code, print_result):
in_db = load_db(in_path)
store = DigikeyStore.from_store_code(store_code)
@@ -59,13 +65,14 @@ def search_parts(in_path: Path, out_path: Path, cache_dir: Path, store_code, pri
out_parts: ObjDb[Part] = ObjDb[Part]()
uri_idx = out_parts.add_unique_index("uri", lambda p: p.uri)
- pn_idx = out_parts.add_index("pn", lambda p: [pn.value for pn in p.get_part_references()], multiple=True)
- spn_idx = out_parts.add_index("spn", lambda p: [pn.value for pn in p.get_spns()], multiple=True)
+ out_parts.add_index("pn", lambda p: [pn.value for pn in p.get_part_references()], multiple=True)
+ out_parts.add_index("spn", lambda p: [pn.value for pn in p.get_spns()], multiple=True)
for xml in in_db.iterparts():
- if xml.supplierProp is not None and xml.supplierProp != store.url:
- continue
-
part = Part(xml)
+ print("Searching for {}".format(part.printable_reference), file=log)
+
+ if xml.supplierProp is not None and xml.supplierProp != store.url:
+ assert False, "Something is fishy" # Not sure why I made this rule
dpn = part.get_only_spn()
mpn = part.get_only_mpn()
@@ -80,8 +87,7 @@ def search_parts(in_path: Path, out_path: Path, cache_dir: Path, store_code, pri
is_mpn = True
if query is None:
- # TODO: use schematic reference
- print("could not find pn or dpn: part.uri={}".format(xml.uriProp))
+ print("Could not find pn or spn to search by", file=log)
continue
out_part = None
@@ -92,6 +98,7 @@ def search_parts(in_path: Path, out_path: Path, cache_dir: Path, store_code, pri
if response.response_type == SearchResponseTypes.SINGLE:
out_part = resolved(store.url, response.products[0])
+ result = "found"
elif response.response_type == SearchResponseTypes.MANY:
# find those with an exact match. Digikey uses a prefix search so a query for "FOO" will return "FOO"
@@ -109,6 +116,7 @@ def search_parts(in_path: Path, out_path: Path, cache_dir: Path, store_code, pri
response = parser.parse_string(client.search(dpn))
if response.response_type == SearchResponseTypes.SINGLE:
out_part = resolved(store.url, response.products[0])
+ result = "found"
else:
result = "many"
@@ -121,8 +129,8 @@ def search_parts(in_path: Path, out_path: Path, cache_dir: Path, store_code, pri
if out_part.uri not in uri_idx:
out_parts.add(out_part)
- if print_result and result:
- print("{}: {}".format(query, result))
+ print("{}: {}".format(query, result), file=log)
+ print("", file=log)
part_db = PartDb()
for part in out_parts: