|
@@ -4,13 +4,14 @@ import importlib
|
4
|
4
|
import warnings
|
5
|
5
|
import copy
|
6
|
6
|
|
7
|
|
-from lodel.plugin import Plugin
|
8
|
7
|
from lodel import logger
|
9
|
8
|
from lodel.settings import Settings
|
10
|
9
|
from lodel.settings.utils import SettingsError
|
11
|
10
|
from .query import LeInsertQuery, LeUpdateQuery, LeDeleteQuery, LeGetQuery
|
12
|
11
|
from .exceptions import *
|
|
12
|
+from lodel.plugin.exceptions import *
|
13
|
13
|
from lodel.plugin.hooks import LodelHook
|
|
14
|
+from lodel.plugin import Plugin, DatasourcePlugin
|
14
|
15
|
from lodel.leapi.datahandlers.base_classes import DatasConstructor
|
15
|
16
|
|
16
|
17
|
##@brief Stores the name of the field present in each LeObject that indicates
|
|
@@ -244,7 +245,7 @@ class LeObject(object):
|
244
|
245
|
else:
|
245
|
246
|
ro_ds, rw_ds = cls._datasource_name
|
246
|
247
|
#Read only datasource initialisation
|
247
|
|
- cls._ro_datasource = cls._init_datasource(ro_ds, True)
|
|
248
|
+ cls._ro_datasource = DatasourcePlugin.init_datasource(ro_ds, True)
|
248
|
249
|
if cls._ro_datasource is None:
|
249
|
250
|
log_msg = "No read only datasource set for LeObject %s"
|
250
|
251
|
log_msg %= cls.__name__
|
|
@@ -254,7 +255,7 @@ class LeObject(object):
|
254
|
255
|
log_msg %= (ro_ds, cls.__name__)
|
255
|
256
|
logger.debug(log_msg)
|
256
|
257
|
#Read write datasource initialisation
|
257
|
|
- cls._rw_datasource = cls._init_datasource(rw_ds, False)
|
|
258
|
+ cls._rw_datasource = DatasourcePlugin.init_datasource(rw_ds, False)
|
258
|
259
|
if cls._ro_datasource is None:
|
259
|
260
|
log_msg = "No read/write datasource set for LeObject %s"
|
260
|
261
|
log_msg %= cls.__name__
|
|
@@ -264,99 +265,6 @@ class LeObject(object):
|
264
|
265
|
log_msg %= (ro_ds, cls.__name__)
|
265
|
266
|
logger.debug(log_msg)
|
266
|
267
|
|
267
|
|
-
|
268
|
|
- ##@brief Replace the _datasource attribute value by a datasource instance
|
269
|
|
- #
|
270
|
|
- #This method is used once at dyncode load to replace the datasource string
|
271
|
|
- #by a datasource instance to avoid doing this operation for each query
|
272
|
|
- #@param ds_name str : The name of the datasource to instanciate
|
273
|
|
- #@param ro bool : if true initialise the _ro_datasource attribute else
|
274
|
|
- #initialise _rw_datasource attribute
|
275
|
|
- #@throw SettingsError if an error occurs
|
276
|
|
- @classmethod
|
277
|
|
- def _init_datasource(cls, ds_name, ro):
|
278
|
|
- expt_msg = "In LeAPI class '%s' " % cls.__name__
|
279
|
|
- if ds_name not in Settings.datasources._fields:
|
280
|
|
- #Checking that datasource exists
|
281
|
|
- expt_msg += "Unknown or unconfigured datasource %s for class %s"
|
282
|
|
- expt_msg %= (ds_name, cls.__name__)
|
283
|
|
- raise SettingsError(expt_msg)
|
284
|
|
- try:
|
285
|
|
- #fetching plugin name
|
286
|
|
- ds_plugin_name, ds_identifier = cls._get_ds_plugin_name(ds_name, ro)
|
287
|
|
- except NameError:
|
288
|
|
- expt_msg += "Datasource %s is missconfigured, missing identifier."
|
289
|
|
- expt_msg %= ds_name
|
290
|
|
- raise SettingsError(expt_msg)
|
291
|
|
- except RuntimeError:
|
292
|
|
- expt_msg += "Error in datasource %s configuration. Trying to use \
|
293
|
|
-a read only as a read&write datasource"
|
294
|
|
- expt_msg %= ds_name
|
295
|
|
- raise SettingsError(expt_msg)
|
296
|
|
- except ValueError as e:
|
297
|
|
- expt_msg += str(e)
|
298
|
|
- raise SettingsError(expt_msg)
|
299
|
|
-
|
300
|
|
- try:
|
301
|
|
- ds_conf = cls._get_ds_connection_conf(ds_identifier, ds_plugin_name)
|
302
|
|
- except NameError as e:
|
303
|
|
- expt_msg += str(e)
|
304
|
|
- raise SettingsError(expt_msg)
|
305
|
|
- #Checks that the datasource plugin exists
|
306
|
|
- ds_plugin_module = Plugin.get(ds_plugin_name).loader_module()
|
307
|
|
- try:
|
308
|
|
- datasource_class = getattr(ds_plugin_module, "Datasource")
|
309
|
|
- except AttributeError as e:
|
310
|
|
- expt_msg += "The datasource plugin %s seems to be invalid. Error \
|
311
|
|
-raised when trying to import Datasource"
|
312
|
|
- expt_msg %= ds_identifier
|
313
|
|
- raise SettingsError(expt_msg)
|
314
|
|
-
|
315
|
|
- return datasource_class(**ds_conf)
|
316
|
|
-
|
317
|
|
- ##@brief Try to fetch a datasource configuration
|
318
|
|
- #@param ds_identifier str : datasource name
|
319
|
|
- #@param ds_plugin_name : datasource plugin name
|
320
|
|
- #@return a dict containing datasource initialisation options
|
321
|
|
- #@throw NameError if a datasource plugin or instance cannot be found
|
322
|
|
- @staticmethod
|
323
|
|
- def _get_ds_connection_conf(ds_identifier,ds_plugin_name):
|
324
|
|
- if ds_plugin_name not in Settings.datasource._fields:
|
325
|
|
- msg = "Unknown or unconfigured datasource plugin %s"
|
326
|
|
- msg %= ds_plugin
|
327
|
|
- raise NameError(msg)
|
328
|
|
- ds_conf = getattr(Settings.datasource, ds_plugin_name)
|
329
|
|
- if ds_identifier not in ds_conf._fields:
|
330
|
|
- msg = "Unknown or unconfigured datasource instance %s"
|
331
|
|
- msg %= ds_identifier
|
332
|
|
- raise NameError(msg)
|
333
|
|
- ds_conf = getattr(ds_conf, ds_identifier)
|
334
|
|
- return {k: getattr(ds_conf,k) for k in ds_conf._fields }
|
335
|
|
-
|
336
|
|
- ##@brief fetch datasource plugin name
|
337
|
|
- #@param ds_name str : datasource name
|
338
|
|
- #@param ro bool : if true consider the datasource as read only
|
339
|
|
- #@return a tuple(DATASOURCE_PLUGIN_NAME, DATASOURCE_CONNECTION_NAME)
|
340
|
|
- #@throw NameError if datasource identifier not found
|
341
|
|
- #@throw RuntimeError if datasource is read_only but ro flag was false
|
342
|
|
- @staticmethod
|
343
|
|
- def _get_ds_plugin_name(ds_name, ro):
|
344
|
|
- datasource_orig_name = ds_name
|
345
|
|
- # fetching connection identifier given datasource name
|
346
|
|
- ds_identifier = getattr(Settings.datasources, ds_name)
|
347
|
|
- read_only = getattr(ds_identifier, 'read_only')
|
348
|
|
- try:
|
349
|
|
- ds_identifier = getattr(ds_identifier, 'identifier')
|
350
|
|
- except NameError as e:
|
351
|
|
- raise e
|
352
|
|
- if read_only and not ro:
|
353
|
|
- raise RuntimeError()
|
354
|
|
- res = ds_identifier.split('.')
|
355
|
|
- if len(res) != 2:
|
356
|
|
- raise ValueError("expected value for identifier is like \
|
357
|
|
-DS_PLUGIN_NAME.DS_INSTANCE_NAME. But got %s" % ds_identifier)
|
358
|
|
- return res
|
359
|
|
-
|
360
|
268
|
##@brief Return the uid of the current LeObject instance
|
361
|
269
|
#@return the uid value
|
362
|
270
|
#@warning Broke multiple uid capabilities
|