1
0
Fork 0
mirror of https://github.com/yweber/lodel2.git synced 2025-10-30 11:09:03 +01:00

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)
This commit is contained in:
Yann 2016-08-29 09:09:19 +02:00
commit 77be17f296
5 changed files with 53 additions and 30 deletions

View file

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

View file

@ -2,13 +2,15 @@ from .plugins import Plugin
from .exceptions import *
from lodel.settings.validator import SettingValidator
_glob_typename = 'extension'
class Extension(Plugin):
_plist_confspecs = {
'section': 'lodel2',
'key': 'extensions',
'default': [],
'validator': SettingValidator('list', none_is_valid = False)}
'validator': SettingValidator(
'plugin', none_is_valid = False, ptype = _glob_typename)}
_type_conf_name = 'extension'
_type_conf_name = _glob_typename

View file

@ -2,6 +2,10 @@ from .plugins import Plugin
from .exceptions import *
from lodel.settings.validator import SettingValidator
_glob_typename = 'ui'
##@brief Handles interfaces plugin
#@note It's a singleton class. Only 1 interface allowed by instance.
class InterfacePlugin(Plugin):
##@brief Singleton instance storage
@ -11,9 +15,10 @@ class InterfacePlugin(Plugin):
'section': 'lodel2',
'key': 'interface',
'default': None,
'validator': SettingValidator('strip', none_is_valid = True)}
'validator': SettingValidator(
'plugin', none_is_valid = True, ptype = _glob_typename)}
_type_conf_name = 'ui'
_type_conf_name = _glob_typename
def __init__(self, name):
if InterfacePlugin._instance is not None:

View file

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

View file

@ -7,7 +7,6 @@ import socket
import inspect
import copy
## @package lodel.settings.validator Lodel2 settings validators/cast module
#
# Validator are registered in the SettingValidator class.
@ -29,11 +28,15 @@ class SettingValidator(object):
_description = dict()
##@brief Instanciate a validator
def __init__(self, name, none_is_valid = False):
#@param name str : validator name
#@param none_is_valid bool : if True None will be validated
#@param **kwargs : more arguement for the validator
def __init__(self, name, none_is_valid = False, **kwargs):
if name is not None and name not in self._validators:
raise NameError("No validator named '%s'" % name)
self.__none_is_valid = none_is_valid
self.__name = name
self._opt_args = kwargs
##@brief Call the validator
# @param value *
@ -45,7 +48,7 @@ class SettingValidator(object):
if self.__none_is_valid and value is None:
return None
try:
return self._validators[self.__name](value)
return self._validators[self.__name](value, **self._opt_args)
except Exception as e:
raise SettingsValidationError(e)
@ -218,6 +221,9 @@ def host_val(value):
msg = "The value '%s' is not a valid host"
raise SettingsValidationError(msg % value)
##@brief Validator for Editorial model component
#
# Designed to validate a conf that indicate a class.field in an EM
def emfield_val(value):
from lodel.plugin.hooks import LodelHook
spl = value.split('.')
@ -239,24 +245,28 @@ def emfield_val(value):
raise SettingsValidationError(msg % value)
return value
def plugin_val(value):
##@brief Validator for plugin name & optionnaly type
#
#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
spl = value.split('.')
if len(spl) != 2:
msg = "Expected a value in the form PLUGIN.TYPE but got : %s"
raise SettingsValidationError(msg % value)
value = tuple(spl)
#Late validation hook
@LodelHook('lodel2_dyncode_bootstraped')
def type_check(hookname, caller, payload):
from lodel import plugin
typesname = { cls.__name__.lower():cls for cls in plugin.PLUGINS_TYPE}
if value[1].lower() not in typesname:
msg = "Following plugin type do not exist in plugin list %s : %s"
raise SettingsValidationError(msg % value)
def plugin_type_checker(hookname, caller, payload):
from lodel.plugin.plugins import Plugin
from lodel.plugin.exceptions import PluginError
try:
plugin = Plugin.get(value)
except PluginError:
msg = "No plugin named %s found"
msg %= value
raise SettingsValidationError(msg)
print("HOOKED CHECK : ", plugin._type_conf_name.lower(), ptype.lower())
if plugin._type_conf_name.lower() != ptype.lower():
msg = "A plugin of type '%s' was expected but found a plugin \
named '%s' that is a '%s' plugin"
msg %= (ptype, value, plugin._type_conf_name)
raise SettingsValidationError(msg)
return value
plug_type_val = plugin_val(value)
return plug_type_val
#
@ -265,8 +275,8 @@ def plugin_val(value):
SettingValidator.register_validator(
'plugin',
plugin_val,
'plugin validator')
plugin_validator,
'plugin name & type validator')
SettingValidator.register_validator(
'dummy',