Browse Source

Solves #123 Add a plugin type validator

- The validator is written and added to all Plugin sublcasses confspec
- Adds the capabilities to give args to validator (here we give the expected type)
Yann Weber 8 years ago
parent
commit
77be17f296

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

@@ -2,6 +2,8 @@ from .plugins import Plugin
2 2
 from .exceptions import *
3 3
 from lodel.settings.validator import SettingValidator
4 4
 
5
+
6
+_glob_typename = 'datasource'
5 7
 ##@brief Designed to handles datasources plugins
6 8
 #
7 9
 #A datasource provide data access to LeAPI typically a connector on a DB
@@ -16,13 +18,15 @@ from lodel.settings.validator import SettingValidator
16 18
 #@todo Write abstract classes for Datasource and MigrationHandler !!!
17 19
 class DatasourcePlugin(Plugin):
18 20
     
21
+    _type_conf_name = _glob_typename
19 22
     ##@brief Stores confspecs indicating where DatasourcePlugin list is stored
20 23
     _plist_confspecs = {
21 24
         'section': 'lodel2',
22 25
         'key': 'datasource_connectors',
23 26
         'default': None,
24
-        'validator': SettingValidator('strip', none_is_valid = False) }
25
-    _type_conf_name = 'datasource'
27
+        'validator': SettingValidator(
28
+            'plugin', none_is_valid = False,
29
+            ptype = _glob_typename) }
26 30
  
27 31
     ##@brief Construct a DatasourcePlugin 
28 32
     #@param name str : plugin name

+ 4
- 2
lodel/plugin/extensions.py View File

@@ -2,13 +2,15 @@ from .plugins import Plugin
2 2
 from .exceptions import *
3 3
 from lodel.settings.validator import SettingValidator
4 4
 
5
+_glob_typename = 'extension'
5 6
 class Extension(Plugin):
6 7
     
7 8
     _plist_confspecs = {
8 9
         'section': 'lodel2',
9 10
         'key': 'extensions',
10 11
         'default': [],
11
-        'validator': SettingValidator('list', none_is_valid = False)}
12
+        'validator': SettingValidator(
13
+            'plugin', none_is_valid = False, ptype = _glob_typename)}
12 14
 
13
-    _type_conf_name = 'extension'
15
+    _type_conf_name = _glob_typename
14 16
 

+ 7
- 2
lodel/plugin/interface.py View File

@@ -2,6 +2,10 @@ from .plugins import Plugin
2 2
 from .exceptions import *
3 3
 from lodel.settings.validator import SettingValidator
4 4
 
5
+_glob_typename = 'ui'
6
+
7
+##@brief Handles interfaces plugin
8
+#@note It's a singleton class. Only 1 interface allowed by instance.
5 9
 class InterfacePlugin(Plugin):
6 10
     
7 11
     ##@brief Singleton instance storage
@@ -11,9 +15,10 @@ class InterfacePlugin(Plugin):
11 15
         'section': 'lodel2',
12 16
         'key': 'interface',
13 17
         'default': None,
14
-        'validator': SettingValidator('strip', none_is_valid = True)}
18
+        'validator': SettingValidator(
19
+            'plugin', none_is_valid = True, ptype = _glob_typename)}
15 20
 
16
-    _type_conf_name = 'ui'
21
+    _type_conf_name = _glob_typename
17 22
     
18 23
     def __init__(self, name):
19 24
         if InterfacePlugin._instance is not None:

+ 4
- 2
lodel/plugin/sessionhandler.py View File

@@ -33,6 +33,7 @@ functions, but no session handler initialized")
33 33
         return super().__getattribute__(name)
34 34
 
35 35
 
36
+_glob_typename = 'session_handler'
36 37
 ##@brief Singleton class designed to handle session handler plugin
37 38
 #
38 39
 #@note This class is a singleton because only one session handler can be
@@ -45,9 +46,10 @@ class SessionHandlerPlugin(Plugin, metaclass=SessionPluginWrapper):
45 46
         'section': 'lodel2',
46 47
         'key': 'session_handler',
47 48
         'default': None,
48
-        'validator': SettingValidator('string', none_is_valid=False)}
49
+        'validator': SettingValidator(
50
+            'plugin', none_is_valid=False,ptype = _glob_typename)}
49 51
 
50
-    _type_conf_name = 'session_handler'
52
+    _type_conf_name = _glob_typename
51 53
             
52 54
     def __init__(self, plugin_name):
53 55
         if self._instance is None:

+ 32
- 22
lodel/settings/validator.py View File

@@ -7,7 +7,6 @@ import socket
7 7
 import inspect
8 8
 import copy
9 9
 
10
-
11 10
 ## @package lodel.settings.validator Lodel2 settings validators/cast module
12 11
 #
13 12
 # Validator are registered in the SettingValidator class.
@@ -29,11 +28,15 @@ class SettingValidator(object):
29 28
     _description = dict()
30 29
     
31 30
     ##@brief Instanciate a validator
32
-    def __init__(self, name, none_is_valid = False):
31
+    #@param name str : validator name
32
+    #@param none_is_valid bool : if True None will be validated
33
+    #@param **kwargs : more arguement for the validator
34
+    def __init__(self, name, none_is_valid = False, **kwargs):
33 35
         if name is not None and name not in self._validators:
34 36
             raise NameError("No validator named '%s'" % name)
35 37
         self.__none_is_valid = none_is_valid
36 38
         self.__name = name
39
+        self._opt_args = kwargs
37 40
 
38 41
     ##@brief Call the validator
39 42
     # @param value *
@@ -45,7 +48,7 @@ class SettingValidator(object):
45 48
         if self.__none_is_valid and value is None:
46 49
             return None
47 50
         try:
48
-            return self._validators[self.__name](value)
51
+            return self._validators[self.__name](value, **self._opt_args)
49 52
         except Exception as e:
50 53
             raise SettingsValidationError(e)
51 54
     
@@ -218,6 +221,9 @@ def host_val(value):
218 221
         msg = "The value '%s' is not a valid host"
219 222
         raise SettingsValidationError(msg % value)
220 223
 
224
+##@brief Validator for Editorial model component
225
+#
226
+# Designed to validate a conf that indicate a class.field in an EM
221 227
 def emfield_val(value):
222 228
     from lodel.plugin.hooks import LodelHook
223 229
     spl = value.split('.')
@@ -239,25 +245,29 @@ def emfield_val(value):
239 245
             raise SettingsValidationError(msg % value)
240 246
     return value
241 247
 
242
-def plugin_val(value):
248
+##@brief Validator for plugin name & optionnaly type
249
+#
250
+#Able to check that the value is a plugin and if it is of a specific type
251
+def plugin_validator(value, ptype = None):
243 252
     from lodel.plugin.hooks import LodelHook
244
-    spl = value.split('.')
245
-    if len(spl) != 2:
246
-        msg = "Expected a value in the form PLUGIN.TYPE but got : %s"
247
-        raise SettingsValidationError(msg % value)
248
-    value = tuple(spl)
249
-    #Late validation hook
250 253
     @LodelHook('lodel2_dyncode_bootstraped')
251
-    def type_check(hookname, caller, payload):
252
-        from lodel import plugin
253
-        typesname = { cls.__name__.lower():cls for cls in plugin.PLUGINS_TYPE}
254
-        if value[1].lower() not in typesname:
255
-            msg = "Following plugin type do not exist in plugin list %s : %s"
256
-            raise SettingsValidationError(msg % value)
257
-        return value
258
-    plug_type_val = plugin_val(value)
259
-    return plug_type_val
260
-
254
+    def plugin_type_checker(hookname, caller, payload):
255
+        from lodel.plugin.plugins import Plugin
256
+        from lodel.plugin.exceptions import PluginError
257
+        try:
258
+            plugin = Plugin.get(value)
259
+        except PluginError:
260
+            msg = "No plugin named %s found"
261
+            msg %= value
262
+            raise SettingsValidationError(msg)
263
+        print("HOOKED CHECK : ", plugin._type_conf_name.lower(), ptype.lower())
264
+        if plugin._type_conf_name.lower() != ptype.lower():
265
+            msg = "A plugin of type '%s' was expected but found a plugin \
266
+named  '%s' that is a '%s' plugin"
267
+            msg %= (ptype, value, plugin._type_conf_name)
268
+            raise SettingsValidationError(msg)
269
+    return value
270
+        
261 271
 
262 272
 #
263 273
 #   Default validators registration
@@ -265,8 +275,8 @@ def plugin_val(value):
265 275
 
266 276
 SettingValidator.register_validator(
267 277
     'plugin',
268
-    plugin_val,
269
-    'plugin validator')
278
+    plugin_validator,
279
+    'plugin name & type validator')
270 280
 
271 281
 SettingValidator.register_validator(
272 282
     'dummy',

Loading…
Cancel
Save