"""Offline exact-part OEM tables and dated wind-assembly crosswalks.
Only public product classifications are selected from the rendered page state.
Unrelated site configuration, prices and inferred mating combinations are omitted.
"""
from __future__ import annotations
import hashlib
import json
import re
from html.parser import HTMLParser
from typing import Any
from energy_data_retrieval.pv_catalogue.catalogue import canonical_json
from .catalogue import stable_id
from .gppd import evidence_record
REFERENCE_IDS = ("harting_09120053101_20261110",)
class _State(HTMLParser):
def __init__(self) -> None:
super().__init__()
self.active = False
self.parts: list[str] = []
self.count = 0
def handle_starttag(self, tag, attrs):
if tag == "script" and dict(attrs).get("id") == "ng-state":
self.count += 1
self.active = True
def handle_data(self, data):
if self.active:
self.parts.append(data)
def handle_endtag(self, tag):
if tag == "script":
self.active = False
class _Text(HTMLParser):
def __init__(self) -> None:
super().__init__()
self.parts: list[str] = []
def handle_starttag(self, tag, attrs):
if tag == "sup":
self.parts.append("^(")
elif tag == "sub":
self.parts.append("_(")
elif tag == "br":
self.parts.append("; ")
def handle_endtag(self, tag):
if tag in {"sup", "sub"}:
self.parts.append(")")
def handle_data(self, data):
self.parts.append(data)
def _text(value: str) -> str:
parser = _Text()
parser.feed(value)
return " ".join("".join(parser.parts).replace("\u200c", "").split())
[docs]
def parse_product(original: bytes, part_number: str) -> dict[str, Any]:
"""Require the exact OEM part; retain units, inequalities and superscripts.
No numerical coercion is applied: UL/CSA voltage, impulse voltage, limiting
temperatures and conditional IP ratings remain separate named observations.
Missing or duplicate product/feature identities fail closed.
"""
if not re.fullmatch(r"\d{11}", part_number):
raise ValueError("Exact eleven-digit part number required")
parser = _State()
parser.feed(original.decode("utf-8"))
if parser.count != 1 or parser.active:
raise ValueError("Missing or incomplete public product state")
state = json.loads("".join(parser.parts))
product = state.get(f'CS|D|CommerceProductService#getProduct?["{part_number}"]')
if not product or product.get("code") != part_number or not product.get("name"):
raise ValueError("Exact product identity absent; redirects are not evidence")
groups = product.get("classifications") or []
if not groups or not any(g.get("name") == "Identification" for g in groups):
raise ValueError("Missing product classifications")
attributes = []
seen = set()
for group in groups:
for feature in group["features"]:
key = (group["code"], feature["code"])
if key in seen:
raise ValueError("Duplicate product attribute")
seen.add(key)
attributes.append(
{
"section": group["name"],
"attribute_id": feature["code"],
"name": feature["name"],
"values": [_text(v["value"]) for v in feature["featureValues"]],
"normalization": (
"HTML to text; superscripts ^(), subscripts _(); units retained"
),
"locator": f"Product details / {group['name']} / {feature['name']}",
}
)
return {"part_number": part_number, "name": product["name"], "attributes": attributes}
[docs]
def stage_connectors(
documents: list[dict[str, Any]],
originals: dict[str, bytes],
counterparts: list[dict[str, Any]],
existing: list[dict[str, Any]],
*,
now: str,
) -> tuple[list[dict[str, Any]], list[dict[str, Any]]]:
"""Review a complete numbered assembly partition without claiming fitment.
Exact part-number equality supports a reference crosswalk, not equivalence
between current OEM specifications and the installed historical revision.
Existing cross-sector IDs must be supplied and are held for explicit reuse.
"""
expected = {r["part_number_as_printed"].replace(" ", "") for r in counterparts}
parts = [d["part_number"] for d in documents]
if len(set(parts)) != len(parts) or set(parts) != expected:
raise ValueError("Incomplete or duplicate counterpart partition")
records, ledger = [], []
for doc in sorted(documents, key=lambda d: d["part_number"]):
part, sid = doc["part_number"], doc["source_id"]
raw = originals[sid]
if hashlib.sha256(raw).hexdigest() != doc["sha256"]:
raise ValueError("Original product checksum mismatch")
product = parse_product(raw, part)
matches = [
r
for r in existing
if r.get("manufacturer", "").casefold() == "harting"
and str(r.get("model", "")).replace(" ", "") == part
]
if matches:
ledger.append(
{
"part_number": part,
"decision": "existing_identity_review",
"record_ids": [r["record_id"] for r in matches],
}
)
continue
applications = [
r for r in counterparts if r["part_number_as_printed"].replace(" ", "") == part
]
if any(not r.get("source_id") or not r.get("locator") for r in applications):
raise ValueError("Counterpart provenance required")
extension = {
"connector_specifications": {
**product,
"source_id": sid,
"sha256": doc["sha256"],
"revision": None,
"published_at": None,
"retrieved_at": now,
"region": "United Kingdom / English public product page",
"assembly_references": applications,
"installed_revision_verified": False,
"specification_scope": "Dated OEM claims, not independently validated performance",
"derating_curves": "Not imported; curve axes/conditions require separate review",
}
}
row = evidence_record(
{
"record_id": stable_id("equipment", "oem-electrical", "HARTING", part),
"entity_type": "equipment",
"record_scope": "configuration",
"name": f"HARTING {part} — {product['name']}",
"manufacturer": "HARTING",
"model": part,
"variant": "OEM part " + part + "; UK public specification",
"equipment_category": "connector",
"capacity_scope": "Connector electrical ratings; no turbine active power",
"external_ids_json": canonical_json({"harting_part_number": part}),
"extensions_json": canonical_json(extension),
"notes": "Exact numbered connector, insert, contact or hood. Assembly references "
"identify dated counterpart requirements only; current OEM revision, installed "
"revision, turbine/project fitment and warranty are unresolved. "
"No active power inferred.",
},
source_id=sid,
now=now,
locator="Product details / exact part number and classifications",
)
sources = sorted({sid, *(r["source_id"] for r in applications)})
row["sources"] = sources
fs = json.loads(row["field_sources_json"])
fs["extensions_json"] = sources
row["field_sources_json"] = canonical_json(fs)
ev = json.loads(row["evidence_json"])
ev["extensions_json"]["supporting_evidence"] = applications
row["evidence_json"] = canonical_json(ev)
records.append(row)
ledger.append(
{
"part_number": part,
"record_id": row["record_id"],
"decision": "addition",
"source_id": sid,
}
)
return records, ledger