pantalaimon-ut/src/config.py

84 lines
2.5 KiB
Python
Raw Normal View History

2020-07-14 00:59:16 +00:00
import configparser
import json
import logging
import os
logging.basicConfig(level=logging.DEBUG)
CONFDIR = '/home/phablet/.config/pantalaimon/'
CONFFILE = 'pantalaimon.conf'
2020-07-14 00:59:16 +00:00
def load():
entries = []
loglevels = [ "warning", "info", "debug", "error" ]
defaults = {
'name': "Default",
'loglevel': "warning",
'loglevel_idx': 0,
'notifications': True
}
if os.path.exists(CONFDIR + CONFFILE):
2020-07-14 00:59:16 +00:00
config = configparser.ConfigParser()
config.read_file(open(CONFDIR + CONFFILE))
2020-07-14 00:59:16 +00:00
for instance in config.sections():
2020-07-14 00:59:16 +00:00
item = {'name': instance}
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
2020-07-14 00:59:16 +00:00
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)
2020-07-14 00:59:16 +00:00
return (defaults, entries)
def save(defaults, data):
2020-07-14 00:59:16 +00:00
logging.debug("save config")
dataobj = json.loads(data)
config = configparser.ConfigParser()
defobj = json.loads(defaults)
config['Default'] = {
'loglevel': defobj['loglevel']
}
2020-07-14 00:59:16 +00:00
for item in dataobj:
2020-09-03 22:04:34 +00:00
if item['ssl']:
sslval = 'true'
else:
sslval = 'false'
2020-09-04 04:39:10 +00:00
2020-07-14 00:59:16 +00:00
config[item['name']] = {}
config[item['name']]['homeserver'] = item['homeserver']
config[item['name']]['listenaddress'] = "127.0.0.1"
config[item['name']]['listenport'] = item['listenport']
2020-09-03 22:04:34 +00:00
config[item['name']]['ssl'] = sslval
2020-07-14 00:59:16 +00:00
config[item['name']]['ignoreverification'] = "true"
config[item['name']]['usekeyring'] = "false"
if 'proxy' in item:
if len(item['proxy']) > 0:
config[item['name']]['proxy'] = item['proxy']
2020-09-04 04:39:10 +00:00
if not os.path.exists(CONFDIR):
os.makedirs(CONFDIR)
with open(CONFDIR + CONFFILE,'w') as configfile:
2020-07-14 00:59:16 +00:00
config.write(configfile)