Read now works, and terraform directory contains example code
This commit is contained in:
1 parent
ef498e4a72
commit
a6f1019175
43 files changed
+4341
-38
No files matched your search
+28
-4
@@ -28,19 +28,27 @@ def extract_cell_value(td) -> str:
|
||||
|
||||
|
||||
def parse_table(table) -> dict:
|
||||
table_id = table.get("id")
|
||||
caption_tag = table.find("caption")
|
||||
caption = caption_tag.get_text(strip=True) if caption_tag else None
|
||||
headers = [th.get_text(strip=True) for th in table.find_all("th", recursive=False)]
|
||||
trs = table.find_all("tr", recursive=False)
|
||||
|
||||
if headers:
|
||||
# Some pages repeat a label across a hidden raw-value column and a
|
||||
# visible display column (e.g. PortSummary's/MAC Table's two
|
||||
# "Interface"/"MAC Address" headers). Building the row dict via zip()
|
||||
# means the later (visible) column wins on a duplicate label -- that
|
||||
# has matched the raw column's value in every case seen so far, but
|
||||
# if a future page's hidden/visible pair ever genuinely differs,
|
||||
# only the visible one survives here.
|
||||
rows = []
|
||||
for tr in trs:
|
||||
tds = tr.find_all("td", recursive=False)
|
||||
if len(tds) != len(headers):
|
||||
continue
|
||||
rows.append({h: extract_cell_value(td) for h, td in zip(headers, tds)})
|
||||
return {"caption": caption, "kind": "tabular", "rows": rows}
|
||||
return {"caption": caption, "kind": "tabular", "rows": rows, "id": table_id}
|
||||
|
||||
record = {}
|
||||
field_names = {}
|
||||
@@ -58,7 +66,13 @@ def parse_table(table) -> dict:
|
||||
field_names[label] = inp["name"]
|
||||
# field_names lets a write action look up the real <INPUT NAME=...> for a
|
||||
# label instead of hardcoding a guessed "v_R_C_1"-style field name.
|
||||
return {"caption": caption, "kind": "scalar", "record": record, "field_names": field_names}
|
||||
return {
|
||||
"caption": caption,
|
||||
"kind": "scalar",
|
||||
"record": record,
|
||||
"field_names": field_names,
|
||||
"id": table_id,
|
||||
}
|
||||
|
||||
|
||||
def parse_xe_tables(html: str) -> list[dict]:
|
||||
@@ -69,8 +83,18 @@ def parse_xe_tables(html: str) -> list[dict]:
|
||||
def fetch_tables(session, path: str) -> dict[str, dict]:
|
||||
"""Fetch path and return {caption: table} for every captioned table.
|
||||
|
||||
Tables with no/blank caption (usually just a submit button row) are
|
||||
dropped since there's nothing useful to key them by."""
|
||||
Tables with no/blank caption (usually just a submit button row, but
|
||||
occasionally a real data table -- e.g. FDBSearch.html's MAC entries
|
||||
table has no caption) are dropped since there's nothing useful to key
|
||||
them by. Use fetch_all_tables() + table["id"] for those instead."""
|
||||
html = switch_client.fetch(session, path)
|
||||
tables = parse_xe_tables(html)
|
||||
return {t["caption"]: t for t in tables if t["caption"]}
|
||||
|
||||
|
||||
def fetch_all_tables(session, path: str) -> list[dict]:
|
||||
"""Fetch path and return every table, including uncaptioned ones, in
|
||||
document order. Look up by table["id"] (the <TABLE ID=...> attribute)
|
||||
when a table has no caption to key it by."""
|
||||
html = switch_client.fetch(session, path)
|
||||
return parse_xe_tables(html)
|
||||
Reference in new issue
Block a user