Browse Source

Add a new settings validator named emfield

Check that an EmClass.EmField exsists in dyncode
Yann Weber 8 years ago
parent
commit
42fc14d7cc
2 changed files with 36 additions and 5 deletions
  1. 1
    2
      install/loader.py
  2. 35
    3
      lodel/settings/validator.py

+ 1
- 2
install/loader.py View File

32
     from lodel.plugin import Plugin
32
     from lodel.plugin import Plugin
33
     logger.debug("Loader.start() called")
33
     logger.debug("Loader.start() called")
34
     Plugin.load_all()
34
     Plugin.load_all()
35
-
36
     LodelHook.call_hook('lodel2_bootstraped', '__main__', None)
35
     LodelHook.call_hook('lodel2_bootstraped', '__main__', None)
37
 
36
 
38
 
37
 
40
 
39
 
41
     start()
40
     start()
42
     if Settings.runtest:
41
     if Settings.runtest:
43
-        start()
44
         import unittest
42
         import unittest
45
         import tests
43
         import tests
46
         loader = unittest.TestLoader()
44
         loader = unittest.TestLoader()
56
     import lodel
54
     import lodel
57
     import leapi_dyncode as dyncode
55
     import leapi_dyncode as dyncode
58
     lodel.dyncode = dyncode
56
     lodel.dyncode = dyncode
57
+    LodelHook.call_hook('lodel2_dyncode_bootstraped', '__main__', None)
59
     LodelHook.call_hook('lodel2_loader_main', '__main__', None)
58
     LodelHook.call_hook('lodel2_loader_main', '__main__', None)
60
 
59
 
61
     #Run interative python
60
     #Run interative python

+ 35
- 3
lodel/settings/validator.py View File

7
 import inspect
7
 import inspect
8
 import copy
8
 import copy
9
 
9
 
10
+from lodel.plugin.hooks import LodelHook
11
+
10
 ## @package lodel.settings.validator Lodel2 settings validators/cast module
12
 ## @package lodel.settings.validator Lodel2 settings validators/cast module
11
 #
13
 #
12
 # Validator are registered in the SettingValidator class.
14
 # Validator are registered in the SettingValidator class.
31
     def __init__(self, name, none_is_valid = False):
33
     def __init__(self, name, none_is_valid = False):
32
         if name is not None and name not in self._validators:
34
         if name is not None and name not in self._validators:
33
             raise NameError("No validator named '%s'" % name)
35
             raise NameError("No validator named '%s'" % name)
36
+        self.__none_is_valid = none_is_valid
34
         self.__name = name
37
         self.__name = name
35
 
38
 
36
     ##@brief Call the validator
39
     ##@brief Call the validator
40
     def __call__(self, value):
43
     def __call__(self, value):
41
         if self.__name is None:
44
         if self.__name is None:
42
             return value
45
             return value
46
+        if self.__none_is_valid and value is None:
47
+            return None
43
         try:
48
         try:
44
             return self._validators[self.__name](value)
49
             return self._validators[self.__name](value)
45
         except Exception as e:
50
         except Exception as e:
152
 
157
 
153
 ##@brief Boolean value validator callback
158
 ##@brief Boolean value validator callback
154
 def boolean_val(value):
159
 def boolean_val(value):
160
+    if isinstance(value, bool):
161
+        return value
155
     if value.strip().lower() == 'true' or value.strip() == '1':
162
     if value.strip().lower() == 'true' or value.strip() == '1':
156
         value = True
163
         value = True
157
     elif value.strip().lower() == 'false' or value.strip() == '0':
164
     elif value.strip().lower() == 'false' or value.strip() == '0':
174
     return value.upper()
181
     return value.upper()
175
 
182
 
176
 def path_val(value):
183
 def path_val(value):
177
-    if not os.path.exists(value):
184
+    if value is None or not os.path.exists(value):
178
         raise SettingsValidationError(
185
         raise SettingsValidationError(
179
                 "path '%s' doesn't exists" % value)
186
                 "path '%s' doesn't exists" % value)
180
     return value
187
     return value
211
         msg = "The value '%s' is not a valid host"
218
         msg = "The value '%s' is not a valid host"
212
         raise SettingsValidationError(msg % value)
219
         raise SettingsValidationError(msg % value)
213
 
220
 
221
+def emfield_val(value):
222
+    spl = value.split('.')
223
+    if len(spl) != 2:
224
+        msg = "Expected a value in the form CLASSNAME.FIELDNAME but got : %s"
225
+        raise SettingsValidationError(msg % value)
226
+    value = tuple(spl)
227
+    #Late validation hook
228
+    @LodelHook('lodel2_dyncode_bootstraped')
229
+    def emfield_conf_check(hookname, caller, payload):
230
+        from lodel import dyncode
231
+        classnames = { cls.__name__.lower():cls for cls in dyncode.dynclasses}
232
+        if value[0].lower() not in classnames:
233
+            msg = "Following dynamic class do not exists in current EM : %s"
234
+            raise SettingsValidationError(msg % value[0])
235
+        ccls = classnames[value[0].lower()]
236
+        if value[1].lower() not in ccls.fieldnames(True):
237
+            msg = "Following field not found in class %s : %s"
238
+            raise SettingsValidationError(msg % value)
239
+            
240
+            
241
+    return value
242
+        
214
 #
243
 #
215
 #   Default validators registration
244
 #   Default validators registration
216
 #
245
 #
270
     host_val,
299
     host_val,
271
     'host validator')
300
     'host validator')
272
 
301
 
302
+SettingValidator.register_validator(
303
+    'emfield',
304
+    emfield_val,
305
+    'EmField name validator')
306
+
273
 SettingValidator.create_list_validator(
307
 SettingValidator.create_list_validator(
274
     'list',
308
     'list',
275
     SettingValidator('strip'),
309
     SettingValidator('strip'),
306
                         SettingValidator('list')),
340
                         SettingValidator('list')),
307
         'sitename': (   'noname',
341
         'sitename': (   'noname',
308
                         SettingValidator('strip')),
342
                         SettingValidator('strip')),
309
-        'lib_path': (   None,
310
-                        SettingValidator('path')),
311
         'runtest': (    False,
343
         'runtest': (    False,
312
                         SettingValidator('bool')),
344
                         SettingValidator('bool')),
313
     },
345
     },

Loading…
Cancel
Save