added default section to config load and save
This commit is contained in:
parent
e4231ce306
commit
4896d2e0c9
1 changed files with 35 additions and 12 deletions
|
@ -11,30 +11,52 @@ CONFFILE = 'pantalaimon.conf'
|
||||||
def load():
|
def load():
|
||||||
entries = []
|
entries = []
|
||||||
|
|
||||||
|
loglevels = [ "warning", "info", "debug", "error" ]
|
||||||
|
|
||||||
|
defaults = {
|
||||||
|
'name': "Default",
|
||||||
|
'loglevel': "warning",
|
||||||
|
'loglevel_idx': 0,
|
||||||
|
'notifications': True
|
||||||
|
}
|
||||||
|
|
||||||
if os.path.exists(CONFDIR + CONFFILE):
|
if os.path.exists(CONFDIR + CONFFILE):
|
||||||
config = configparser.ConfigParser()
|
config = configparser.ConfigParser()
|
||||||
config.read_file(open(CONFDIR + CONFFILE))
|
config.read_file(open(CONFDIR + CONFFILE))
|
||||||
|
|
||||||
for instance in config.sections():
|
for instance in config.sections():
|
||||||
|
|
||||||
item = {'name': instance}
|
item = {'name': instance}
|
||||||
item['homeserver'] = config[instance].get('homeserver')
|
|
||||||
item['listenaddress'] = config[instance].get('listenaddress')
|
|
||||||
item['listenport'] = config[instance].get('listenport')
|
|
||||||
item['proxy'] = config[instance].get('proxy')
|
|
||||||
item['ssl'] = config[instance].getboolean('ssl', True)
|
|
||||||
item['ignoreverification'] = config[instance].getboolean('ignoreverification', True)
|
|
||||||
item['usekeyring'] = config[instance].get('usekeyring')
|
|
||||||
|
|
||||||
entries.append(item)
|
if instance == "Default":
|
||||||
|
item['loglevel'] = config[instance].get('loglevel', "warning")
|
||||||
|
item['loglevel_idx'] = loglevels.index(config[instance].get('loglevel', 0))
|
||||||
|
item['notifications'] = config[instance].getboolean('notifications', True)
|
||||||
|
defaults = item
|
||||||
|
|
||||||
return entries
|
else:
|
||||||
|
item['homeserver'] = config[instance].get('homeserver')
|
||||||
|
item['listenaddress'] = config[instance].get('listenaddress')
|
||||||
|
item['listenport'] = config[instance].get('listenport')
|
||||||
|
item['proxy'] = config[instance].get('proxy')
|
||||||
|
item['ssl'] = config[instance].getboolean('ssl', True)
|
||||||
|
item['ignoreverification'] = config[instance].getboolean('ignoreverification', True)
|
||||||
|
item['usekeyring'] = config[instance].get('usekeyring')
|
||||||
|
entries.append(item)
|
||||||
|
|
||||||
def save(data):
|
return (defaults, entries)
|
||||||
|
|
||||||
|
def save(defaults, data):
|
||||||
logging.debug("save config")
|
logging.debug("save config")
|
||||||
dataobj = json.loads(data)
|
dataobj = json.loads(data)
|
||||||
|
|
||||||
config = configparser.ConfigParser()
|
config = configparser.ConfigParser()
|
||||||
|
|
||||||
|
defobj = json.loads(defaults)
|
||||||
|
config['Default'] = {
|
||||||
|
'loglevel': defobj['loglevel']
|
||||||
|
}
|
||||||
|
|
||||||
for item in dataobj:
|
for item in dataobj:
|
||||||
if item['ssl']:
|
if item['ssl']:
|
||||||
sslval = 'true'
|
sslval = 'true'
|
||||||
|
@ -49,8 +71,9 @@ def save(data):
|
||||||
config[item['name']]['ignoreverification'] = "true"
|
config[item['name']]['ignoreverification'] = "true"
|
||||||
config[item['name']]['usekeyring'] = "false"
|
config[item['name']]['usekeyring'] = "false"
|
||||||
|
|
||||||
if len(item['proxy']) > 0:
|
if 'proxy' in item:
|
||||||
config[item['name']]['proxy'] = item['proxy']
|
if len(item['proxy']) > 0:
|
||||||
|
config[item['name']]['proxy'] = item['proxy']
|
||||||
|
|
||||||
if not os.path.exists(CONFDIR):
|
if not os.path.exists(CONFDIR):
|
||||||
os.makedirs(CONFDIR)
|
os.makedirs(CONFDIR)
|
||||||
|
|
Loading…
Reference in a new issue