refactor: don't shadow outer variable

This commit is contained in:
Daniel Langbein 2023-06-21 17:29:35 +02:00
parent 98e47b9a38
commit c36b3e1961

View File

@ -12,70 +12,73 @@ from de.p1st.monitor.loggers.swap import SwapLogger
from de.p1st.monitor.loggers.temp import TempLogger from de.p1st.monitor.loggers.temp import TempLogger
from de.p1st.monitor.logger import Logger from de.p1st.monitor.logger import Logger
def get_or_raise(cfg: configparser.SectionProxy, key: str) -> str: def get_or_raise(cfg: configparser.SectionProxy, key: str) -> str:
if key in cfg: if key in cfg:
return cfg[key] return cfg[key]
else: else:
raise LoggerArgEx(f'Missing key {key} in section {cfg.name}') raise LoggerArgEx(f'Missing key {key} in section {cfg.name}')
def get_loggers() -> tuple[list[Logger], list[LoggerArgEx]]: def get_loggers() -> tuple[list[Logger], list[LoggerArgEx]]:
def temp(cfg: configparser.SectionProxy) -> Logger: def temp(cfg_: configparser.SectionProxy) -> Logger:
sensor = get_or_raise(cfg, 'sensor') sensor = get_or_raise(cfg_, 'sensor')
label = get_or_raise(cfg, 'label') label = get_or_raise(cfg_, 'label')
warn_if_above = float(cfg['warn_if_above']) if 'warn_if_above' in cfg else None warn_if_above = float(cfg_['warn_if_above']) if 'warn_if_above' in cfg_ else None
warn_threshold = int(cfg.get('warn_threshold', '1')) warn_threshold = int(cfg_.get('warn_threshold', '1'))
warn_data_range = int(cfg.get('warn_data_range', '1')) warn_data_range = int(cfg_.get('warn_data_range', '1'))
return TempLogger(sensor, label, warn_if_above, warn_threshold, warn_data_range) return TempLogger(sensor, label, warn_if_above, warn_threshold, warn_data_range)
def cpu1(cfg: configparser.SectionProxy) -> Logger:
warn_if_above = float(cfg['warn_if_above']) if 'warn_if_above' in cfg else None def cpu1(cfg_: configparser.SectionProxy) -> Logger:
warn_threshold = int(cfg.get('warn_threshold', '1')) warn_if_above = float(cfg_['warn_if_above']) if 'warn_if_above' in cfg_ else None
warn_data_range = int(cfg.get('warn_data_range', '1')) warn_threshold = int(cfg_.get('warn_threshold', '1'))
warn_data_range = int(cfg_.get('warn_data_range', '1'))
return CPULogger1(warn_if_above, warn_threshold, warn_data_range) return CPULogger1(warn_if_above, warn_threshold, warn_data_range)
def cpu5(cfg: configparser.SectionProxy) -> Logger: def cpu5(cfg_: configparser.SectionProxy) -> Logger:
warn_if_above = float(cfg['warn_if_above']) if 'warn_if_above' in cfg else None warn_if_above = float(cfg_['warn_if_above']) if 'warn_if_above' in cfg_ else None
warn_threshold = int(cfg.get('warn_threshold', '1')) warn_threshold = int(cfg_.get('warn_threshold', '1'))
warn_data_range = int(cfg.get('warn_data_range', '1')) warn_data_range = int(cfg_.get('warn_data_range', '1'))
return CPULogger5(warn_if_above, warn_threshold, warn_data_range) return CPULogger5(warn_if_above, warn_threshold, warn_data_range)
def cpu15(cfg: configparser.SectionProxy) -> Logger: def cpu15(cfg_: configparser.SectionProxy) -> Logger:
warn_if_above = float(cfg['warn_if_above']) if 'warn_if_above' in cfg else None warn_if_above = float(cfg_['warn_if_above']) if 'warn_if_above' in cfg_ else None
warn_threshold = int(cfg.get('warn_threshold', '1')) warn_threshold = int(cfg_.get('warn_threshold', '1'))
warn_data_range = int(cfg.get('warn_data_range', '1')) warn_data_range = int(cfg_.get('warn_data_range', '1'))
return CPULogger15(warn_if_above, warn_threshold, warn_data_range) return CPULogger15(warn_if_above, warn_threshold, warn_data_range)
def net(cfg: configparser.SectionProxy) -> Logger: def net(cfg_: configparser.SectionProxy) -> Logger:
network_interface = get_or_raise(cfg, 'network_interface') network_interface = get_or_raise(cfg_, 'network_interface')
return NetworkLogger(network_interface) return NetworkLogger(network_interface)
def filesystem(cfg: configparser.SectionProxy) -> Logger: def filesystem(cfg_: configparser.SectionProxy) -> Logger:
uuid = cfg.get('uuid', None) uuid = cfg_.get('uuid', None)
mountpoint = Path(cfg.get('mountpoint')) if 'mountpoint' in cfg else None mountpoint = Path(cfg_.get('mountpoint')) if 'mountpoint' in cfg_ else None
unmounted_ok = bool(cfg.get('unmounted_ok', 'false')) unmounted_ok = bool(cfg_.get('unmounted_ok', 'false'))
warn_if_above = float(cfg.get('warn_if_above', '1.0')) warn_if_above = float(cfg_.get('warn_if_above', '1.0'))
warn_threshold = int(cfg.get('warn_threshold', '1')) warn_threshold = int(cfg_.get('warn_threshold', '1'))
warn_data_range = int(cfg.get('warn_data_range', '1')) warn_data_range = int(cfg_.get('warn_data_range', '1'))
return FilesystemLogger(uuid, mountpoint, unmounted_ok, warn_if_above, warn_threshold, warn_data_range) return FilesystemLogger(uuid, mountpoint, unmounted_ok, warn_if_above, warn_threshold, warn_data_range)
def drive(cfg: configparser.SectionProxy) -> Logger: def drive(cfg_: configparser.SectionProxy) -> Logger:
uuid = cfg.get('uuid', None) uuid = cfg_.get('uuid', None)
device = Path(cfg.get('device')) if 'device' in cfg else None device = Path(cfg_.get('device')) if 'device' in cfg_ else None
warn_if_above = int(cfg['warn_if_above']) if 'warn_if_above' in cfg else None warn_if_above = int(cfg_['warn_if_above']) if 'warn_if_above' in cfg_ else None
warn_threshold = int(cfg.get('warn_threshold', '1')) warn_threshold = int(cfg_.get('warn_threshold', '1'))
warn_data_range = int(cfg.get('warn_data_range', '1')) warn_data_range = int(cfg_.get('warn_data_range', '1'))
return DriveLogger(uuid, device, warn_if_above, warn_threshold, warn_data_range) return DriveLogger(uuid, device, warn_if_above, warn_threshold, warn_data_range)
def memory(cfg: configparser.SectionProxy) -> Logger: def memory(cfg_: configparser.SectionProxy) -> Logger:
warn_if_above = float(cfg.get('warn_if_above', '1.0')) warn_if_above = float(cfg_.get('warn_if_above', '1.0'))
warn_threshold = int(cfg.get('warn_threshold', '1')) warn_threshold = int(cfg_.get('warn_threshold', '1'))
warn_data_range = int(cfg.get('warn_data_range', '1')) warn_data_range = int(cfg_.get('warn_data_range', '1'))
return MemoryLogger(warn_if_above, warn_threshold, warn_data_range) return MemoryLogger(warn_if_above, warn_threshold, warn_data_range)
def swap(cfg: configparser.SectionProxy) -> Logger:
warn_if_above = float(cfg.get('warn_if_above', '1.0'))
warn_threshold = int(cfg.get('warn_threshold', '1'))
warn_data_range = int(cfg.get('warn_data_range', '1'))
return SwapLogger(warn_if_above, warn_threshold, warn_data_range)
def swap(cfg_: configparser.SectionProxy) -> Logger:
warn_if_above = float(cfg_.get('warn_if_above', '1.0'))
warn_threshold = int(cfg_.get('warn_threshold', '1'))
warn_data_range = int(cfg_.get('warn_data_range', '1'))
return SwapLogger(warn_if_above, warn_threshold, warn_data_range)
mapping = { mapping = {
'temp': temp, 'temp': temp,