|
@@ -2,6 +2,11 @@
|
2
|
2
|
|
3
|
3
|
import importlib
|
4
|
4
|
|
|
5
|
+from lodel.plugin import Plugins
|
|
6
|
+from lodel import logger
|
|
7
|
+from lodel.settings import Settings
|
|
8
|
+from lodel.settings.utils import SettingsError
|
|
9
|
+
|
5
|
10
|
class LeApiErrors(Exception):
|
6
|
11
|
##@brief Instanciate a new exceptions handling multiple exceptions
|
7
|
12
|
# @param msg str : Exception message
|
|
@@ -179,6 +184,51 @@ class LeObject(object):
|
179
|
184
|
except KeyError:
|
180
|
185
|
raise NameError("No field named '%s' in %s" % ( fieldname,
|
181
|
186
|
cls.__name__))
|
|
187
|
+
|
|
188
|
+ ##@brief Replace the _datasource attribute value by a datasource instance
|
|
189
|
+ #
|
|
190
|
+ # This method is used once at dyncode load to replace the datasource string
|
|
191
|
+ # by a datasource instance to avoid doing this operation for each query
|
|
192
|
+ @classmethod
|
|
193
|
+ def _init_datasource(cls):
|
|
194
|
+ expt_msg = "In LeAPI class '%s' " % cls.__name__
|
|
195
|
+ if cls._datasource not in Settings.datasources._fields:
|
|
196
|
+ expt_msg += "Unknow or unconfigured datasource %s"
|
|
197
|
+ expt_msg %= (cls._datasource, cls.__name__)
|
|
198
|
+ raise SettingsError(expt_msg)
|
|
199
|
+
|
|
200
|
+ ds_identifier = getattr(Settings.datasources, cls._datasource)
|
|
201
|
+ try:
|
|
202
|
+ ds_identifier = getattr(ds_identifier, 'identifier')
|
|
203
|
+ except NameError:
|
|
204
|
+ expt_msg += "Datasource %s is missconfigured, missing identifier."
|
|
205
|
+ expt_msg %= cls._datasource
|
|
206
|
+ raise SettingsError(expt_msg)
|
|
207
|
+
|
|
208
|
+ ds_plugin, ds_name = ds_identifier.split('.')
|
|
209
|
+ #Checks that the datasource is configured
|
|
210
|
+ if ds_plugin not in Settings.datasource._fields:
|
|
211
|
+ expt_msg += "Unknown or unconfigured datasource plugin %s"
|
|
212
|
+ expt_msg %= ds_plugin
|
|
213
|
+ raise SettingsError(expt_msg)
|
|
214
|
+
|
|
215
|
+ ds_conf = getattr(Settings.datasource, ds_plugin)
|
|
216
|
+ if ds_name not in ds_conf._fields:
|
|
217
|
+ expt_msg += "Unknown or unconfigured datasource instance %s"
|
|
218
|
+ expt_msg %= ds_identifier
|
|
219
|
+ raise SettingsError(expt_msg)
|
|
220
|
+
|
|
221
|
+ ds_conf = getattr(ds_conf, ds_name)
|
|
222
|
+ #Checks that the datasource plugin exists
|
|
223
|
+ ds_plugin_module = Plugins.plugin_module(ds_plugin)
|
|
224
|
+ try:
|
|
225
|
+ cls._datasource = getattr(ds_plugin_module, "Datasource")
|
|
226
|
+ except AttributeError as e:
|
|
227
|
+ raise e
|
|
228
|
+ expt_msg += "The datasource plugin %s seems to be invalid. Error raised when trying to import Datasource"
|
|
229
|
+ expt_msg %= ds_identifier
|
|
230
|
+ raise SettingsError(expt_msg)
|
|
231
|
+ logger.debug("Datasource initialized for LeObject %s" % cls.__name__)
|
182
|
232
|
|
183
|
233
|
##@brief Read only access to all datas
|
184
|
234
|
# @note for fancy data accessor use @ref LeObject.g attribute @ref LeObjectValues instance
|