def merge_config_file(tplfilename, customtplfilename, newfilename, cfgdict, ipbxengine=None): """ Generate a configuration file from a template """ filename, filestat = _find_template_file(tplfilename, customtplfilename, newfilename) xdict = dict(cfgdict) if ipbxengine == 'asterisk': cfg = AsteriskConfigParser(filename=filename) newcfg = AsteriskConfigParser() putfunc = newcfg.append for directive in cfg.directives(): newcfg.add_directive(directive[0], directive[1]) else: cfg = OrderedConf.OrderedRawConf(filename=filename) newcfg = OrderedConf.OrderedRawConf(allow_multiple=False) putfunc = newcfg.set for sec in cfg: secname = sec.get_name() newcfg.add_section(secname) for opt in sec: optname = opt.get_name() if xdict.has_key(secname) \ and xdict[secname].has_key(optname): value = xdict[secname][optname] del xdict[secname][optname] if not xdict[secname]: del xdict[secname] else: value = opt.get_value() putfunc(secname, optname, value) for sec, options in xdict.iteritems(): if not newcfg.has_section(sec): newcfg.add_section(sec) for optname, optvalue in options.iteritems(): putfunc(sec, optname, optvalue) _write_config_file(newfilename, newcfg, filestat)
def asterisk_extconfig(tplfilename, customtplfilename, newfilename, extconfig, dbtype, dbname): """ Generate Asterisk extconfig.conf from a template """ filename, filestat = _find_template_file(tplfilename, customtplfilename, newfilename) customfile = customtplfilename == filename cfg = AsteriskConfigParser(filename=filename) newcfg = AsteriskConfigParser() for directive in cfg.directives(): newcfg.add_directive(directive[0], directive[1]) extcfg = dict(extconfig) if customfile and cfg.has_section('settings'): for optname in extcfg.keys(): if not cfg.has_option('settings', optname): del extcfg[optname] for sec in cfg: secname = sec.get_name() newcfg.add_section(secname) for opt in sec: optname = opt.get_name() optvalue = opt.get_value() if secname != 'settings' or not extcfg.has_key(optname): newcfg.append(secname, optname, optvalue) continue values = WIZARD_ASTERISK_EXTCONFIG_RE(optvalue) line = [] if not values.group(1) or not customfile: line.append(dbtype) else: line.append(values.group(1)) if not values.group(2) or not customfile: line.append('"%s"' % dbname) else: line.append(values.group(2)) if values.group(3) is None: line.append(extcfg[optname]) else: line.append(values.group(3)) newcfg.append(secname, optname, ','.join(line)) del extcfg[optname] for optname, table in extcfg.iteritems(): newcfg.append('settings', optname, '%s,"%s",%s' % (dbtype, dbname, table)) _write_config_file(newfilename, newcfg, filestat)
def asterisk_modules_config(tplfilename, customtplfilename, newfilename, modules): """ Generate Asterisk modules.conf from a template """ filename, filestat = _find_template_file(tplfilename, customtplfilename, newfilename) customfile = customtplfilename == filename cfg = AsteriskConfigParser(filename=filename) newcfg = AsteriskConfigParser() for directive in cfg.directives(): newcfg.add_directive(directive[0], directive[1]) mods = list(modules) appendmods = True if customfile and cfg.has_option('modules', 'noload'): for optname, optvalue in cfg.items('modules'): if optname == 'noload' and optvalue in mods: mods.remove(optvalue) for sec in cfg: secname = sec.get_name() newcfg.add_section(secname) for opt in sec: optname = opt.get_name() optvalue = opt.get_value() if secname != 'modules': newcfg.append(secname, optname, optvalue) continue if optname in ('preload', 'load', 'noload'): if appendmods: appendmods = False for module in mods: newcfg.append(secname, 'preload', module) if optvalue in mods: continue newcfg.append(secname, optname, optvalue) if appendmods: appendmods = False if not newcfg.has_section('modules'): newcfg.add_section('modules') for module in mods: newcfg.append('modules', 'preload', module) _write_config_file(newfilename, newcfg, filestat)