dashboard/app/utils/sysfs.py
2026-08-31 00:47:02 +02:00

52 lines
1.1 KiB
Python

def read_str(path: str) -> str | None:
"""Read a sysfs file, returning its stripped contents.
Args:
path: path under /sys.
Returns:
The file contents, or None if it cannot be read.
"""
try:
with open(path) as f:
return f.read().strip()
except OSError:
return None
def read_int(path: str) -> int | None:
"""Read a sysfs file as an integer.
Args:
path: path under /sys.
Returns:
The parsed value, or None if the file cannot be read or does
not contain an integer.
"""
v = read_str(path)
if v is None:
return None
try:
return int(v)
except ValueError:
return None
def read_float(path: str) -> float | None:
"""Read a sysfs file as a float.
Args:
path: path under /sys.
Returns:
The parsed value, or None if the file cannot be read or does
not contain a number.
"""
v = read_str(path)
if v is None:
return None
try:
return float(v)
except ValueError:
return None