aboutsummaryrefslogtreecommitdiff
path: root/src/ee/digikey
diff options
context:
space:
mode:
authorTrygve Laugstøl <trygvis@inamo.no>2019-02-26 23:08:19 +0100
committerTrygve Laugstøl <trygvis@inamo.no>2019-02-26 23:08:19 +0100
commit97c8bb9db96e27051f8746865f657408263db0b8 (patch)
tree604cb6e9b87e79f43e54940adbc83520e89940fe /src/ee/digikey
parent80e0623913e87c6480049520590e424a831e0401 (diff)
downloadee-python-97c8bb9db96e27051f8746865f657408263db0b8.tar.gz
ee-python-97c8bb9db96e27051f8746865f657408263db0b8.tar.bz2
ee-python-97c8bb9db96e27051f8746865f657408263db0b8.tar.xz
ee-python-97c8bb9db96e27051f8746865f657408263db0b8.zip
o Creating a PartDb that manages a file system directory with one xml
file per part. o Switching xml-based code to use PartDb.
Diffstat (limited to 'src/ee/digikey')
-rw-r--r--src/ee/digikey/import_parts.py100
-rw-r--r--src/ee/digikey/search_parts.py (renamed from src/ee/digikey/refresh_parts.py)25
2 files changed, 11 insertions, 114 deletions
diff --git a/src/ee/digikey/import_parts.py b/src/ee/digikey/import_parts.py
deleted file mode 100644
index 748bbef..0000000
--- a/src/ee/digikey/import_parts.py
+++ /dev/null
@@ -1,100 +0,0 @@
-import os
-from typing import List, MutableMapping
-
-from ee.xml import bomFile
-from ee.xml.bom_file_utils import *
-from ee.xml.uris import DIGIKEY_URI
-
-__all__ = ["import_parts"]
-
-
-class Entry(object):
- def __init__(self, new: bool, part: bomFile.Part):
- self.new = new
- self.part = part
-
- self.pn = find_pn(part)
- self.dpn = find_dpn(part, DIGIKEY_URI)
-
-
-def import_parts(in_path, out_path):
- print("in: {}, out: {}".format(in_path, out_path))
-
- in_file = bomFile.parse(in_path, True)
- if in_file.partsProp is None:
- in_file.partsProp = bomFile.PartList()
- in_part_list = in_file.partsProp.partProp # type: List[bomFile.Part]
-
- print("in file: {} parts".format(len(in_part_list)))
-
- if os.path.isfile(out_path):
- out_file = bomFile.parse(out_path, True)
- else:
- out_file = bomFile.BomFile()
-
- if out_file.partsProp is None:
- out_file.partsProp = bomFile.PartList()
- out_part_list = out_file.partsProp.partProp # type: List[bomFile.Part]
- print("out file: {} parts".format(len(out_part_list)))
-
- existing_parts = [] # type: List[Entry]
- pn_index = {} # type: MutableMapping[str, Entry]
- dpn_index = {} # type: MutableMapping[str, Entry]
- new_entry_added = 0
-
- def add_entry(e: Entry):
- existing_parts.append(e)
- pn_index[e.pn] = e
- dpn_index[e.dpn] = e
-
- if e.new:
- out_part_list.append(e.part)
- nonlocal new_entry_added
- new_entry_added = new_entry_added + 1
-
- print("len(out_part_list)={}".format(len(out_part_list)))
- for part in out_part_list: # type: bomFile.Part
- entry = Entry(False, part)
- add_entry(entry)
-
- print("loaded {} existing parts".format(len(existing_parts)))
-
- for part in in_part_list:
- pn_value = find_pn(part)
-
- if pn_value is None:
- print("Skipping part with no part number: id={}".format(part.idProp))
- continue
-
- entry = pn_index.get(pn_value)
-
- if entry is not None:
- print("Already imported pn_value={}".format(pn_value))
- continue
-
- print("Importing {}".format(pn_value))
-
- pns = bomFile.PartNumberList()
-
- if pn_value is not None:
- pns.add_part_number(bomFile.PartNumber(value=pn_value))
-
- dpn_value = find_dpn(part, DIGIKEY_URI)
- if dpn_value is not None:
- pns.add_part_number(bomFile.PartNumber(value=dpn_value, distributor=DIGIKEY_URI))
-
- if len(pns.part_numberProp) == 0:
- continue
-
- new_part = bomFile.Part(part_numbers=pns)
- entry = Entry(True, new_part)
- add_entry(entry)
-
- if new_entry_added:
- print("Imported {} entries".format(new_entry_added))
- tmp_path = out_path + ".tmp"
- with open(tmp_path, "w") as f:
- out_file.export(f, 0, name_="bom-file")
- os.rename(tmp_path, out_path)
- else:
- print("no new entries")
diff --git a/src/ee/digikey/refresh_parts.py b/src/ee/digikey/search_parts.py
index 87edf2f..62bb4ee 100644
--- a/src/ee/digikey/refresh_parts.py
+++ b/src/ee/digikey/search_parts.py
@@ -1,13 +1,13 @@
-import os
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 bomFile, bom_file_utils
from ee.xml.bomFile import DigikeyDistributorInfo
from ee.xml.uris import DIGIKEY_URI
-__all__ = ["refresh_parts"]
+__all__ = ["search_parts"]
def resolved(di: DigikeyDistributorInfo, part: bomFile.Part, p: DigikeyProduct):
@@ -23,17 +23,16 @@ def resolved(di: DigikeyDistributorInfo, part: bomFile.Part, p: DigikeyProduct):
facts.append(bomFile.Fact(key=a.attribute_type.id, label=a.attribute_type.label, value=a.value))
-def refresh_parts(in_path: Path, out_path: Path, cache_dir: Path, force_refresh: bool):
- print("in: {}, out: {}".format(in_path, out_path))
+def search_parts(in_dir: Path, out_dir: Path, cache_dir: Path, force_refresh: bool):
+ print("in: {}, out: {}".format(in_dir, out_dir))
- in_file = bomFile.parse(str(in_path), True)
- if in_file.partsProp is None:
- in_file.partsProp = bomFile.PartList()
+ in_db = load_db(in_dir)
+ out_parts = PartDb()
parser = DigikeyParser(Digikey())
client = DigikeyClient(cache_dir)
- for part in in_file.partsProp.partProp: # type: bomFile.Part
+ for part in in_db.iterparts():
dpn = bom_file_utils.find_dpn(part, DIGIKEY_URI)
mpn = bom_file_utils.find_pn(part)
@@ -89,9 +88,7 @@ def refresh_parts(in_path: Path, out_path: Path, cache_dir: Path, force_refresh:
elif response.response_type == SearchResponseTypes.NO_MATCHES:
di.stateProp = "not-found"
- out_path = in_path
- out_file = in_file
- tmp_path = str(out_path) + ".tmp"
- with open(tmp_path, "w") as f:
- out_file.export(f, 0, name_="bom-file")
- os.rename(tmp_path, str(out_path))
+ out_parts.add_entry(part, True)
+
+ print("Saving {} work parts".format(out_parts.size()))
+ save_db(out_dir, out_parts)