|
@@ -4,7 +4,7 @@ import bson
|
4
|
4
|
from bson.son import SON
|
5
|
5
|
from collections import OrderedDict
|
6
|
6
|
import pymongo
|
7
|
|
-from pymongo import MongoClient
|
|
7
|
+# from pymongo import MongoClient
|
8
|
8
|
from pymongo.errors import BulkWriteError
|
9
|
9
|
import urllib
|
10
|
10
|
|
|
@@ -19,42 +19,10 @@ class MongoDbDataSourceError(Exception):
|
19
|
19
|
|
20
|
20
|
class MongoDbDataSource(GenericDataSource):
|
21
|
21
|
|
22
|
|
- MANDATORY_CONNECTION_ARGS = ('host', 'port', 'login', 'password', 'dbname')
|
23
|
|
-
|
24
|
22
|
## @brief Instanciates a Database object given a connection name
|
25
|
23
|
# @param connection_name str
|
26
|
24
|
def __init__(self, connection_name='default'):
|
27
|
|
- connection_args = self._get_connection_args(connection_name)
|
28
|
|
- login, password, host, port, dbname = MongoDbDataSource._check_connection_args(connection_args)
|
29
|
|
-
|
30
|
|
- # Creating of the connection
|
31
|
|
- connection_string = 'mongodb://%s:%s@%s:%s' % (login, password, host, port)
|
32
|
|
- self.connection = MongoClient(connection_string)
|
33
|
|
- # Getting the database
|
34
|
|
- self.database = self.connection[dbname]
|
35
|
|
-
|
36
|
|
- ## @brief Gets the settings given a connection name
|
37
|
|
- # @param connection_name str
|
38
|
|
- # @return dict
|
39
|
|
- # @TODO Change the return value using the Lodel 2 settings module
|
40
|
|
- def _get_connection_args(self, connection_name):
|
41
|
|
- return {'host': 'localhost', 'port': 28015, 'login': 'lodel_admin', 'password': 'lapwd', 'dbname': 'lodel'}
|
42
|
|
-
|
43
|
|
- ## @brief checks if the connection args are valid and complete
|
44
|
|
- # @param connection_args dict
|
45
|
|
- # @return bool
|
46
|
|
- # @todo checks on the argument types can be added here
|
47
|
|
- @classmethod
|
48
|
|
- def _check_connection_args(cls, connection_args):
|
49
|
|
- errors = []
|
50
|
|
- for connection_arg in cls.MANDATORY_CONNECTION_ARGS:
|
51
|
|
- if connection_arg not in connection_args:
|
52
|
|
- errors.append("Datasource connection error : %s parameter is missing." % connection_arg)
|
53
|
|
- if len(errors) > 0 :
|
54
|
|
- raise MongoDbDataSourceError("\r\n-".join(errors))
|
55
|
|
-
|
56
|
|
- return (connection_args['login'], connection_args['password'], connection_args['host'],
|
57
|
|
- connection_args['port'], connection_args['dbname'])
|
|
25
|
+ self.database = utils.mongodbconnect(connection_name)
|
58
|
26
|
|
59
|
27
|
## @brief returns a selection of documents from the datasource
|
60
|
28
|
# @param target_cls Emclass
|