def write_db(self, config_dir, meta, engine, conn): """Convert file contents into database entries Keyword Arguments: config_dir: Location of file to convert meta: sqlalchemy Metadata engine: sqlalchemy Engine used for database management conn: sqlaclemy Connection to the database """ conf = astconfigparser.MultiOrderedConfigParser() conf.read(os.path.join(config_dir, self.filename)) for title, sections in conf.sections().iteritems(): LOGGER.info("Inspecting objects with title {0}".format(title)) for section in sections: obj_type = section.get('type')[0] sorcery_section = self.find_section_for_object(obj_type) if not sorcery_section: LOGGER.info("No corresponding section found for object " "type {0}".format(obj_type)) continue table = Table(self.sections[sorcery_section][obj_type], meta, autoload=True, autoload_with=engine) vals = {'id': title} for key in section.keys(): if key != 'type': vals[key] = ";".join( value.replace(";", "^3B") for value in section.get(key)) conn.execute(table.insert().values(**vals))
def convert(sip, filename, non_mappings, include): """ Entry point for configuration file conversion. This function will create a pjsip.conf object and begin to map specific sections from sip.conf into it. Returns the new pjsip.conf object once completed """ pjsip = astconfigparser.MultiOrderedConfigParser() non_mappings[filename] = astdicts.MultiOrderedDict() nmapped = non_mapped(non_mappings[filename]) if not include: # Don't duplicate transport and registration configs map_system(sip, pjsip, nmapped) map_transports(sip, pjsip, nmapped) map_registrations(sip, pjsip, nmapped) map_auth(sip, pjsip, nmapped) for section in sip.sections(): if section == 'authentication': pass else: map_peer(sip, section, pjsip, nmapped) find_non_mapped(sip.defaults(), nmapped) find_non_mapped(sip.sections(), nmapped) for key, val in sip.includes().iteritems(): pjsip.add_include(PREFIX + key, convert(val, PREFIX + key, non_mappings, True)[0]) return pjsip, non_mappings
def convert(sip, filename, non_mappings): res_sip = astconfigparser.MultiOrderedConfigParser() non_mappings[filename] = astdicts.MultiOrderedDict() nmapped = non_mapped(non_mappings[filename]) for section in sip.sections(): if section == 'authentication': pass else: map_peer(sip, section, res_sip, nmapped) find_non_mapped(sip.defaults(), nmapped) find_non_mapped(sip.sections(), nmapped) for key, val in sip.includes().iteritems(): res_sip.add_include(PREFIX + key, convert(val, PREFIX + key, non_mappings)[0]) return res_sip, non_mappings
"The input-file defaults to 'sip.conf'.\n" \ "The output-file defaults to 'pjsip.conf'." parser = optparse.OptionParser(usage=usage) parser.add_option('-p', '--prefix', dest='prefix', default=PREFIX, help='output prefix for include files') options, args = parser.parse_args() PREFIX = options.prefix sip_filename = args[0] if len(args) else 'sip.conf' pjsip_filename = args[1] if len(args) == 2 else 'pjsip.conf' return sip_filename, pjsip_filename if __name__ == "__main__": sip_filename, pjsip_filename = cli_options() # configuration parser for sip.conf sip = astconfigparser.MultiOrderedConfigParser() print('Please, report any issue at:') print(' https://issues.asterisk.org/') print('Reading ' + sip_filename) sip.read(sip_filename) print('Converting to PJSIP...') pjsip, non_mappings = convert(sip, pjsip_filename, dict(), False) print('Writing ' + pjsip_filename) write_pjsip(pjsip_filename, pjsip, non_mappings)