Browse Source

Enable factory in Plugin

- now plugin is able to instanciate using the good child class
- the discover cache now contains plugin's type
Yann Weber 8 years ago
parent
commit
34c1784907

+ 6
- 4
lodel/plugin/datasource_plugin.py View File

@@ -12,10 +12,12 @@ class DatasourcePlugin(Plugin):
12 12
     
13 13
     def __init__(self, name):
14 14
         super().__init__(name)
15
-        self.__datasource_cls = self.loader_module().Datasource
15
+        self.__datasource_cls = None
16 16
 
17
-    def datasource(self):
18
-        return self.__datasource
17
+    def datasource_cls(self):
18
+        if self.__datasource_cls is None:
19
+            self.__datasource_cls = self.loader_module().Datasource
20
+        return self.__datasource_cls
19 21
 
20 22
     def migration_handler(self):
21 23
         return self.loader_module().migration_handler_class()
@@ -111,7 +113,7 @@ but %s is a %s" % (ds_name, pinstance.__class__.__name__))
111 113
     #@throw PluginTypeError if ds_name is not the name of a DatasourcePlugin
112 114
     @classmethod
113 115
     def get_datasource(cls, ds_plugin_name):
114
-        return cls.get(ds_plugin_name).datasource()
116
+        return cls.get(ds_plugin_name).datasource_cls()
115 117
 
116 118
     @classmethod
117 119
     def get_migration_handler(cls, ds_plugin_name):

+ 38
- 9
lodel/plugin/plugins.py View File

@@ -27,7 +27,7 @@ VIRTUAL_TEMP_PACKAGE_NAME = 'lodel.plugin_tmp'
27 27
 ##@brief Plugin init filename
28 28
 INIT_FILENAME = '__init__.py' # Loaded with settings
29 29
 PLUGIN_NAME_VARNAME = '__plugin_name__'
30
-PLUGIN_TYPE_VARNAME = '__type__'
30
+PLUGIN_TYPE_VARNAME = '__plugin_type__'
31 31
 PLUGIN_VERSION_VARNAME = '__version__'
32 32
 CONFSPEC_FILENAME_VARNAME = '__confspec__'
33 33
 CONFSPEC_VARNAME = 'CONFSPEC'
@@ -42,8 +42,8 @@ DEFAULT_PLUGINS_PATH_LIST = ['./plugins']
42 42
 MANDATORY_VARNAMES = [PLUGIN_NAME_VARNAME, LOADER_FILENAME_VARNAME, 
43 43
     PLUGIN_VERSION_VARNAME]
44 44
 
45
-EXTENSIONS = 'default'
46
-PLUGINS_TYPES = [EXTENSIONS, 'datasource', 'session_handler', 'ui']
45
+DEFAULT_PLUGIN_TYPE = 'extension'
46
+PLUGINS_TYPES = [DEFAULT_PLUGIN_TYPE, 'datasource', 'session_handler', 'ui']
47 47
 
48 48
 
49 49
 ##@brief Describe and handle version numbers
@@ -249,7 +249,7 @@ init file. Malformed plugin"
249 249
         try:
250 250
             self.__type = getattr(self.module, PLUGIN_TYPE_VARNAME)
251 251
         except AttributeError:
252
-            self.__type = EXTENSIONS
252
+            self.__type = DEFAULT_PLUGIN_TYPE
253 253
         self.__type = str(self.__type).lower()
254 254
         if self.__type not in PLUGINS_TYPES:
255 255
             raise PluginError("Unknown plugin type '%s'" % self.__type)
@@ -427,6 +427,17 @@ name differ from the one found in plugin's init file"
427 427
     def confspecs(self):
428 428
         return copy.copy(self.__confspecs)
429 429
 
430
+    ##@brief Attempt to read plugin discover cache
431
+    #@note If no cache yet make a discover with default plugin directory
432
+    #@return a dict (see @ref _discover() )
433
+    @classmethod
434
+    def plugin_cache(cls):
435
+        if not os.path.isfile(DISCOVER_CACHE_FILENAME):
436
+            cls.discover()
437
+        with open(DISCOVER_CACHE_FILENAME) as pdcache_fd:
438
+            res = json.load(pdcache_fd)
439
+        return res
440
+
430 441
     ##@brief Register a new plugin
431 442
     # 
432 443
     #@param plugin_name str : The plugin name
@@ -434,11 +445,23 @@ name differ from the one found in plugin's init file"
434 445
     #@throw PluginError
435 446
     @classmethod
436 447
     def register(cls, plugin_name):
448
+        from .datasource_plugin import DatasourcePlugin
437 449
         if plugin_name in cls._plugin_instances:
438 450
             msg = "Plugin allready registered with same name %s"
439 451
             msg %= plugin_name
440 452
             raise PluginError(msg)
441
-        plugin = cls(plugin_name)
453
+        #Here we check that previous discover found a plugin with that name
454
+        pdcache = cls.plugin_cache()
455
+        if plugin_name not in pdcache['plugins']:
456
+            raise PluginError("No plugin named %s found" % plugin_name)
457
+        pinfos = pdcache['plugins'][plugin_name]
458
+        ptype = pinfos['type']
459
+        if ptype == 'datasource':
460
+            pcls = DatasourcePlugin
461
+        else:
462
+            pcls = cls
463
+        print(plugin_name, ptype, pcls)
464
+        plugin = pcls(plugin_name)
442 465
         cls._plugin_instances[plugin_name] = plugin
443 466
         logger.debug("Plugin %s available." % plugin)
444 467
         return plugin
@@ -531,8 +554,7 @@ name differ from the one found in plugin's init file"
531 554
         result = dict()
532 555
         for pinfos in tmp_res:
533 556
             pname = pinfos['name']
534
-            if (
535
-                    pname in result 
557
+            if (    pname in result 
536 558
                     and pinfos['version'] > result[pname]['version'])\
537 559
                 or pname not in result:
538 560
                 result[pname] = pinfos
@@ -618,16 +640,23 @@ name differ from the one found in plugin's init file"
618 640
                 log_msg %= (attr_name, INIT_FILENAME)
619 641
                 logger.debug(log_msg)
620 642
                 return False
643
+        #Fetching plugin's version
621 644
         try:
622 645
             pversion = getattr(initmod, PLUGIN_VERSION_VARNAME)
623
-        except PluginError as e:
646
+        except (NameError, AttributeError) as e:
624 647
             msg = "Invalid plugin version found in %s : %s"
625 648
             msg %= (path, e)
626 649
             raise PluginError(msg)
650
+        #Fetching plugin's type
651
+        try:
652
+            ptype = getattr(initmod, PLUGIN_TYPE_VARNAME)
653
+        except (NameError, AttributeError) as e:
654
+            ptype = DEFAULT_PLUGIN_TYPE
627 655
         pname = getattr(initmod, PLUGIN_NAME_VARNAME)
628 656
         return {'name': pname,
629 657
             'version': pversion,
630
-            'path': path}
658
+            'path': path,
659
+            'type': ptype}
631 660
     
632 661
     ##@brief Import init file from a plugin path
633 662
     #@param path str : Directory path

+ 1
- 0
plugins/dummy_datasource/__init__.py View File

@@ -1,6 +1,7 @@
1 1
 from lodel.settings.validator import SettingValidator
2 2
 from .datasource import DummyDatasource as Datasource
3 3
 
4
+__plugin_type__ = 'datasource'
4 5
 __plugin_name__ = "dummy_datasource"
5 6
 __version__ = '0.0.1'
6 7
 __loader__ = 'main.py'

+ 1
- 0
plugins/mongodb_datasource/__init__.py View File

@@ -1,4 +1,5 @@
1 1
 #-*- coding: utf-8 -*-
2
+__plugin_type__ = 'datasource'
2 3
 __plugin_name__ = 'mongodb_datasource'
3 4
 __version__ = '0.0.1'
4 5
 __plugin_type__ = 'datasource'

Loading…
Cancel
Save