Browse Source

Add a new sttings validator + use it

- Written a new custom_list validator, it takes another validator name as argument (in order to validate items of list)
- Using it to validate list of plugins names
Yann Weber 7 years ago
parent
commit
9d273e6fef

BIN
examples/em_test.pickle View File


+ 1
- 0
globconf.d/lodel2.ini View File

@@ -1,6 +1,7 @@
1 1
 [lodel2]
2 2
 debug = False
3 3
 sitename = noname
4
+datasource_connectors = dummy_datasource
4 5
 
5 6
 [lodel2.logging.stderr]
6 7
 level = ERROR

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

@@ -95,8 +95,11 @@ class DatasourcePlugin(Plugin):
95 95
         'key': 'datasource_connectors',
96 96
         'default': None,
97 97
         'validator': SettingValidator(
98
-            'plugin', none_is_valid = False,
99
-            ptype = _glob_typename) }
98
+            'custom_list', none_is_valid = False,
99
+            validator_name = 'plugin', validator_kwargs = {
100
+                'ptype': _glob_typename,
101
+                'none_is_valid': False})
102
+        }
100 103
  
101 104
     ##@brief Construct a DatasourcePlugin 
102 105
     #@param name str : plugin name

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

@@ -8,9 +8,13 @@ class Extension(Plugin):
8 8
     _plist_confspecs = {
9 9
         'section': 'lodel2',
10 10
         'key': 'extensions',
11
-        'default': [],
11
+        'default': None,
12 12
         'validator': SettingValidator(
13
-            'plugin', none_is_valid = False, ptype = _glob_typename)}
13
+            'custom_list', none_is_valid = True,
14
+            validator_name = 'plugin', validator_kwargs = {
15
+                'ptype': _glob_typename,
16
+                'none_is_valid': False})
17
+        }
14 18
 
15 19
     _type_conf_name = _glob_typename
16 20
 

+ 17
- 2
lodel/settings/validator.py View File

@@ -48,7 +48,8 @@ class SettingValidator(object):
48 48
         if self.__none_is_valid and value is None:
49 49
             return None
50 50
         try:
51
-            return self._validators[self.__name](value, **self._opt_args)
51
+            ret = self._validators[self.__name](value, **self._opt_args)
52
+            return ret
52 53
         except Exception as e:
53 54
             raise SettingsValidationError(e)
54 55
     
@@ -250,10 +251,13 @@ def emfield_val(value):
250 251
 #Able to check that the value is a plugin and if it is of a specific type
251 252
 def plugin_validator(value, ptype = None):
252 253
     from lodel.plugin.hooks import LodelHook
254
+    value = copy.copy(value)
253 255
     @LodelHook('lodel2_dyncode_bootstraped')
254 256
     def plugin_type_checker(hookname, caller, payload):
255 257
         from lodel.plugin.plugins import Plugin
256 258
         from lodel.plugin.exceptions import PluginError
259
+        if value is None:
260
+            return
257 261
         try:
258 262
             plugin = Plugin.get(value)
259 263
         except PluginError:
@@ -266,7 +270,13 @@ named  '%s' that is a '%s' plugin"
266 270
             msg %= (ptype, value, plugin._type_conf_name)
267 271
             raise SettingsValidationError(msg)
268 272
     return value
269
-        
273
+
274
+def custom_list_validator(value, validator_name, validator_kwargs = None):
275
+    validator_kwargs = dict() if validator_kwargs is None else validator_kwargs
276
+    validator = SettingValidator(validator_name, **validator_kwargs)
277
+    for item in value.split():
278
+        validator(item)
279
+    return value.split()
270 280
 
271 281
 #
272 282
 #   Default validators registration
@@ -277,6 +287,11 @@ SettingValidator.register_validator(
277 287
     plugin_validator,
278 288
     'plugin name & type validator')
279 289
 
290
+SettingValidator.register_validator(
291
+    'custom_list',
292
+    custom_list_validator,
293
+    'A list validator that takes a "validator_name" as argument')
294
+
280 295
 SettingValidator.register_validator(
281 296
     'dummy',
282 297
     lambda value:value,

Loading…
Cancel
Save