first attempt at error messages

This commit is contained in:
Martin Zietz
2021-02-03 13:04:56 +01:00
parent 2a2a538fd8
commit 5cbd5bb69f
2 changed files with 22 additions and 11 deletions
+22 -8
View File
@@ -7,6 +7,7 @@ import numpy as np
import serial
import traceback
from tkinter import *
from tkinter import messagebox
from configparser import ConfigParser
@@ -198,26 +199,39 @@ def read_config(section, key): # read specific value from config file
raise KeyError("Could not find key", key, "in config file.")
def edit_config(section, key, value): # edit specific value in config file
def edit_config(section, key, value, override=False): # edit specific value in config file
config_object = ConfigParser() # initialize config parser
value_ok = False
# Value checking:
# ToDo: make pop-up warning messages that can be waived
try:
if section in g.AXIS_NAMES: # only check numerical values
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)]
min_value = g.default_arrays[key][2][g.AXIS_NAMES.index(section)] # get min value
if value > max_value:
raise ValueError("Attempted to write too high value for", section, key, "to config file:",
value, ", max.", max_value, "allowed. Excessive values may damage equipment!")
message = "Attempted to write too high value for {s} {k} to config file:\n" \
"{v}, max. {mv} allowed. Excessive values may damage equipment!\n" \
"Do you really want to use this value?".format(s=section, k=key, v=value, mv=max_value)
raise ValueError(message)
elif value < min_value:
raise ValueError("Attempted to write too low value for", section, key, "to config file:",
value, ", max.", max_value, "allowed. Excessive values may damage equipment!")
message = "Attempted to write too low value for {s} {k} to config file:\n" \
"{v}, max. {mv} allowed. Excessive values may damage equipment!\n" \
"Do you really want to use this value?".format(s=section, k=key, v=value, mv=min_value)
raise ValueError(message)
else:
value_ok = True
except KeyError as e:
ui_print("Error while editing config file:", e)
raise KeyError("Could not find section", section, "in config file.")
except ValueError as e: # value too high/low
ui_print(e)
else: # no errors so far
value_ok = False
# display pop-up message to ask user if he really wants the value
answer = messagebox.askquestion("Value out of bounds", e) # becomes 'yes' or 'no' depending on user choice
if answer == 'yes': override = True
else: override = False
else: # no errors
value_ok = True
if value_ok or override: # value is ok or user has chosen to use it anyway
try:
config_object.read(g.CONFIG_FILE) # open config file
section_obj = config_object[section] # get relevant section
-3
View File
@@ -38,6 +38,3 @@ default_ports = {
"xy_port": "COM1", # Serial port where PSU for X- and Y-Axes is connected
"z_port": "COM2", # Serial port where PSU for Z-Axis is connected
}
maximums = {
}