From: <umg...@us...> - 2007-06-22 15:33:14
|
Revision: 440 http://svn.sourceforge.net/pybridge/?rev=440&view=rev Author: umgangee Date: 2007-06-22 08:33:12 -0700 (Fri, 22 Jun 2007) Log Message: ----------- Use ConfigObj to manage database connection configuration, remove homebrew ConfigParser wrapper. Modified Paths: -------------- trunk/pybridge/pybridge/server/__init__.py trunk/pybridge/pybridge/server/database.py Added Paths: ----------- trunk/pybridge/pybridge/server/config.py Removed Paths: ------------- trunk/pybridge/pybridge/settings.py Modified: trunk/pybridge/pybridge/server/__init__.py =================================================================== --- trunk/pybridge/pybridge/server/__init__.py 2007-06-22 15:30:41 UTC (rev 439) +++ trunk/pybridge/pybridge/server/__init__.py 2007-06-22 15:33:12 UTC (rev 440) @@ -19,11 +19,13 @@ from twisted.cred import checkers, credentials, portal from twisted.spread import pb +import config +config.load() + from pybridge.server.checker import Checker from pybridge.server.realm import Realm from pybridge.server.server import Server - server = Server() realm = Realm() checker = Checker() Added: trunk/pybridge/pybridge/server/config.py =================================================================== --- trunk/pybridge/pybridge/server/config.py (rev 0) +++ trunk/pybridge/pybridge/server/config.py 2007-06-22 15:33:12 UTC (rev 440) @@ -0,0 +1,55 @@ +# PyBridge -- online contract bridge made easy. +# Copyright (C) 2004-2007 PyBridge Project. +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + + +""" +Manages PyBridge server configuration file. +""" + +from StringIO import StringIO +from configobj import ConfigObj +from validate import Validator + +import pybridge.environment as env + +# Config spec +spec = StringIO("""# PyBridge server configuration file + +[Database] + Engine = option('sqlite', 'sapdb', 'postgresql', 'firebird', 'maxdb', 'sybase', 'interbase', 'psycopg', 'mysql', 'mssql', 'postgres', default='sqlite') + DatabaseName = string # Or path to database file if using sqlite. + User = string # Not used with sqlite. + Password = string # Not used with sqlite. + Host = string # Leave empty for localhost. + Port = integer # Leave empty for default. +""") + + +config = None +val = Validator() + +def load(): + global config + filename = env.find_config_server('server.cfg') + config = ConfigObj(filename, create_empty=True, configspec=spec) + config.validate(val, copy=True) + +def save(): + global config + config.validate(val, copy=True) + config.write() + Modified: trunk/pybridge/pybridge/server/database.py =================================================================== --- trunk/pybridge/pybridge/server/database.py 2007-06-22 15:30:41 UTC (rev 439) +++ trunk/pybridge/pybridge/server/database.py 2007-06-22 15:33:12 UTC (rev 440) @@ -22,48 +22,51 @@ from sqlobject.inheritance import InheritableSQLObject from twisted.python import log -import pybridge.environment as env -from pybridge.settings import Settings +from config import config +from pybridge import environment as env -# TODO: when fields are not empty, avoid writing default values to fields. - -configfile = env.find_config_server('server.cfg') -settings = Settings(configfile, ['database']) - - # Initiate connection to the appropriate database backend. # See http://sqlobject.org/SQLObject.html#declaring-the-class # This code has been tested with the SQLite database backend. If you experience # problems with databases supported by SQLObject, please file a bug report. -backend = settings.database.get('backend', 'sqlite') # Default to SQLite. -settings.database['backend'] = backend +engine = config['Database'].get('Engine', 'sqlite') # Default to SQLite. -if backend == 'sqlite': - dbfile = settings.database.get('dbfile') - if dbfile is None: - dbfile = env.find_config_server('pybridge-server.db') - settings.database['dbfile'] = dbfile +if engine == 'sqlite': + dbfile = config['Database'].get('DatabaseName', + env.find_config_server('pybridge-server.db')) connection_string = "sqlite://" + dbfile + else: - username = settings.database.get('username', '') - password = settings.database.get('password', '') - hostname = settings.database.get('hostname', 'localhost') - dbname = settings.database.get('dbname', 'pybridge') - connection_string = "%s://%s:%s/%s" % (username, password, hostname, dbname) + username = config['Database'].get('Username', '') + password = config['Database'].get('Password', '') + host = config['Database'].get('Host', 'localhost') + port = config['Database'].get('Port', '') + dbname = config['Database'].get('DatabaseName', 'pybridge') -settings.save() + # Standard URI syntax (from http://sqlobject.org/SQLObject.html): + # scheme://[user[:password]@]host[:port]/database[?parameters] + connection_string = engine + '://' + if username: + connection_string += username + if password: + connection_string += ':' + password + connection_string += '@' + connection_string += host + if port: + connection_string += ':' + str(port) + connection_string += '/' + dbname try: connection = connectionForURI(connection_string) # TODO: fix for Win32. - log.msg("Connection to %s database succeeded" % backend) + log.msg("Connection to %s database succeeded" % engine) except Exception, e: log.err(e) log.msg("Could not connect to %s database with URI: %s" - % (backend, connection_string)) - log.msg("Please check configuration file %s" % configfile) + % (engine, connection_string)) + log.msg("Please check configuration file.") raise SystemExit # Database connection is required for server operation. sqlhub.processConnection = connection # Set all SQLObjects to use connection. Deleted: trunk/pybridge/pybridge/settings.py =================================================================== --- trunk/pybridge/pybridge/settings.py 2007-06-22 15:30:41 UTC (rev 439) +++ trunk/pybridge/pybridge/settings.py 2007-06-22 15:33:12 UTC (rev 440) @@ -1,53 +0,0 @@ -# PyBridge -- online contract bridge made easy. -# Copyright (C) 2004-2007 PyBridge Project. -# -# This program is free software; you can redistribute it and/or -# modify it under the terms of the GNU General Public License -# as published by the Free Software Foundation; either version 2 -# of the License, or (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program; if not, write to the Free Software -# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - - -import ConfigParser - - -class Settings: - """A wrapper for ConfigParser.""" - - - def __init__(self, path, sections): - """Make section key/value pairs into attributes of this object. - - @param path: the location of configuration file to load. - @param sections: a list of section names to be made available. - """ - self._path = path - self._config = ConfigParser.SafeConfigParser() - self._config.read(path) - - for section in sections: - # Create sections if they do not exist. - if not self._config.has_section(section): - self._config.add_section(section) - # Make items in section available as - items = {} - for key, value in self._config.items(section): - items[key] = value - setattr(self, section.lower(), items) # self.<section> = items - - - def save(self): - """Writes contents of section/item dicts back to file.""" - for section in self._config.sections(): - for key, value in getattr(self, section.lower()).items(): - self._config.set(section, key, value) - self._config.write(file(self._path, 'w')) - This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |