Преглед изворни кода

Plugin discover & clear bugfixes

- Child classes where not cleared when Plugin.clear() was called. Now child classes implements a clear_cls() method to clear a Plugin child class
- When discovering plugins if during a is_plugin_dir() check we import two times in the same virtual module name, all non existing attributes values are taken from prvious imported file (the bug was on plugin_type when webui was imported just before dummy, then dummy was an interface)
Yann Weber пре 8 година
родитељ
комит
fdc985c732
3 измењених фајлова са 42 додато и 3 уклоњено
  1. 9
    0
      lodel/plugin/interface.py
  2. 24
    3
      lodel/plugin/plugins.py
  3. 9
    0
      lodel/plugin/sessionhandler.py

+ 9
- 0
lodel/plugin/interface.py Прегледај датотеку

@@ -25,3 +25,12 @@ class InterfacePlugin(Plugin):
25 25
             raise PluginError("Maximum one interface allowed")
26 26
         super().__init__(name)
27 27
         self._instance = self
28
+
29
+    ##@brief Clear class
30
+    #@see plugins.Plugin::clear()
31
+    @classmethod
32
+    def clear_cls(cls):
33
+        if cls._instance is not None:
34
+            inst = cls._instance
35
+            cls._instance = None
36
+            del(inst)

+ 24
- 3
lodel/plugin/plugins.py Прегледај датотеку

@@ -216,6 +216,13 @@ class MetaPlugType(type):
216 216
         if ptype_name not in cls._all_ptypes:
217 217
             raise PluginError("Unknown plugin type '%s'" % ptype_name)
218 218
         return cls._all_ptypes[ptype_name]
219
+    
220
+    ##@brief Call the clear classmethod on each child classes
221
+    @classmethod
222
+    def clear_cls(cls):
223
+        for pcls in cls._all_ptypes.values():
224
+            pcls.clear_cls()
225
+        
219 226
 
220 227
 ##@brief Handle plugins
221 228
 #@ingroup lodel2_plugins
@@ -254,6 +261,12 @@ class Plugin(object, metaclass=MetaPlugType):
254 261
     #None in abstract classes and implemented by child classes
255 262
     _type_conf_name = None
256 263
 
264
+    ##@brief Stores virtual modules uniq key
265
+    #@note When testing if a dir contains a plugin, if we reimport the __init__
266
+    #in a module with the same name, all non existing value (plugin_type for
267
+    #example) are replaced by previous plugin values
268
+    _mod_cnt = 0
269
+
257 270
     ##@brief Plugin class constructor
258 271
     #
259 272
     # Called by setting in early stage of lodel2 boot sequence using classmethod
@@ -656,6 +669,12 @@ file : '%s'. Running discover again..." % DISCOVER_CACHE_FILENAME)
656 669
             cls._plugin_instances = dict()
657 670
         if cls._load_called != []:
658 671
             cls._load_called = []
672
+        MetaPlugType.clear_cls()
673
+    
674
+    ##@brief Designed to be implemented by child classes
675
+    @classmethod
676
+    def clear_cls(cls):
677
+        pass
659 678
     
660 679
     ##@brief Reccursively walk throught paths to find plugin, then stores
661 680
     #found plugin in a file...
@@ -684,6 +703,7 @@ file : '%s'. Running discover again..." % DISCOVER_CACHE_FILENAME)
684 703
                 #dropped
685 704
                 pass
686 705
         result = {'path_list': paths, 'plugins': result}
706
+        print("DEUG ",result['plugins'])
687 707
         #Writing to cache
688 708
         if not no_cache:
689 709
             with open(DISCOVER_CACHE_FILENAME, 'w+') as pdcache:
@@ -789,11 +809,12 @@ file : '%s'. Running discover again..." % DISCOVER_CACHE_FILENAME)
789 809
     #@param path str : Directory path
790 810
     #@return a tuple (init_module, module_name)
791 811
     @classmethod
792
-    def import_init(self, path):
812
+    def import_init(cls, path):
813
+        cls._mod_cnt += 1 # in order to ensure module name unicity
793 814
         init_source = os.path.join(path, INIT_FILENAME)
794
-        temp_module = '%s.%s.%s' % (
815
+        temp_module = '%s.%s.%s%d' % (
795 816
             VIRTUAL_TEMP_PACKAGE_NAME, os.path.basename(os.path.dirname(path)),
796
-            'test_init')
817
+            'test_init', cls._mod_cnt)
797 818
         try:
798 819
             loader = SourceFileLoader(temp_module, init_source)
799 820
         except (ImportError, FileNotFoundError) as e:

+ 9
- 0
lodel/plugin/sessionhandler.py Прегледај датотеку

@@ -57,3 +57,12 @@ class SessionHandlerPlugin(Plugin, metaclass=SessionPluginWrapper):
57 57
             self.__class__._instance = self
58 58
         else:
59 59
             raise RuntimeError("A SessionHandler Plugin is already plug")
60
+    
61
+    ##@brief Clear class
62
+    #@see plugins.Plugin::clear()
63
+    @classmethod
64
+    def clear_cls(cls):
65
+        if cls._instance is not None:
66
+            inst = cls._instance
67
+            cls._instance = None
68
+            del(inst)

Loading…
Откажи
Сачувај