From c36b3e19612d09b4bf863b5bcb7a557a8ebd832b Mon Sep 17 00:00:00 2001 From: Daniel Langbein Date: Wed, 21 Jun 2023 17:29:35 +0200 Subject: [PATCH] refactor: don't shadow outer variable --- src/de/p1st/monitor/cfg/loggers.py | 87 +++++++++++++++--------------- 1 file changed, 45 insertions(+), 42 deletions(-) diff --git a/src/de/p1st/monitor/cfg/loggers.py b/src/de/p1st/monitor/cfg/loggers.py index b8df095..822b38c 100644 --- a/src/de/p1st/monitor/cfg/loggers.py +++ b/src/de/p1st/monitor/cfg/loggers.py @@ -12,70 +12,73 @@ from de.p1st.monitor.loggers.swap import SwapLogger from de.p1st.monitor.loggers.temp import TempLogger from de.p1st.monitor.logger import Logger + def get_or_raise(cfg: configparser.SectionProxy, key: str) -> str: if key in cfg: return cfg[key] else: raise LoggerArgEx(f'Missing key {key} in section {cfg.name}') + def get_loggers() -> tuple[list[Logger], list[LoggerArgEx]]: - def temp(cfg: configparser.SectionProxy) -> Logger: - sensor = get_or_raise(cfg, 'sensor') - label = get_or_raise(cfg, 'label') - 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_data_range = int(cfg.get('warn_data_range', '1')) + def temp(cfg_: configparser.SectionProxy) -> Logger: + sensor = get_or_raise(cfg_, 'sensor') + label = get_or_raise(cfg_, 'label') + 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_data_range = int(cfg_.get('warn_data_range', '1')) 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 - warn_threshold = int(cfg.get('warn_threshold', '1')) - warn_data_range = int(cfg.get('warn_data_range', '1')) + + def cpu1(cfg_: configparser.SectionProxy) -> Logger: + 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_data_range = int(cfg_.get('warn_data_range', '1')) return CPULogger1(warn_if_above, warn_threshold, warn_data_range) - def cpu5(cfg: configparser.SectionProxy) -> Logger: - 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_data_range = int(cfg.get('warn_data_range', '1')) + def cpu5(cfg_: configparser.SectionProxy) -> Logger: + 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_data_range = int(cfg_.get('warn_data_range', '1')) return CPULogger5(warn_if_above, warn_threshold, warn_data_range) - def cpu15(cfg: configparser.SectionProxy) -> Logger: - 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_data_range = int(cfg.get('warn_data_range', '1')) + def cpu15(cfg_: configparser.SectionProxy) -> Logger: + 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_data_range = int(cfg_.get('warn_data_range', '1')) return CPULogger15(warn_if_above, warn_threshold, warn_data_range) - def net(cfg: configparser.SectionProxy) -> Logger: - network_interface = get_or_raise(cfg, 'network_interface') + def net(cfg_: configparser.SectionProxy) -> Logger: + network_interface = get_or_raise(cfg_, 'network_interface') return NetworkLogger(network_interface) - def filesystem(cfg: configparser.SectionProxy) -> Logger: - uuid = cfg.get('uuid', None) - mountpoint = Path(cfg.get('mountpoint')) if 'mountpoint' in cfg else None - unmounted_ok = bool(cfg.get('unmounted_ok', 'false')) - 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')) + def filesystem(cfg_: configparser.SectionProxy) -> Logger: + uuid = cfg_.get('uuid', None) + mountpoint = Path(cfg_.get('mountpoint')) if 'mountpoint' in cfg_ else None + unmounted_ok = bool(cfg_.get('unmounted_ok', 'false')) + 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 FilesystemLogger(uuid, mountpoint, unmounted_ok, warn_if_above, warn_threshold, warn_data_range) - def drive(cfg: configparser.SectionProxy) -> Logger: - uuid = cfg.get('uuid', 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_threshold = int(cfg.get('warn_threshold', '1')) - warn_data_range = int(cfg.get('warn_data_range', '1')) + def drive(cfg_: configparser.SectionProxy) -> Logger: + uuid = cfg_.get('uuid', 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_threshold = int(cfg_.get('warn_threshold', '1')) + warn_data_range = int(cfg_.get('warn_data_range', '1')) return DriveLogger(uuid, device, warn_if_above, warn_threshold, warn_data_range) - def memory(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')) + def memory(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 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 = { 'temp': temp,