from pathlib import Path from ee.kicad import sch_fact_types from ee.logging import log from ee.part import PartDb, load_db, save_db, Part, fact_keys, common_fact_types from ee.xml import types, uris __all__ = ["pn_part_search_list"] ignored_ee_component_types = [ "mechanical", uris.NET_TIE, uris.TEST_POINT, ] ignored_part_classes_for_value_based_lookups = [ uris.CAPACITOR, uris.INDUCTOR, uris.RESISTOR, ] def valid_pns(part: Part): """Check if the part has any MPNs or SPNs""" return len(part.get_mpns()) > 0 or len(part.get_spns()) > 0 def get_value(part: Part): """Check if the part has a value and it is not a resistor, capacitor or inductor. Their value is not useful for a part number-based lookup""" value, typ = part.facts.get_values(sch_fact_types.value, common_fact_types.ee_component_type) if typ not in ignored_part_classes_for_value_based_lookups: return value def pn_part_search_list(in_path: Path, out_path: Path, supplier: str): in_parts = load_db(in_path) out_parts = PartDb() count = 0 skipped = list() for xml in in_parts.iterparts(): count += 1 part = Part(xml) refs = [ref.referenceProp for ref in part.get_schematic_references()] component_type = part.facts.get_value(fact_keys.ee_component_type) if component_type: if component_type in ignored_ee_component_types: log.info("Skipping part {}: ignored type".format(part.printable_reference)) skipped.append(refs) continue if not valid_pns(part): value = get_value(part) if not value: log.info("Skipping part {}: no part number".format(part.printable_reference)) skipped.append(refs) continue # Use the value of the component as a part number to search for xml.referencesProp.part_number.append(types.PartNumber(value=value)) new_part = types.Part() new_part.referencesProp = xml.referencesProp out_parts.add_entry(new_part, True) if len(skipped) > 0: log.info("Skipped {} of {} parts".format(len(skipped), count)) save_db(out_path, out_parts)