aboutsummaryrefslogtreecommitdiff
path: root/src/ee/digikey
diff options
context:
space:
mode:
authorTrygve Laugstøl <trygvis@inamo.no>2019-05-14 21:29:04 +0200
committerTrygve Laugstøl <trygvis@inamo.no>2019-05-14 21:29:04 +0200
commitb9da2d88e21e5edda04e928352b45d203147be26 (patch)
tree2b62b862abe546d87e016a45e2df74d3ea7ba7a4 /src/ee/digikey
parentee62dd01720c8481599a717d067014164af7e096 (diff)
downloadee-python-b9da2d88e21e5edda04e928352b45d203147be26.tar.gz
ee-python-b9da2d88e21e5edda04e928352b45d203147be26.tar.bz2
ee-python-b9da2d88e21e5edda04e928352b45d203147be26.tar.xz
ee-python-b9da2d88e21e5edda04e928352b45d203147be26.zip
ee.xsd:
o Removing distributor info, wasn't useful. o Removing part type, using a fact instead. part-search-list: o Putting in some smart rules about values for parts. Might be too smart for its own good. o Removing duplication checking, that is up to the searcher to decide.
Diffstat (limited to 'src/ee/digikey')
-rw-r--r--src/ee/digikey/search_parts.py38
1 files changed, 22 insertions, 16 deletions
diff --git a/src/ee/digikey/search_parts.py b/src/ee/digikey/search_parts.py
index c248831..5d6a17a 100644
--- a/src/ee/digikey/search_parts.py
+++ b/src/ee/digikey/search_parts.py
@@ -1,6 +1,7 @@
from pathlib import Path
from typing import List
+from ee.db import ObjDb
from ee.digikey import Digikey, DigikeyParser, DigikeyClient, SearchResponseTypes, DigikeyProduct, DigikeyStore
from ee.part import PartDb, load_db, save_db, Part
from ee.xml import types
@@ -14,7 +15,6 @@ def resolved(supplier, p: DigikeyProduct) -> Part:
xml = types.Part(uri="https://digikey.com/pn#{}".format(p.part_number),
supplier=supplier,
description=p.description,
- distributor_info=types.DistributorInfo(state="resolved"),
links=types.LinkList(),
facts=types.FactList(),
references=types.ReferenceList())
@@ -49,15 +49,18 @@ def resolved(supplier, p: DigikeyProduct) -> Part:
return part
-def search_parts(in_path: Path, out_path: Path, cache_dir: Path, store_code):
+def search_parts(in_path: Path, out_path: Path, cache_dir: Path, store_code, print_result=True):
in_db = load_db(in_path)
- out_parts = PartDb()
store = DigikeyStore.from_store_code(store_code)
parser = DigikeyParser(Digikey())
client = DigikeyClient(store.products_url, cache_dir)
+ 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)
for xml in in_db.iterparts():
if xml.supplierProp is not None and xml.supplierProp != store.url:
continue
@@ -81,12 +84,8 @@ def search_parts(in_path: Path, out_path: Path, cache_dir: Path, store_code):
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(),
- supplier=store.url,
- references=xml.referencesProp)
- di = out_part.distributor_infoProp
+ out_part = None
+ result = None
text = client.search(query)
response = parser.parse_string(text)
@@ -103,7 +102,7 @@ def search_parts(in_path: Path, out_path: Path, cache_dir: Path, store_code):
filtered_products = [p for p in response.products if get_field(p) == query]
if len(filtered_products) == 0:
- di.stateProp = "not-found"
+ result = "not-found"
else:
dpn = sorted(filtered_products, key=lambda p: p.part_number)[0].part_number
@@ -111,14 +110,21 @@ def search_parts(in_path: Path, out_path: Path, cache_dir: Path, store_code):
if response.response_type == SearchResponseTypes.SINGLE:
out_part = resolved(store.url, response.products[0])
else:
- di.stateProp = "many"
+ result = "many"
elif response.response_type == SearchResponseTypes.TOO_MANY:
- di.stateProp = "too-many"
+ result = "too-many"
elif response.response_type == SearchResponseTypes.NO_MATCHES:
- di.stateProp = "not-found"
+ result = "not-found"
- out_parts.add_entry(out_part, True)
+ if out_part:
+ if out_part.uri not in uri_idx:
+ out_parts.add(out_part)
- print("Saving {} work parts".format(out_parts.size()))
- save_db(out_path, out_parts, sort=True)
+ if print_result and result:
+ print("{}: {}".format(query, result))
+
+ part_db = PartDb()
+ for part in out_parts:
+ part_db.add_entry(part, True)
+ save_db(out_path, part_db, sort=True)