]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
added engine_from_config() function for helping to create_engine()
authorMike Bayer <mike_mp@zzzcomputing.com>
Tue, 14 Aug 2007 00:40:23 +0000 (00:40 +0000)
committerMike Bayer <mike_mp@zzzcomputing.com>
Tue, 14 Aug 2007 00:40:23 +0000 (00:40 +0000)
from an .ini style config

lib/sqlalchemy/__init__.py
lib/sqlalchemy/engine/__init__.py
lib/sqlalchemy/engine/url.py
test/engine/parseconnect.py

index ab12ecf4b2101a7af7efd79738727cf1b453a3f4..bdd3604de9801a5b0074253b453e32cdb1b98c40 100644 (file)
@@ -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'
index 2ef163c037041b0b15501d1face467f68bc50ceb..acac8180ce4e2ce3212d3879730d55a2227e102a 100644 (file)
@@ -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
index 98d4d442e1784fe182390b221a8ab0c6e09878c4..31546bae8f4308980df2d4667b1519a214991af2 100644 (file)
@@ -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
index fef3742cf8af20d798aa0f1b647639e24d286495..14def8d3b07b916411a6d4f57e36162fcc4e1a9a 100644 (file)
@@ -5,7 +5,7 @@ from testlib import *
 
         
 class ParseConnectTest(PersistTest):
-    def testrfc1738(self):
+    def test_rfc1738(self):
         for text in (
             'dbtype://username:password@hostspec:110//usr/db_file.db',
             'dbtype://username:password@hostspec/database',
@@ -37,21 +37,35 @@ class ParseConnectTest(PersistTest):
 
 class CreateEngineTest(PersistTest):
     """test that create_engine arguments of different types get propigated properly"""
-    def testconnectquery(self):
+    def test_connect_query(self):
         dbapi = MockDBAPI(foober='12', lala='18', fooz='somevalue')
         
         # start the postgres dialect, but put our mock DBAPI as the module instead of psycopg
         e = create_engine('postgres://scott:tiger@somehost/test?foober=12&lala=18&fooz=somevalue', module=dbapi)
         c = e.connect()
 
-    def testkwargs(self):
+    def test_kwargs(self):
         dbapi = MockDBAPI(foober=12, lala=18, hoho={'this':'dict'}, fooz='somevalue')
 
         # start the postgres dialect, but put our mock DBAPI as the module instead of psycopg
         e = create_engine('postgres://scott:tiger@somehost/test?fooz=somevalue', connect_args={'foober':12, 'lala':18, 'hoho':{'this':'dict'}}, module=dbapi)
         c = e.connect()
 
-    def testcustom(self):
+    def test_engine_from_config(self):
+        dbapi = MockDBAPI()
+
+        config = {
+            'sqlalchemy.url':'postgres://scott:tiger@somehost/test?fooz=somevalue',
+            'sqlalchemy.echo':'1',
+            'sqlalchemy.pool_recycle':50
+        }
+
+        e = engine_from_config(config, module=dbapi)
+        assert e.pool._recycle == 50
+        assert e.echo is True
+        assert e.url == url.make_url('postgres://scott:tiger@somehost/test?fooz=somevalue')
+        
+    def test_custom(self):
         dbapi = MockDBAPI(foober=12, lala=18, hoho={'this':'dict'}, fooz='somevalue')
 
         def connect():
@@ -61,12 +75,12 @@ class CreateEngineTest(PersistTest):
         e = create_engine('postgres://', creator=connect, module=dbapi)
         c = e.connect()
     
-    def testrecycle(self):
+    def test_recycle(self):
         dbapi = MockDBAPI(foober=12, lala=18, hoho={'this':'dict'}, fooz='somevalue')
         e = create_engine('postgres://', pool_recycle=472, module=dbapi)
         assert e.pool._recycle == 472
         
-    def testbadargs(self):
+    def test_badargs(self):
         # good arg, use MockDBAPI to prevent oracle import errors
         e = create_engine('oracle://', use_ansi=True, module=MockDBAPI())
         
@@ -124,7 +138,7 @@ class CreateEngineTest(PersistTest):
         except exceptions.DBAPIError:
             assert True
     
-    def testurlattr(self):
+    def test_urlattr(self):
         """test the url attribute on ``Engine``."""
         
         e = create_engine('mysql://scott:tiger@localhost/test', module=MockDBAPI())
@@ -134,7 +148,7 @@ class CreateEngineTest(PersistTest):
         assert e.url.username == e2.url.username == 'scott'
         assert e2.url is u
         
-    def testpoolargs(self):
+    def test_poolargs(self):
         """test that connection pool args make it thru"""
         e = create_engine('postgres://', creator=None, pool_recycle=50, echo_pool=None, module=MockDBAPI())
         assert e.pool._recycle == 50