Files
periscope/periscope/src/backend/services/passive_from_value.py
T
michele 07ad5c72fa Fix PCB software false positives (2.60.11).
KiCad 9/10 name-only nets fill the index. PE-LAY-004 counts tracks, vias,
and pours and skips NC nets. Missing I_load is INSUFFICIENT. EP pads are
not PE-BOM-011; abs-max binds the named pin. Kelvin/EMI/PI ignore NC and
IN±. LQW decodes nH; BLM/FB is a bead, not DCR as Z.
2026-09-21 11:06:38 +02:00

138 lines
4.5 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""Parse a BOM Value string into typed passive specs without an LLM."""
from __future__ import annotations
import re
from backend.periscopex.models import ComponentModel, SimpleComponentSpecs
from backend.periscopex.resolve_passives import simple_to_typed_passive_specs
from backend.services.passive_from_distributor import _spice
_PLACEHOLDER = re.compile(
r"^(?:dnp|dni|dns|nc|n/?c|n/?a|np|nfs|tbd|todo|jumper|jmp|short|open|-|—|)$",
re.I,
)
_CAP = re.compile(
r"^(?P<num>\d+(?:\.\d+)?)\s*(?P<mul>[pnuμµmk])?\s*[fF]$",
)
_IND = re.compile(
r"^(?P<num>\d+(?:\.\d+)?)\s*(?P<mul>[pnuμµmk])?\s*H$",
re.I,
)
_RES_UNIT = re.compile(
r"^(?P<num>\d+(?:\.\d+)?)\s*(?P<mul>[pnuμµmkM])?\s*(?:ohms?|Ω|R)$",
re.I,
)
_RES_BARE_MUL = re.compile(
r"^(?P<num>\d+(?:\.\d+)?)(?P<mul>[kKmM])$",
)
_EURO_R = re.compile(
r"^(?P<a>\d+)[kK](?P<b>\d+)$",
)
_FB = re.compile(
r"^(?P<num>\d+(?:\.\d+)?)\s*(?P<mul>[kKmM])?\s*(?:ohms?|Ω|R)"
r"(?:@\s*(?P<freq>\d+(?:\.\d+)?\s*(?:kHz|MHz|GHz|Hz)))?$",
re.I,
)
def is_placeholder_value(value: str) -> bool:
return bool(_PLACEHOLDER.match((value or "").strip()))
def _model(mpn: str, subtype: str, values: dict[str, str]) -> ComponentModel | None:
specs = SimpleComponentSpecs(
specs_type="passive",
component_subtype=subtype,
values=values,
)
try:
typed = simple_to_typed_passive_specs(specs)
except (ValueError, TypeError):
return None
return ComponentModel(mpn=mpn, specs=typed)
def specs_from_bom_value(
mpn: str, value: str, ref_prefix: str,
) -> ComponentModel | None:
"""Map ``18pF`` / ``4.7k`` / ``10uH`` / ``600R@100MHz`` to typed specs.
Returns None for placeholders (DNP, NC, JUMPER) and for strings that
are not a single passive value. Callers must not save the result to the
shared library.
"""
raw = (value or "").strip()
prefix = (ref_prefix or "").upper()
if not raw or is_placeholder_value(raw):
return None
compact = re.sub(r"\s+", "", raw)
if prefix == "FB" or "@" in compact:
m = _FB.match(compact) or _FB.match(raw)
if m and "@" in compact:
z = _spice(m.group("num"), m.group("mul"), "ohm")
freq = re.sub(r"\s+", "", m.group("freq") or "")
values = {
"impedance_ohm": z,
"value_formatted": f"{z}@{freq}" if freq else z,
}
return _model(mpn, "passive.ferrite_bead", values)
if prefix == "FB":
# Ohm without @freq is DCR, not Z. Do not invent impedance.
m_dcr = _RES_UNIT.match(compact) or _RES_BARE_MUL.match(compact)
dcr = None
if m_dcr:
dcr = _spice(m_dcr.group("num"), m_dcr.group("mul"), "ohm")
from_mpn = None
try:
from backend.services.passive_from_mpn import specs_from_mpn
from_mpn = specs_from_mpn(mpn)
except Exception:
from_mpn = None
if from_mpn is not None:
return from_mpn
values: dict[str, str] = {"value_formatted": "FB"}
if dcr is not None:
values["dcr_ohms"] = dcr
return _model(mpn, "passive.ferrite_bead", values)
if prefix in {"C", ""}:
m = _CAP.match(compact) or _CAP.match(raw)
if m:
farads = _spice(m.group("num"), m.group("mul"), "F")
return _model(mpn, "passive.capacitor", {
"value_farads": farads,
"value_formatted": farads,
})
if prefix in {"L", ""}:
m = _IND.match(compact) or _IND.match(raw)
if m:
henries = _spice(m.group("num"), m.group("mul"), "H")
return _model(mpn, "passive.inductor", {
"value_henries": henries,
"value_formatted": henries,
})
if prefix in {"R", ""}:
m = _EURO_R.match(compact)
if m:
ohms = float(f"{m.group('a')}.{m.group('b')}") * 1e3
formatted = _spice(str(ohms), None, "ohm")
return _model(mpn, "passive.resistor", {
"value_ohms": formatted,
"value_formatted": formatted,
})
m = _RES_UNIT.match(compact) or _RES_BARE_MUL.match(compact)
if m:
formatted = _spice(m.group("num"), m.group("mul"), "ohm")
return _model(mpn, "passive.resistor", {
"value_ohms": formatted,
"value_formatted": formatted,
})
return None