read_conf.py
3.32 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
from opensipkd.tcp.connection import join_ip_port
from configparser import (
ConfigParser,
NoOptionError,
)
PREFIX_SECTION = 'host_'
listen_ports = list()
ip_conf = dict()
name_conf = dict()
allowed_ips = list()
def get_conf(ip, port):
key = join_ip_port(ip, port)
return ip_conf[key]
def get_str(conf, section, option, default):
try:
return conf.get(section, option)
except NoOptionError:
return default
def get_boolean(conf, section, option, default):
try:
return conf.getboolean(section, option)
except NoOptionError:
return default
def get_int(conf, section, option, default):
try:
return conf.getint(section, option)
except NoOptionError:
return default
def get_list(conf, section, option, default):
s = get_str(conf, section, option, '')
if s:
return s.split()
return default
MSG_DUPLICATE = 'IP {ip} port {port} ganda. Perbaiki konfigurasi.'
def validate_ip_port(cfg):
ip_port = join_ip_port(cfg['ip'], cfg['port'])
if ip_port not in ip_conf:
return ip_port
msg = MSG_DUPLICATE.format(ip=cfg['ip'], port=cfg['port'])
raise Exception(msg)
def get_module_object(name):
module_obj = __import__(name)
sub_obj = None
for sub in name.split('.')[1:]:
if sub_obj:
sub_obj = getattr(sub_obj, sub)
else:
sub_obj = getattr(module_obj, sub)
return sub_obj
def get_streamer_class(name):
obj = get_module_object('opensipkd.streamer.' + name)
return obj.Streamer
def append_others(cfg, conf, section):
for key, val in conf.items(section):
if key in cfg:
continue
cfg[key] = val
def load_module(cfg, conf, section):
cfg['module'] = conf.get(section, 'module')
cfg['module_obj'] = get_module_object(cfg['module'])
append_others(cfg, conf, section)
module_section = 'module_' + cfg['module']
if conf.has_section(module_section):
append_others(cfg, conf, module_section)
try:
f_init = getattr(cfg['module_obj'], 'init')
f_init(cfg)
except AttributeError:
pass
def read_section(conf, section):
cfg = dict()
cfg['ip'] = conf.get(section, 'ip')
cfg['port'] = conf.getint(section, 'port')
ip_port = validate_ip_port(cfg)
cfg['name'] = section.lstrip(PREFIX_SECTION)
cfg['streamer'] = get_str(conf, section, 'streamer', 'none')
cfg['streamer_cls'] = get_streamer_class(cfg['streamer'])
cfg['ip'] = conf.get(section, 'ip')
cfg['port'] = conf.getint(section, 'port')
cfg['listen'] = get_boolean(conf, section, 'listen', True)
cfg['echo'] = get_boolean(conf, section, 'echo', not cfg['listen'])
cfg['timeout'] = get_int(conf, section, 'timeout', 60)
load_module(cfg, conf, section)
if cfg['listen']:
if cfg['port'] not in listen_ports:
listen_ports.append(cfg['port'])
if cfg['ip'] not in allowed_ips:
allowed_ips.append(cfg['ip'])
ip_conf[ip_port] = name_conf[cfg['name']] = dict(cfg)
def read_conf(conf_file):
conf = ConfigParser()
conf.read(conf_file)
for section in conf.sections():
if section.find(PREFIX_SECTION) != 0:
continue
if not get_boolean(conf, section, 'active', True):
continue
read_section(conf, section)
return ip_conf