1
1
import atexit
2
2
import os .path
3
- import mongoengine
4
3
import shutil
5
4
import subprocess
6
5
import sys
7
6
import tempfile
8
7
import time
9
8
10
9
from flask import current_app
10
+ import mongoengine
11
+ from mongoengine import connection
11
12
from pymongo import MongoClient , ReadPreference , errors
12
- from subprocess import Popen , PIPE
13
13
from pymongo .errors import InvalidURI
14
- from mongoengine import connection
15
14
16
15
__all__ = (
17
16
'create_connection' , 'disconnect' , 'get_connection' ,
28
27
_process = None
29
28
_app_instance = current_app
30
29
30
+
31
31
class InvalidSettingsError (Exception ):
32
32
pass
33
33
34
+
34
35
class ConnectionError (Exception ):
35
36
pass
36
37
38
+
37
39
def disconnect (alias = DEFAULT_CONNECTION_NAME , preserved = False ):
38
40
global _connections , _process , _tmpdir
39
41
@@ -58,6 +60,7 @@ def disconnect(alias=DEFAULT_CONNECTION_NAME, preserved=False):
58
60
if os .path .exists (sock_file ):
59
61
os .remove ("{0}/{1}" .format (tempfile .gettempdir (), sock_file ))
60
62
63
+
61
64
def _validate_settings (is_test , temp_db , preserved , conn_host ):
62
65
"""
63
66
Validate unitest settings to ensure
@@ -81,11 +84,13 @@ def _validate_settings(is_test, temp_db, preserved, conn_host):
81
84
'only when `TESTING` is set to true.' )
82
85
raise InvalidSettingsError (msg )
83
86
87
+
84
88
def __get_app_config (key ):
85
89
return (_app_instance .get (key , False )
86
90
if isinstance (_app_instance , dict )
87
91
else _app_instance .config .get (key , False ))
88
92
93
+
89
94
def get_connection (alias = DEFAULT_CONNECTION_NAME , reconnect = False ):
90
95
global _connections
91
96
set_global_attributes ()
@@ -175,25 +180,30 @@ def get_connection(alias=DEFAULT_CONNECTION_NAME, reconnect=False):
175
180
176
181
return mongoengine .connection .get_db (alias )
177
182
183
+
178
184
def _sys_exec (cmd , shell = True , env = None ):
179
185
if env is None :
180
186
env = os .environ
181
187
182
- a = Popen (cmd , shell = shell , stdout = PIPE , stderr = PIPE , env = env )
188
+ a = subprocess .Popen (cmd , shell = shell , stdout = subprocess .PIPE ,
189
+ stderr = subprocess .PIPE , env = env )
183
190
a .wait () # Wait for process to terminate
184
191
if a .returncode : # Not 0 => Error has occured
185
192
raise Exception (a .communicate ()[1 ])
186
193
return a .communicate ()[0 ]
187
194
195
+
188
196
def set_global_attributes ():
189
197
setattr (connection , '_connection_settings' , _connection_settings )
190
198
setattr (connection , '_connections' , _connections )
191
199
setattr (connection , 'disconnect' , disconnect )
192
200
201
+
193
202
def get_db (alias = DEFAULT_CONNECTION_NAME , reconnect = False ):
194
203
set_global_attributes ()
195
204
return connection .get_db (alias , reconnect )
196
205
206
+
197
207
def _register_test_connection (port , db_alias , preserved ):
198
208
global _process , _tmpdir
199
209
@@ -248,6 +258,7 @@ def _register_test_connection(port, db_alias, preserved):
248
258
_connections [db_alias ] = _conn
249
259
return _conn
250
260
261
+
251
262
def _resolve_settings (conn_setting , removePass = True ):
252
263
253
264
if conn_setting and isinstance (conn_setting , dict ):
@@ -295,6 +306,7 @@ def _resolve_settings(conn_setting, removePass=True):
295
306
return resolved
296
307
return conn_setting
297
308
309
+
298
310
def fetch_connection_settings (config , removePass = True ):
299
311
"""
300
312
Fetch DB connection settings from FlaskMongoEngine
@@ -328,6 +340,7 @@ def fetch_connection_settings(config, removePass=True):
328
340
# Connection settings provided in standard format.
329
341
return _resolve_settings (config , removePass )
330
342
343
+
331
344
def create_connection (config , app ):
332
345
"""
333
346
Connection is created based on application configuration
0 commit comments