From cf4c049a28c6718f1363c4e371ca141aaf679db6 Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Tue, 14 May 2019 23:36:04 +0200 Subject: digikey-search-parts: Logging output to file. --- src/ee/digikey/search_parts.py | 28 ++++++++++++++++++---------- src/ee/tools/digikey_search_parts.py | 5 ++++- src/ee/tools/templates/build.ninja.j2 | 2 +- 3 files changed, 23 insertions(+), 12 deletions(-) (limited to 'src') 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: diff --git a/src/ee/tools/digikey_search_parts.py b/src/ee/tools/digikey_search_parts.py index 28ccbb9..44da21e 100644 --- a/src/ee/tools/digikey_search_parts.py +++ b/src/ee/tools/digikey_search_parts.py @@ -26,4 +26,7 @@ cache_dir = project.cache_dir / "digikey" store_code = args.store -search_parts(Path(args.in_path), Path(args.out), cache_dir, store_code) +out_path = Path(args.out) +log_path = out_path.parent / (out_path.name.replace(".xml", ".rst")) + +search_parts(Path(args.in_path), out_path, log_path, cache_dir, store_code) diff --git a/src/ee/tools/templates/build.ninja.j2 b/src/ee/tools/templates/build.ninja.j2 index 47a099f..76da4cf 100644 --- a/src/ee/tools/templates/build.ninja.j2 +++ b/src/ee/tools/templates/build.ninja.j2 @@ -79,7 +79,7 @@ build ee/sch.xml: kicad-make-bom $sch build ee/{{ s }}/pn-part-search-list.xml: pn-part-search-list ee/sch.xml supplier = {{ s }} -build ee/{{ s }}/downloaded.xml: {{ s }}-search-parts ee/{{ s }}/pn-part-search-list.xml +build ee/{{ s }}/downloaded.xml | ee/{{ s }}/downloaded.rst: {{ s }}-search-parts ee/{{ s }}/pn-part-search-list.xml build ee/{{ s }}/parts.xml: {{ s }}-normalize-facts ee/{{ s }}/downloaded.xml -- cgit v1.2.3