summaryrefslogtreecommitdiff
path: root/contrib/scripts/sip_to_pjsip/sqlconfigparser.py
diff options
context:
space:
mode:
Diffstat (limited to 'contrib/scripts/sip_to_pjsip/sqlconfigparser.py')
-rw-r--r--contrib/scripts/sip_to_pjsip/sqlconfigparser.py69
1 files changed, 69 insertions, 0 deletions
diff --git a/contrib/scripts/sip_to_pjsip/sqlconfigparser.py b/contrib/scripts/sip_to_pjsip/sqlconfigparser.py
new file mode 100644
index 000000000..e87224ff1
--- /dev/null
+++ b/contrib/scripts/sip_to_pjsip/sqlconfigparser.py
@@ -0,0 +1,69 @@
+from astconfigparser import MultiOrderedConfigParser
+
+import MySQLdb
+import traceback
+
+class SqlConfigParser(MultiOrderedConfigParser):
+
+ _tablename = "sippeers"
+
+ def __init__(self,tablename="sippeers"):
+ self._tablename=tablename
+ MultiOrderedConfigParser.__init__(self)
+
+ def connect(self, user, password, host, port, database):
+ self.cnx = MySQLdb.connect(user=user,passwd=password,host=host,port=int(port),db=database)
+
+ def read(self, filename, sect=None):
+ MultiOrderedConfigParser.read(self, filename, sect)
+ # cursor = self.cnx.cursor(dictionary=True)
+ cursor = self.cnx.cursor(cursorclass=MySQLdb.cursors.DictCursor)
+ cursor.execute("SELECT * from `" + MySQLdb.escape_string(self._tablename) + "`")
+ rows = cursor.fetchall()
+
+ for row in rows:
+ sect = self.add_section(row['name'])
+ for key in row:
+ if (row[key] != None):
+ for elem in str(row[key]).split(";"):
+ sect[key] = elem
+ #sect[key] = str(row[key]).split(";")
+
+ def write_dicts(self, config_file, mdicts):
+ """Write the contents of the mdicts to the specified config file"""
+ for section, sect_list in mdicts.iteritems():
+ # every section contains a list of dictionaries
+ for sect in sect_list:
+ sql = "INSERT INTO "
+ if (sect.get('type')[0] == "endpoint"):
+ sql += "ps_endpoints "
+ elif (sect.get('type')[0] == "aor" and section != "sbc"):
+ sql += "ps_aors "
+ elif (sect.get('type')[0] == "identify"):
+ sql += "ps_endpoint_id_ips"
+ else:
+ continue
+
+ sql += " SET `id` = " + "\"" + MySQLdb.escape_string(section) + "\""
+ for key, val_list in sect.iteritems():
+ if key == "type":
+ continue
+ # every value is also a list
+
+ key_val = " `" + key + "`"
+ key_val += " = " + "\"" + MySQLdb.escape_string(";".join(val_list)) + "\""
+ sql += ","
+ sql += key_val
+
+ config_file.write("%s;\n" % (sql))
+
+ def write(self, config_file):
+ """Write configuration information out to a file"""
+ try:
+ self.write_dicts(config_file, self._sections)
+ except Exception,e:
+ print "Could not open file ", config_file, " for writing"
+ traceback.print_exc()
+
+
+