Source code for energy_data_retrieval.wind_catalogue.edockets

"""Offline discovery of exact named Oregon docket filings, not plant identity."""

from html.parser import HTMLParser
from urllib.parse import parse_qs, urljoin, urlsplit


[docs] def select_filings(html: str, *, docket_id: str, entity_name: str) -> list[dict[str, str]]: """Select named descriptions paired with their immediately preceding filing row. Preserve filing dates and document URLs. A selected document is a review candidate, never a canonical plant or proof of commissioning. Exact name matching requires a separator, so Farm 2 does not match Farm 20. """ if not docket_id.isdigit() or not entity_name.strip(): raise ValueError("Require numeric docket ID and exact entity name") base = "https://apps.puc.state.or.us/edockets/" class Rows(HTMLParser): def __init__(self): super().__init__() self.text = [] self.links = [] self.previous = None self.selected = [] def handle_starttag(self, tag, attrs): if tag == "tr": self.text, self.links = [], [] if tag == "a": href = dict(attrs).get("href") if href: self.links.append(urljoin(base, href)) def handle_data(self, data): self.text.append(data) def handle_endtag(self, tag): if tag != "tr": return text = " ".join(" ".join(self.text).split()) description = text.removeprefix("Description ") if description.startswith(entity_name): suffix = description[len(entity_name) :] if not suffix or suffix[0] in " ,'\u2019:": if not self.previous: raise ValueError("Missing filing header") header, links = self.previous if not header.startswith("Date:") or len(links) != 1: raise ValueError("Ambiguous filing header") url = urlsplit(links[0]) query = parse_qs(url.query) if ( url.scheme != "https" or url.netloc != "apps.puc.state.or.us" or url.path != "/edockets/edocs.asp" or query.get("DocketID") != [docket_id] or len(query.get("FileName", [])) != 1 ): raise ValueError("Unexpected document URL or docket") self.selected.append( { "filing": header, "description": description, "url": links[0], "file": query["FileName"][0], } ) self.previous = (text, self.links[:]) self.text, self.links = [], [] parser = Rows() parser.feed(html) parser.close() files = [x["file"] for x in parser.selected] if len(files) != len(set(files)): raise ValueError("Duplicate document in selected partition") return parser.selected