change sanity check code for config values

This commit is contained in:
erpalma
2018-07-01 18:28:05 +02:00
parent b57bbbc42c
commit eb9ccddccc

View File

@@ -30,6 +30,8 @@ VOLTAGE_PLANES = {
'ANALOGIO': 4, 'ANALOGIO': 4,
} }
TRIP_TEMP_RANGE = (40, 98)
power = {'source': None, 'method': 'polling'} power = {'source': None, 'method': 'polling'}
@@ -77,16 +79,31 @@ def load_config():
config = configparser.ConfigParser() config = configparser.ConfigParser()
config.read(CONFIG_PATH) config.read(CONFIG_PATH)
# config values sanity check
for power_source in ('AC', 'BATTERY'): for power_source in ('AC', 'BATTERY'):
assert 0 < config.getfloat(power_source, 'Update_Rate_s') for option in (
assert 0 < config.getfloat(power_source, 'PL1_Tdp_W') 'Update_Rate_s',
assert 0 < config.getfloat(power_source, 'PL1_Duration_s') 'PL1_Tdp_W',
assert 0 < config.getfloat(power_source, 'PL2_Tdp_W') 'PL1_Duration_s',
assert 0 < config.getfloat(power_source, 'PL2_Duration_S') 'PL2_Tdp_W',
assert 40 < config.getfloat(power_source, 'Trip_Temp_C') < 98 '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: 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 return config