From 3e4231e871dfee46722c9d951c8e923df6ed3494 Mon Sep 17 00:00:00 2001 From: Yann Date: Wed, 7 Sep 2016 16:55:02 +0200 Subject: [PATCH] Add an abtract datasource to make Datasource classes of plugins inherit from it refs #132 --- lodel/plugin/datasource_plugin.py | 66 ++++++++++++++++++++++++ plugins/dummy_datasource/datasource.py | 4 +- plugins/mongodb_datasource/datasource.py | 4 +- 3 files changed, 71 insertions(+), 3 deletions(-) diff --git a/lodel/plugin/datasource_plugin.py b/lodel/plugin/datasource_plugin.py index 63a6a49..233272c 100644 --- a/lodel/plugin/datasource_plugin.py +++ b/lodel/plugin/datasource_plugin.py @@ -1,5 +1,6 @@ from .plugins import Plugin from .exceptions import * +from lodel.exceptions import * from lodel.settings.validator import SettingValidator @@ -162,6 +163,71 @@ but %s is a %s" % (ds_name, pinstance.__class__.__name__)) def get_migration_handler(cls, ds_plugin_name): return cls.get(ds_plugin_name).migration_handler_cls() +class AbstractDatasource(object): + + ##@brief Trigger LodelFatalError when abtract method called + @staticmethod + def _abs_err(): + raise LodelFatalError("This method is abstract and HAVE TO be \ +reimplemented by plugin datasource child class") + + ##@brief The constructor + def __init__(self, *conn_args, **conn_kwargs): + self._abs_err() + + ##@brief Provide a new uniq numeric ID + #@param emcomp LeObject subclass (not instance) : To know on wich things we + #have to be uniq + #@return an integer + def new_numeric_id(self, emcomp): + self._abs_err() + + ##@brief returns a selection of documents from the datasource + #@param target_cls Emclass + #@param field_list list + #@param filters list : List of filters + #@param rel_filters list : List of relational filters + #@param order list : List of column to order. ex: order = [('title', 'ASC'),] + #@param group list : List of tupple representing the column to group together. ex: group = [('title', 'ASC'),] + #@param limit int : Number of records to be returned + #@param offset int: used with limit to choose the start record + #@param instanciate bool : If true, the records are returned as instances, else they are returned as dict + #@return list + def select(self, target, field_list, filters, rel_filters=None, order=None, group=None, limit=None, offset=0, + instanciate=True): + self._abs_err() + + ##@brief Deletes records according to given filters + #@param target Emclass : class of the record to delete + #@param filters list : List of filters + #@param relational_filters list : List of relational filters + #@return int : number of deleted records + def delete(self, target, filters, relational_filters): + self._abs_err() + + ## @brief updates records according to given filters + #@param target Emclass : class of the object to insert + #@param filters list : List of filters + #@param relational_filters list : List of relational filters + #@param upd_datas dict : datas to update (new values) + #@return int : Number of updated records + def update(self, target, filters, relational_filters, upd_datas): + self._abs_err() + + ## @brief Inserts a record in a given collection + # @param target Emclass : class of the object to insert + # @param new_datas dict : datas to insert + # @return the inserted uid + def insert(self, target, new_datas): + self._abs_err() + + ## @brief Inserts a list of records in a given collection + # @param target Emclass : class of the objects inserted + # @param datas_list list : list of dict + # @return list : list of the inserted records' ids + def insert_multi(self, target, datas_list): + self._abs_err() + ##@page lodel2_datasources Lodel2 datasources # #@par lodel2_datasources_intro Intro diff --git a/plugins/dummy_datasource/datasource.py b/plugins/dummy_datasource/datasource.py index f269778..d83798a 100644 --- a/plugins/dummy_datasource/datasource.py +++ b/plugins/dummy_datasource/datasource.py @@ -1,6 +1,8 @@ #-*- coding:utf-8 -*- -class DummyDatasource(object): +from lodel.plugin.datasource_plugin import AbstractDatasource + +class DummyDatasource(AbstractDatasource): def __init__(self, *conn_args, **conn_kwargs): self.conn_args = conn_args diff --git a/plugins/mongodb_datasource/datasource.py b/plugins/mongodb_datasource/datasource.py index 726bfc0..24d6d03 100644 --- a/plugins/mongodb_datasource/datasource.py +++ b/plugins/mongodb_datasource/datasource.py @@ -13,6 +13,7 @@ from lodel import logger from lodel.leapi.leobject import CLASS_ID_FIELDNAME from lodel.leapi.datahandlers.base_classes import Reference, MultipleRef from lodel.exceptions import LodelException, LodelFatalError +from lodel.plugin.datasource_plugin import AbstractDatasource from . import utils from .exceptions import * @@ -22,8 +23,7 @@ from .utils import object_collection_name, collection_name, \ ##@brief Datasource class #@ingroup plugin_mongodb_datasource -#@todo Make it inherit from an abstract datasource ! -class MongoDbDatasource(object): +class MongoDbDatasource(AbstractDatasource): ##@brief Stores existing connections #