driver / standard (pull_request) Successful in 16s
driver / lint (pull_request) Successful in 13s
driver / types (pull_request) Successful in 52s
driver / test (3.12) (pull_request) Successful in 55s
driver / test (3.13) (pull_request) Successful in 45s
driver / lowest (pull_request) Successful in 25s
driver / build (pull_request) Successful in 26s
driver / security (pull_request) Successful in 58s
CI / driver (pull_request) Successful in 2m22s
driver / publish (pull_request) Skipped
The checks against line breaks and double quotes made the driver module 27 lines longer than the quality ratchet allows (2014, recorded 1987) and set_interface 53 lines long (limit 50). The checks move to cli_values.py (check_cli_values); the uptime parser, a pure parser without the driver, moves to parsers.py as parse_uptime_seconds and reads its four units in one loop; set_vlan and set_interface say in one docstring line what they refuse. Behaviour is unchanged; the uptime test gains a string without days and an empty one. The baseline is lowered to what is measured now (1971).
84 lines
2.8 KiB
Python
84 lines
2.8 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
|
|
|
|
|
|
#: The parts of a ``Running Time`` and their seconds: ``5 day(s) 2 hour(s) 35 min(s) 16 sec(s)``.
|
|
_UPTIME_UNITS = (("day", 86400), ("hour", 3600), ("min", 60), ("sec", 1))
|
|
|
|
|
|
def parse_uptime_seconds(uptime_str: str) -> float:
|
|
"""Convert a TP-Link uptime string to seconds; a missing part counts as 0.
|
|
|
|
Expected formats:
|
|
``5 day(s) 2 hour(s) 35 min(s) 16 sec(s)``
|
|
``0 day(s) 0 hour(s) 5 min(s) 3 sec(s)``
|
|
"""
|
|
total = 0
|
|
for unit, seconds in _UPTIME_UNITS:
|
|
match = re.search(rf"(\d+)\s+{unit}", uptime_str, re.I)
|
|
if match:
|
|
total += int(match.group(1)) * seconds
|
|
return float(total)
|