read_conf.py 3.69 KB
from opensipkd.tcp.connection import join_ip_port
try:
    from configparser import (
        ConfigParser,
        NoOptionError,
        )
except ImportError:
    from ConfigParser import (
        ConfigParser,
        NoOptionError,
        )


listen_ports = []
ip_conf = {}
name_conf = {}
allowed_ips = []
web = {} 


def get_conf(ip, port):
    key = join_ip_port(ip, port)
    return ip_conf[key]


def get_web_port():
    return 'port' in web and web['port']


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


MSG_DUPLICATE = 'IP {ip} port {port} ganda. Perbaiki konfigurasi.'

def validate_ip_port(ip_port):
    if ip_port in ip_conf:
        cfg = ip_conf[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):
    # Konfigurasi tambahan terkait host
    for key, val in conf.items(section):
        if key in cfg:
            continue
        cfg[key] = val
    # Konfigurasi tambahan terkait modul
    section = 'module_' + cfg['module']
    if not conf.has_section(section):
        return
    for key, val in conf.items(section):
        cfg[key] = val

 
def read_conf(conf_file):
    conf = ConfigParser()
    conf.read(conf_file)
    for section in conf.sections():
        if section == 'web':
            web['port'] = conf.getint(section, 'port')
            continue
        if section.find('host_') < 0:
            continue
        try:
            active = conf.getboolean(section, 'active')
        except NoOptionError:
            active = True 
        if not active:
            continue
        ip = conf.get(section, 'ip')
        port = conf.getint(section, 'port')
        ip_port = join_ip_port(ip, port)
        validate_ip_port(ip_port)
        cfg = dict()
        cfg['ip'] = ip
        cfg['port'] = port
        cfg['name'] = name = section.split('_')[1]
        cfg['streamer'] = get_str(conf, section, 'streamer', 'none')
        cfg['streamer_cls'] = get_streamer_class(cfg['streamer'])
        cfg['module'] = get_str(conf, section, 'module', 'opensipkd.iso8583.network')
        cfg['module_obj'] = get_module_object(cfg['module'])
        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)
        module_section = 'module_' + cfg['module']
        if conf.has_section(module_section):
            for key in conf.options(module_section):
                cfg[key] = conf.get(module_section, key)
        cfg['module_obj'].init(cfg)
        append_others(cfg, conf, section)
        if cfg['listen']:
            if port not in listen_ports:
                listen_ports.append(port)
            if ip not in allowed_ips:
                allowed_ips.append(ip)
        ip_conf[ip_port] = name_conf[name] = dict(cfg)