import configparser import json import logging import os logging.basicConfig(level=logging.DEBUG) CONFDIR = '/home/phablet/.config/pantalaimon/' CONFFILE = 'pantalaimon.conf' def load(): entries = [] if os.path.exists(CONFDIR + CONFFILE): config = configparser.ConfigParser() config.read_file(open(CONFDIR + CONFFILE)) for instance in config.sections(): 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) return entries def save(data): logging.debug("save config") dataobj = json.loads(data) config = configparser.ConfigParser() for item in dataobj: if item['ssl']: sslval = 'true' else: sslval = 'false' config[item['name']] = {} config[item['name']]['homeserver'] = item['homeserver'] config[item['name']]['listenaddress'] = "127.0.0.1" config[item['name']]['listenport'] = item['listenport'] config[item['name']]['ssl'] = sslval config[item['name']]['ignoreverification'] = "true" config[item['name']]['usekeyring'] = "false" if len(item['proxy']) > 0: config[item['name']]['proxy'] = item['proxy'] if not os.path.exists(CONFDIR): os.makedirs(CONFDIR) with open(CONFDIR + CONFFILE,'w') as configfile: config.write(configfile)