driver / standard (pull_request) Successful in 18s
driver / lint (pull_request) Successful in 16s
driver / test (3.12) (pull_request) Successful in 1m5s
driver / types (pull_request) Successful in 1m12s
driver / test (3.13) (pull_request) Successful in 1m4s
driver / lowest (pull_request) Successful in 30s
driver / build (pull_request) Successful in 33s
driver / security (pull_request) Successful in 1m13s
CI / driver (pull_request) Successful in 2m55s
driver / publish (pull_request) Skipped
The switch's community table starts with an Index column. The getter took the first word of each row as the community, so "public" was reported as "1", and read the access from the second word, the name, so every community came out read-only. It now uses the header-based community table parser: the name from Name, the access from Type. Closes #14
65 lines
2.2 KiB
Python
65 lines
2.2 KiB
Python
"""Pure parsers: the switch's CLI output in, plain values out."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import re
|
|
|
|
_DASHES = re.compile(r"^-{4,}$")
|
|
|
|
|
|
def _community_table(output: str) -> tuple[list[str], list[list[str]]]:
|
|
"""The header words of ``show snmp-server community`` and the rows below its dashes."""
|
|
header: list[str] = []
|
|
rows: list[list[str]] = []
|
|
previous: list[str] = []
|
|
in_table = False
|
|
for line in output.splitlines():
|
|
words = line.split()
|
|
if not words:
|
|
continue
|
|
if in_table:
|
|
rows.append(words)
|
|
elif _DASHES.match(words[0]):
|
|
header, in_table = previous, True
|
|
else:
|
|
previous = words
|
|
return header, rows
|
|
|
|
|
|
def _column(header: list[str], label: str) -> int | None:
|
|
"""The index of the header word containing *label*, case-insensitively."""
|
|
return next((i for i, word in enumerate(header) if label in word.lower()), None)
|
|
|
|
|
|
def parse_snmp_communities(output: str) -> list[str]:
|
|
"""The community names in ``show snmp-server community``, in the switch's order.
|
|
|
|
The table starts with an ``Index`` column: the name is the column headed
|
|
``Name``, found in the header line above the dashes. Without one, no name.
|
|
"""
|
|
header, rows = _community_table(output)
|
|
name = _column(header, "name")
|
|
if name is None:
|
|
return []
|
|
return [row[name] for row in rows if name < len(row)]
|
|
|
|
|
|
def parse_snmp_community_access(output: str) -> dict[str, str]:
|
|
"""Each community in ``show snmp-server community`` and its access, ``"rw"`` or ``"ro"``.
|
|
|
|
The name is the column headed ``Name``, the access the column headed ``Type``
|
|
(``read-only``, ``read-write``): a community is ``"rw"`` when its type says
|
|
write, ``"ro"`` otherwise. Without a ``Name`` column, no community.
|
|
"""
|
|
header, rows = _community_table(output)
|
|
name, kind = _column(header, "name"), _column(header, "type")
|
|
access: dict[str, str] = {}
|
|
if name is None:
|
|
return access
|
|
for row in rows:
|
|
if name >= len(row):
|
|
continue
|
|
typ = row[kind].lower() if kind is not None and kind < len(row) else ""
|
|
access[row[name]] = "rw" if "write" in typ else "ro"
|
|
return access
|