diff options
author | Trygve Laugstøl <trygvis@inamo.no> | 2019-02-28 08:26:52 +0100 |
---|---|---|
committer | Trygve Laugstøl <trygvis@inamo.no> | 2019-02-28 08:26:52 +0100 |
commit | 650b4ad3774c4cc454a02073808d981f6873036d (patch) | |
tree | 8644b59ba7bc716538c8039df150996766f70a5b | |
parent | 002aa1d05d09ddb6a7e116ff9b0f7fed33f4552d (diff) | |
download | ee-python-650b4ad3774c4cc454a02073808d981f6873036d.tar.gz ee-python-650b4ad3774c4cc454a02073808d981f6873036d.tar.bz2 ee-python-650b4ad3774c4cc454a02073808d981f6873036d.tar.xz ee-python-650b4ad3774c4cc454a02073808d981f6873036d.zip |
o Create a new part when storing result, use Digikey's PN as id.
-rw-r--r-- | src/ee/digikey/search_parts.py | 76 |
1 files changed, 42 insertions, 34 deletions
diff --git a/src/ee/digikey/search_parts.py b/src/ee/digikey/search_parts.py index cad611b..9c8eb74 100644 --- a/src/ee/digikey/search_parts.py +++ b/src/ee/digikey/search_parts.py @@ -9,17 +9,26 @@ from ee.xml.uris import DIGIKEY_URI, make_digikey_fact_key __all__ = ["search_parts"] -def resolved(di: bomFile.DistributorInfo, part: bomFile.Part, p: DigikeyProduct): - di.stateProp = "resolved" - - # Remove the old list - part.factsProp = bomFile.FactList() +def resolved(p: DigikeyProduct) -> bomFile.Part: + part = bomFile.Part(id=p.part_number, + distributor_info=bomFile.DistributorInfo(), + facts=bomFile.FactList(), + part_numbers=bomFile.PartNumberList()) + part.distributor_infoProp.stateProp = "resolved" + + part_numbers = part.part_numbersProp.part_numberProp + + part_numbers.append(bomFile.PartNumber(value=p.part_number, distributor=DIGIKEY_URI)) + if p.mpn: + part_numbers.append(bomFile.PartNumber(value=p.mpn)) facts: List[bomFile.Fact] = part.factsProp.factProp for a in p.attributes: key = make_digikey_fact_key(a.attribute_type.id) facts.append(bomFile.Fact(key=key, label=a.attribute_type.label, value=a.value)) + return part + def search_parts(in_dir: Path, out_dir: Path, cache_dir: Path, force_refresh: bool): print("in: {}, out: {}".format(in_dir, out_dir)) @@ -47,44 +56,43 @@ def search_parts(in_dir: Path, out_dir: Path, cache_dir: Path, force_refresh: bo print("could not find pn or dpn: part.id={}".format(part.idProp)) continue - di = part.distributor_infoProp # type: bomFile.DistributorInfo + out_id = query + out_part = bomFile.Part(id=out_id, + distributor_info=bomFile.DistributorInfo(), + part_numbers=part.part_numbersProp) + di = out_part.distributor_infoProp - if di is None: - di = bomFile.DistributorInfo() - part.distributor_infoProp = di + text = client.search(query) + response = parser.parse_string(text) - if force_refresh or di.stateProp != "resolved": - text = client.search(query) - response = parser.parse_string(text) + if response.response_type == SearchResponseTypes.SINGLE: + out_part = resolved(response.products[0]) + elif response.response_type == SearchResponseTypes.MANY: - if response.response_type == SearchResponseTypes.SINGLE: - resolved(di, part, response.products[0]) - 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" + # and "FOOT". + def get_field(p): + return p.mpn if is_mpn else p.part_number - # find those with an exact match. Digikey uses a prefix search so a query for "FOO" will return "FOO" - # and "FOOT". - def get_field(p): - return p.mpn if is_mpn else p.part_number + filtered_products = [p for p in response.products if get_field(p) == query] - filtered_products = [p for p in response.products if get_field(p) == query] + if len(filtered_products) == 0: + di.stateProp = "not-found" + else: + dpn = sorted(filtered_products, key=lambda p: p.part_number)[0].part_number - if len(filtered_products) == 0: - di.stateProp = "not-found" + response = parser.parse_string(client.search(dpn)) + if response.response_type == SearchResponseTypes.SINGLE: + out_part = resolved(response.products[0]) else: - dpn = sorted(filtered_products, key=lambda p: p.part_number)[0].part_number + di.stateProp = "many" - response = parser.parse_string(client.search(dpn)) - if response.response_type == SearchResponseTypes.SINGLE: - resolved(di, part, response.products[0]) - else: - di.stateProp = "many" - - elif response.response_type == SearchResponseTypes.TOO_MANY: - di.stateProp = "too-many" - elif response.response_type == SearchResponseTypes.NO_MATCHES: - di.stateProp = "not-found" + elif response.response_type == SearchResponseTypes.TOO_MANY: + di.stateProp = "too-many" + elif response.response_type == SearchResponseTypes.NO_MATCHES: + di.stateProp = "not-found" - out_parts.add_entry(part, True) + out_parts.add_entry(out_part, True) print("Saving {} work parts".format(out_parts.size())) save_db(out_dir, out_parts) |