From 5113561a8cc4a94e3b0baa97a81a394651c8e5b5 Mon Sep 17 00:00:00 2001 From: Yannik Schmidt Date: Fri, 26 Feb 2021 12:54:34 +0100 Subject: [PATCH] refactor spacing in cfg parse --- src/main/python/config_parse.py | 134 +++++++++++++++----------------- 1 file changed, 64 insertions(+), 70 deletions(-) diff --git a/src/main/python/config_parse.py b/src/main/python/config_parse.py index bd4d09e..40ce57d 100644 --- a/src/main/python/config_parse.py +++ b/src/main/python/config_parse.py @@ -5,81 +5,75 @@ conf = None default_conf = None def parse_config(): - global conf - global default_conf - - conf = configparser.ConfigParser() - conf.read("ths_config.txt") - default_conf = configparser.ConfigParser() - default_conf.read("ths_readonly_default.conf") - - if conf == None or (len(conf.sections()) == 0 and len(default_conf.sections()) == 0): - print("Error: Missing configuration file, cannot continue") - raise Exception("Missing configuration file") + global conf + global default_conf + + conf = configparser.ConfigParser() + conf.read("ths_config.txt") + default_conf = configparser.ConfigParser() + default_conf.read("ths_readonly_default.conf") + + if conf == None or (len(conf.sections()) == 0 and len(default_conf.sections()) == 0): + print("Error: Missing configuration file, cannot continue") + raise Exception("Missing configuration file") def get_keys(like=None): - ret = conf["plot"].keys() - if like != None: - ret = list(filter(lambda x:like in x,ret)) - if len(ret) == 0: - print("No options that contain the string '%s'"%like) - return "" - return ret + ret = conf["plot"].keys() + if like != None: + ret = list(filter(lambda x:like in x,ret)) + if len(ret) == 0: + print("No options that contain the string '%s'"%like) + return "" + return ret def change_cfg(key,value): - global conf - if conf == None: - parse_config() - confs = conf["plot"] - v = str(value) - key = str(key) - if key not in confs: - return False - else: - confs[key] = value - return True + global conf + if conf == None: + parse_config() + confs = conf["plot"] + v = str(value) + key = str(key) + if key not in confs: + return False + else: + confs[key] = value + return True - - - def CFG(tag): - global conf - global default_conf - - if conf == None: - parse_config() - if len(default_conf.sections()) > 0: - default_confs = default_conf["plot"] - else: - default_confs = None - confs = conf["plot"] - - if tag in confs: - return parse_cfg(confs[tag]) - elif default_confs != None and tag in default_confs: - print("Warning: %s no found in configuration, defaulting to %s" % (str(tag),str(default_conf[tag])),sys.stderr) - return parse_cfg(default_confs[tag]) - else: - raise Exception("Error: configuration option %s not found in configuration and no default value for it, cannot continue, exit." % str(tag)) + global conf + global default_conf + + if conf == None: + parse_config() + if len(default_conf.sections()) > 0: + default_confs = default_conf["plot"] + else: + default_confs = None + confs = conf["plot"] + + if tag in confs: + return parse_cfg(confs[tag]) + elif default_confs != None and tag in default_confs: + print("Warning: %s no found in configuration, defaulting to %s" % (str(tag),str(default_conf[tag])),sys.stderr) + return parse_cfg(default_confs[tag]) + else: + raise Exception("Error: configuration option %s not found in configuration and no default value for it, cannot continue, exit." % str(tag)) def parse_cfg(c): - if c == None: - raise Exception("Config key (%s) found but has no value. Cannot continue, exit." % str(c)) - c = c.strip("'") - c = c.strip('"') - if c in ["yes","ja","True","Yes","Ja","true"]: - return True - if c in ["no","nein","False","No","Nein","false"]: - return False - try: - return int(c) - except ValueError: - pass - try: - return float(c) - except ValueError: - pass - return c - - -CFG("show_avg") + if c == None: + raise Exception("Config key (%s) found but has no value. Cannot continue, exit." % str(c)) + c = c.strip("'") + c = c.strip('"') + if c in ["yes","ja","True","Yes","Ja","true"]: + return True + if c in ["no","nein","False","No","Nein","false"]: + return False + try: + return int(c) + except ValueError: + pass + try: + return float(c) + except ValueError: + pass + return c