1
0
Fork 0
mirror of https://github.com/yweber/lodel2.git synced 2026-03-14 15:22:02 +01:00

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
This commit is contained in:
Yann 2016-09-08 14:46:27 +02:00
commit 9d273e6fef
5 changed files with 29 additions and 6 deletions

Binary file not shown.

View file

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

View file

@ -95,8 +95,11 @@ class DatasourcePlugin(Plugin):
'key': 'datasource_connectors', 'key': 'datasource_connectors',
'default': None, 'default': None,
'validator': SettingValidator( 'validator': SettingValidator(
'plugin', none_is_valid = False, 'custom_list', none_is_valid = False,
ptype = _glob_typename) } validator_name = 'plugin', validator_kwargs = {
'ptype': _glob_typename,
'none_is_valid': False})
}
##@brief Construct a DatasourcePlugin ##@brief Construct a DatasourcePlugin
#@param name str : plugin name #@param name str : plugin name

View file

@ -8,9 +8,13 @@ class Extension(Plugin):
_plist_confspecs = { _plist_confspecs = {
'section': 'lodel2', 'section': 'lodel2',
'key': 'extensions', 'key': 'extensions',
'default': [], 'default': None,
'validator': SettingValidator( 'validator': SettingValidator(
'plugin', none_is_valid = False, ptype = _glob_typename)} 'custom_list', none_is_valid = True,
validator_name = 'plugin', validator_kwargs = {
'ptype': _glob_typename,
'none_is_valid': False})
}
_type_conf_name = _glob_typename _type_conf_name = _glob_typename

View file

@ -48,7 +48,8 @@ class SettingValidator(object):
if self.__none_is_valid and value is None: if self.__none_is_valid and value is None:
return None return None
try: try:
return self._validators[self.__name](value, **self._opt_args) ret = self._validators[self.__name](value, **self._opt_args)
return ret
except Exception as e: except Exception as e:
raise SettingsValidationError(e) raise SettingsValidationError(e)
@ -250,10 +251,13 @@ def emfield_val(value):
#Able to check that the value is a plugin and if it is of a specific type #Able to check that the value is a plugin and if it is of a specific type
def plugin_validator(value, ptype = None): def plugin_validator(value, ptype = None):
from lodel.plugin.hooks import LodelHook from lodel.plugin.hooks import LodelHook
value = copy.copy(value)
@LodelHook('lodel2_dyncode_bootstraped') @LodelHook('lodel2_dyncode_bootstraped')
def plugin_type_checker(hookname, caller, payload): def plugin_type_checker(hookname, caller, payload):
from lodel.plugin.plugins import Plugin from lodel.plugin.plugins import Plugin
from lodel.plugin.exceptions import PluginError from lodel.plugin.exceptions import PluginError
if value is None:
return
try: try:
plugin = Plugin.get(value) plugin = Plugin.get(value)
except PluginError: except PluginError:
@ -267,6 +271,12 @@ named '%s' that is a '%s' plugin"
raise SettingsValidationError(msg) raise SettingsValidationError(msg)
return value return value
def custom_list_validator(value, validator_name, validator_kwargs = None):
validator_kwargs = dict() if validator_kwargs is None else validator_kwargs
validator = SettingValidator(validator_name, **validator_kwargs)
for item in value.split():
validator(item)
return value.split()
# #
# Default validators registration # Default validators registration
@ -277,6 +287,11 @@ SettingValidator.register_validator(
plugin_validator, plugin_validator,
'plugin name & type validator') 'plugin name & type validator')
SettingValidator.register_validator(
'custom_list',
custom_list_validator,
'A list validator that takes a "validator_name" as argument')
SettingValidator.register_validator( SettingValidator.register_validator(
'dummy', 'dummy',
lambda value:value, lambda value:value,