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:
1 parent
5b24675a12
commit
09029ca0a4
29 files changed
+985
-456
No files matched your search
@@ -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
|
||||
Reference in new issue
Block a user