add DynOptionDescription

This commit is contained in:
2014-06-19 23:22:39 +02:00
parent 888446e4c5
commit b64189f763
21 changed files with 2382 additions and 543 deletions

View File

@ -1,6 +1,7 @@
from .masterslave import MasterSlaves
from .optiondescription import OptionDescription
from .baseoption import Option, SymLinkOption, submulti
from .optiondescription import OptionDescription, DynOptionDescription, \
SynDynOptionDescription
from .baseoption import Option, SymLinkOption, DynSymLinkOption, submulti
from .option import (ChoiceOption, BoolOption, IntOption, FloatOption,
StrOption, UnicodeOption, IPOption, PortOption,
NetworkOption, NetmaskOption, BroadcastOption,
@ -8,9 +9,10 @@ from .option import (ChoiceOption, BoolOption, IntOption, FloatOption,
FilenameOption)
__all__ = ('MasterSlaves', 'OptionDescription', 'Option', 'SymLinkOption',
'ChoiceOption', 'BoolOption', 'IntOption', 'FloatOption',
'StrOption', 'UnicodeOption', 'IPOption', 'PortOption',
'NetworkOption', 'NetmaskOption', 'BroadcastOption',
'DomainnameOption', 'EmailOption', 'URLOption', 'UsernameOption',
'FilenameOption', 'submulti')
__all__ = ('MasterSlaves', 'OptionDescription', 'DynOptionDescription',
'SynDynOptionDescription', 'Option', 'SymLinkOption',
'DynSymLinkOption', 'ChoiceOption', 'BoolOption',
'IntOption', 'FloatOption', 'StrOption', 'UnicodeOption',
'IPOption', 'PortOption', 'NetworkOption', 'NetmaskOption',
'BroadcastOption', 'DomainnameOption', 'EmailOption', 'URLOption',
'UsernameOption', 'FilenameOption', 'submulti')

View File

@ -40,7 +40,8 @@ class SubMulti(object):
submulti = SubMulti()
name_regexp = re.compile(r'^\d+')
allowed_character = '[a-z\d\-_]'
name_regexp = re.compile(r'^[a-z]{0}*$'.format(allowed_character))
forbidden_names = ('iter_all', 'iter_group', 'find', 'find_first',
'make_dict', 'unwrap_from_path', 'read_only',
'read_write', 'getowner', 'set_contexts')
@ -48,51 +49,51 @@ forbidden_names = ('iter_all', 'iter_group', 'find', 'find_first',
def valid_name(name):
"an option's name is a str and does not start with 'impl' or 'cfgimpl'"
if not isinstance(name, str):
if not isinstance(name, str): # pragma: optional cover
return False
if re.match(name_regexp, name) is None and not name.startswith('_') \
if re.match(name_regexp, name) is not None and not name.startswith('_') \
and name not in forbidden_names \
and not name.startswith('impl_') \
and not name.startswith('cfgimpl_'):
return True
else:
else: # pragma: optional cover
return False
def validate_callback(callback, callback_params, type_):
if type(callback) != FunctionType:
if type(callback) != FunctionType: # pragma: optional cover
raise ValueError(_('{0} must be a function').format(type_))
if callback_params is not None:
if not isinstance(callback_params, dict):
if not isinstance(callback_params, dict): # pragma: optional cover
raise ValueError(_('{0}_params must be a dict').format(type_))
for key, callbacks in callback_params.items():
if key != '' and len(callbacks) != 1:
if key != '' and len(callbacks) != 1: # pragma: optional cover
raise ValueError(_("{0}_params with key {1} mustn't have "
"length different to 1").format(type_,
key))
if not isinstance(callbacks, tuple):
if not isinstance(callbacks, tuple): # pragma: optional cover
raise ValueError(_('{0}_params must be tuple for key "{1}"'
).format(type_, key))
for callbk in callbacks:
if isinstance(callbk, tuple):
if len(callbk) == 1:
if callbk != (None,):
if callbk != (None,): # pragma: optional cover
raise ValueError(_('{0}_params with length of '
'tuple as 1 must only have '
'None as first value'))
elif len(callbk) != 2:
elif len(callbk) != 2: # pragma: optional cover
raise ValueError(_('{0}_params must only have 1 or 2 '
'as length'))
else:
option, force_permissive = callbk
if type_ == 'validator' and not force_permissive:
if type_ == 'validator' and not force_permissive: # pragma: optional cover
raise ValueError(_('validator not support tuple'))
if not isinstance(option, Option) and not \
isinstance(option, SymLinkOption):
isinstance(option, SymLinkOption): # pragma: optional cover
raise ValueError(_('{0}_params must have an option'
' not a {0} for first argument'
).format(type_, type(option)))
if force_permissive not in [True, False]:
if force_permissive not in [True, False]: # pragma: optional cover
raise ValueError(_('{0}_params must have a boolean'
' not a {0} for second argument'
).format(type_, type(
@ -104,11 +105,23 @@ def validate_callback(callback, callback_params, type_):
class Base(StorageBase):
__slots__ = tuple()
def impl_set_callback(self, callback, callback_params=None):
if callback is None and callback_params is not None: # pragma: optional cover
raise ValueError(_("params defined for a callback function but "
"no callback defined"
" yet for option {0}").format(
self.impl_getname()))
self._validate_callback(callback, callback_params)
if callback is not None:
validate_callback(callback, callback_params, 'callback')
self._callback = callback
self._callback_params = callback_params
def __init__(self, name, doc, default=None, default_multi=None,
requires=None, multi=False, callback=None,
callback_params=None, validator=None, validator_params=None,
properties=None, warnings_only=False):
if not valid_name(name):
if not valid_name(name): # pragma: optional cover
raise ValueError(_("invalid name: {0} for option").format(name))
self._name = name
self._readonly = False
@ -120,13 +133,13 @@ class Base(StorageBase):
else:
self._calc_properties = frozenset()
self._requires = []
if not multi and default_multi is not None:
if not multi and default_multi is not None: # pragma: optional cover
raise ValueError(_("a default_multi is set whereas multi is False"
" in option: {0}").format(name))
if default_multi is not None:
try:
self._validate(default_multi)
except ValueError as err:
except ValueError as err: # pragma: optional cover
raise ValueError(_("invalid default_multi value {0} "
"for option {1}: {2}").format(
str(default_multi), name, err))
@ -135,16 +148,9 @@ class Base(StorageBase):
if default is None:
default = []
self._default_multi = default_multi
if callback is not None and ((not multi and (default is not None or
default_multi is not None))
or (multi and (default != [] or
default_multi is not None))
):
raise ValueError(_("default value not allowed if option: {0} "
"is calculated").format(name))
if properties is None:
properties = tuple()
if not isinstance(properties, tuple):
if not isinstance(properties, tuple): # pragma: optional cover
raise TypeError(_('invalid properties type {0} for {1},'
' must be a tuple').format(
type(properties),
@ -154,16 +160,7 @@ class Base(StorageBase):
self._validator = validator
if validator_params is not None:
self._validator_params = validator_params
if callback is None and callback_params is not None:
raise ValueError(_("params defined for a callback function but "
"no callback defined"
" yet for option {0}").format(name))
if callback is not None:
validate_callback(callback, callback_params, 'callback')
self._callback = callback
if callback_params is not None:
self._callback_params = callback_params
if self._calc_properties != frozenset([]) and properties is not tuple():
if self._calc_properties != frozenset([]) and properties is not tuple(): # pragma: optional cover
set_forbidden_properties = self._calc_properties & set(properties)
if set_forbidden_properties != frozenset():
raise ValueError('conflict: properties already set in '
@ -173,12 +170,23 @@ class Base(StorageBase):
self._default = []
else:
self._default = default
if callback is not False:
self.impl_set_callback(callback, callback_params)
self._properties = properties
self._warnings_only = warnings_only
ret = super(Base, self).__init__()
self.impl_validate(self._default)
return ret
def impl_is_optiondescription(self):
return self.__class__.__name__ in ['OptionDescription',
'DynOptionDescription',
'SynDynOptionDescription']
def impl_is_dynoptiondescription(self):
return self.__class__.__name__ in ['DynOptionDescription',
'SynDynOptionDescription']
class BaseOption(Base):
"""This abstract base class stands for attribute access
@ -204,9 +212,9 @@ class BaseOption(Base):
"""
if key in self._informations:
return self._informations[key]
elif default is not undefined:
elif default is not undefined: # pragma: optional cover
return default
else:
else: # pragma: optional cover
raise ValueError(_("information's item not found: {0}").format(
key))
@ -246,6 +254,52 @@ class BaseOption(Base):
else:
self._state_requires = new_value
def _impl_convert_callback(self, descr, load=False):
if self.__class__.__name__ == 'OptionDescription' or \
isinstance(self, SymLinkOption):
return
if not load and self._callback is None:
self._state_callback = None
self._state_callback_params = None
elif load and self._state_callback is None:
self._callback = None
self._callback_params = None
del(self._state_callback)
del(self._state_callback_params)
else:
if load:
callback = self._state_callback
callback_params = self._state_callback_params
else:
callback = self._callback
callback_params = self._callback_params
self._state_callback_params = None
if callback_params is not None:
cllbck_prms = {}
for key, values in callback_params.items():
vls = []
for value in values:
if isinstance(value, tuple):
if load:
value = (descr.impl_get_opt_by_path(value[0]),
value[1])
else:
value = (descr.impl_get_path_by_opt(value[0]),
value[1])
vls.append(value)
cllbck_prms[key] = tuple(vls)
else:
cllbck_prms = None
if load:
del(self._state_callback)
del(self._state_callback_params)
self._callback = callback
self._callback_params = cllbck_prms
else:
self._state_callback = callback
self._state_callback_params = cllbck_prms
# serialize
def _impl_getstate(self, descr):
"""the under the hood stuff that need to be done
@ -270,7 +324,7 @@ class BaseOption(Base):
"""
try:
self._stated
except AttributeError:
except AttributeError: # pragma: optional cover
raise SystemError(_('cannot serialize Option, '
'only in OptionDescription'))
slots = set()
@ -311,7 +365,7 @@ class BaseOption(Base):
self._readonly = self._state_readonly
del(self._state_readonly)
del(self._stated)
except AttributeError:
except AttributeError: # pragma: optional cover
pass
def __setstate__(self, state):
@ -350,7 +404,7 @@ class BaseOption(Base):
pass
elif name != '_readonly':
is_readonly = self.impl_is_readonly()
if is_readonly:
if is_readonly: # pragma: optional cover
raise AttributeError(_("'{0}' ({1}) object attribute '{2}' is"
" read-only").format(
self.__class__.__name__,
@ -369,6 +423,12 @@ class BaseOption(Base):
def impl_getname(self):
return self._name
def impl_getpath(self, context):
return context.cfgimpl_get_description().impl_get_path_by_opt(self)
def impl_get_callback(self):
return self._callback, self._callback_params
class OnlyOption(BaseOption):
__slots__ = tuple()
@ -440,25 +500,31 @@ class Option(OnlyOption):
:param warnings_only: specific raise error for warning
:type warnings_only: `boolean`
"""
if context is not None:
if context is not undefined:
descr = context.cfgimpl_get_description()
all_cons_vals = []
for opt in all_cons_opts:
#get value
if option == opt:
if (isinstance(opt, DynSymLinkOption) and option._dyn == opt._dyn) or \
option == opt:
opt_value = value
else:
#if context, calculate value, otherwise get default value
if context is not None:
opt_value = context.getattr(
descr.impl_get_path_by_opt(opt), validate=False,
force_permissive=True)
if context is not undefined:
if isinstance(opt, DynSymLinkOption):
path = opt.impl_getpath(context)
else:
path = descr.impl_get_path_by_opt(opt)
opt_value = context.getattr(path, validate=False,
force_permissive=True)
else:
opt_value = opt.impl_getdefault()
#append value
if not self.impl_is_multi() or option == opt:
if not self.impl_is_multi() or (isinstance(opt, DynSymLinkOption)
and option._dyn == opt._dyn) or \
option == opt:
all_cons_vals.append(opt_value)
elif self.impl_is_submulti():
try:
@ -476,8 +542,9 @@ class Option(OnlyOption):
return
getattr(self, func)(all_cons_opts, all_cons_vals, warnings_only)
def impl_validate(self, value, context=None, validate=True,
force_index=None, force_submulti_index=None):
def impl_validate(self, value, context=undefined, validate=True,
force_index=None, force_submulti_index=None,
current_opt=undefined):
"""
:param value: the option's value
:param context: Config's context
@ -493,6 +560,8 @@ class Option(OnlyOption):
"""
if not validate:
return
if current_opt is undefined:
current_opt = self
def val_validator(val):
if self._validator is not None:
@ -520,7 +589,7 @@ class Option(OnlyOption):
# option validation
try:
self._validate(_value, context)
except ValueError as err:
except ValueError as err: # pragma: optional cover
log.debug('do_validation: value: {0}, index: {1}, '
'submulti_index: {2}'.format(_value, _index,
submulti_index),
@ -533,9 +602,9 @@ class Option(OnlyOption):
# valid with self._validator
val_validator(_value)
# if not context launch consistency validation
if context is not None:
descr._valid_consistency(self, _value, context, _index,
submulti_index)
if context is not undefined:
descr._valid_consistency(current_opt, _value, context,
_index, submulti_index)
self._second_level_validation(_value, self._warnings_only)
except ValueError as error:
log.debug(_('do_validation for {0}: error in value').format(
@ -550,9 +619,9 @@ class Option(OnlyOption):
if error is None and warning is None:
try:
# if context launch consistency validation
if context is not None:
descr._valid_consistency(self, _value, context, _index,
submulti_index)
if context is not undefined:
descr._valid_consistency(current_opt, _value, context,
_index, submulti_index)
except ValueError as error:
log.debug(_('do_validation for {0}: error in consistency').format(
self.impl_getname()), exc_info=True)
@ -572,14 +641,14 @@ class Option(OnlyOption):
self._name, error))
# generic calculation
if context is not None:
if context is not undefined:
descr = context.cfgimpl_get_description()
if not self.impl_is_multi():
do_validation(value, None, None)
elif force_index is not None:
if self.impl_is_submulti() and force_submulti_index is None:
if not isinstance(value, list):
if not isinstance(value, list): # pragma: optional cover
raise ValueError(_("invalid value {0} for option {1} which"
" must be a list").format(
value, self.impl_getname()))
@ -588,13 +657,13 @@ class Option(OnlyOption):
else:
do_validation(value, force_index, force_submulti_index)
else:
if not isinstance(value, list):
if not isinstance(value, list): # pragma: optional cover
raise ValueError(_("invalid value {0} for option {1} which "
"must be a list").format(value,
self.impl_getname()))
for idx, val in enumerate(value):
if self.impl_is_submulti() and force_submulti_index is None:
if not isinstance(val, list):
if not isinstance(val, list): # pragma: optional cover
raise ValueError(_("invalid value {0} for option {1} "
"which must be a list of list"
"").format(value,
@ -651,9 +720,6 @@ class Option(OnlyOption):
else:
return True
def impl_get_callback(self):
return self._callback, self._callback_params
#def impl_getkey(self, value):
# return value
@ -673,18 +739,33 @@ class Option(OnlyOption):
:type other_opts: `list` of `tiramisu.option.Option`
:param params: extra params (only warnings_only are allowed)
"""
if self.impl_is_readonly():
if self.impl_is_readonly(): # pragma: optional cover
raise AttributeError(_("'{0}' ({1}) cannont add consistency, option is"
" read-only").format(
self.__class__.__name__,
self._name))
warnings_only = params.get('warnings_only', False)
if self._is_subdyn():
dynod = self._subdyn
else:
dynod = None
for opt in other_opts:
if not isinstance(opt, Option):
if not isinstance(opt, Option): # pragma: optional cover
raise ConfigError(_('consistency must be set with an option'))
if self is opt:
if opt._is_subdyn():
if dynod is None:
raise ConfigError(_('almost one option in consistency is '
'in a dynoptiondescription but not all'))
if dynod != opt._subdyn:
raise ConfigError(_('option in consistency must be in same'
' dynoptiondescription'))
dynod = opt._subdyn
elif dynod is not None:
raise ConfigError(_('almost one option in consistency is in a '
'dynoptiondescription but not all'))
if self is opt: # pragma: optional cover
raise ConfigError(_('cannot add consistency with itself'))
if self.impl_is_multi() != opt.impl_is_multi():
if self.impl_is_multi() != opt.impl_is_multi(): # pragma: optional cover
raise ConfigError(_('every options in consistency must be '
'multi or none'))
func = '_cons_{0}'.format(func)
@ -694,7 +775,7 @@ class Option(OnlyOption):
if self.impl_is_multi():
for idx, val in enumerate(value):
if not self.impl_is_submulti():
self._launch_consistency(func, self, val, None, idx,
self._launch_consistency(func, self, val, undefined, idx,
None, all_cons_opts,
warnings_only)
else:
@ -704,7 +785,7 @@ class Option(OnlyOption):
all_cons_opts,
warnings_only)
else:
self._launch_consistency(func, self, value, None, None,
self._launch_consistency(func, self, value, undefined, None,
None, all_cons_opts, warnings_only)
self._add_consistency(func, all_cons_opts, params)
self.impl_validate(self.impl_getdefault())
@ -720,49 +801,6 @@ class Option(OnlyOption):
raise ValueError(msg.format(opts[idx_inf].impl_getname(),
opts[idx_inf + idx_sup + 1].impl_getname()))
def _impl_convert_callbacks(self, descr, load=False):
if not load and self._callback is None:
self._state_callback = None
self._state_callback_params = None
elif load and self._state_callback is None:
self._callback = None
self._callback_params = None
del(self._state_callback)
del(self._state_callback_params)
else:
if load:
callback = self._state_callback
callback_params = self._state_callback_params
else:
callback = self._callback
callback_params = self._callback_params
self._state_callback_params = None
if callback_params is not None:
cllbck_prms = {}
for key, values in callback_params.items():
vls = []
for value in values:
if isinstance(value, tuple):
if load:
value = (descr.impl_get_opt_by_path(value[0]),
value[1])
else:
value = (descr.impl_get_path_by_opt(value[0]),
value[1])
vls.append(value)
cllbck_prms[key] = tuple(vls)
else:
cllbck_prms = None
if load:
del(self._state_callback)
del(self._state_callback_params)
self._callback = callback
self._callback_params = cllbck_prms
else:
self._state_callback = callback
self._state_callback_params = cllbck_prms
# serialize/unserialize
def _impl_convert_consistencies(self, descr, load=False):
"""during serialization process, many things have to be done.
@ -801,6 +839,24 @@ class Option(OnlyOption):
def _second_level_validation(self, value, warnings_only):
pass
def _impl_to_dyn(self, name, path):
return DynSymLinkOption(name, self, dyn=path)
def _validate_callback(self, callback, callback_params):
try:
default_multi = self._default_multi
except AttributeError:
default_multi = None
if callback is not None and ((not self._multi and
(self._default is not None or
default_multi is not None))
or (self._multi and
(self._default != [] or
default_multi is not None))
): # pragma: optional cover
raise ValueError(_("default value not allowed if option: {0} "
"is calculated").format(self.impl_getname()))
def validate_requires_arg(requires, name):
"""check malformed requirements
@ -819,60 +875,60 @@ def validate_requires_arg(requires, name):
# start parsing all requires given by user (has dict)
# transforme it to a tuple
for require in requires:
if not type(require) == dict:
if not type(require) == dict: # pragma: optional cover
raise ValueError(_("malformed requirements type for option:"
" {0}, must be a dict").format(name))
valid_keys = ('option', 'expected', 'action', 'inverse', 'transitive',
'same_action')
unknown_keys = frozenset(require.keys()) - frozenset(valid_keys)
if unknown_keys != frozenset():
raise ValueError('malformed requirements for option: {0}'
if unknown_keys != frozenset(): # pragma: optional cover
raise ValueError(_('malformed requirements for option: {0}'
' unknown keys {1}, must only '
'{2}'.format(name,
unknown_keys,
valid_keys))
'{2}').format(name,
unknown_keys,
valid_keys))
# prepare all attributes
try:
option = require['option']
expected = require['expected']
action = require['action']
except KeyError:
except KeyError: # pragma: optional cover
raise ValueError(_("malformed requirements for option: {0}"
" require must have option, expected and"
" action keys").format(name))
if action == 'force_store_value':
if action == 'force_store_value': # pragma: optional cover
raise ValueError(_("malformed requirements for option: {0}"
" action cannot be force_store_value"
).format(name))
inverse = require.get('inverse', False)
if inverse not in [True, False]:
if inverse not in [True, False]: # pragma: optional cover
raise ValueError(_('malformed requirements for option: {0}'
' inverse must be boolean'))
transitive = require.get('transitive', True)
if transitive not in [True, False]:
if transitive not in [True, False]: # pragma: optional cover
raise ValueError(_('malformed requirements for option: {0}'
' transitive must be boolean'))
same_action = require.get('same_action', True)
if same_action not in [True, False]:
if same_action not in [True, False]: # pragma: optional cover
raise ValueError(_('malformed requirements for option: {0}'
' same_action must be boolean'))
if not isinstance(option, Option):
if not isinstance(option, Option): # pragma: optional cover
raise ValueError(_('malformed requirements '
'must be an option in option {0}').format(name))
if option.impl_is_multi():
if option.impl_is_multi(): # pragma: optional cover
raise ValueError(_('malformed requirements option {0} '
'must not be a multi for {1}').format(
option.impl_getname(), name))
if expected is not None:
try:
option._validate(expected)
except ValueError as err:
except ValueError as err: # pragma: optional cover
raise ValueError(_('malformed requirements second argument '
'must be valid for option {0}'
': {1}').format(name, err))
if action in config_action:
if inverse != config_action[action]:
if inverse != config_action[action]: # pragma: optional cover
raise ValueError(_("inconsistency in action types"
" for option: {0}"
" action: {1}").format(name, action))
@ -897,23 +953,20 @@ def validate_requires_arg(requires, name):
class SymLinkOption(OnlyOption):
#FIXME : et avec sqlalchemy ca marche vraiment ?
__slots__ = ('_opt', '_state_opt')
#not return _opt consistencies
#_consistencies = None
def __init__(self, name, opt):
self._name = name
if not isinstance(opt, Option):
if not isinstance(opt, Option): # pragma: optional cover
raise ValueError(_('malformed symlinkoption '
'must be an option '
'for symlink {0}').format(name))
self._opt = opt
self._readonly = True
return super(Base, self).__init__()
super(Base, self).__init__()
def __getattr__(self, name):
if name in ('_opt', '_opt_type', '_readonly', 'impl_getname'):
def __getattr__(self, name, context=undefined):
if name in ('_opt', '_opt_type', '_readonly', 'impl_getpath'):
return object.__getattr__(self, name)
else:
return getattr(self._opt, name)
@ -929,3 +982,29 @@ class SymLinkOption(OnlyOption):
def impl_get_information(self, key, default=undefined):
return self._opt.impl_get_information(key, default)
class DynSymLinkOption(SymLinkOption):
__slots__ = ('_dyn',)
def __init__(self, name, opt, dyn):
self._dyn = dyn
super(DynSymLinkOption, self).__init__(name, opt)
def impl_getsuffix(self):
return self._dyn.split('.')[-1][len(self._opt.impl_getname()):]
def impl_getpath(self, context):
path = self._opt.impl_getpath(context)
base_path = '.'.join(path.split('.')[:-2])
if self.impl_is_master_slaves() and base_path is not '':
base_path = base_path + self.impl_getsuffix()
if base_path == '':
return self._dyn
else:
return base_path + '.' + self._dyn
def impl_validate(self, value, context=undefined, validate=True,
force_index=None, force_submulti_index=None):
return self._opt.impl_validate(value, context, validate, force_index,
force_submulti_index, current_opt=self)

View File

@ -22,63 +22,92 @@
from tiramisu.i18n import _
from tiramisu.setting import log, undefined
from tiramisu.error import SlaveError, ConfigError
from .baseoption import SymLinkOption, Option
from .baseoption import DynSymLinkOption, SymLinkOption, Option
class MasterSlaves(object):
__slots__ = ('master', 'slaves')
def __init__(self, name, childs):
def __init__(self, name, childs, validate=True):
#if master (same name has group) is set
#for collect all slaves
self.master = None
slaves = []
for child in childs:
if isinstance(child, SymLinkOption):
if isinstance(child, SymLinkOption): # pragma: optional cover
raise ValueError(_("master group {0} shall not have "
"a symlinkoption").format(name))
if not isinstance(child, Option):
if not isinstance(child, Option): # pragma: optional cover
raise ValueError(_("master group {0} shall not have "
"a subgroup").format(name))
if not child.impl_is_multi():
if not child.impl_is_multi(): # pragma: optional cover
raise ValueError(_("not allowed option {0} "
"in group {1}"
": this option is not a multi"
"").format(child._name, name))
if child._name == name:
"").format(child.impl_getname(), name))
if child.impl_getname() == name:
self.master = child
else:
slaves.append(child)
if self.master is None:
if self.master is None: # pragma: optional cover
raise ValueError(_('master group with wrong'
' master name for {0}'
).format(name))
callback, callback_params = self.master.impl_get_callback()
if callback is not None and callback_params is not None:
for key, callbacks in callback_params.items():
for callbk in callbacks:
if isinstance(callbk, tuple):
if callbk[0] in slaves:
raise ValueError(_("callback of master's option shall "
"not refered a slave's ones"))
if validate:
callback, callback_params = self.master.impl_get_callback()
if callback is not None and callback_params is not None: # pragma: optional cover
for key, callbacks in callback_params.items():
for callbk in callbacks:
if isinstance(callbk, tuple):
if callbk[0] in slaves:
raise ValueError(_("callback of master's option shall "
"not refered a slave's ones"))
#everything is ok, store references
self.slaves = tuple(slaves)
for child in childs:
child._master_slaves = self
def is_master(self, opt):
return opt == self.master
return opt == self.master or (isinstance(opt, DynSymLinkOption) and
opt._opt == self.master)
def getmaster(self, opt):
if isinstance(opt, DynSymLinkOption):
suffix = opt.impl_getsuffix()
name = self.master.impl_getname() + suffix
base_path = opt._dyn.split('.')[0] + '.'
path = base_path + name
master = self.master._impl_to_dyn(name, path)
else: # pragma: no dynoptiondescription cover
master = self.master
return master
def getslaves(self, opt):
if isinstance(opt, DynSymLinkOption):
for slave in self.slaves:
suffix = opt.impl_getsuffix()
name = slave.impl_getname() + suffix
base_path = opt._dyn.split('.')[0] + '.'
path = base_path + name
yield slave._impl_to_dyn(name, path)
else: # pragma: no dynoptiondescription cover
for slave in self.slaves:
yield slave
def in_same_group(self, opt):
return opt == self.master or opt in self.slaves
if isinstance(opt, DynSymLinkOption):
return opt._opt == self.master or opt._opt in self.slaves
else: # pragma: no dynoptiondescription cover
return opt == self.master or opt in self.slaves
def reset(self, values):
for slave in self.slaves:
def reset(self, opt, values):
#FIXME pas de opt ???
for slave in self.getslaves(opt):
values.reset(slave)
def pop(self, values, index):
def pop(self, opt, values, index):
#FIXME pas test de meta ...
for slave in self.slaves:
for slave in self.getslaves(opt):
if not values.is_default_owner(slave, validate_properties=False,
validate_meta=False):
values._get_cached_item(slave, validate=False,
@ -89,7 +118,7 @@ class MasterSlaves(object):
def getitem(self, values, opt, path, validate, force_permissive,
force_properties, validate_properties, slave_path=undefined,
slave_value=undefined):
if opt == self.master:
if self.is_master(opt):
return self._getmaster(values, opt, path, validate,
force_permissive, force_properties,
validate_properties, slave_path,
@ -108,9 +137,9 @@ class MasterSlaves(object):
validate_properties)
if validate is True:
masterlen = len(value)
for slave in self.slaves:
for slave in self.getslaves(opt):
try:
slave_path = values._get_opt_path(slave)
slave_path = slave.impl_getpath(values._getcontext())
if c_slave_path == slave_path:
slave_value = c_slave_value
else:
@ -121,8 +150,8 @@ class MasterSlaves(object):
None, False,
None) # not undefined
slavelen = len(slave_value)
self.validate_slave_length(masterlen, slavelen, slave._name)
except ConfigError:
self.validate_slave_length(masterlen, slavelen, slave.impl_getname(), opt)
except ConfigError: # pragma: optional cover
pass
return value
@ -136,10 +165,10 @@ class MasterSlaves(object):
return self.get_slave_value(values, opt, value, validate, validate_properties)
def setitem(self, values, opt, value, path):
if opt == self.master:
if self.is_master(opt):
masterlen = len(value)
for slave in self.slaves:
slave_path = values._get_opt_path(slave)
for slave in self.getslaves(opt):
slave_path = slave.impl_getpath(values._getcontext())
slave_value = values._get_validated_value(slave,
slave_path,
False,
@ -147,24 +176,28 @@ class MasterSlaves(object):
None, False,
None) # not undefined
slavelen = len(slave_value)
self.validate_slave_length(masterlen, slavelen, slave._name)
self.validate_slave_length(masterlen, slavelen, slave.impl_getname(), opt)
else:
self.validate_slave_length(self.get_length(values), len(value),
opt._name, setitem=True)
self.validate_slave_length(self.get_length(values, opt,
slave_path=path), len(value),
opt.impl_getname(), opt, setitem=True)
def get_length(self, values, validate=True, slave_path=undefined,
def get_length(self, values, opt, validate=True, slave_path=undefined,
slave_value=undefined):
masterp = values._get_opt_path(self.master)
return len(self.getitem(values, self.master, masterp, validate, False,
"""get master len with slave option"""
masterp = self.getmaster(opt).impl_getpath(values._getcontext())
if slave_value is undefined:
slave_path = undefined
return len(self.getitem(values, self.getmaster(opt), masterp, validate, False,
None, True, slave_path, slave_value))
def validate_slave_length(self, masterlen, valuelen, name, setitem=False):
if valuelen > masterlen or (valuelen < masterlen and setitem):
def validate_slave_length(self, masterlen, valuelen, name, opt, setitem=False):
if valuelen > masterlen or (valuelen < masterlen and setitem): # pragma: optional cover
log.debug('validate_slave_length: masterlen: {0}, valuelen: {1}, '
'setitem: {2}'.format(masterlen, valuelen, setitem))
raise SlaveError(_("invalid len for the slave: {0}"
" which has {1} as master").format(
name, self.master._name))
name, self.getmaster(opt).impl_getname()))
def get_slave_value(self, values, opt, value, validate=True,
validate_properties=True):
@ -190,11 +223,11 @@ class MasterSlaves(object):
list is greater than master: raise SlaveError
"""
#if slave, had values until master's one
path = values._get_opt_path(opt)
masterlen = self.get_length(values, validate, path, value)
path = opt.impl_getpath(values._getcontext())
masterlen = self.get_length(values, opt, validate, path, value)
valuelen = len(value)
if validate:
self.validate_slave_length(masterlen, valuelen, opt._name)
self.validate_slave_length(masterlen, valuelen, opt.impl_getname(), opt)
if valuelen < masterlen:
for num in range(0, masterlen - valuelen):
index = valuelen + num

View File

@ -23,7 +23,7 @@ import re
import sys
from IPy import IP
from types import FunctionType
from tiramisu.setting import log
from tiramisu.setting import log, undefined
from tiramisu.error import ConfigError, ContextError
from tiramisu.i18n import _
@ -48,7 +48,7 @@ class ChoiceOption(Option):
"""
if isinstance(values, FunctionType):
validate_callback(values, values_params, 'values')
elif not isinstance(values, tuple):
elif not isinstance(values, tuple): # pragma: optional cover
raise TypeError(_('values must be a tuple or a function for {0}'
).format(name))
self._extra = {'_choice_values': values,
@ -74,20 +74,20 @@ class ChoiceOption(Option):
values = carry_out_calculation(self, config=context,
callback=values,
callback_params=values_params)
if not isinstance(values, list):
if not isinstance(values, list): # pragma: optional cover
raise ConfigError(_('calculated values for {0} is not a list'
'').format(self.impl_getname()))
return values
def _validate(self, value, context=None):
def _validate(self, value, context=undefined):
try:
values = self.impl_get_values(context)
if not value in values:
if not value in values: # pragma: optional cover
raise ValueError(_('value {0} is not permitted, '
'only {1} is allowed'
'').format(value,
values))
except ContextError:
except ContextError: # pragma: optional cover
log.debug('ChoiceOption validation, disabled because no context')
@ -95,39 +95,39 @@ class BoolOption(Option):
"represents a choice between ``True`` and ``False``"
__slots__ = tuple()
def _validate(self, value, context=None):
def _validate(self, value, context=undefined):
if not isinstance(value, bool):
raise ValueError(_('invalid boolean'))
raise ValueError(_('invalid boolean')) # pragma: optional cover
class IntOption(Option):
"represents a choice of an integer"
__slots__ = tuple()
def _validate(self, value, context=None):
def _validate(self, value, context=undefined):
if not isinstance(value, int):
raise ValueError(_('invalid integer'))
raise ValueError(_('invalid integer')) # pragma: optional cover
class FloatOption(Option):
"represents a choice of a floating point number"
__slots__ = tuple()
def _validate(self, value, context=None):
def _validate(self, value, context=undefined):
if not isinstance(value, float):
raise ValueError(_('invalid float'))
raise ValueError(_('invalid float')) # pragma: optional cover
class StrOption(Option):
"represents the choice of a string"
__slots__ = tuple()
def _validate(self, value, context=None):
def _validate(self, value, context=undefined):
if not isinstance(value, str):
raise ValueError(_('invalid string'))
raise ValueError(_('invalid string')) # pragma: optional cover
if sys.version_info[0] >= 3:
if sys.version_info[0] >= 3: # pragma: optional cover
#UnicodeOption is same as StrOption in python 3+
class UnicodeOption(StrOption):
__slots__ = tuple()
@ -138,9 +138,9 @@ else:
__slots__ = tuple()
_empty = u''
def _validate(self, value, context=None):
def _validate(self, value, context=undefined):
if not isinstance(value, unicode):
raise ValueError(_('invalid unicode'))
raise ValueError(_('invalid unicode')) # pragma: optional cover
class IPOption(Option):
@ -165,31 +165,31 @@ class IPOption(Option):
properties=properties,
warnings_only=warnings_only)
def _validate(self, value, context=None):
def _validate(self, value, context=undefined):
# sometimes an ip term starts with a zero
# but this does not fit in some case, for example bind does not like it
try:
for val in value.split('.'):
if val.startswith("0") and len(val) > 1:
raise ValueError(_('invalid IP'))
except AttributeError:
raise ValueError(_('invalid IP')) # pragma: optional cover
except AttributeError: # pragma: optional cover
#if integer for example
raise ValueError(_('invalid IP'))
# 'standard' validation
try:
IP('{0}/32'.format(value))
except ValueError:
except ValueError: # pragma: optional cover
raise ValueError(_('invalid IP'))
def _second_level_validation(self, value, warnings_only):
ip = IP('{0}/32'.format(value))
if not self._extra['_allow_reserved'] and ip.iptype() == 'RESERVED':
if not self._extra['_allow_reserved'] and ip.iptype() == 'RESERVED': # pragma: optional cover
if warnings_only:
msg = _("IP is in reserved class")
else:
msg = _("invalid IP, mustn't be in reserved class")
raise ValueError(msg)
if self._extra['_private_only'] and not ip.iptype() == 'PRIVATE':
if self._extra['_private_only'] and not ip.iptype() == 'PRIVATE': # pragma: optional cover
if warnings_only:
msg = _("IP is not in private class")
else:
@ -198,11 +198,11 @@ class IPOption(Option):
def _cons_in_network(self, opts, vals, warnings_only):
if len(vals) != 3:
raise ConfigError(_('invalid len for vals'))
raise ConfigError(_('invalid len for vals')) # pragma: optional cover
if None in vals:
return
ip, network, netmask = vals
if IP(ip) not in IP('{0}/{1}'.format(network, netmask)):
if IP(ip) not in IP('{0}/{1}'.format(network, netmask)): # pragma: optional cover
if warnings_only:
msg = _('IP {0} ({1}) not in network {2} ({3}) with netmask {4}'
' ({5})')
@ -247,12 +247,12 @@ class PortOption(Option):
elif not allowed:
is_finally = True
elif allowed and is_finally:
raise ValueError(_('inconsistency in allowed range'))
raise ValueError(_('inconsistency in allowed range')) # pragma: optional cover
if allowed:
extra['_max_value'] = ports_max[index]
if extra['_max_value'] is None:
raise ValueError(_('max value is empty'))
raise ValueError(_('max value is empty')) # pragma: optional cover
self._extra = extra
super(PortOption, self).__init__(name, doc, default=default,
@ -266,8 +266,8 @@ class PortOption(Option):
properties=properties,
warnings_only=warnings_only)
def _validate(self, value, context=None):
if self._extra['_allow_range'] and ":" in str(value):
def _validate(self, value, context=undefined):
if self._extra['_allow_range'] and ":" in str(value): # pragma: optional cover
value = str(value).split(':')
if len(value) != 2:
raise ValueError(_('invalid port, range must have two values '
@ -281,9 +281,9 @@ class PortOption(Option):
for val in value:
try:
val = int(val)
except ValueError:
except ValueError: # pragma: optional cover
raise ValueError(_('invalid port'))
if not self._extra['_min_value'] <= val <= self._extra['_max_value']:
if not self._extra['_min_value'] <= val <= self._extra['_max_value']: # pragma: optional cover
raise ValueError(_('invalid port, must be an between {0} '
'and {1}').format(self._extra['_min_value'],
self._extra['_max_value']))
@ -293,15 +293,15 @@ class NetworkOption(Option):
"represents the choice of a network"
__slots__ = tuple()
def _validate(self, value, context=None):
def _validate(self, value, context=undefined):
try:
IP(value)
except ValueError:
except ValueError: # pragma: optional cover
raise ValueError(_('invalid network address'))
def _second_level_validation(self, value, warnings_only):
ip = IP(value)
if ip.iptype() == 'RESERVED':
if ip.iptype() == 'RESERVED': # pragma: optional cover
if warnings_only:
msg = _("network address is in reserved class")
else:
@ -313,10 +313,10 @@ class NetmaskOption(Option):
"represents the choice of a netmask"
__slots__ = tuple()
def _validate(self, value, context=None):
def _validate(self, value, context=undefined):
try:
IP('0.0.0.0/{0}'.format(value))
except ValueError:
except ValueError: # pragma: optional cover
raise ValueError(_('invalid netmask address'))
def _cons_network_netmask(self, opts, vals, warnings_only):
@ -334,7 +334,7 @@ class NetmaskOption(Option):
def __cons_netmask(self, opts, val_netmask, val_ipnetwork, make_net,
warnings_only):
if len(opts) != 2:
raise ConfigError(_('invalid len for opts'))
raise ConfigError(_('invalid len for opts')) # pragma: optional cover
msg = None
try:
ip = IP('{0}/{1}'.format(val_ipnetwork, val_netmask),
@ -347,14 +347,14 @@ class NetmaskOption(Option):
except ValueError:
pass
else:
if make_net:
if make_net: # pragma: optional cover
msg = _("invalid IP {0} ({1}) with netmask {2},"
" this IP is a network")
except ValueError:
except ValueError: # pragma: optional cover
if not make_net:
msg = _('invalid network {0} ({1}) with netmask {2}')
if msg is not None:
if msg is not None: # pragma: optional cover
raise ValueError(msg.format(val_ipnetwork, opts[1].impl_getname(),
val_netmask))
@ -362,15 +362,15 @@ class NetmaskOption(Option):
class BroadcastOption(Option):
__slots__ = tuple()
def _validate(self, value, context=None):
def _validate(self, value, context=undefined):
try:
IP('{0}/32'.format(value))
except ValueError:
except ValueError: # pragma: optional cover
raise ValueError(_('invalid broadcast address'))
def _cons_broadcast(self, opts, vals, warnings_only):
if len(vals) != 3:
raise ConfigError(_('invalid len for vals'))
raise ConfigError(_('invalid len for vals')) # pragma: optional cover
if None in vals:
return
broadcast, network, netmask = vals
@ -378,7 +378,7 @@ class BroadcastOption(Option):
raise ValueError(_('invalid broadcast {0} ({1}) with network {2} '
'({3}) and netmask {4} ({5})').format(
broadcast, opts[0].impl_getname(), network,
opts[1].impl_getname(), netmask, opts[2].impl_getname()))
opts[1].impl_getname(), netmask, opts[2].impl_getname())) # pragma: optional cover
class DomainnameOption(Option):
@ -396,12 +396,12 @@ class DomainnameOption(Option):
properties=None, allow_ip=False, type_='domainname',
warnings_only=False, allow_without_dot=False):
if type_ not in ['netbios', 'hostname', 'domainname']:
raise ValueError(_('unknown type_ {0} for hostname').format(type_))
raise ValueError(_('unknown type_ {0} for hostname').format(type_)) # pragma: optional cover
self._extra = {'_dom_type': type_}
if allow_ip not in [True, False]:
raise ValueError(_('allow_ip must be a boolean'))
raise ValueError(_('allow_ip must be a boolean')) # pragma: optional cover
if allow_without_dot not in [True, False]:
raise ValueError(_('allow_without_dot must be a boolean'))
raise ValueError(_('allow_without_dot must be a boolean')) # pragma: optional cover
self._extra['_allow_ip'] = allow_ip
self._extra['_allow_without_dot'] = allow_without_dot
end = ''
@ -410,17 +410,17 @@ class DomainnameOption(Option):
if self._extra['_dom_type'] != 'netbios':
allow_number = '\d'
else:
allow_number = ''
allow_number = '' # pragma: optional cover
if self._extra['_dom_type'] == 'netbios':
length = 14
length = 14 # pragma: optional cover
elif self._extra['_dom_type'] == 'hostname':
length = 62
length = 62 # pragma: optional cover
elif self._extra['_dom_type'] == 'domainname':
length = 62
if allow_without_dot is False:
extrachar_mandatory = '\.'
else:
extrachar = '\.'
extrachar = '\.' # pragma: optional cover
end = '+[a-z]*'
self._extra['_domain_re'] = re.compile(r'^(?:[a-z{0}][a-z\d\-{1}]{{,{2}}}{3}){4}$'
''.format(allow_number, extrachar, length,
@ -436,37 +436,37 @@ class DomainnameOption(Option):
properties=properties,
warnings_only=warnings_only)
def _validate(self, value, context=None):
if self._extra['_allow_ip'] is True:
def _validate(self, value, context=undefined):
if self._extra['_allow_ip'] is True: # pragma: optional cover
try:
IP('{0}/32'.format(value))
return
except ValueError:
pass
if self._extra['_dom_type'] == 'domainname' and not self._extra['_allow_without_dot'] and \
'.' not in value:
'.' not in value: # pragma: optional cover
raise ValueError(_("invalid domainname, must have dot"))
if len(value) > 255:
raise ValueError(_("invalid domainname's length (max 255)"))
raise ValueError(_("invalid domainname's length (max 255)")) # pragma: optional cover
if len(value) < 2:
raise ValueError(_("invalid domainname's length (min 2)"))
raise ValueError(_("invalid domainname's length (min 2)")) # pragma: optional cover
if not self._extra['_domain_re'].search(value):
raise ValueError(_('invalid domainname'))
raise ValueError(_('invalid domainname')) # pragma: optional cover
class EmailOption(DomainnameOption):
__slots__ = tuple()
username_re = re.compile(r"^[\w!#$%&'*+\-/=?^`{|}~.]+$")
def _validate(self, value, context=None):
def _validate(self, value, context=undefined):
splitted = value.split('@', 1)
try:
username, domain = splitted
except ValueError:
except ValueError: # pragma: optional cover
raise ValueError(_('invalid email address, must contains one @'
))
if not self.username_re.search(username):
raise ValueError(_('invalid username in email address'))
raise ValueError(_('invalid username in email address')) # pragma: optional cover
super(EmailOption, self)._validate(domain)
@ -475,9 +475,9 @@ class URLOption(DomainnameOption):
proto_re = re.compile(r'(http|https)://')
path_re = re.compile(r"^[a-z0-9\-\._~:/\?#\[\]@!%\$&\'\(\)\*\+,;=]+$")
def _validate(self, value, context=None):
def _validate(self, value, context=undefined):
match = self.proto_re.search(value)
if not match:
if not match: # pragma: optional cover
raise ValueError(_('invalid url, must start with http:// or '
'https://'))
value = value[len(match.group(0)):]
@ -493,17 +493,17 @@ class URLOption(DomainnameOption):
try:
domain, port = splitted
except ValueError:
except ValueError: # pragma: optional cover
domain = splitted[0]
port = 0
if not 0 <= int(port) <= 65535:
raise ValueError(_('invalid url, port must be an between 0 and '
'65536'))
'65536')) # pragma: optional cover
# validate domainname
super(URLOption, self)._validate(domain)
# validate file
if files is not None and files != '' and not self.path_re.search(files):
raise ValueError(_('invalid url, must ends with filename'))
raise ValueError(_('invalid url, must ends with filename')) # pragma: optional cover
class UsernameOption(Option):
@ -511,17 +511,17 @@ class UsernameOption(Option):
#regexp build with 'man 8 adduser' informations
username_re = re.compile(r"^[a-z_][a-z0-9_-]{0,30}[$a-z0-9_-]{0,1}$")
def _validate(self, value, context=None):
def _validate(self, value, context=undefined):
match = self.username_re.search(value)
if not match:
raise ValueError(_('invalid username'))
raise ValueError(_('invalid username')) # pragma: optional cover
class FilenameOption(Option):
__slots__ = tuple()
path_re = re.compile(r"^[a-zA-Z0-9\-\._~/+]+$")
def _validate(self, value, context=None):
def _validate(self, value, context=undefined):
match = self.path_re.search(value)
if not match:
raise ValueError(_('invalid filename'))
raise ValueError(_('invalid filename')) # pragma: optional cover

View File

@ -19,16 +19,21 @@
# the whole pypy projet is under MIT licence
# ____________________________________________________________
from copy import copy
import re
from tiramisu.i18n import _
from tiramisu.setting import groups # , log
from .baseoption import BaseOption
from tiramisu.setting import groups, undefined # , log
from .baseoption import BaseOption, DynSymLinkOption, SymLinkOption, \
allowed_character
from . import MasterSlaves
from tiramisu.error import ConfigError, ConflictError, ValueWarning
from tiramisu.storage import get_storages_option
from tiramisu.autolib import carry_out_calculation
StorageOptionDescription = get_storages_option('optiondescription')
name_regexp = re.compile(r'^{0}*$'.format(allowed_character))
class OptionDescription(BaseOption, StorageOptionDescription):
@ -42,16 +47,31 @@ class OptionDescription(BaseOption, StorageOptionDescription):
:param children: a list of options (including optiondescriptions)
"""
super(OptionDescription, self).__init__(name, doc=doc, requires=requires, properties=properties)
child_names = [child.impl_getname() for child in children]
super(OptionDescription, self).__init__(name, doc=doc,
requires=requires,
properties=properties,
callback=False)
child_names = []
dynopt_names = []
for child in children:
name = child.impl_getname()
child_names.append(name)
if isinstance(child, DynOptionDescription):
dynopt_names.append(name)
#better performance like this
valid_child = copy(child_names)
valid_child.sort()
old = None
for child in valid_child:
if child == old:
if child == old: # pragma: optional cover
raise ConflictError(_('duplicate option name: '
'{0}').format(child))
if dynopt_names:
for dynopt in dynopt_names:
if child != dynopt and child.startswith(dynopt):
raise ConflictError(_('option must not start as '
'dynoptiondescription'))
old = child
self._add_children(child_names, children)
self._cache_paths = None
@ -74,19 +94,7 @@ class OptionDescription(BaseOption, StorageOptionDescription):
"""returns a list of all paths in self, recursively
_currpath should not be provided (helps with recursion)
"""
if _currpath is None:
_currpath = []
paths = []
for option in self.impl_getchildren():
attr = option.impl_getname()
if isinstance(option, OptionDescription):
if include_groups:
paths.append('.'.join(_currpath + [attr]))
paths += option.impl_getpaths(include_groups=include_groups,
_currpath=_currpath + [attr])
else:
paths.append('.'.join(_currpath + [attr]))
return paths
return _impl_getpaths(self, include_groups, _currpath)
def impl_build_cache_consistency(self, _consistencies=None, cache_option=None):
#FIXME cache_option !
@ -96,7 +104,7 @@ class OptionDescription(BaseOption, StorageOptionDescription):
cache_option = []
else:
init = False
for option in self.impl_getchildren():
for option in self._impl_getchildren(dyn=False):
cache_option.append(option._get_id())
if not isinstance(option, OptionDescription):
for func, all_cons_opts, params in option._get_consistencies():
@ -111,7 +119,7 @@ class OptionDescription(BaseOption, StorageOptionDescription):
self._cache_consistencies = {}
for opt, cons in _consistencies.items():
#FIXME dans le cache ...
if opt._get_id() not in cache_option:
if opt._get_id() not in cache_option: # pragma: optional cover
raise ConfigError(_('consistency with option {0} '
'which is not in Config').format(
opt.impl_getname()))
@ -125,13 +133,13 @@ class OptionDescription(BaseOption, StorageOptionDescription):
cache_option = []
else:
init = False
for option in self.impl_getchildren():
for option in self._impl_getchildren(dyn=False):
#FIXME specifique id for sqlalchemy?
#FIXME avec sqlalchemy ca marche le multi parent ? (dans des configs différentes)
#if option.id is None:
# raise SystemError(_("an option's id should not be None "
# "for {0}").format(option.impl_getname()))
if option._get_id() in cache_option:
if option._get_id() in cache_option: # pragma: optional cover
raise ConflictError(_('duplicate option: {0}').format(option))
cache_option.append(option._get_id())
option._readonly = True
@ -147,7 +155,7 @@ class OptionDescription(BaseOption, StorageOptionDescription):
:param group_type: an instance of `GroupType` or `MasterGroupType`
that lives in `setting.groups`
"""
if self._group_type != groups.default:
if self._group_type != groups.default: # pragma: optional cover
raise TypeError(_('cannot change group_type if already set '
'(old {0}, new {1})').format(self._group_type,
group_type))
@ -155,7 +163,7 @@ class OptionDescription(BaseOption, StorageOptionDescription):
self._group_type = group_type
if isinstance(group_type, groups.MasterGroupType):
MasterSlaves(self.impl_getname(), self.impl_getchildren())
else:
else: # pragma: optional cover
raise ValueError(_('group_type: {0}'
' not allowed').format(group_type))
@ -166,18 +174,29 @@ class OptionDescription(BaseOption, StorageOptionDescription):
if self._cache_consistencies is None:
return True
#consistencies is something like [('_cons_not_equal', (opt1, opt2))]
consistencies = self._cache_consistencies.get(option)
if isinstance(option, DynSymLinkOption):
consistencies = self._cache_consistencies.get(option._opt)
else:
consistencies = self._cache_consistencies.get(option)
if consistencies is not None:
for func, all_cons_opts, params in consistencies:
warnings_only = params.get('warnings_only', False)
#all_cons_opts[0] is the option where func is set
if isinstance(option, DynSymLinkOption):
subpath = '.'.join(option._dyn.split('.')[:-1])
namelen = len(option._opt.impl_getname())
suffix = option.impl_getname()[namelen:]
opts = []
for opt in all_cons_opts:
name = opt.impl_getname() + suffix
path = subpath + '.' + name
opts.append(opt._impl_to_dyn(name, path))
else:
opts = all_cons_opts
try:
all_cons_opts[0]._launch_consistency(func, option,
value,
context, index,
submulti_idx,
all_cons_opts,
warnings_only)
opts[0]._launch_consistency(func, option, value, context,
index, submulti_idx, opts,
warnings_only)
except ValueError as err:
if warnings_only:
raise ValueWarning(err.message, option)
@ -189,12 +208,13 @@ class OptionDescription(BaseOption, StorageOptionDescription):
:param descr: parent :class:`tiramisu.option.OptionDescription`
"""
if descr is None:
self.impl_build_cache_consistency()
#FIXME faut le desactiver ?
#self.impl_build_cache_consistency()
self.impl_build_cache_option()
descr = self
super(OptionDescription, self)._impl_getstate(descr)
self._state_group_type = str(self._group_type)
for option in self.impl_getchildren():
for option in self._impl_getchildren():
option._impl_getstate(descr)
def __getstate__(self):
@ -224,9 +244,12 @@ class OptionDescription(BaseOption, StorageOptionDescription):
self.impl_build_cache_option()
descr = self
self._group_type = getattr(groups, self._state_group_type)
if isinstance(self._group_type, groups.MasterGroupType):
MasterSlaves(self.impl_getname(), self.impl_getchildren(),
validate=False)
del(self._state_group_type)
super(OptionDescription, self)._impl_setstate(descr)
for option in self.impl_getchildren():
for option in self._impl_getchildren(dyn=False):
option._impl_setstate(descr)
def __setstate__(self, state):
@ -235,3 +258,129 @@ class OptionDescription(BaseOption, StorageOptionDescription):
self._stated
except AttributeError:
self._impl_setstate()
def _impl_get_suffixes(self, context):
callback, callback_params = self.impl_get_callback()
if callback_params is None:
callback_params = {}
values = carry_out_calculation(self, config=context,
callback=callback,
callback_params=callback_params)
if len(values) > len(set(values)):
raise ConfigError(_('DynOptionDescription callback return not uniq value'))
for val in values:
if not isinstance(val, str) or re.match(name_regexp, val) is None:
raise ValueError(_("invalid suffix: {0} for option").format(val))
return values
def _impl_search_dynchild(self, name=undefined, context=undefined):
ret = []
for child in self._impl_st_getchildren():
cname = child.impl_getname()
if isinstance(child, DynOptionDescription) and \
(name is undefined or name.startswith(cname)):
path = cname
for value in child._impl_get_suffixes(context):
if name is undefined:
ret.append(SynDynOptionDescription(child, cname + value, path + value, value))
elif name == cname + value:
return SynDynOptionDescription(child, name, path + value, value)
return ret
def _impl_get_dynchild(self, child, suffix):
name = child.impl_getname() + suffix
path = self._name + suffix + '.' + name
if isinstance(child, OptionDescription):
return SynDynOptionDescription(child, name, path, suffix)
else:
return child._impl_to_dyn(name, path)
def _impl_getchildren(self, dyn=True, context=undefined):
for child in self._impl_st_getchildren():
cname = child._name
if dyn and child.impl_is_dynoptiondescription():
path = cname
for value in child._impl_get_suffixes(context):
yield SynDynOptionDescription(child,
cname + value,
path + value, value)
else:
yield child
def impl_getchildren(self):
return list(self._impl_getchildren())
class DynOptionDescription(OptionDescription):
def __init__(self, name, doc, children, requires=None, properties=None,
callback=None, callback_params=None):
for child in children:
if isinstance(child, OptionDescription):
if child.impl_get_group_type() != groups.master:
raise ConfigError(_('cannot set optiondescription in an '
'dynoptiondescription'))
for chld in child._impl_getchildren():
chld._subdyn = self
if isinstance(child, SymLinkOption):
raise ConfigError(_('cannot set symlinkoption in an '
'dynoptiondescription'))
child._subdyn = self
super(DynOptionDescription, self).__init__(name, doc, children,
requires, properties)
self.impl_set_callback(callback, callback_params)
def _validate_callback(self, callback, callback_params):
if callback is None:
raise ConfigError(_('callback is mandatory for dynoptiondescription'))
class SynDynOptionDescription(object):
__slots__ = ('_opt', '_name', '_path', '_suffix')
def __init__(self, opt, name, path, suffix):
self._opt = opt
self._name = name
self._path = path
self._suffix = suffix
def __getattr__(self, name, context=undefined):
if name in dir(self._opt):
return getattr(self._opt, name)
return self._opt._getattr(name, self._name, self._suffix, context)
def impl_getname(self):
return self._name
def _impl_getchildren(self, dyn=True, context=undefined):
children = []
for child in self._opt._impl_getchildren():
children.append(self._opt._impl_get_dynchild(child, self._suffix))
return children
def impl_getchildren(self):
return self._impl_getchildren()
def impl_getpath(self, context):
return self._path
def impl_getpaths(self, include_groups=False, _currpath=None):
return _impl_getpaths(self, include_groups, _currpath)
def _impl_getpaths(klass, include_groups, _currpath):
"""returns a list of all paths in klass, recursively
_currpath should not be provided (helps with recursion)
"""
if _currpath is None:
_currpath = []
paths = []
for option in klass._impl_getchildren():
attr = option.impl_getname()
if option.impl_is_optiondescription():
if include_groups:
paths.append('.'.join(_currpath + [attr]))
paths += option.impl_getpaths(include_groups=include_groups,
_currpath=_currpath + [attr])
else:
paths.append('.'.join(_currpath + [attr]))
return paths