diff options
author | Peter Kozak <spag@golwen.net> | 2013-05-17 09:35:24 +0200 |
---|---|---|
committer | Peter Kozak <spag@golwen.net> | 2013-05-17 09:35:24 +0200 |
commit | 45347d2094d79eaecb3df7d234b9bd7e436275c7 (patch) | |
tree | 8c8696bca22c2ea8a61f6a05fa55c6a286ba2d0b /misc/mon_ami | |
parent | ca490d7215ffb6015573b690cd5b1a53d9b67e34 (diff) | |
parent | 6a1961283914a6b87400bf0b7b492fa9f8692c03 (diff) |
Merge branch 'develop' of github.com:amooma/GS4 into develop
Diffstat (limited to 'misc/mon_ami')
-rw-r--r-- | misc/mon_ami/mon_ami_main.py | 9 | ||||
-rw-r--r-- | misc/mon_ami/mysqlr.py | 135 |
2 files changed, 141 insertions, 3 deletions
diff --git a/misc/mon_ami/mon_ami_main.py b/misc/mon_ami/mon_ami_main.py index 72220e5..fe43618 100644 --- a/misc/mon_ami/mon_ami_main.py +++ b/misc/mon_ami/mon_ami_main.py @@ -9,7 +9,7 @@ from signal import signal, SIGHUP, SIGTERM, SIGINT from optparse import OptionParser from freeswitch import FreeswitchEventSocket from mon_ami_server import MonAMIServer -from sqliter import SQLiteR +from mysqlr import MySQLR def signal_handler(signal_number, frame): global event_socket @@ -40,7 +40,7 @@ def user_password_authentication(user_name, password): return True return False - db = SQLiteR(configuration_options.user_db_name) + db = MySQLR(configuration_options.user_db_name, configuration_options.user_db_user, configuration_options.user_db_password) if not db.connect(): lerror('cound not connect to user database "%s"' % configuration_options.user_db_name) return False @@ -53,6 +53,7 @@ def user_password_authentication(user_name, password): return True linfo('user-password authentication failed - user: %s, password: %s' % (user_name, '*' * len(str(password)))) + return False def main(): @@ -76,7 +77,9 @@ def main(): option_parser.add_option("-p", "--port", "--ami-port", action="store", type="int", dest="ami_port", default=5038) # User database - option_parser.add_option("--user-db-name", action="store", type="string", dest="user_db_name", default='/opt/GS5/db/development.sqlite3') + option_parser.add_option("--user-db-name", action="store", type="string", dest="user_db_name", default='gemeinschaft') + option_parser.add_option("--user-db-user", action="store", type="string", dest="user_db_user", default='gemeinschaft') + option_parser.add_option("--user-db-password", action="store", type="string", dest="user_db_password", default='gemeinschaft') option_parser.add_option("--user-db-table", action="store", type="string", dest="user_db_table", default='sip_accounts') option_parser.add_option("--user-db-name-row", action="store", type="string", dest="user_db_name_row", default='auth_name') option_parser.add_option("--user-db-password-row", action="store", type="string", dest="user_db_password_row", default='password') diff --git a/misc/mon_ami/mysqlr.py b/misc/mon_ami/mysqlr.py new file mode 100644 index 0000000..7ddf27a --- /dev/null +++ b/misc/mon_ami/mysqlr.py @@ -0,0 +1,135 @@ +# -*- coding: utf-8 -*- +# MySQL library + +import MySQLdb + +class MySQLR(): + + def __init__(self, database=None, user=None, password='', address='127.0.0.1', port=3306): + self.db_name = database + if (self.db_name == None): + self.db_name = ':memory:' + self.db_conn = None + self.db_cursor = None + self.db_port = int(port) + self.db_address = address + self.db_user = user + self.db_password = password + + def record_factory(self, cursor, row): + record = dict() + for index, column in enumerate(cursor.description): + record[column[0]] = row[index] + return record + + def connect(self, isolation_level = None): + try: + self.db_conn = MySQLdb.connect(host=self.db_address, port=self.db_port, user=self.db_user, passwd=self.db_password, db=self.db_name) + self.db_cursor = self.db_conn.cursor() + except: + return False + + return True + + def disconnect(self): + try: + self.db_nonn.close() + except: + return False + return True + + def execute(self, query, parameters = []): + try: + return self.db_cursor.execute(query, parameters) + except: + return False + + def fetch_row(self): + return self.db_cursor.fetchone() + + def fetch_rows(self): + return self.db_cursor.fetchall() + + def execute_get_rows(self, query, parameters = []): + if (self.execute(query, parameters)): + return self.fetch_rows() + else: + return False + + def execute_get_row(self, query, parameters = []): + query = "%s LIMIT 1" % query + if (self.execute(query, parameters)): + return self.fetch_row() + else: + return False + + def execute_get_value(self, query, parameters = []): + row = self.execute_get_row(query, parameters) + if (row): + return row[0] + else: + return row + + def create_table(self, table, structure, primary_key = None): + columns = list() + for row in structure: + key, value = row.items()[0] + sql_type = "VARCHAR(255)" + sql_key = '' + if (key == primary_key): + sql_key = 'PRIMARY KEY' + type_r = value.split(':', 1) + type_n = type_r[0] + if (type_n == 'integer'): + sql_type = 'INTEGER' + elif (type_n == 'string'): + try: + sql_type = "VARCHAR(%s)" % type_r[1] + except IndexError, e: + sql_type = "VARCHAR(255)" + + columns.append('"%s" %s %s' % (key, sql_type, sql_key)) + + query = 'CREATE TABLE "%s" (%s)' % (table, ', '.join(columns)) + return self.execute(query) + + def save(self, table, row): + keys = row.keys() + query = 'INSERT OR REPLACE INTO "%s" (%s) VALUES (:%s)' % (table, ', '.join(keys), ', :'.join(keys)) + + return self.execute(query, row) + + def find_sql(self, table, rows = None): + values = list() + if (rows): + if (type(rows) == type(list())): + rows_list = rows + else: + rows_list = list() + rows_list.append(rows) + + query_parts = list() + + for row in rows_list: + statements = list() + for key, value in row.items(): + if (value == None): + statements.append("`%s` IS %s" % (key, '%s')) + else: + statements.append("`%s` = %s" % (key, '%s')) + values.append(value) + query_parts.append('(%s)' % ' AND '.join(statements)) + + query = 'SELECT * FROM `%s` WHERE %s' % (table, ' OR '.join(query_parts)) + else: + query = 'SELECT * FROM `%s`' % table + return query, values + + def find(self, table, row = None): + query, value = self.find_sql(table, row) + + return self.execute_get_row(query, value) + + def findall(self, table, row = None): + query, values = self.find_sql(table, row) + return self.execute_get_rows(query, values) |