浏览代码

Add an abtract datasource to make Datasource classes of plugins inherit from it

refs #132
Yann Weber 8 年前
父节点
当前提交
3e4231e871
共有 3 个文件被更改,包括 71 次插入3 次删除
  1. 66
    0
      lodel/plugin/datasource_plugin.py
  2. 3
    1
      plugins/dummy_datasource/datasource.py
  3. 2
    2
      plugins/mongodb_datasource/datasource.py

+ 66
- 0
lodel/plugin/datasource_plugin.py 查看文件

@@ -1,5 +1,6 @@
1 1
 from .plugins import Plugin
2 2
 from .exceptions import *
3
+from lodel.exceptions import *
3 4
 from lodel.settings.validator import SettingValidator
4 5
 
5 6
 
@@ -162,6 +163,71 @@ but %s is a %s" % (ds_name, pinstance.__class__.__name__))
162 163
     def get_migration_handler(cls, ds_plugin_name):
163 164
         return cls.get(ds_plugin_name).migration_handler_cls()
164 165
 
166
+class AbstractDatasource(object):
167
+    
168
+    ##@brief Trigger LodelFatalError when abtract method called
169
+    @staticmethod
170
+    def _abs_err():
171
+        raise LodelFatalError("This method is abstract and HAVE TO be \
172
+reimplemented by plugin datasource child class")
173
+    
174
+    ##@brief The constructor
175
+    def __init__(self, *conn_args, **conn_kwargs):
176
+        self._abs_err()
177
+
178
+    ##@brief Provide a new uniq numeric ID
179
+    #@param emcomp LeObject subclass (not instance) : To know on wich things we
180
+    #have to be uniq
181
+    #@return an integer
182
+    def new_numeric_id(self, emcomp):
183
+        self._abs_err()
184
+
185
+    ##@brief returns a selection of documents from the datasource
186
+    #@param target_cls Emclass
187
+    #@param field_list list
188
+    #@param filters list : List of filters
189
+    #@param rel_filters list : List of relational filters
190
+    #@param order list : List of column to order. ex: order = [('title', 'ASC'),]
191
+    #@param group list : List of tupple representing the column to group together. ex: group = [('title', 'ASC'),]
192
+    #@param limit int : Number of records to be returned
193
+    #@param offset int: used with limit to choose the start record
194
+    #@param instanciate bool : If true, the records are returned as instances, else they are returned as dict
195
+    #@return list
196
+    def select(self, target, field_list, filters, rel_filters=None, order=None, group=None, limit=None, offset=0,
197
+               instanciate=True):
198
+        self._abs_err()
199
+
200
+    ##@brief Deletes records according to given filters
201
+    #@param target Emclass : class of the record to delete
202
+    #@param filters list : List of filters
203
+    #@param relational_filters list : List of relational filters
204
+    #@return int : number of deleted records
205
+    def delete(self, target, filters, relational_filters):
206
+        self._abs_err()
207
+
208
+    ## @brief updates records according to given filters
209
+    #@param target Emclass : class of the object to insert
210
+    #@param filters list : List of filters
211
+    #@param relational_filters list : List of relational filters
212
+    #@param upd_datas dict : datas to update (new values)
213
+    #@return int : Number of updated records
214
+    def update(self, target, filters, relational_filters, upd_datas):
215
+        self._abs_err()
216
+
217
+    ## @brief Inserts a record in a given collection
218
+    # @param target Emclass : class of the object to insert
219
+    # @param new_datas dict : datas to insert
220
+    # @return the inserted uid
221
+    def insert(self, target, new_datas):
222
+        self._abs_err()
223
+
224
+    ## @brief Inserts a list of records in a given collection
225
+    # @param target Emclass : class of the objects inserted
226
+    # @param datas_list list : list of dict
227
+    # @return list : list of the inserted records' ids
228
+    def insert_multi(self, target, datas_list):
229
+        self._abs_err()
230
+
165 231
 ##@page lodel2_datasources Lodel2 datasources
166 232
 #
167 233
 #@par lodel2_datasources_intro Intro

+ 3
- 1
plugins/dummy_datasource/datasource.py 查看文件

@@ -1,6 +1,8 @@
1 1
 #-*- coding:utf-8 -*-
2 2
 
3
-class DummyDatasource(object):
3
+from lodel.plugin.datasource_plugin import AbstractDatasource
4
+
5
+class DummyDatasource(AbstractDatasource):
4 6
     
5 7
     def __init__(self, *conn_args, **conn_kwargs):
6 8
         self.conn_args = conn_args

+ 2
- 2
plugins/mongodb_datasource/datasource.py 查看文件

@@ -13,6 +13,7 @@ from lodel import logger
13 13
 from lodel.leapi.leobject import CLASS_ID_FIELDNAME
14 14
 from lodel.leapi.datahandlers.base_classes import Reference, MultipleRef
15 15
 from lodel.exceptions import LodelException, LodelFatalError
16
+from lodel.plugin.datasource_plugin import AbstractDatasource
16 17
 
17 18
 from . import utils
18 19
 from .exceptions import *
@@ -22,8 +23,7 @@ from .utils import object_collection_name, collection_name, \
22 23
 
23 24
 ##@brief Datasource class
24 25
 #@ingroup plugin_mongodb_datasource
25
-#@todo Make it inherit from an abstract datasource !
26
-class MongoDbDatasource(object):
26
+class MongoDbDatasource(AbstractDatasource):
27 27
 
28 28
     ##@brief Stores existing connections
29 29
     #

正在加载...
取消
保存