Permalink
Browse files

Init pyramid_mongo

  • Loading branch information...
0 parents commit ffb8b4412502bd853a7d88bd29322b59401d774e @llacroix llacroix committed May 1, 2012
Showing with 99 additions and 0 deletions.
  1. +2 −0 .gitignore
  2. +68 −0 pyramid_mongo/__init__.py
  3. +3 −0 setup.cfg
  4. +26 −0 setup.py
@@ -0,0 +1,2 @@
+*.pyc
+*.egg-info
@@ -0,0 +1,68 @@
+from pyramid.exceptions import ConfigurationError
+from pymongo import Connection
+
+URI = 'mongo.uri'
+USERNAME = 'mongo.username'
+PASSWORD = 'mongo.password'
+DBNAME = 'mongo.db'
+
+def get_connection(config):
+ registry = config.registry
+
+ uri = registry.settings.get(URI)
+
+ if uri is None:
+ raise ConfigurationError('There is no configured "mongo.uri"')
+
+ conn = Connection(uri)
+
+ if conn is None:
+ raise ConfigurationError('Cannot create a connection with "mongo.uri"')
+
+ return conn
+
+def get_db(request, name=None):
+ dbname = name
+ registry = request.registry
+
+ if name is None:
+ dbname = registry.settings.get(DBNAME)
+
+ if dbname is None:
+ raise ConfigurationError('There is no defined database name')
+
+ mongodbs = getattr(request, '_mongo_dbs', dict())
+
+ db = mongodbs.get(dbname)
+
+ if db is None:
+ conn = registry._mongo_conn
+
+ if conn is None:
+ raise ConfigurationError('There is no database connection available')
+
+ db = conn[dbname]
+
+ mongodbs[dbname] = db
+ request._mongo_dbs = mongodbs
+
+ username = registry.settings.get(USERNAME)
+ password = registry.settings.get(PASSWORD)
+
+ if not username is None and not password is None:
+ db.authenticate(username, password)
+
+ def end_request(request):
+ db.connection.end_request()
+
+ request.add_finished_callback(end_request)
+
+ return db
+
+
+def includeme(config):
+ """
+ Get a mongodb instance from the URI in the config file
+ mongodb.uri
+ """
+ config.registry._mongo_conn = get_connection(config)
@@ -0,0 +1,3 @@
+[egg_info]
+tag_build = dev
+tag_svn_revision = true
@@ -0,0 +1,26 @@
+from setuptools import setup, find_packages
+import sys, os
+
+version = '0.0'
+
+setup(name='pyramid_mongo',
+ version=version,
+ description="A simple package to handle mongodb in pyramid",
+ long_description="""\
+""",
+ classifiers=[], # Get strings from http://pypi.python.org/pypi?%3Aaction=list_classifiers
+ keywords='',
+ author='Lo\xc3\xafc Faure-Lacroix',
+ author_email='lamerstar@gmail.com',
+ url='delicieuxgateau.ca',
+ license='',
+ packages=find_packages(exclude=['ez_setup', 'examples', 'tests']),
+ include_package_data=True,
+ zip_safe=False,
+ install_requires=[
+ # -*- Extra requirements: -*-
+ ],
+ entry_points="""
+ # -*- Entry points: -*-
+ """,
+ )

0 comments on commit ffb8b44

Please sign in to comment.