Browse Source

Continue to split _init_datasource method of LeObject

Yann Weber 8 years ago
parent
commit
2da4b82770
1 changed files with 52 additions and 32 deletions
  1. 52
    32
      lodel/leapi/leobject.py

+ 52
- 32
lodel/leapi/leobject.py View File

211
     #@param ds_name str : The name of the datasource to instanciate
211
     #@param ds_name str : The name of the datasource to instanciate
212
     #@param ro bool : if true initialise the _ro_datasource attribute else
212
     #@param ro bool : if true initialise the _ro_datasource attribute else
213
     #initialise _rw_datasource attribute
213
     #initialise _rw_datasource attribute
214
+    #@throw SettingsError if an error occurs
214
     @classmethod
215
     @classmethod
215
     def _init_datasource(cls, ds_name, ro):
216
     def _init_datasource(cls, ds_name, ro):
216
-        ds_plugin, ds_name = cls._get_ds_plugin_name(ds_name, ro)
217
-        #Checks that the datasource is configured
218
-        if ds_plugin not in Settings.datasource._fields:
219
-            expt_msg += "Unknown or unconfigured datasource plugin %s"
220
-            expt_msg %= ds_plugin
217
+        expt_msg = "In LeAPI class '%s' " % cls.__name__
218
+        if ds_name not in Settings.datasources._fields:
219
+            #Checking that datasource exists
220
+            expt_msg += "Unknow or unconfigured datasource %s"
221
+            expt_msg %= (ds_name, cls.__name__)
221
             raise SettingsError(expt_msg)
222
             raise SettingsError(expt_msg)
222
-        # fetching datasource configuration
223
-        ds_conf = getattr(Settings.datasource, ds_plugin)
224
-        if ds_name not in ds_conf._fields:
225
-            expt_msg += "Unknown or unconfigured datasource instance %s"
226
-            expt_msg %= ds_identifier
223
+        try:
224
+            #fetching plugin name
225
+            ds_plugin_name, ds_name = cls._get_ds_plugin_name(ds_name, ro)
226
+        except NameError:
227
+            expt_msg += "Datasource %s is missconfigured, missing identifier."
228
+            expt_msg %= ds_name
229
+            raise SettingsError(expt_msg)
230
+        except RuntimeError:
231
+            expt_msg += "Error in datasource %s configuration. Trying to use \
232
+a read only as a read&write datasource"
233
+            expt_msg %= ds_name
234
+            raise SettingsError(expt_msg)
235
+        
236
+        try:
237
+            ds_conf = cls._get_ds_connection_conf(ds_name, ds_plugin_name)
238
+        except NameError as e:
239
+            expt_msg += str(e)
227
             raise SettingsError(expt_msg)
240
             raise SettingsError(expt_msg)
228
-
229
-        ds_conf = getattr(ds_conf, ds_name)
230
         #Checks that the datasource plugin exists
241
         #Checks that the datasource plugin exists
231
-        ds_plugin_module = Plugin.get(ds_plugin).loader_module()
242
+        ds_plugin_module = Plugin.get(ds_plugin_name).loader_module()
232
         try:
243
         try:
233
             datasource_class = getattr(ds_plugin_module, "Datasource")
244
             datasource_class = getattr(ds_plugin_module, "Datasource")
234
         except AttributeError as e:
245
         except AttributeError as e:
236
 raised when trying to import Datasource"
247
 raised when trying to import Datasource"
237
             expt_msg %= ds_identifier
248
             expt_msg %= ds_identifier
238
             raise SettingsError(expt_msg)
249
             raise SettingsError(expt_msg)
239
-        ds_conf_old = ds_conf
240
-        ds_conf = dict()
241
-        for k in ds_conf_old._fields:
242
-            ds_conf[k] = getattr(ds_conf_old, k)
243
 
250
 
244
         return datasource_class(**ds_conf)
251
         return datasource_class(**ds_conf)
245
 
252
 
253
+    ##@brief Try to fetch a datasource configuration
254
+    #@param ds_name str : datasource name
255
+    #@param ds_plugin_name : datasource plugin name
256
+    #@return a dict containing datasource initialisation options
257
+    #@throw NameError if a datasource plugin or instance cannot be found
258
+    @staticmethod
259
+    def _get_ds_connection_conf(ds_name,ds_plugin_name):
260
+        if ds_plugin_name not in Settings.datasource._fields:
261
+            msg = "Unknown or unconfigured datasource plugin %s"
262
+            msg %= ds_plugin
263
+            raise NameError(msg)
264
+        ds_conf = getattr(Settings.datasource, ds_plugin_name)
265
+        if ds_name not in ds_conf._fields:
266
+            msg = "Unknown or unconfigured datasource instance %s"
267
+            msg %= ds_identifier
268
+            raise NameError(msg)
269
+        ds_conf = getattr(ds_conf, ds_name)
270
+        return {k: getattr(ds_conf,k) for k in ds_conf._fields }
271
+
246
     ##@brief fetch datasource plugin name
272
     ##@brief fetch datasource plugin name
273
+    #@param ds_name str : datasource name
274
+    #@param ro bool : if true consider the datasource as read only
247
     #@return a tuple(DATASOURCE_PLUGIN_NAME, DATASOURCE_CONNECTION_NAME)
275
     #@return a tuple(DATASOURCE_PLUGIN_NAME, DATASOURCE_CONNECTION_NAME)
248
-    @classmethod
249
-    def _get_ds_plugin_name(cls, ds_name, ro):
250
-        expt_msg = "In LeAPI class '%s' " % cls.__name__
276
+    #@throw NameError if datasource identifier not found
277
+    #@throw RuntimeError if datasource is read_only but ro flag was false
278
+    @staticmethod
279
+    def _get_ds_plugin_name(ds_name, ro):
251
         datasource_orig_name = ds_name
280
         datasource_orig_name = ds_name
252
-        if ds_name not in Settings.datasources._fields:
253
-            expt_msg += "Unknow or unconfigured datasource %s"
254
-            expt_msg %= (ds_name, cls.__name__)
255
-            raise SettingsError(expt_msg)
256
         # fetching connection identifier given datasource name
281
         # fetching connection identifier given datasource name
257
         ds_identifier = getattr(Settings.datasources, ds_name)
282
         ds_identifier = getattr(Settings.datasources, ds_name)
258
         read_only = getattr(ds_identifier, 'read_only')
283
         read_only = getattr(ds_identifier, 'read_only')
259
         try:
284
         try:
260
             ds_identifier = getattr(ds_identifier, 'identifier')
285
             ds_identifier = getattr(ds_identifier, 'identifier')
261
-        except NameError:
262
-            expt_msg += "Datasource %s is missconfigured, missing identifier."
263
-            expt_msg %= ds_name
264
-            raise SettingsError(expt_msg)
286
+        except NameError as e:
287
+            raise e
265
         if read_only and not ro:
288
         if read_only and not ro:
266
-            expt_msg += "Error in datasource %s configuration. Trying to use \
267
-a read only as a read&write datasource"
268
-            expt_msg %= ds_name
269
-            raise SettingsError(expt_msg)
289
+            raise RuntimeError()
270
         return ds_identifier.split('.')
290
         return ds_identifier.split('.')
271
     
291
     
272
     ##@brief Return the uid of the current LeObject instance
292
     ##@brief Return the uid of the current LeObject instance

Loading…
Cancel
Save