From 5cbd5bb69f0f0547ff23594d867612bb9d8d66d1 Mon Sep 17 00:00:00 2001 From: Martin Zietz Date: Wed, 3 Feb 2021 13:04:56 +0100 Subject: [PATCH] first attempt at error messages --- cage_func.py | 30 ++++++++++++++++++++++-------- globals.py | 3 --- 2 files changed, 22 insertions(+), 11 deletions(-) diff --git a/cage_func.py b/cage_func.py index 69a5a81..0b07836 100644 --- a/cage_func.py +++ b/cage_func.py @@ -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 diff --git a/globals.py b/globals.py index 26033da..95dfcbc 100644 --- a/globals.py +++ b/globals.py @@ -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 = { - -}