浏览代码

Adding a new class methos in plugins.pyto clear the class variables

Adding tests for the Plugin class
prieto 8 年前
父节点
当前提交
73d911f091
共有 2 个文件被更改,包括 95 次插入6 次删除
  1. 35
    6
      lodel/plugin/plugins.py
  2. 60
    0
      tests/plugin/test_plugin.py

+ 35
- 6
lodel/plugin/plugins.py 查看文件

@@ -16,7 +16,7 @@ import plugins
16 16
 # - main.py containing hooks registration etc
17 17
 # - confspec.py containing a configuration specification dictionary named CONFSPEC
18 18
 
19
-##@brief The package in wich we will load plugins modules
19
+##@brief The package in which we will load plugins modules
20 20
 VIRTUAL_PACKAGE_NAME = 'lodel.plugins'
21 21
 INIT_FILENAME = '__init__.py' # Loaded with settings
22 22
 CONFSPEC_FILENAME_VARNAME = '__confspec__'
@@ -26,7 +26,26 @@ PLUGIN_DEPS_VARNAME = '__plugin_deps__'
26 26
 ACTIVATE_METHOD_NAME = '_activate'
27 27
 
28 28
 class PluginError(Exception):
29
-    pass
29
+    def __init__(self, msg = "Unknow error", exceptions = None):
30
+        self._msg = msg
31
+        self._exceptions = dict() if exceptions is None else exceptions
32
+
33
+    def __repr__(self):
34
+        return self.__str__()
35
+
36
+    def __str__(self):
37
+        msg = self._msg
38
+        if isinstance(self._exceptions, dict):
39
+            for_iter = self._exceptions.items()
40
+        else:
41
+            for_iter = enumerate(self.__exceptions)
42
+        for obj, expt in for_iter:
43
+            msg += "\n\t{expt_obj} : ({expt_name}) {expt_msg}; ".format(
44
+                    expt_obj = obj,
45
+                    expt_name=expt.__class__.__name__,
46
+                    expt_msg=str(expt)
47
+            )
48
+        return msg
30 49
 
31 50
 ##@brief Handle plugins
32 51
 #
@@ -58,22 +77,23 @@ class Plugin(object):
58 77
     # @throw PluginError
59 78
     def __init__(self, plugin_name):
60 79
         self.started()
61
-        
62 80
         self.name = plugin_name
63 81
         self.path = self.plugin_path(plugin_name)
82
+        print(self.path)
64 83
         self.module = None
65 84
         self.__confspecs = dict()
66 85
         self.loaded = False
67 86
         
68 87
         # Importing __init__.py
69
-        plugin_module = '%s.%s' % ( VIRTUAL_PACKAGE_NAME,
88
+        plugin_module = '%s.%s' % (VIRTUAL_PACKAGE_NAME,
70 89
                                     plugin_name)
90
+
71 91
         init_source = self.path + INIT_FILENAME
72 92
         try:
73 93
             loader = SourceFileLoader(plugin_module, init_source)
74 94
             self.module = loader.load_module()
75 95
         except ImportError as e:
76
-            raise PluginError("Failed to load plugin '%s'. It seems that the plugin name is not valid" % plugin_name)
96
+             raise PluginError("Failed to load plugin '%s'. It seems that the plugin name is not valid" % plugin_name)
77 97
 
78 98
         # loading confspecs
79 99
         try:
@@ -169,7 +189,7 @@ class Plugin(object):
169 189
         
170 190
     ##@brief Load a plugin
171 191
     #
172
-    #Loading a plugin mean importing a file. The filename is defined in the 
192
+    #Loading a plugin means importing a file. The filename is defined in the 
173 193
     #plugin's __init__.py file in a LOADER_FILENAME_VARNAME variable.
174 194
     #
175 195
     #The loading process has to take care of other things :
@@ -325,6 +345,15 @@ class Plugin(object):
325 345
         res = cls._plugin_directories is not None
326 346
         if raise_if_not and not res:
327 347
             raise RuntimeError("Class Plugins is not initialized")
348
+            
349
+    @classmethod
350
+    def clear(cls):
351
+        if cls._plugin_directories is not None:
352
+            cls._plugin_directories = None
353
+        if cls._plugin_instances != dict():
354
+            cls._plugin_instances = dict()
355
+        if cls._load_called != []:
356
+            cls._load_called = []
328 357
 
329 358
 ##@page lodel2_plugins Lodel2 plugins system
330 359
 #

+ 60
- 0
tests/plugin/test_plugin.py 查看文件

@@ -0,0 +1,60 @@
1
+#-*- coding: utf-8 -*-
2
+
3
+import unittest
4
+
5
+from lodel.plugin.plugins import Plugin, PluginError
6
+
7
+class PluginTestCase(unittest.TestCase):
8
+
9
+    def test_plugin_init_right_name(self):
10
+        Plugin.start(['/home/helene/lodel2/plugins'],['dummy'])
11
+        Plugin.clear()
12
+        
13
+    # With a wrong plugin name, a NameError Exception has to be raised at line 318 of plugin.py
14
+    def test_plugin_init_wrong_name(self):
15
+        with self.assertRaises(NameError):
16
+            Plugin.start(['/home/helene/lodel2/plugins'],['wrong_plugin_name'])
17
+        Plugin.clear()
18
+        
19
+    # With a wrong plugin name, a NameError Exception has to be raised at line 318 of plugin.py
20
+    def test_plugin_init_right_wrong_name(self):
21
+        with self.assertRaises(NameError):
22
+            Plugin.start(['/home/helene/lodel2/plugins'],['dummy', 'wrong_plugin_name'])
23
+        Plugin.clear()
24
+    
25
+    def test_plugin_started(self):
26
+        with self.assertRaises(RuntimeError):
27
+            Plugin.started()
28
+            
29
+    def test_plugin_plugin_path(self):
30
+        Plugin.start(['/home/helene/lodel2/plugins'],['dummy'])
31
+        self.assertEqual(Plugin.plugin_path('dummy'), '/home/helene/lodel2/plugins/dummy/')
32
+        Plugin.clear()
33
+        
34
+    def test_plugin_get(self):
35
+        Plugin.start(['/home/helene/lodel2/plugins'],['dummy'])
36
+        with self.assertRaises(PluginError):
37
+            Plugin.get('wrong_plugin_name')
38
+        self.assertTrue(isinstance(Plugin.get('dummy'), Plugin))
39
+        Plugin.clear()
40
+        
41
+    def test_plugin_register(self):
42
+        with self.assertRaises(RuntimeError):
43
+            Plugin.register('dummy')
44
+        Plugin.start(['/home/helene/lodel2/plugins'],['dummy'])
45
+        with self.assertRaises(PluginError):
46
+            Plugin.register('dummy')
47
+        Plugin.clear()
48
+        
49
+    def test_plugin_load_all(self):
50
+        pass
51
+        
52
+    
53
+     
54
+        
55
+        
56
+        
57
+        
58
+        
59
+        
60
+        

正在加载...
取消
保存