1- import atexit , os .path , time , mongoengine , sys
2- import shutil , subprocess , tempfile
1+ import atexit
2+ import os .path
3+ import mongoengine
4+ import shutil
5+ import subprocess
6+ import sys
7+ import tempfile
8+ import time
9+
310from flask import current_app
4- from pymongo import MongoClient , ReadPreference , errors , uri_parser
11+ from pymongo import MongoClient , ReadPreference , errors
512from subprocess import Popen , PIPE
613from pymongo .errors import InvalidURI
714from mongoengine import connection
2431class InvalidSettingsError (Exception ):
2532 pass
2633
34+ class ConnectionError (Exception ):
35+ pass
36+
2737def disconnect (alias = DEFAULT_CONNECTION_NAME , preserved = False ):
2838 global _connections , _process , _tmpdir
2939
3040 if alias in _connections :
31- conn = get_connection (alias = alias );
41+ conn = get_connection (alias = alias )
3242 client = conn .client
3343 if client :
3444 client .close ()
@@ -46,8 +56,7 @@ def disconnect(alias=DEFAULT_CONNECTION_NAME, preserved=False):
4656 if os .path .exists (_tmpdir ):
4757 shutil .rmtree (_tmpdir , ignore_errors = True )
4858 if os .path .exists (sock_file ):
49- os .remove ("{0}/{1}" .\
50- format (tempfile .gettempdir (), sock_file ))
59+ os .remove ("{0}/{1}" .format (tempfile .gettempdir (), sock_file ))
5160
5261def _validate_settings (is_test , temp_db , preserved , conn_host ):
5362 """
@@ -56,21 +65,20 @@ def _validate_settings(is_test, temp_db, preserved, conn_host):
5665 connection.
5766
5867 """
59- if (not isinstance (is_test , bool )
60- or not isinstance (temp_db , bool )
61- or not isinstance (preserved , bool )):
62- msg = '`TESTING`, `TEMP_DB`, and `PRESERVE_TEMP_DB`' \
63- ' must be boolean values'
68+ if (not isinstance (is_test , bool ) or not isinstance (temp_db , bool ) or
69+ not isinstance (preserved , bool )):
70+ msg = ('`TESTING`, `TEMP_DB`, and `PRESERVE_TEMP_DB`'
71+ ' must be boolean values' )
6472 raise InvalidSettingsError (msg )
6573
6674 elif not is_test and conn_host .startswith ('mongomock://' ):
67- msg = "`MongoMock` connection is only required for `unittest`." \
68- "To enable this set `TESTING` to true`."
75+ msg = ( "`MongoMock` connection is only required for `unittest`."
76+ "To enable this set `TESTING` to true`." )
6977 raise InvalidURI (msg )
7078
7179 elif not is_test and temp_db or preserved :
72- msg = '`TESTING` and/or `TEMP_DB` can be used ' \
73- 'only when `TESTING` is set to true.'
80+ msg = ( '`TESTING` and/or `TEMP_DB` can be used '
81+ 'only when `TESTING` is set to true.' )
7482 raise InvalidSettingsError (msg )
7583
7684def __get_app_config (key ):
@@ -117,7 +125,7 @@ def get_connection(alias=DEFAULT_CONNECTION_NAME, reconnect=False):
117125 return _register_test_connection (port , db_alias , preserved )
118126
119127 elif (conn_host .startswith ('mongomock://' ) and
120- mongoengine .VERSION < (0 , 10 , 6 )):
128+ mongoengine .VERSION < (0 , 10 , 6 )):
121129 # Use MongoClient from mongomock
122130 try :
123131 import mongomock
@@ -212,16 +220,16 @@ def _register_test_connection(port, db_alias, preserved):
212220
213221 if _conn is None :
214222 _process = subprocess .Popen ([
215- 'mongod' , '--bind_ip' , 'localhost' ,
216- '--port' , str (port ),
217- '--dbpath' , _tmpdir ,
218- '--nojournal' , '--nohttpinterface' ,
219- '--noauth' , '--smallfiles' ,
220- '--syncdelay' , '0' ,
221- '--maxConns' , '10' ,
222- '--nssize' , '1' , ],
223- stdout = open (os .devnull , 'wb' ),
224- stderr = subprocess .STDOUT )
223+ 'mongod' , '--bind_ip' , 'localhost' ,
224+ '--port' , str (port ),
225+ '--dbpath' , _tmpdir ,
226+ '--nojournal' , '--nohttpinterface' ,
227+ '--noauth' , '--smallfiles' ,
228+ '--syncdelay' , '0' ,
229+ '--maxConns' , '10' ,
230+ '--nssize' , '1' , ],
231+ stdout = open (os .devnull , 'wb' ),
232+ stderr = subprocess .STDOUT )
225233 atexit .register (disconnect , preserved = preserved )
226234
227235 # wait for the instance db to be ready
@@ -232,7 +240,8 @@ def _register_test_connection(port, db_alias, preserved):
232240 _conn = MongoClient ('localhost' , port )
233241 except errors .ConnectionFailure :
234242 continue
235- else : break
243+ else :
244+ break
236245 else :
237246 msg = 'Cannot connect to the mongodb test instance'
238247 raise mongoengine .ConnectionError (msg )
@@ -358,7 +367,7 @@ def create_connection(config, app):
358367 _app_instance = app if app else config
359368
360369 if config is None or not isinstance (config , dict ):
361- raise Exception ("Invalid application configuration" );
370+ raise InvalidSettingsError ("Invalid application configuration" )
362371
363372 conn_settings = fetch_connection_settings (config , False )
364373
0 commit comments