/
configuration.py
63 lines (59 loc) · 1.87 KB
/
configuration.py
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
#!/usr/bin/env python3
##
# Nicolas THIBAUT
# nicolas.thibaut@uppersafe.com
##
# -*- coding: utf-8 -*-
import sys, os, yaml
class configuration(dict):
def __init__(self):
self.file = "config.default.yml"
if os.path.isfile("config.yml"):
self.file = "config.yml"
try:
self.reload()
except Exception as error:
print(error)
sys.exit()
def reload(self):
with open(self.file, "r+") as fp:
self.data = yaml.load(fp)
self.verify({
"db": str(),
"host": str(),
"port": int(),
"mode": str(),
"verbose": bool(),
"workers": int(),
"refreshDelay": int(),
"queryTimeout": int(),
"queryUserAgent": str(),
"groupRange": bool(),
"filterMode": str(),
"publish": str(),
"monitor": str(),
"network": {
"eth": str(),
"ppp": str(),
"tun": str()
},
"unbound": {
"verbosity": int(),
"hide-version": bool(),
"interface": str(),
"access-control": str()
},
"feeds": list(),
"exemptions": list()
}, self.data)
self.update(self.data)
return 0
def verify(self, conf, data):
for key, value in conf.items():
if key not in data:
raise Exception(str("Error in configuration = variable '{}' is not set").format(key))
if type(data.get(key)) != type(value):
raise Exception(str("Error in configuration = variable '{}' is not type '{}'").format(key, value.__class__.__name__))
if isinstance(data.get(key), dict):
self.verify(value, data.get(key))
return 0