From 1666cb723a96d33e249057c1f5fdb28b59f01dba Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Sun, 1 Oct 2017 13:06:09 +0200 Subject: o Smarter searching in case of multiple hits. Look for the MPN we want and not just all products to find a unique one. --- src/ee/tools/digikey_download_facts.py | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/src/ee/tools/digikey_download_facts.py b/src/ee/tools/digikey_download_facts.py index c947b67..9842c40 100644 --- a/src/ee/tools/digikey_download_facts.py +++ b/src/ee/tools/digikey_download_facts.py @@ -54,10 +54,10 @@ def on_product(product: DigikeyProduct): repo.save(product) -parts = [] +queries = [] if args.part: - [parts.append(Query(True, p)) for p in args.part] + [queries.append(Query(True, p)) for p in args.part] if args.sch: from ee.kicad import read_schematic, to_bom @@ -66,14 +66,14 @@ if args.sch: for c in to_bom(sch): digikey = c.get_field("digikey") if digikey: - parts.append(Query(False, digikey.value)) + queries.append(Query(False, digikey.value)) mpn = c.get_field("mpn") if mpn: - parts.append(Query(True, mpn.value)) + queries.append(Query(True, mpn.value)) -parts = sorted(set(parts)) +queries = sorted(set(queries)) -for q in parts: +for q in queries: 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: log.info("Already have facts for {}".format(p)) @@ -89,16 +89,17 @@ for q in parts: 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)] + # A search for "FOO" might return products "FOO" and "FOOT" so we pick out the ones with the matching mpn/dpn. + if q.is_mpn: + viable_products = [p for p in response.products if p.mpn == q.query] + else: + viable_products = [p for p in response.products if p.part_number == q.query] - if len(hits) == 1: - (mpn, products) = hits[0] + if len(viable_products): + # Pick the first one, should be as good as any + part_number = viable_products[0].part_number - part_number = products[0].part_number - 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)) + log.info("Got many hits for term '{}', will use {} for downloading attributes.".format(q.query, part_number)) todos.append(part_number) else: for k, g in hits: -- cgit v1.2.3