From 37e4be24129b6980e3e9fced7345d4a596af3d58 Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Sun, 14 Apr 2019 19:41:30 +0200 Subject: digikey: o More flexibility, making room for the multiple digikey stores. o Removing URLs from core code. new tools: split_parts_by_supplier. --- src/ee/tools/digikey_download_facts.py | 8 +++- src/ee/tools/digikey_search_parts.py | 8 +++- src/ee/tools/split_parts_by_supplier.py | 69 +++++++++++++++++++++++++++++++++ src/ee/tools/templates/build.ninja.j2 | 8 ++-- 4 files changed, 86 insertions(+), 7 deletions(-) create mode 100644 src/ee/tools/split_parts_by_supplier.py (limited to 'src/ee/tools') diff --git a/src/ee/tools/digikey_download_facts.py b/src/ee/tools/digikey_download_facts.py index 3ab8551..5cfdba7 100644 --- a/src/ee/tools/digikey_download_facts.py +++ b/src/ee/tools/digikey_download_facts.py @@ -5,6 +5,9 @@ import ee.digikey as dk from ee.digikey import SearchResponseTypes, DigikeyProduct from ee.tools import log +if True: + raise Exception("This module is deprecated.") + @total_ordering class Query(object): @@ -45,7 +48,7 @@ parser.add_argument("--force", args = parser.parse_args() digikey = dk.Digikey() -client = dk.DigikeyClient(on_download=log.debug) +client = dk.DigikeyClient("https://www.digikey.com/products/en", on_download=log.debug) parser = dk.DigikeyParser(digikey) repo = dk.DigikeyRepository(digikey, args.out) @@ -99,7 +102,8 @@ for q in queries: # Pick the first one, should be as good as any part_number = sorted(viable_products, key=lambda p: p.part_number)[0].part_number - log.info("Got many hits for term '{}', will use {} for downloading attributes.".format(q.query, part_number)) + log.info( + "Got many hits for term '{}', will use {} for downloading attributes.".format(q.query, part_number)) todos.append(part_number) else: log.warn("Got many results: {}".format(", ".join([p.part_number for p in response.products]))) diff --git a/src/ee/tools/digikey_search_parts.py b/src/ee/tools/digikey_search_parts.py index 6cf104d..28ccbb9 100644 --- a/src/ee/tools/digikey_search_parts.py +++ b/src/ee/tools/digikey_search_parts.py @@ -15,9 +15,15 @@ parser.add_argument("--out", required=True, metavar="PART DB") +parser.add_argument("--store", + default="us", + metavar="STORE CODE") + args = parser.parse_args() project = Project.load() cache_dir = project.cache_dir / "digikey" -search_parts(Path(args.in_path), Path(args.out), cache_dir) +store_code = args.store + +search_parts(Path(args.in_path), Path(args.out), cache_dir, store_code) diff --git a/src/ee/tools/split_parts_by_supplier.py b/src/ee/tools/split_parts_by_supplier.py new file mode 100644 index 0000000..1ebf094 --- /dev/null +++ b/src/ee/tools/split_parts_by_supplier.py @@ -0,0 +1,69 @@ +import argparse +from pathlib import Path +from typing import List + +from ee.db import ObjDb +from ee.part import Part, load_db, save_db, PartDb +from ee.project import Project + + +class OrderPart(object): + def __init__(self, order_part: Part, part: Part): + self.order_part = order_part + self.part = part + + +def uri_fn(part: Part): + return part.uri + + +def split_parts_by_supplier(project: Project, order_file: Path, part_dbs: List[Path], out_dir: Path): + parts: ObjDb[Part] = ObjDb[Part]() + part_by_uri = parts.add_unique_index("uri", uri_fn) + + for part_db in part_dbs: + for xml in load_db(part_db).iterparts(): + parts.add(Part(xml)) + + order_parts: ObjDb[OrderPart] = ObjDb() + supplier_idx = order_parts.add_index("supplier", lambda op: op.part.supplier) + for xml in load_db(order_file).iterparts(): + order_part = Part(xml) + part = part_by_uri.get_single(order_part.get_exactly_one_part_reference().part_uriProp) + order_parts.add(OrderPart(order_part, part)) + + for supplier, parts_for_supplier in supplier_idx.items(): + desc = project.get_supplier_by_uri(supplier) + + print("{}: {}".format(desc.name, len(parts))) + # supplier_db: ObjDb[Part] = ObjDb[Part]() + # supplier_db.add_unique_index("uri", uri_fn) + + supplier_descriptor = project.get_supplier_by_uri(supplier) + + db = PartDb() + for part_for_supplier in parts_for_supplier: + db.add_entry(part_for_supplier.part.underlying, False) + + save_db(out_dir / "{}.xml".format(supplier_descriptor.key), db, sort=True) + + +parser = argparse.ArgumentParser() + +parser.add_argument("--parts", + required=True, + metavar="PART DB") + +parser.add_argument("--part-db", + nargs="*", + required=True, + metavar="PART DB") + +parser.add_argument("--out-dir", + metavar="DIR FOR PART DBS") + +args = parser.parse_args() + +part_db_dirs = [Path(part_db) for part_db in args.part_db] + +split_parts_by_supplier(Project.load(), Path(args.parts), part_db_dirs, Path(args.out_dir)) diff --git a/src/ee/tools/templates/build.ninja.j2 b/src/ee/tools/templates/build.ninja.j2 index ea979da..d62f350 100644 --- a/src/ee/tools/templates/build.ninja.j2 +++ b/src/ee/tools/templates/build.ninja.j2 @@ -45,9 +45,9 @@ rule create-bom description = create-bom command = $ee create-bom --schematic $schematic --part-db $part_dbs --out $out $strategy -rule export-order - description = export-order - command = $ee export-order --order $order $part_dbs --out-dir $out_dir +rule split-parts-by-supplier + description = split-parts-by-supplier + command = $ee split-parts-by-supplier --parts $order $part_dbs --out-dir $out_dir rule import-parts-yaml description = import-parts-yaml $in @@ -90,7 +90,7 @@ build ee/bom.xml | $report_dir/bom.rst: create-bom ee/sch.xml {%- for p in part_ strategy = --strategy {{ project.cfg["create-bom"]["strategy"] }} {%- endif %} -build ee/orders/index.xml: export-order ee/bom.xml {%- for p in part_dbs %} {{ p }}.xml{% endfor %} +build ee/orders/index.xml: split-parts-by-supplier ee/bom.xml {%- for p in part_dbs %} {{ p }}.xml{% endfor %} order = ee/bom.xml part_dbs ={%- for p in part_dbs %} --part-db {{ p }}.xml{% endfor %} out_dir = ee/orders -- cgit v1.2.3