Files
Helmholtz_Test_Bench/config_handling.py
T
2021-02-07 15:15:05 +01:00

116 lines
5.5 KiB
Python

from configparser import ConfigParser
from tkinter import messagebox
import globals as g
import cage_func as func
# noinspection PyPep8Naming
import User_Interface as ui
global CONFIG_FILE # variable storing the path of the used config file
global CONFIG_OBJECT # object of type ConfigParser(), storing all configuration information
def get_config_from_file(file):
config_object = ConfigParser() # initialize config parser
config_object.read(file) # open config file
return config_object # return config object, that contains all info from the file
def write_config_to_file(config_object): # write contents of config object to a config file
with open(CONFIG_FILE, 'w') as conf: # Write changes to config file
config_object.write(conf)
def read_from_config(section, key, config_object): # read specific value from config object
try:
section_obj = config_object[section] # get relevant section
value = section_obj[key] # get relevant value in the section
return value
except KeyError as e:
ui.ui_print("Error while reading config file:", e)
raise KeyError("Could not find key", key, "in config file.")
def edit_config(section, key, value, override=False): # edit specific value in config file
config_object = CONFIG_OBJECT
# ToDo: add check for data types, e.g. int for arduino ports
# Check if value to write is within acceptable limits (set in globals.py)
try:
value_ok = 'OK'
if section in g.AXIS_NAMES and not override: # only check numerical values and not if overridden by user
value_ok = func.value_in_limits(section, key, value) # check if value is ok, too high or too low
max_value = g.default_arrays[key][1][g.AXIS_NAMES.index(section)] # get max value
min_value = g.default_arrays[key][2][g.AXIS_NAMES.index(section)] # get min value
if value_ok == 'HIGH':
message = "Prevented writing too high value for {s} {k} to config file:\n" \
"{v}, max. {mv} allowed. Erroneous values may damage equipment!" \
.format(s=section, k=key, v=value, mv=max_value)
raise ValueError(message)
elif value_ok == 'LOW':
message = "Prevented writing too low value for {s} {k} to config file:\n" \
"{v}, max. {mv} allowed. Erroneous values may damage equipment!" \
.format(s=section, k=key, v=value, mv=min_value)
raise ValueError(message)
if value_ok == 'OK' or override: # value is within limits or user has overridden
try:
section_obj = config_object[section] # get relevant section
except KeyError:
ui.ui_print("Could not find section", section, "in config file, creating new.")
config_object.add_section(section)
section_obj = config_object[section]
try:
section_obj[key] = str(value) # set relevant value in the section
except KeyError:
ui.ui_print("Could not find key", key, "in config file, creating new.")
config_object.set(section, key, str(value))
except KeyError as e:
ui.ui_print("Error while editing config file:", e)
raise KeyError("Could not find key", key, "in config file.")
def check_config(config_object): # check all numeric values in the config and see if they are within safe limits
ui.ui_print("Checking config file for values exceeding limits:")
i = 0
concerns = {} # initialize dictionary for found problems
problem_counter = 0
for axis in g.AXIS_NAMES:
concerns[axis] = [] # create dictionary entry for this axis
for key in g.default_arrays.keys(): # go over entries in this axis
value = float(read_from_config(axis, key, config_object)) # read value to check from config file
max_value = g.default_arrays[key][1][i] # get max value
min_value = g.default_arrays[key][2][i] # get min value
if not min_value <= value <= max_value: # value is not in safe limits
concerns[axis].append(key) # add this entry to the problem dictionary
problem_counter += 1
if len(concerns[axis]) == 0:
concerns[axis].append("No problems detected.")
ui.ui_print(axis, ":", *concerns[axis]) # print out results for this axis
i += 1
if problem_counter > 0: # some values are not ok
# shop pup-up warning message:
messagebox.showwarning("Warning!", "Found values exceeding limits in config file. Check values "
"to ensure correct operation and avoid equipment damage!")
g.app.show_frame(ui.Configuration) # open configuration window so user can check values
def reset_config_to_default(file): # reset values in config object to defaults (stored in globals.py)
config = ConfigParser() # initialize global config object
global CONFIG_OBJECT
CONFIG_OBJECT = config
i = 0
for axis_name in g.AXIS_NAMES: # go through axes
config.add_section(axis_name) # add section for this axis
for key in g.default_arrays.keys(): # go through dictionary with default values
config.set(axis_name, key, str(g.default_arrays[key][0][i])) # set value
i += 1
config.add_section("PORTS") # add section for PSU serial ports
for key in g.default_ports.keys():
config.set("PORTS", key, str(g.default_ports[key]))