mirror of
https://github.com/FAUSheppy/ths-datenlogger
synced 2025-12-06 12:11:35 +01:00
refactor spacing in cfg parse
This commit is contained in:
@@ -5,81 +5,75 @@ conf = None
|
|||||||
default_conf = None
|
default_conf = None
|
||||||
|
|
||||||
def parse_config():
|
def parse_config():
|
||||||
global conf
|
global conf
|
||||||
global default_conf
|
global default_conf
|
||||||
|
|
||||||
conf = configparser.ConfigParser()
|
conf = configparser.ConfigParser()
|
||||||
conf.read("ths_config.txt")
|
conf.read("ths_config.txt")
|
||||||
default_conf = configparser.ConfigParser()
|
default_conf = configparser.ConfigParser()
|
||||||
default_conf.read("ths_readonly_default.conf")
|
default_conf.read("ths_readonly_default.conf")
|
||||||
|
|
||||||
if conf == None or (len(conf.sections()) == 0 and len(default_conf.sections()) == 0):
|
if conf == None or (len(conf.sections()) == 0 and len(default_conf.sections()) == 0):
|
||||||
print("Error: Missing configuration file, cannot continue")
|
print("Error: Missing configuration file, cannot continue")
|
||||||
raise Exception("Missing configuration file")
|
raise Exception("Missing configuration file")
|
||||||
|
|
||||||
def get_keys(like=None):
|
def get_keys(like=None):
|
||||||
ret = conf["plot"].keys()
|
ret = conf["plot"].keys()
|
||||||
if like != None:
|
if like != None:
|
||||||
ret = list(filter(lambda x:like in x,ret))
|
ret = list(filter(lambda x:like in x,ret))
|
||||||
if len(ret) == 0:
|
if len(ret) == 0:
|
||||||
print("No options that contain the string '%s'"%like)
|
print("No options that contain the string '%s'"%like)
|
||||||
return ""
|
return ""
|
||||||
return ret
|
return ret
|
||||||
|
|
||||||
def change_cfg(key,value):
|
def change_cfg(key,value):
|
||||||
global conf
|
global conf
|
||||||
if conf == None:
|
if conf == None:
|
||||||
parse_config()
|
parse_config()
|
||||||
confs = conf["plot"]
|
confs = conf["plot"]
|
||||||
v = str(value)
|
v = str(value)
|
||||||
key = str(key)
|
key = str(key)
|
||||||
if key not in confs:
|
if key not in confs:
|
||||||
return False
|
return False
|
||||||
else:
|
else:
|
||||||
confs[key] = value
|
confs[key] = value
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def CFG(tag):
|
def CFG(tag):
|
||||||
global conf
|
global conf
|
||||||
global default_conf
|
global default_conf
|
||||||
|
|
||||||
if conf == None:
|
if conf == None:
|
||||||
parse_config()
|
parse_config()
|
||||||
if len(default_conf.sections()) > 0:
|
if len(default_conf.sections()) > 0:
|
||||||
default_confs = default_conf["plot"]
|
default_confs = default_conf["plot"]
|
||||||
else:
|
else:
|
||||||
default_confs = None
|
default_confs = None
|
||||||
confs = conf["plot"]
|
confs = conf["plot"]
|
||||||
|
|
||||||
if tag in confs:
|
if tag in confs:
|
||||||
return parse_cfg(confs[tag])
|
return parse_cfg(confs[tag])
|
||||||
elif default_confs != None and tag in default_confs:
|
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)
|
print("Warning: %s no found in configuration, defaulting to %s" % (str(tag),str(default_conf[tag])),sys.stderr)
|
||||||
return parse_cfg(default_confs[tag])
|
return parse_cfg(default_confs[tag])
|
||||||
else:
|
else:
|
||||||
raise Exception("Error: configuration option %s not found in configuration and no default value for it, cannot continue, exit." % str(tag))
|
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):
|
def parse_cfg(c):
|
||||||
if c == None:
|
if c == None:
|
||||||
raise Exception("Config key (%s) found but has no value. Cannot continue, exit." % str(c))
|
raise Exception("Config key (%s) found but has no value. Cannot continue, exit." % str(c))
|
||||||
c = c.strip("'")
|
c = c.strip("'")
|
||||||
c = c.strip('"')
|
c = c.strip('"')
|
||||||
if c in ["yes","ja","True","Yes","Ja","true"]:
|
if c in ["yes","ja","True","Yes","Ja","true"]:
|
||||||
return True
|
return True
|
||||||
if c in ["no","nein","False","No","Nein","false"]:
|
if c in ["no","nein","False","No","Nein","false"]:
|
||||||
return False
|
return False
|
||||||
try:
|
try:
|
||||||
return int(c)
|
return int(c)
|
||||||
except ValueError:
|
except ValueError:
|
||||||
pass
|
pass
|
||||||
try:
|
try:
|
||||||
return float(c)
|
return float(c)
|
||||||
except ValueError:
|
except ValueError:
|
||||||
pass
|
pass
|
||||||
return c
|
return c
|
||||||
|
|
||||||
|
|
||||||
CFG("show_avg")
|
|
||||||
|
|||||||
Reference in New Issue
Block a user