"""Offline Nexans product cards: exact listed configurations, never a cross product."""
from __future__ import annotations
import hashlib
import math
import re
from html.parser import HTMLParser
from typing import Any
from urllib.parse import urljoin
from energy_data_retrieval.pv_catalogue.catalogue import canonical_json
from .catalogue import stable_id
from .gppd import evidence_record
REFERENCE_IDS = ("nexans_tslfo_catalogue_2026",)
class _Cards(HTMLParser):
def __init__(self) -> None:
super().__init__()
self.cards: list[dict[str, Any]] = []
self.card: dict[str, Any] | None = None
self.depth = 0
self.title = False
self.reference = False
def handle_starttag(self, tag, attrs):
a = dict(attrs)
if tag == "div":
if self.depth:
self.depth += 1
elif "product__list__item" in a.get("class", "").split():
self.depth = 1
self.card = {"attributes": a, "title": [], "reference": [], "urls": []}
if self.card is not None:
if tag == "h3":
self.title = True
if tag == "p" and a.get("class") == "product__list__item__ref":
self.reference = True
if tag == "a" and self.title and a.get("href"):
self.card["urls"].append(a["href"])
def handle_data(self, data):
if self.card is not None:
if self.title:
self.card["title"].append(data)
if self.reference:
self.card["reference"].append(data)
def handle_endtag(self, tag):
if tag == "h3":
self.title = False
if tag == "p":
self.reference = False
if tag == "div" and self.depth:
self.depth -= 1
if not self.depth:
assert self.card is not None
self.cards.append(self.card)
self.card = None
[docs]
def stage_cables(original: bytes, review: dict[str, Any], *, now: str) -> list[dict[str, Any]]:
"""Require a checksummed complete card roster; retain missing ratings as null.
Catalogue URL IDs are distinguished from article numbers, which may be blank.
Current catalogue specifications do not establish historical installed variants.
"""
if hashlib.sha256(original).hexdigest() != review["sha256"]:
raise ValueError("Cable original checksum mismatch")
parser = _Cards()
parser.feed(original.decode("utf-8"))
if parser.depth or not parser.cards:
raise ValueError("Incomplete or absent cable cards")
result, ids = [], []
for card in parser.cards:
name = " ".join("".join(card["title"]).split())
if len(card["urls"]) != 1:
raise ValueError("One exact product URL required")
url = urljoin(review["url"], card["urls"][0])
match = re.search(r"/product~([A-Za-z0-9]+)~\.html$", url)
rating = re.search(r"^TSLF-O (24|36)kV ", name)
if not match or not rating or not url.startswith("https://www.nexans.no/"):
raise ValueError("Unexpected product identity or voltage label")
ident = match[1]
ids.append(ident)
ref = re.search(r"Nexans art\.nr\.\s*(\d+)", "".join(card["reference"]))
if ref and ref[1] != ident:
raise ValueError("Article number disagrees with catalogue identity")
attrs = card["attributes"]
metrics = {}
definitions = {
"section": ("conductor_cross_section", "mm2", None),
"diamextnom": ("nominal_outer_diameter", "mm", None),
"diamtube": ("tube_diameter", "mm", None),
"weightm": ("approximate_linear_mass", "kg/m", None),
"admcurbf15": ("current_rating_flat", "A", "buried at 15 degC; flat formation"),
"admcurbt15": ("current_rating_trefoil", "A", "buried at 15 degC; trefoil formation"),
"voltmaxk": ("maximum_operating_voltage", "kV", None),
"maxpultension": ("maximum_pulling_tension", "kN", None),
}
for key, (field, unit, conditions) in definitions.items():
raw = attrs.get("data-" + key)
value = None if raw in (None, "", "-") else float(raw.replace(",", "."))
if value is not None and (not math.isfinite(value) or value < 0):
raise ValueError("Invalid cable rating")
metrics[field] = dict(value=value, unit=unit, conditions=conditions, original_value=raw)
maximum = metrics["maximum_operating_voltage"]["value"]
if maximum is not None and maximum != float(rating[1]):
raise ValueError("Voltage label and published maximum disagree")
result.append(
evidence_record(
{
"record_id": stable_id("equipment", "Nexans", ident),
"entity_type": "equipment",
"record_scope": "configuration",
"name": name,
"manufacturer": "Nexans",
"model": name,
"variant": "Norwegian catalogue product " + ident,
"equipment_category": "power_cable",
"capacity_scope": "Exact listed cable configuration; no turbine power rating",
"official_url": url,
"source_revision": "Undated Norwegian catalogue retrieved " + now[:10],
"external_ids_json": canonical_json(
{
"nexans_catalogue_id": ident,
**({"nexans_article_number": ref[1]} if ref else {}),
}
),
"extensions_json": canonical_json(
{
"cable_specifications": metrics,
"packaging": attrs.get("data-packaging"),
"installed_variant": None,
"article_number": ref[1] if ref else None,
"wind_application_boundary": (
"TSLF-O family documented at Bessakerfjellet; "
"exact installed article, cross-section and revision not disclosed"
),
}
),
"notes": (
"OEM specifications, not independently validated performance. "
"Linear mass is not total cable mass. "
"No installed-product compatibility inferred."
),
},
source_id=review["source_id"],
now=now,
locator="Product card / " + ident,
)
)
if len(ids) != len(set(ids)) or sorted(ids) != sorted(review["expected_ids"]):
raise ValueError("Incomplete or duplicate cable partition")
return sorted(result, key=lambda r: r["record_id"])