From eb9ccddcccba33bf0909b98ebdbff51bfbaa67e4 Mon Sep 17 00:00:00 2001 From: erpalma Date: Sun, 1 Jul 2018 18:28:05 +0200 Subject: [PATCH] change sanity check code for config values --- lenovo_fix.py | 31 ++++++++++++++++++++++++------- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/lenovo_fix.py b/lenovo_fix.py index 314fc82..dad0ed1 100755 --- a/lenovo_fix.py +++ b/lenovo_fix.py @@ -30,6 +30,8 @@ VOLTAGE_PLANES = { 'ANALOGIO': 4, } +TRIP_TEMP_RANGE = (40, 98) + power = {'source': None, 'method': 'polling'} @@ -77,16 +79,31 @@ def load_config(): config = configparser.ConfigParser() config.read(CONFIG_PATH) + # config values sanity check for power_source in ('AC', 'BATTERY'): - assert 0 < config.getfloat(power_source, 'Update_Rate_s') - assert 0 < config.getfloat(power_source, 'PL1_Tdp_W') - assert 0 < config.getfloat(power_source, 'PL1_Duration_s') - assert 0 < config.getfloat(power_source, 'PL2_Tdp_W') - assert 0 < config.getfloat(power_source, 'PL2_Duration_S') - assert 40 < config.getfloat(power_source, 'Trip_Temp_C') < 98 + for option in ( + 'Update_Rate_s', + 'PL1_Tdp_W', + 'PL1_Duration_s', + 'PL2_Tdp_W', + 'PL2_Duration_S', + ): + config.set(power_source, option, str(max(0, config.getfloat(power_source, option)))) + + trip_temp = config.getfloat(power_source, 'Trip_Temp_C') + valid_trip_temp = min(TRIP_TEMP_RANGE[1], max(TRIP_TEMP_RANGE[0], trip_temp)) + if trip_temp != valid_trip_temp: + config.set(power_source, 'Trip_Temp_C', str(valid_trip_temp)) + print('[!] Overriding invalid "Trip_Temp_C" value in "{:s}": {:.1f} -> {:.1f}'.format( + power_source, trip_temp, valid_trip_temp)) for plane in VOLTAGE_PLANES: - assert config.getfloat('UNDERVOLT', plane) <= 0 + value = config.getfloat('UNDERVOLT', plane) + valid_value = min(0, value) + if value != valid_value: + config.set('UNDERVOLT', plane, str(valid_value)) + print('[!] Overriding invalid "UNDERVOLT" value in "{:s}" voltage plane: {:.0f} -> {:.0f}'.format( + plane, value, valid_value)) return config