aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTrygve Laugstøl <trygvis@inamo.no>2019-02-27 16:09:53 +0100
committerTrygve Laugstøl <trygvis@inamo.no>2019-02-27 16:09:53 +0100
commit0ce5c1e6dcea6643cf72de885c0aca52bf4ac28c (patch)
treeef9109b762a46079ce91aa3376f727aa76abf8b8
parentf14a40a4f80275daceeb73f944e1aa1f84d16c79 (diff)
downloadee-python-0ce5c1e6dcea6643cf72de885c0aca52bf4ac28c.tar.gz
ee-python-0ce5c1e6dcea6643cf72de885c0aca52bf4ac28c.tar.bz2
ee-python-0ce5c1e6dcea6643cf72de885c0aca52bf4ac28c.tar.xz
ee-python-0ce5c1e6dcea6643cf72de885c0aca52bf4ac28c.zip
o Always sorting index.xml for better git diffs.
o Normalizing more Digikey facts. o When creating distributor search lists, create a new plain <part> with containing only the part numbers.
-rw-r--r--src/ee/__init__.py2
-rw-r--r--src/ee/digikey/normalize_facts.py150
-rw-r--r--src/ee/part/__init__.py5
-rw-r--r--src/ee/part/create_distributor_search_list.py7
-rw-r--r--src/ee/tools/digikey_normalize_facts.py19
5 files changed, 179 insertions, 4 deletions
diff --git a/src/ee/__init__.py b/src/ee/__init__.py
index c7674c4..8a128f6 100644
--- a/src/ee/__init__.py
+++ b/src/ee/__init__.py
@@ -39,7 +39,7 @@ class EeVal(object):
r = re.compile(
"([0-9]+\\.[0-9]+|\\.?[0-9]+|[0-9]+\\.?) *([" + "".join(exponents.keys()) + "]?) *(" + "|".join(units) + "?)")
- def __init__(self, s, value=None, exp=None, unit=None):
+ def __init__(self, s=None, value=None, exp=None, unit=None):
# This is where I regret having a string in the API at once.
if s:
val = EeVal.parse(s)
diff --git a/src/ee/digikey/normalize_facts.py b/src/ee/digikey/normalize_facts.py
new file mode 100644
index 0000000..1ddf78c
--- /dev/null
+++ b/src/ee/digikey/normalize_facts.py
@@ -0,0 +1,150 @@
+import re
+from pathlib import Path
+from typing import List, Tuple, Union
+
+from ee import EeVal, EeException
+from ee.part import PartDb, load_db, save_db
+from ee.xml import bomFile, uris
+
+__all__ = ["normalize_facts"]
+
+
+# TODO: this should be moved to a generic normalizer
+def handle_tolerance(tolerance: bomFile.Fact, capacitance: bomFile.Fact) -> List[bomFile.Fact]:
+ cap_value = float(EeVal.parse(capacitance.valueProp))
+
+ s = tolerance.valueProp
+ print("tolerance: {}".format(s))
+ is_plus_minus = s.startswith("±")
+ s = s[1:] if is_plus_minus else s
+
+ is_percent = s.endswith("%")
+
+ is_farad = s.endswith("F")
+
+ print("tolerance={}, is_plus_minus={}, is_farad={}, is_percent={}".format(
+ tolerance.valueProp, is_plus_minus, is_farad, is_percent))
+ print("capacitance={}".format(EeVal.parse(capacitance.valueProp)))
+
+ low_pct = low_value = None
+ high_pct = high_value = None
+ facts = [] # type: List[Tuple[str, str, Union[str, EeVal]]]
+
+ if is_farad:
+ s = s[0:-1] if is_farad else s
+
+ try:
+ farad = EeVal.parse(s)
+ except EeException:
+ farad = None
+
+ elif is_percent:
+ s = s[0:-1]
+ pct_value = int(s)
+
+ if is_plus_minus:
+ low_pct = high_pct = str(pct_value)
+ pct = float(pct_value) / 100
+ low_value = EeVal(value=cap_value * (1 - pct), exp=1, unit="F")
+ high_value = EeVal(value=cap_value * (1 + pct), exp=1, unit="F")
+ else:
+ raise EeException("bad combination")
+
+ if low_pct:
+ facts.append(("tolerance-percent-lower", "Tolerance, lower (%)", low_pct))
+ if high_pct:
+ facts.append(("tolerance-percent-upper", "Tolerance, upper (%)", high_pct))
+ if low_value:
+ facts.append(("tolerance-lower", "Tolerance, lower", low_value))
+ if high_value:
+ facts.append(("tolerance-upper", "Tolerance, upper", high_value))
+
+ if low_pct and low_pct == high_pct:
+ facts.append(("tolerance-percent", "Tolerance (±%)", low_pct))
+
+ return [bomFile.Fact(key=uris.make_fact_key(key), label=label, value=str(value)) for key, label, value in facts]
+
+
+_operating_temperature_re = re.compile("-([0-9]+)°C ~ ([0-9])+°C")
+_height_seated_re = re.compile("([0-9]+\\.[0-9]+)\" \\(([0-9]+\\.[0-9]+)mm\\)")
+_land_size_re = re.compile("([0-9]+\\.[0-9]+)\" L x ([0-9]+\\.[0-9]+)\" W "
+ "\\(([0-9]+\\.[0-9]+)mm x ([0-9]+\\.[0-9]+)mm\\)") # 0.260" L x 0.260" W (6.60mm x 6.60mm)
+
+
+def normalize_facts(in_dir: Path, out_dir: Path):
+ print("in: {}, out: {}".format(in_dir, out_dir))
+
+ in_db = load_db(in_dir)
+ out_parts = PartDb()
+
+ for part in in_db.iterparts(): # type: bomFile.Part
+ fact_list: bomFile.FactList = part.factsProp
+ if fact_list is None:
+ continue
+
+ in_facts: List[bomFile.Fact] = fact_list.factProp
+ out_facts = []
+
+ for f in in_facts:
+ key = uris.parse_digikey_fact_key(f.keyProp)
+
+ out_fact = None
+
+ value = f.valueProp
+
+ if value == "-":
+ continue
+
+ try:
+ if key == 3:
+ out_fact = bomFile.Fact(key=uris.make_fact_key("tolerance"), label="Tolerance", value=value)
+ elif key == 252:
+ m = _operating_temperature_re.match(value)
+ if m:
+ mn = m.group(1)
+ mx = m.group(2)
+
+ out_facts.extend([
+ bomFile.Fact(key=uris.make_fact_key("temperature-min"), label="Min temperature", value=mn),
+ bomFile.Fact(key=uris.make_fact_key("temperature-max"), label="Max temperature", value=mx)])
+ elif key == 1500:
+ m = _height_seated_re.match(value)
+
+ if m:
+ out_fact = bomFile.Fact(key=uris.make_fact_key("part-height"), label="Height", value=m.group(2))
+ else:
+ print("{}: bad str: '{}'".format(key, value))
+ elif key == 884:
+ m = _land_size_re.match(value)
+
+ if m:
+ out_facts.extend([
+ bomFile.Fact(key=uris.make_fact_key("land-size-x"),
+ label="Land size, X",
+ value=m.group(3)),
+ bomFile.Fact(key=uris.make_fact_key("land-size-y"),
+ label="Land size, Y",
+ value=m.group(4))])
+ elif key == 2049:
+ val = EeVal.parse(value)
+ out_fact = bomFile.Fact(key=uris.make_fact_key("capacitance"), label="Capacitance", value=str(val))
+ elif key == 2079:
+ val = EeVal.parse(value)
+ out_fact = bomFile.Fact(key=uris.make_fact_key("voltage-rating"), label="Voltage rating",
+ value=str(val))
+ else:
+ pass # fact ignored
+ except EeException:
+ pass
+
+ if out_fact:
+ out_facts.append(out_fact)
+
+ if len(out_facts) == 0:
+ continue
+
+ part.factsProp.factProp = out_facts
+ out_parts.add_entry(part, True)
+
+ print("Saving {} work parts".format(out_parts.size()))
+ save_db(out_dir, out_parts)
diff --git a/src/ee/part/__init__.py b/src/ee/part/__init__.py
index 84a6174..e0b0087 100644
--- a/src/ee/part/__init__.py
+++ b/src/ee/part/__init__.py
@@ -91,7 +91,10 @@ def save_db(dir_path: Path, db: PartDb):
idx.filesProp = indexFile.FileList()
files = idx.filesProp.fileProp
- for part in db.iterparts():
+ parts = db.iterparts()
+ parts = sorted(parts, key=lambda p: p.idProp)
+
+ for part in parts:
id_ = part.id
path = dir_path / "{}.xml".format(id_)
with path.open("w") as f:
diff --git a/src/ee/part/create_distributor_search_list.py b/src/ee/part/create_distributor_search_list.py
index ef91e1f..cfbe0d5 100644
--- a/src/ee/part/create_distributor_search_list.py
+++ b/src/ee/part/create_distributor_search_list.py
@@ -1,6 +1,7 @@
from pathlib import Path
from ee.part import PartDb, load_db, save_db
+from ee.xml import bomFile
from ee.xml.bom_file_utils import *
__all__ = ["create_distributor_search_list"]
@@ -26,8 +27,10 @@ def create_distributor_search_list(in_dir: Path, out_dir: Path):
if entry is not None:
continue
- part.id = pn_value
- out_parts.add_entry(part, True)
+ new_part = bomFile.Part(id=pn_value)
+ new_part.part_numbersProp = part.part_numbersProp
+
+ out_parts.add_entry(new_part, True)
print("Saving {} work parts".format(out_parts.size()))
save_db(out_dir, out_parts)
diff --git a/src/ee/tools/digikey_normalize_facts.py b/src/ee/tools/digikey_normalize_facts.py
new file mode 100644
index 0000000..07cc0d7
--- /dev/null
+++ b/src/ee/tools/digikey_normalize_facts.py
@@ -0,0 +1,19 @@
+import argparse
+from pathlib import Path
+
+from ee.digikey.normalize_facts import normalize_facts
+
+parser = argparse.ArgumentParser()
+
+parser.add_argument("--in",
+ dest="in_",
+ required=True,
+ metavar="FILE")
+
+parser.add_argument("--out",
+ required=True,
+ metavar="FILE")
+
+args = parser.parse_args()
+
+normalize_facts(Path(args.in_), Path(args.out))