"""Offline Fosen Vind fact-card inventory; reported output is not measured yield."""
from __future__ import annotations
import hashlib
import math
import re
from html.parser import HTMLParser
from typing import Any
class _Cards(HTMLParser):
def __init__(self) -> None:
super().__init__()
self.depth = 0
self.heading = False
self.item = False
self.name: list[str] = []
self.words: list[str] = []
self.items: list[str] = []
self.cards: list[tuple[str, list[str]]] = []
def handle_starttag(self, tag: str, attrs: list[tuple[str, str | None]]) -> None:
if tag == "div":
if self.depth:
self.depth += 1
elif "featured-content-block" in (dict(attrs).get("class") or "").split():
self.depth = 1
self.name, self.items = [], []
if self.depth and tag == "h2":
self.heading = True
if self.depth and tag == "li":
if self.item:
raise ValueError("Nested fact-card list item")
self.item, self.words = True, []
def handle_data(self, data: str) -> None:
if self.heading:
self.name.append(data)
if self.item:
self.words.append(data)
def handle_endtag(self, tag: str) -> None:
if tag == "h2":
self.heading = False
if tag == "li" and self.item:
self.items.append(" ".join("".join(self.words).split()))
self.item = False
if tag == "div" and self.depth:
self.depth -= 1
if not self.depth:
if self.item or self.heading:
raise ValueError("Unclosed card field")
self.cards.append((" ".join("".join(self.name).split()), self.items))
[docs]
def parse_fosen_cards(
original: bytes, *, sha256: str, expected_names: list[str]
) -> list[dict[str, Any]]:
"""Require an exact complete card roster and retain units and source labels.
Tower height is not silently called hub height. Annual generation has no
calendar period, gross/net boundary or measured/modeled classification in
these cards. No capacity factor or operational performance is inferred.
"""
if hashlib.sha256(original).hexdigest() != sha256:
raise ValueError("Original checksum mismatch")
parser = _Cards()
parser.feed(original.decode("utf-8"))
parser.close()
if parser.depth or parser.item or parser.heading:
raise ValueError("Truncated fact-card document")
names = [n for n, _ in parser.cards]
if not names or len(names) != len(set(names)) or sorted(names) != sorted(expected_names):
raise ValueError("Incomplete or duplicate fact-card roster")
fields = {
"generation_gwh": ("Produksjon (GWh)", "", "GWh"),
"installed_mw": ("Installert effekt (MW)", "", "MW"),
"rotor_m": ("Rotordiameter", "meter", "m"),
"tower_height_m": ("Tårnhøyde", "meter", "m"),
"turbines": ("Vindturbiner", "", "count"),
"road_length_km": ("Veier, adkomst og interne (km)", "", "km"),
"planning_area_km2": ("Planområde", "km2", "km2"),
"construction_year": ("Byggestart", "", "year"),
"assembly_year": ("Turbinmontasje", "", "year"),
"commissioning_year": ("Idriftsettelse", "", "year"),
}
results = []
for name, items in parser.cards:
facts: dict[str, Any] = {"name": name, "source_items": items}
for key, (label, suffix, unit) in fields.items():
matches = [i for i in items if re.match(re.escape(label) + r"(?:\s|:|$)", i)]
if len(matches) != 1:
raise ValueError(f"Missing or duplicate {label}")
number = matches[0][len(label) :].lstrip(": ").removesuffix(suffix).strip()
if not re.fullmatch(r"\d+(?:,\d+)?", number):
raise ValueError(f"Invalid Norwegian number for {label}")
value = float(number.replace(",", "."))
if not math.isfinite(value):
raise ValueError("Nonfinite fact-card number")
if key.endswith("year") or key == "turbines":
if not value.is_integer():
raise ValueError("Fractional count or year")
value = int(value)
facts[key] = {"value": value, "unit": unit, "original": matches[0]}
models = [i for i in items if re.match(r"Turbin(?:\s|:)", i)]
if len(models) != 1:
raise ValueError("Missing or duplicate turbine model label")
facts["model_label"] = models[0].removeprefix("Turbin").lstrip(": ")
facts["generation_basis"] = {"period": None, "gross_net": None, "kind": None}
facts["exact_equipment_revision"] = None
results.append(facts)
return sorted(results, key=lambda r: r["name"])