From 8d78e11a4fad7b5861f7e37a817aec200ae985c9 Mon Sep 17 00:00:00 2001 From: Mika Bostrom Date: Sun, 16 Jan 2011 10:43:36 +0200 Subject: [PATCH] Add support for specifying DB port In case someone needs to connect to a DB engine in non-standard port, defining the TCP port is required. This is a rare corner case which should only arise when a given DB host runs multiple versions of the same DB engine. However, the support is relatively straightforward to implement and it shouldn't conflict with any other options. --- pyfpdb/Configuration.py | 19 +++++++++++++------ pyfpdb/Database.py | 13 ++++++++++--- pyfpdb/HUD_config.xml.example | 6 ++++++ 3 files changed, 29 insertions(+), 9 deletions(-) diff --git a/pyfpdb/Configuration.py b/pyfpdb/Configuration.py index a0ad4bad..3a5e0787 100644 --- a/pyfpdb/Configuration.py +++ b/pyfpdb/Configuration.py @@ -409,12 +409,13 @@ class Database: self.db_name = node.getAttribute("db_name") self.db_desc = node.getAttribute("db_desc") self.db_server = node.getAttribute("db_server").lower() - self.db_ip = node.getAttribute("db_ip") + self.db_ip = node.getAttribute("db_ip") + self.db_port = node.getAttribute("db_port") self.db_user = node.getAttribute("db_user") self.db_pass = node.getAttribute("db_pass") self.db_selected = string_to_bool(node.getAttribute("default"), default=False) - log.debug("Database db_name:'%(name)s' db_server:'%(server)s' db_ip:'%(ip)s' db_user:'%(user)s' db_pass (not logged) selected:'%(sel)s'" \ - % { 'name':self.db_name, 'server':self.db_server, 'ip':self.db_ip, 'user':self.db_user, 'sel':self.db_selected} ) + log.debug("Database db_name:'%(name)s' db_server:'%(server)s' db_ip:'%(ip)s' db_port:'%(port)s' db_user:'%(user)s' db_pass (not logged) selected:'%(sel)s'" \ + % { 'name':self.db_name, 'server':self.db_server, 'ip':self.db_ip, 'port':self.db_port, 'user':self.db_user, 'sel':self.db_selected} ) def __str__(self): temp = 'Database = ' + self.db_name + '\n' @@ -1059,6 +1060,9 @@ class Config: try: db['db-host'] = self.supported_databases[name].db_ip except: pass + try: db['db-port'] = self.supported_databases[name].db_port + except KeyError: pass + try: db['db-user'] = self.supported_databases[name].db_user except: pass @@ -1072,15 +1076,16 @@ class Config: return db - def set_db_parameters(self, db_name = 'fpdb', db_ip = None, db_user = None, - db_pass = None, db_desc = None, db_server = None, - default = "False"): + def set_db_parameters(self, db_name = 'fpdb', db_ip = None, db_port = None, + db_user = None, db_pass = None, db_desc = None, + db_server = None, default = "False"): db_node = self.get_db_node(db_name) default = default.lower() defaultb = string_to_bool(default, False) if db_node != None: if db_desc is not None: db_node.setAttribute("db_desc", db_desc) if db_ip is not None: db_node.setAttribute("db_ip", db_ip) + if db_port is not None: db_node.setAttribute("db_port", db_port) if db_user is not None: db_node.setAttribute("db_user", db_user) if db_pass is not None: db_node.setAttribute("db_pass", db_pass) if db_server is not None: db_node.setAttribute("db_server", db_server) @@ -1094,6 +1099,7 @@ class Config: if self.supported_databases.has_key(db_name): if db_desc is not None: self.supported_databases[db_name].dp_desc = db_desc if db_ip is not None: self.supported_databases[db_name].dp_ip = db_ip + if db_port is not None: self.supported_databases[db_name].dp_port = db_port if db_user is not None: self.supported_databases[db_name].dp_user = db_user if db_pass is not None: self.supported_databases[db_name].dp_pass = db_pass if db_server is not None: self.supported_databases[db_name].dp_server = db_server @@ -1148,6 +1154,7 @@ class Config: if self.supported_databases.has_key(db_name): if db_desc is not None: self.supported_databases[db_name].dp_desc = db_desc if db_ip is not None: self.supported_databases[db_name].dp_ip = db_ip + if db_port is not None: self.supported_databases[db_name].dp_port = db_port if db_user is not None: self.supported_databases[db_name].dp_user = db_user if db_pass is not None: self.supported_databases[db_name].dp_pass = db_pass if db_server is not None: self.supported_databases[db_name].dp_server = db_server diff --git a/pyfpdb/Database.py b/pyfpdb/Database.py index 8ed0e3bc..96f7ec38 100644 --- a/pyfpdb/Database.py +++ b/pyfpdb/Database.py @@ -254,6 +254,7 @@ class Database: self.db_server = db_params['db-server'] self.database = db_params['db-databaseName'] self.host = db_params['db-host'] + self.db_port = db_params['db-port'] self.db_path = '' gen = c.get_general_params() self.day_start = 0 @@ -349,6 +350,7 @@ class Database: try: self.connect(backend=db['db-backend'], host=db['db-host'], + port=db['db-port'], database=db['db-databaseName'], user=db['db-user'], password=db['db-password']) @@ -363,14 +365,17 @@ class Database: self.db_server = db_params['db-server'] self.database = db_params['db-databaseName'] self.host = db_params['db-host'] + self.db_port = db_params['db-port'] - def connect(self, backend=None, host=None, database=None, - user=None, password=None, create=False): + def connect(self, backend=None, host=None, port=None, + database=None, user=None, password=None, + create=False): """Connects a database with the given parameters""" if backend is None: raise FpdbError('Database backend not defined') self.backend = backend self.host = host + self.port = port self.user = user self.password = password self.database = database @@ -382,7 +387,8 @@ class Database: if use_pool: MySQLdb = pool.manage(MySQLdb, pool_size=5) try: - self.connection = MySQLdb.connect(host=host, user=user, passwd=password, db=database, use_unicode=True) + self.connection = MySQLdb.connect(host=host, port=port, user=user, + passwd=password, db=database, use_unicode=True) self.__connected = True #TODO: Add port option except MySQLdb.Error, ex: @@ -416,6 +422,7 @@ class Database: if not self.is_connected(): try: self.connection = psycopg2.connect(host = host, + port = port, user = user, password = password, database = database) diff --git a/pyfpdb/HUD_config.xml.example b/pyfpdb/HUD_config.xml.example index 7bb30e0f..a3df15a2 100644 --- a/pyfpdb/HUD_config.xml.example +++ b/pyfpdb/HUD_config.xml.example @@ -796,6 +796,12 @@ Left-Drag to Move" +