mirror of
https://github.com/yweber/lodel2.git
synced 2026-01-07 07:42:14 +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:
parent
6bfb862e8e
commit
9d273e6fef
5 changed files with 29 additions and 6 deletions
Binary file not shown.
|
|
@ -1,6 +1,7 @@
|
|||
[lodel2]
|
||||
debug = False
|
||||
sitename = noname
|
||||
datasource_connectors = dummy_datasource
|
||||
|
||||
[lodel2.logging.stderr]
|
||||
level = ERROR
|
||||
|
|
|
|||
|
|
@ -95,8 +95,11 @@ class DatasourcePlugin(Plugin):
|
|||
'key': 'datasource_connectors',
|
||||
'default': None,
|
||||
'validator': SettingValidator(
|
||||
'plugin', none_is_valid = False,
|
||||
ptype = _glob_typename) }
|
||||
'custom_list', none_is_valid = False,
|
||||
validator_name = 'plugin', validator_kwargs = {
|
||||
'ptype': _glob_typename,
|
||||
'none_is_valid': False})
|
||||
}
|
||||
|
||||
##@brief Construct a DatasourcePlugin
|
||||
#@param name str : plugin name
|
||||
|
|
|
|||
|
|
@ -8,9 +8,13 @@ class Extension(Plugin):
|
|||
_plist_confspecs = {
|
||||
'section': 'lodel2',
|
||||
'key': 'extensions',
|
||||
'default': [],
|
||||
'default': None,
|
||||
'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
|
||||
|
||||
|
|
|
|||
|
|
@ -48,7 +48,8 @@ class SettingValidator(object):
|
|||
if self.__none_is_valid and value is None:
|
||||
return None
|
||||
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:
|
||||
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
|
||||
def plugin_validator(value, ptype = None):
|
||||
from lodel.plugin.hooks import LodelHook
|
||||
value = copy.copy(value)
|
||||
@LodelHook('lodel2_dyncode_bootstraped')
|
||||
def plugin_type_checker(hookname, caller, payload):
|
||||
from lodel.plugin.plugins import Plugin
|
||||
from lodel.plugin.exceptions import PluginError
|
||||
if value is None:
|
||||
return
|
||||
try:
|
||||
plugin = Plugin.get(value)
|
||||
except PluginError:
|
||||
|
|
@ -266,7 +270,13 @@ named '%s' that is a '%s' plugin"
|
|||
msg %= (ptype, value, plugin._type_conf_name)
|
||||
raise SettingsValidationError(msg)
|
||||
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
|
||||
|
|
@ -277,6 +287,11 @@ SettingValidator.register_validator(
|
|||
plugin_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(
|
||||
'dummy',
|
||||
lambda value:value,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue