aboutsummaryrefslogtreecommitdiff
path: root/src/ee/digikey/search_parts.py
diff options
context:
space:
mode:
Diffstat (limited to 'src/ee/digikey/search_parts.py')
-rw-r--r--src/ee/digikey/search_parts.py60
1 files changed, 32 insertions, 28 deletions
diff --git a/src/ee/digikey/search_parts.py b/src/ee/digikey/search_parts.py
index 358266a..7d19c5b 100644
--- a/src/ee/digikey/search_parts.py
+++ b/src/ee/digikey/search_parts.py
@@ -2,47 +2,46 @@ from pathlib import Path
from typing import List
from ee.digikey import Digikey, DigikeyParser, DigikeyClient, SearchResponseTypes, DigikeyProduct
-from ee.part import PartDb, load_db, save_db
-from ee.xml import types, bom_file_utils
+from ee.part import PartDb, load_db, save_db, Part
+from ee.xml import types
from ee.xml.uris import DIGIKEY_URI, make_digikey_fact_key
__all__ = ["search_parts"]
-def resolved(p: DigikeyProduct) -> types.Part:
+def resolved(p: DigikeyProduct) -> Part:
# TODO: fix uri
- part = types.Part(uri="https://digikey.com/pn#{}".format(p.part_number),
- distributor_info=types.DistributorInfo(),
- links=types.LinkList(),
- facts=types.FactList(),
- references=types.ReferencesList())
- part.distributor_infoProp.stateProp = "resolved"
- links = part.linksProp.link
+ xml = types.Part(uri="https://digikey.com/pn#{}".format(p.part_number),
+ supplier=DIGIKEY_URI,
+ description=p.description,
+ distributor_info=types.DistributorInfo(state="resolved"),
+ links=types.LinkList(),
+ facts=types.FactList(),
+ references=types.ReferencesList())
+ part = Part(xml)
if p.url:
- links.append(types.Link(url=p.url, relation="canonical", media_type="text/html"))
+ part.get_links().append(types.Link(url=p.url, relation="canonical", media_type="text/html"))
for d in p.documents:
title = "{}: {}".format(d.kind, d.title)
- links.append(types.Link(url=d.url, relation="http://purl.org/ee/link-relation#documentation",
- media_type="text/html", title=title))
+ part.get_links().append(types.Link(url=d.url, relation="http://purl.org/ee/link-relation#documentation",
+ media_type="text/html", title=title))
- supplier_part_numbers = part.referencesProp.supplier_part_numberProp
- supplier_part_numbers.append(types.SupplierPartNumber(value=p.part_number, supplier=DIGIKEY_URI))
+ part.add_spn(p.part_number)
- part_numbers = part.referencesProp.part_numberProp
if p.mpn:
- part_numbers.append(types.PartNumber(value=p.mpn))
- facts: List[types.Fact] = part.factsProp.factProp
+ part.add_mpn(p.mpn)
+ facts: List[types.Fact] = xml.factsProp.factProp
for a in p.attributes:
key = make_digikey_fact_key(a.attribute_type.id)
facts.append(types.Fact(key=key, label=a.attribute_type.label, value=a.value))
if len(p.price_breaks):
- part.price_breaksProp = types.PriceBreakList()
+ xml.price_breaksProp = types.PriceBreakList()
- price_breaks: List[types.PriceBreak] = part.price_breaksProp.price_break
+ price_breaks: List[types.PriceBreak] = xml.price_breaksProp.price_break
for pb in p.price_breaks:
amount = types.Amount(value=str(pb.per_piece_price.amount), currency=pb.per_piece_price.currency)
price_breaks.append(types.PriceBreak(pb.quantity, amount=amount))
@@ -57,29 +56,34 @@ def search_parts(in_path: Path, out_path: Path, cache_dir: Path):
parser = DigikeyParser(Digikey())
client = DigikeyClient(cache_dir)
- for part in in_db.iterparts():
- dpn = next((p.valueProp for p in bom_file_utils.supplier_part_numbers(part)
- if p.supplierProp == DIGIKEY_URI), None)
- mpn = next((p.valueProp for p in bom_file_utils.part_numbers(part)), None)
+ for xml in in_db.iterparts():
+ if xml.supplierProp is not None and xml.supplierProp != DIGIKEY_URI:
+ continue
+
+ part = Part(xml)
+
+ dpn = part.get_only_spn()
+ mpn = part.get_only_mpn()
is_mpn = query = None
if dpn is not None:
- query = dpn
+ query = dpn.valueProp
is_mpn = False
elif mpn is not None:
- query = mpn
+ query = mpn.valueProp
is_mpn = True
if query is None:
# TODO: use schematic reference
- print("could not find pn or dpn: part.uri={}".format(part.uriProp))
+ print("could not find pn or dpn: part.uri={}".format(xml.uriProp))
continue
out_id = query
out_part = types.Part(uri=out_id,
distributor_info=types.DistributorInfo(),
- references=part.referencesProp)
+ supplier=DIGIKEY_URI,
+ references=xml.referencesProp)
di = out_part.distributor_infoProp
text = client.search(query)