~thrrgilag/pantalaimon-ut

pantalaimon-ut/src/config.py -rw-r--r-- 2.7 KiB
cb1aa162Morgan McMillian updated links in README 7 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import configparser
import json
import logging
import os

logging.basicConfig(level=logging.DEBUG)

CONFDIR = '/home/phablet/.config/pantalaimon/'
CONFFILE = 'pantalaimon.conf'

def load():
    entries = []

    loglevels = [ "warning", "info", "debug", "error" ]

    defaults = {
        'name': "Default",
        'loglevel': "warning",
        'loglevel_idx': 0,
        'notifications': True
    }

    if os.path.exists(CONFDIR + CONFFILE):
        config = configparser.ConfigParser()
        config.read_file(open(CONFDIR + CONFFILE))

        for instance in config.sections():

            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

            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)

    return (defaults, entries)

def save(defaults, data):
    logging.debug("save config")
    dataobj = json.loads(data)

    config = configparser.ConfigParser()

    defobj = json.loads(defaults)
    config['Default'] = {
        'loglevel': defobj['loglevel']
    }

    if defobj['loglevel'] == 'debug':
        config['Default']['DebugEncryption'] = 'true'
    else:
        config['Default']['DebugEncryption'] = 'false'

    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 'proxy' in item:
            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)