style: ruff format and the lint rules of the driver standard

Mechanical: ruff format, and ruff check --fix with the rule set every NAPALM
driver uses (E, F, I, UP, B at line length 100, Python 3.10). Mapping and
Iterable come from collections.abc, imports are sorted. What ruff cannot
fix comes in the next commit.
This commit is contained in:
Christian Manivong committed 2026-10-23 08:39:45 +02:00
1 parent 5b24675a12
commit 09029ca0a4
29 files changed
+985 -456

No files matched your search

+2 -1
View File
@@ -2,7 +2,8 @@
from __future__ import annotations
from typing import Any, Iterable, Mapping
from collections.abc import Iterable, Mapping
from typing import Any
from .common import boolean, text
+16 -13
View File
@@ -2,21 +2,24 @@
from __future__ import annotations
from typing import Any, Mapping
from collections.abc import Mapping
from typing import Any
# DMI defaults that board vendors leave in place (Supermicro boards ship most of them).
PLACEHOLDERS = frozenset({
"to be filled by o.e.m.",
"default string",
"not specified",
"not applicable",
"n/a",
"none",
"0123456789",
"system serial number",
"system product name",
"system manufacturer",
})
PLACEHOLDERS = frozenset(
{
"to be filled by o.e.m.",
"default string",
"not specified",
"not applicable",
"n/a",
"none",
"0123456789",
"system serial number",
"system product name",
"system manufacturer",
}
)
def text(value: Any) -> str | None:
+43 -16
View File
@@ -16,12 +16,20 @@ left out.
from __future__ import annotations
from typing import Any, Iterable, Mapping
from collections.abc import Iterable, Mapping
from typing import Any
from .common import link, number, present, status, text, unique_name
_SENSOR_TYPES_BY_UNITS = {"Cel": "Temperature", "V": "Voltage", "W": "Power", "A": "Current",
"RPM": "Rotational", "%": "Percent", "Hz": "Frequency"}
_SENSOR_TYPES_BY_UNITS = {
"Cel": "Temperature",
"V": "Voltage",
"W": "Power",
"A": "Current",
"RPM": "Rotational",
"%": "Percent",
"Hz": "Frequency",
}
def environment_sources(chassis: Mapping[str, Any]) -> dict[str, Any]:
@@ -138,8 +146,11 @@ def parse_power(power: Mapping[str, Any]) -> dict[str, Any]:
if not psu["present"]:
continue
name = unique_name(supplies, psu["name"], item.get("MemberId", index))
supplies[name] = {"status": psu["status"], "capacity": psu["capacity"],
"output": psu["output"]}
supplies[name] = {
"status": psu["status"],
"capacity": psu["capacity"],
"output": psu["output"],
}
voltages: dict[str, Any] = {}
for index, item in _members(power, "Voltages"):
@@ -204,8 +215,9 @@ def parse_fan(fan: Mapping[str, Any]) -> dict[str, Any]:
}
def parse_power_supply(supply: Mapping[str, Any],
metrics: Mapping[str, Any] | None = None) -> dict[str, Any]:
def parse_power_supply(
supply: Mapping[str, Any], metrics: Mapping[str, Any] | None = None
) -> dict[str, Any]:
"""One member of ``PowerSubsystem/PowerSupplies``, with its ``Metrics`` if read.
An item of the legacy ``Power.PowerSupplies`` array parses too (``MemberId`` as id,
@@ -273,16 +285,24 @@ def parse_thermal_metrics(metrics: Mapping[str, Any]) -> dict[str, Any]:
return {"temperature": temperature}
def build_environment(*, fans: Iterable[Mapping[str, Any]] = (),
power_supplies: Iterable[Mapping[str, Any]] = (),
sensors: Iterable[Mapping[str, Any]] = ()) -> dict[str, Any]:
def build_environment(
*,
fans: Iterable[Mapping[str, Any]] = (),
power_supplies: Iterable[Mapping[str, Any]] = (),
sensors: Iterable[Mapping[str, Any]] = (),
) -> dict[str, Any]:
"""Combine parsed fans, power supplies and sensors into the shape of the legacy parsers.
``consumed_watts`` comes from the chassis total power sensor
(``ReadingType`` Power, ``ElectricalContext`` Total, ``PhysicalContext`` Chassis).
"""
env: dict[str, Any] = {"fans": {}, "temperature": {}, "power": {}, "voltages": {},
"consumed_watts": None}
env: dict[str, Any] = {
"fans": {},
"temperature": {},
"power": {},
"voltages": {},
"consumed_watts": None,
}
for fan in fans:
if fan["present"]:
name = unique_name(env["fans"], fan["name"], fan["id"])
@@ -290,8 +310,11 @@ def build_environment(*, fans: Iterable[Mapping[str, Any]] = (),
for psu in power_supplies:
if psu["present"]:
name = unique_name(env["power"], psu["name"], psu["id"])
env["power"][name] = {"status": psu["status"], "capacity": psu["capacity"],
"output": psu["output"]}
env["power"][name] = {
"status": psu["status"],
"capacity": psu["capacity"],
"output": psu["output"],
}
for sensor in sensors:
reading = sensor["reading"]
if reading is None:
@@ -303,7 +326,11 @@ def build_environment(*, fans: Iterable[Mapping[str, Any]] = (),
elif sensor["reading_type"] == "Voltage":
name = unique_name(env["voltages"], sensor["name"], sensor["id"])
env["voltages"][name] = {"voltage": reading, **flags}
elif (sensor["reading_type"] == "Power" and sensor["electrical_context"] == "Total"
and sensor["physical_context"] == "Chassis" and env["consumed_watts"] is None):
elif (
sensor["reading_type"] == "Power"
and sensor["electrical_context"] == "Total"
and sensor["physical_context"] == "Chassis"
and env["consumed_watts"] is None
):
env["consumed_watts"] = reading
return env
+2 -1
View File
@@ -2,7 +2,8 @@
from __future__ import annotations
from typing import Any, Mapping
from collections.abc import Mapping
from typing import Any
from .common import boolean, link_list, status, text
+14 -7
View File
@@ -2,7 +2,8 @@
from __future__ import annotations
from typing import Any, Mapping
from collections.abc import Mapping
from typing import Any
from .common import boolean, ident, integer, link, link_list, location_label, present, status, text
@@ -39,8 +40,9 @@ def parse_memory(dimm: Mapping[str, Any]) -> dict[str, Any]:
return {
"id": text(dimm.get("Id")),
"name": text(dimm.get("Name")),
"location": (location_label(dimm) or text(dimm.get("DeviceLocator"))
or text(dimm.get("Name"))),
"location": (
location_label(dimm) or text(dimm.get("DeviceLocator")) or text(dimm.get("Name"))
),
"capacity_mib": capacity,
"memory_device_type": text(dimm.get("MemoryDeviceType")),
"base_module_type": text(dimm.get("BaseModuleType")),
@@ -62,7 +64,8 @@ def _controller(ctrl: Mapping[str, Any]) -> dict[str, Any]:
"model": ident(ctrl.get("Model")),
"serial_number": ident(ctrl.get("SerialNumber")),
"firmware_version": text(ctrl.get("FirmwareVersion")),
"speed_gbps": ctrl.get("SpeedGbps") if isinstance(ctrl.get("SpeedGbps"), (int, float))
"speed_gbps": ctrl.get("SpeedGbps")
if isinstance(ctrl.get("SpeedGbps"), (int, float))
else None,
"state": state,
"health": health,
@@ -83,7 +86,8 @@ def parse_storage(storage: Mapping[str, Any]) -> dict[str, Any]:
"state": state,
"health": health,
"controllers": [_controller(c) for c in controllers if isinstance(c, Mapping)]
if isinstance(controllers, list) else [],
if isinstance(controllers, list)
else [],
"controllers_path": link(storage, "Controllers"),
"drive_paths": link_list(storage, "Drives"),
}
@@ -117,8 +121,11 @@ def parse_simple_storage(storage: Mapping[str, Any]) -> dict[str, Any]:
"""
state, health = status(storage)
devices = storage.get("Devices")
drives = [parse_drive(dev) for dev in devices if isinstance(dev, Mapping)] \
if isinstance(devices, list) else []
drives = (
[parse_drive(dev) for dev in devices if isinstance(dev, Mapping)]
if isinstance(devices, list)
else []
)
return {
"id": text(storage.get("Id")),
"name": text(storage.get("Name")),
+2 -1
View File
@@ -2,7 +2,8 @@
from __future__ import annotations
from typing import Any, Mapping
from collections.abc import Mapping
from typing import Any
from .common import boolean, integer, link, status, text
+5 -3
View File
@@ -3,7 +3,8 @@
from __future__ import annotations
import ipaddress
from typing import Any, Mapping
from collections.abc import Mapping
from typing import Any
from .common import boolean, integer, link_list, links, number, status, text
@@ -132,8 +133,9 @@ def parse_network_protocol(proto: Mapping[str, Any]) -> dict[str, Any]:
for community in snmp.get("CommunityStrings") or []:
if isinstance(community, Mapping):
# The community string itself is never kept, even if a BMC returns it.
communities.append({"name": text(community.get("Name")),
"access": text(community.get("AccessMode"))})
communities.append(
{"name": text(community.get("Name")), "access": text(community.get("AccessMode"))}
)
return {
"hostname": text(proto.get("HostName")),
+2 -1
View File
@@ -2,7 +2,8 @@
from __future__ import annotations
from typing import Any, Mapping
from collections.abc import Mapping
from typing import Any
from .common import link, links, text
+11 -7
View File
@@ -2,7 +2,8 @@
from __future__ import annotations
from typing import Any, Mapping
from collections.abc import Mapping
from typing import Any
from .common import ident, integer, link_list, links, number, status, text
@@ -10,10 +11,12 @@ from .common import ident, integer, link_list, links, number, status, text
def parse_system(system: Mapping[str, Any]) -> dict[str, Any]:
"""Identity, summaries and links of the managed server."""
state, health = status(system)
cpus = system.get("ProcessorSummary") if isinstance(system.get("ProcessorSummary"),
Mapping) else {}
memory = system.get("MemorySummary") if isinstance(system.get("MemorySummary"),
Mapping) else {}
cpus = (
system.get("ProcessorSummary")
if isinstance(system.get("ProcessorSummary"), Mapping)
else {}
)
memory = system.get("MemorySummary") if isinstance(system.get("MemorySummary"), Mapping) else {}
return {
"id": text(system.get("Id")),
"name": text(system.get("Name")),
@@ -44,8 +47,9 @@ def parse_power_state(system: Mapping[str, Any]) -> str | None:
return text(system.get("PowerState"))
def parse_reset_action(resource: Mapping[str, Any],
action: str = "#ComputerSystem.Reset") -> dict[str, Any]:
def parse_reset_action(
resource: Mapping[str, Any], action: str = "#ComputerSystem.Reset"
) -> dict[str, Any]:
"""Target and allowed ``ResetType`` values of a reset action.
``allowed`` is ``None`` when the service does not list the values inline. Then