From 0285ec6beb695bf38ae5cccef8245e04fe5585a0 Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Tue, 14 Aug 2007 00:40:23 +0000 Subject: added engine_from_config() function for helping to create_engine() from an .ini style config --- lib/sqlalchemy/__init__.py | 2 +- lib/sqlalchemy/engine/__init__.py | 28 ++++++++++++++++++++++++++++ lib/sqlalchemy/engine/url.py | 12 +++++++++++- 3 files changed, 40 insertions(+), 2 deletions(-) (limited to 'lib/sqlalchemy') diff --git a/lib/sqlalchemy/__init__.py b/lib/sqlalchemy/__init__.py index ab12ecf4b..bdd3604de 100644 --- a/lib/sqlalchemy/__init__.py +++ b/lib/sqlalchemy/__init__.py @@ -7,7 +7,7 @@ from sqlalchemy.types import * from sqlalchemy.sql import * from sqlalchemy.schema import * -from sqlalchemy.engine import create_engine +from sqlalchemy.engine import create_engine, engine_from_config __version__ = 'svn' diff --git a/lib/sqlalchemy/engine/__init__.py b/lib/sqlalchemy/engine/__init__.py index 2ef163c03..acac8180c 100644 --- a/lib/sqlalchemy/engine/__init__.py +++ b/lib/sqlalchemy/engine/__init__.py @@ -48,6 +48,7 @@ The package is represented among several individual modules, including: import sqlalchemy.databases from sqlalchemy.engine.base import * from sqlalchemy.engine import strategies +from sqlalchemy import util def engine_descriptors(): """Provide a listing of all the database implementations supported. @@ -151,3 +152,30 @@ def create_engine(*args, **kwargs): strategy = kwargs.pop('strategy', default_strategy) strategy = strategies.strategies[strategy] return strategy.create(*args, **kwargs) + +def engine_from_config(configuration, prefix='sqlalchemy.', **kwargs): + """Create a new Engine instance using a configuration dictionary. + + the dictionary is typically produced from a config file where keys are prefixed, + such as sqlalchemy.url, sqlalchemy.echo, etc. The 'prefix' argument indicates + the prefix to be searched for. + + A select set of keyword arguments will be "coerced" to their expected type based on + string values. in a future release, this functionality will be expanded to include + dialect-specific arguments. + """ + + opts = dict([(key[len(prefix):], configuration[key]) for key in configuration if key.startswith(prefix)]) + for opt, type_ in ( + ('convert_unicode', bool), + ('pool_timeout', int), + ('echo', bool), + ('echo_pool', bool), + ('pool_recycle', int), + ): + util.coerce_kw_type(opts, opt, type_) + opts.update(kwargs) + url = opts.pop('url') + return create_engine(url, **opts) + + \ No newline at end of file diff --git a/lib/sqlalchemy/engine/url.py b/lib/sqlalchemy/engine/url.py index 98d4d442e..31546bae8 100644 --- a/lib/sqlalchemy/engine/url.py +++ b/lib/sqlalchemy/engine/url.py @@ -66,7 +66,17 @@ class URL(object): keys.sort() s += '?' + "&".join(["%s=%s" % (k, self.query[k]) for k in keys]) return s - + + def __eq__(self, other): + return \ + isinstance(other, URL) and \ + self.drivername == other.drivername and \ + self.username == other.username and \ + self.password == other.password and \ + self.host == other.host and \ + self.database == other.database and \ + self.query == other.query + def get_dialect(self): """Return the SQLAlchemy database dialect class corresponding to this URL's driver name.""" dialect=None -- cgit v1.2.1