diff options
Diffstat (limited to 'src/ee/digikey')
-rw-r--r-- | src/ee/digikey/functions.py (renamed from src/ee/digikey/normalize_facts.py) | 66 |
1 files changed, 35 insertions, 31 deletions
diff --git a/src/ee/digikey/normalize_facts.py b/src/ee/digikey/functions.py index 1ffc16a..047fad1 100644 --- a/src/ee/digikey/normalize_facts.py +++ b/src/ee/digikey/functions.py @@ -1,9 +1,8 @@ import re -from pathlib import Path from typing import List, Tuple, Union, Mapping, Callable, Any from ee import EeVal, EeException -from ee.part import PartDb, load_db, save_db +from ee.part import Part from ee.xml import types, uris __all__ = ["normalize_facts"] @@ -97,7 +96,7 @@ _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) -parsers: Mapping[int, Callable[[str], Any]] = { +_parsers: Mapping[int, Callable[[str], Any]] = { 3: no_parser("tolerance"), 1471: ee_val_parser("voltage-input-min"), # Voltage - Input (Min) 573: ee_val_parser("voltage-input-max"), # Voltage - Input (Max) @@ -113,41 +112,46 @@ parsers: Mapping[int, Callable[[str], Any]] = { } -def normalize_facts(in_path: Path, out_path: Path): - in_db = load_db(in_path) - out_parts = PartDb() +# noinspection PyUnusedLocal +def normalize_facts(**kwargs): + parsers: Mapping[str, Callable[[str], Any]] = {uris.make_digikey_fact_key(k): v for k, v in _parsers.items()} - for part in in_db.iterparts(): # type: types.Part - fact_list: types.FactList = part.factsProp - if fact_list is None: - continue + def on_part(part: Part) -> Part: + for dk_key, parser in parsers.items(): + value = part.facts.get_value(dk_key) - in_facts: List[types.Fact] = fact_list.factProp - out_facts = [] + if value is None or value == "-": + continue - for f in in_facts: - key = uris.parse_digikey_fact_key(f.keyProp) + res = parser(value) - value = f.valueProp + if res is not None: + if isinstance(res, list): + for key, value in res: + key = uris.make_fact_key(key) + part.facts.add(key, value) + else: + key, value = res + key = uris.make_fact_key(key) + part.facts.add(key, value) + + return part + + return on_part - if value == "-": - continue - parser = parsers.get(key) +def default(**kwargs): + function_factories = [ + normalize_facts, + ] - if parser is not None: - res = parser(value) + functions = [factory(**kwargs) for factory in function_factories] + functions = [f for f in functions if f is not None] - if res is not None: - if isinstance(res, list): - results = res - else: - results = [res] - facts = [types.Fact(key=uris.make_fact_key(key), value=value) for key, value in results] - out_facts.extend(facts) + def on_part(part: Part) -> Part: + for f in functions: + part = f(part) - part.factsProp.factProp = out_facts - out_parts.add_entry(part, True) + return part - print("Saving {} work parts".format(out_parts.size())) - save_db(out_path, out_parts, sort=True) + return on_part |