Permalink
Browse files

Support connection options for ming/mongodb

  • Loading branch information...
1 parent c2e2cd3 commit 0215cee937ce01a640b5c4f3300b3b7e26d09d95 @amol- amol- committed Nov 26, 2012
Showing with 32 additions and 4 deletions.
  1. +21 −4 tg/configuration/app_config.py
  2. +11 −0 tg/configuration/utils.py
View
25 tg/configuration/app_config.py
@@ -19,6 +19,7 @@
import tg
from tg import TGApp
+from tg.configuration.utils import coerce_config
from tg.util import Bunch, get_partial_dict, DottedFileNameFinder
from routes import Mapper
@@ -551,12 +552,28 @@ def setup_ming(self):
try:
from ming import create_datastore
from urlparse import urljoin
- def create_ming_datastore(url, database):
- return create_datastore(urljoin(url, database))
+ def create_ming_datastore(url, database, **kw):
+ return create_datastore(urljoin(url, database), **kw)
except ImportError:
from ming.datastore import DataStore
- def create_ming_datastore(url, database):
- return DataStore(url, database=database)
+ def create_ming_datastore(url, database, **kw):
+ return DataStore(url, database=database, **kw)
+
+ def mongo_read_pref(value):
+ from pymongo.read_preferences import ReadPreference
+ return getattr(ReadPreference, value)
+
+ datastore_options = coerce_config(config, 'ming.connection.', {'max_pool_size':asint,
+ 'network_timeout':asint,
+ 'tz_aware':asbool,
+ 'safe':asbool,
+ 'journal':asbool,
+ 'wtimeout':asint,
+ 'fsync':asbool,
+ 'ssl':asbool,
+ 'read_preference':mongo_read_pref})
+ datastore_options.pop('host', None)
+ datastore_options.pop('port', None)
datastore = create_ming_datastore(config['ming.url'], config['ming.db'])
config['pylons.app_globals'].ming_datastore = datastore
View
11 tg/configuration/utils.py
@@ -0,0 +1,11 @@
+def coerce_config(configuration, prefix, converters):
+ """Convert configuration values to expected types."""
+
+ options = dict((key[len(prefix):], configuration[key])
+ for key in configuration if key.startswith(prefix))
+
+ for option, converter in converters.items():
+ if option in options:
+ options[option] = converter(options[option])
+
+ return options

0 comments on commit 0215cee

Please sign in to comment.