Permalink
Browse files

update to reflect upstream changes

See http://bit.ly/QNuF17 for more details about the new connection classes and safe by default details.
  • Loading branch information...
Reed O'Brien
Reed O'Brien committed Dec 7, 2012
1 parent 6a73efb commit 20b5b2abd6eadeb5f7e0757e19c88dfd17700df2
Showing with 50 additions and 8 deletions.
  1. +47 −3 lumin/db.py
  2. +3 −5 lumin/tests/test_db.py
View
@@ -14,7 +14,8 @@ class IMongoDBConnection(Interface): # pragma: nocover
def get_mongodb(registry):
- db_name = registry.settings['db_name']
+
+ db_name = registry.settings['mongodb.db_name']
db = registry.getUtility(IMongoDBConnection)[db_name]
db.add_son_manipulator(ColanderNullTransformer())
return db
@@ -35,7 +36,50 @@ def add_mongodb(event):
event.request.fs = MockGridFS(event.request.db)
-def register_mongodb(config, db_uri, slave_okay=False, conn=None):
- conn = conn if conn else pymongo.Connection(db_uri, slave_okay=slave_okay)
+def connection_from_settings(settings):
+ """
+ :param settings: a `pyramid.config.Configurator.registry.settings`
+ instance. It should contain a client class to use for the connection as
+ mongodb.client_class and a connection string as a uri as mongodb.db_uri
+ appropriate client class options for the `mongodb.client_class` specified
+ or the `pymongo.mongo_client.MongoClient <http://bit.ly/YK37OJ>`_
+ class by default. I.e. see `pymongo.mongo_client.MongoClient <http://bit.ly/YK37OJ>`_
+ for a list of options appropriate to
+ `pymongo.mongo_client.MongoClient <http://bit.ly/YK37OJ>`
+ """
+ mongo_options = {k.split("mongodb.options.")[-1]: v for k, v in \
+ settings.items() if k.startswith('mongodb.options.')}
+ connclass = getattr(pymongo,
+ settings.get("mongodb.connection_class", "MongoClient"))
+ return connclass(settings.get('mongodb.db_uri'), **mongo_options)
+
+
+def register_mongodb(config, conn=None):
+ """
+ Register a mongodb connection with the configuration registry.
+ This is added as a `pyramid.config.Configurator <http://bit.ly/QNns19>`_
+ durective in :meth:`lumin.includeme`. Then a database connection can be
+ created and registered in the `pyramid.config.Configurator <http://bit.ly/QNns19>`_
+ during application startup.
+
+ .. code-block:: python
+
+ config = Configurator(...)
+ ...
+ conn = pymongo.MongoClient(...)
+ config.register_mongodb(conn=conn)
+
+ Alternatively you can pass nothing and lumin will attempt to create a
+ connection from the `pyramid.config.Configurator <http://bit.ly/QNns19>`_
+ `pyramid.registry.Registry.settings <http://bit.ly/QNrUNp>`_. See
+ :meth:`lumin.db.connection_from_settings` for more details.
+
+ .. code-block:: python
+
+ config = Configurator(...)
+ config = config.register_mongodb()
+ """
+
+ conn = connection_from_settings(config.registry.settings)
config.registry.registerUtility(conn, IMongoDBConnection)
return conn
View
@@ -18,8 +18,8 @@ def _includelumin(self):
def _registerdb(self):
conn = Connection()
- self.config.registry.settings['db_name'] = 'frozznob'
- self.config.register_mongodb('mongodb://localhost', conn=conn)
+ self.config.registry.settings['mongodb.db_name'] = 'frozznob'
+ self.config.register_mongodb(conn=conn)
def test_register_mongodb_directive(self):
self._includelumin()
@@ -36,10 +36,8 @@ def test_register_mongodb_directive(self):
def test_get_mongodb(self):
self._includelumin()
self._registerdb()
-
from lumin.db import get_mongodb
- from mongomock import Database
-
+ from pymongo.database import Database
conn = get_mongodb(self.config.registry)
self.assertTrue(isinstance(conn, Database))

0 comments on commit 20b5b2a

Please sign in to comment.