From 4dc296a6f0eaf285f08e7d48453a9c445f36b607 Mon Sep 17 00:00:00 2001 From: Martin Zietz Date: Thu, 28 Jan 2021 17:38:15 +0100 Subject: [PATCH] added edit_config function --- cage_func.py | 23 ++++++++++++++++++----- globals.py | 6 ------ 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/cage_func.py b/cage_func.py index f23105b..140ca09 100644 --- a/cage_func.py +++ b/cage_func.py @@ -172,20 +172,33 @@ class ArduinoCtrl(Arduino): self.digitalWrite(pin, "LOW") -def read_config(section, key): # attempt to read config file +def read_config(section, key): # read specific value from config file # ToDo (optional) better error handling - file = g.CONFIG_FILE config_object = ConfigParser() try: - config_object.read(file) + config_object.read(g.CONFIG_FILE) section_obj = config_object[section] return section_obj[key] except KeyError as e: - ui_print("Error while reading config file: e") + ui_print("Error while reading config file:", e) raise KeyError("Could not find key", key, "in config file.") -def create_default_config(file): +def edit_config(section, key, value): # edit specific value in config file + config_object = ConfigParser() + try: + config_object.read(g.CONFIG_FILE) + section_obj = config_object[section] + section_obj[key] = str(value) + + with open(g.CONFIG_FILE, 'w') as conf: # Write changes back to file + config_object.write(conf) + except KeyError as e: + ui_print("Error while editing config file:", e) + raise KeyError("Could not find key", key, "in config file.") + + +def create_default_config(file): # create config file from default values (stored in globals.py) config = ConfigParser() i = 0 diff --git a/globals.py b/globals.py index 173f7c0..bf6bf24 100644 --- a/globals.py +++ b/globals.py @@ -17,12 +17,6 @@ AXIS_NAMES = ["X-Axis", "Y-Axis", "Z-Axis"] global CONFIG_FILE -'''global COIL_CONST -global RESISTANCES -global MAX_WATTS -global MAX_VOLTS -global AMBIENT_FIELD''' - global XY_PORT global Z_PORT global RELAY_PINS