Merge branch 'master' into lgpl
This commit is contained in:
@ -19,15 +19,16 @@
|
||||
# ____________________________________________________________
|
||||
"enables us to carry out a calculation and return an option's value"
|
||||
from tiramisu.error import PropertiesOptionError, ConfigError
|
||||
from tiramisu.setting import multitypes
|
||||
from tiramisu.i18n import _
|
||||
# ____________________________________________________________
|
||||
|
||||
|
||||
def carry_out_calculation(name, config, callback, callback_params,
|
||||
def carry_out_calculation(option, config, callback, callback_params,
|
||||
index=None, max_len=None):
|
||||
"""a function that carries out a calculation for an option's value
|
||||
|
||||
:param name: the option name (`opt._name`)
|
||||
:param name: the option
|
||||
:param config: the context config in order to have
|
||||
the whole options available
|
||||
:param callback: the name of the callback function
|
||||
@ -48,13 +49,13 @@ def carry_out_calculation(name, config, callback, callback_params,
|
||||
Values could have multiple values only when key is ''.
|
||||
|
||||
* if no callback_params:
|
||||
=> calculate()
|
||||
=> calculate(<function func at 0x2092320>, [], {})
|
||||
|
||||
* if callback_params={'': ('yes',)}
|
||||
=> calculate('yes')
|
||||
=> calculate(<function func at 0x2092320>, ['yes'], {})
|
||||
|
||||
* if callback_params={'value': ('yes',)}
|
||||
=> calculate(value='yes')
|
||||
=> calculate(<function func at 0x165b320>, [], {'value': 'yes'})
|
||||
|
||||
* if callback_params={'': ('yes', 'no')}
|
||||
=> calculate('yes', 'no')
|
||||
@ -62,58 +63,71 @@ def carry_out_calculation(name, config, callback, callback_params,
|
||||
* if callback_params={'value': ('yes', 'no')}
|
||||
=> ValueError()
|
||||
|
||||
* if callback_params={'': (['yes', 'no'],)}
|
||||
=> calculate(<function func at 0x176b320>, ['yes', 'no'], {})
|
||||
|
||||
* if callback_params={'value': ('yes', 'no')}
|
||||
=> raises ValueError()
|
||||
|
||||
* if callback_params={'': ((opt1, False),)}
|
||||
|
||||
- a simple option:
|
||||
opt1 == 11
|
||||
=> calculate(11)
|
||||
=> calculate(<function func at 0x1cea320>, [11], {})
|
||||
|
||||
- a multi option:
|
||||
- a multi option and not master/slave:
|
||||
opt1 == [1, 2, 3]
|
||||
=> calculate(1)
|
||||
=> calculate(2)
|
||||
=> calculate(3)
|
||||
=> calculate(<function func at 0x223c320>, [[1, 2, 3]], {})
|
||||
|
||||
- option is master or slave of opt1:
|
||||
opt1 == [1, 2, 3]
|
||||
=> calculate(<function func at 0x223c320>, [1], {})
|
||||
=> calculate(<function func at 0x223c320>, [2], {})
|
||||
=> calculate(<function func at 0x223c320>, [3], {})
|
||||
|
||||
- opt is a master or slave but not related to option:
|
||||
opt1 == [1, 2, 3]
|
||||
=> calculate(<function func at 0x11b0320>, [[1, 2, 3]], {})
|
||||
|
||||
* if callback_params={'value': ((opt1, False),)}
|
||||
|
||||
- a simple option:
|
||||
opt1 == 11
|
||||
=> calculate(value=11)
|
||||
=> calculate(<function func at 0x17ff320>, [], {'value': 11})
|
||||
|
||||
- a multi option:
|
||||
opt1 == [1, 2, 3]
|
||||
=> calculate(value=1)
|
||||
=> calculate(value=2)
|
||||
=> calculate(value=3)
|
||||
=> calculate(<function func at 0x1262320>, [], {'value': [1, 2, 3]})
|
||||
|
||||
* if callback_params={'': ((opt1, False), (opt2, False))}
|
||||
|
||||
- two single options
|
||||
opt1 = 11
|
||||
opt2 = 12
|
||||
=> calculate(<function func at 0x217a320>, [11, 12], {})
|
||||
|
||||
- a multi option with a simple option
|
||||
opt1 == [1, 2, 3]
|
||||
opt2 == 11
|
||||
=> calculate(1, 11)
|
||||
=> calculate(2, 11)
|
||||
=> calculate(3, 11)
|
||||
opt2 == 12
|
||||
=> calculate(<function func at 0x2153320>, [[1, 2, 3], 12], {})
|
||||
|
||||
- a multi option with an other multi option but with same length
|
||||
opt1 == [1, 2, 3]
|
||||
opt2 == [11, 12, 13]
|
||||
=> calculate(1, 11)
|
||||
=> calculate(2, 12)
|
||||
=> calculate(3, 13)
|
||||
=> calculate(<function func at 0x1981320>, [[1, 2, 3], [11, 12, 13]], {})
|
||||
|
||||
- a multi option with an other multi option but with different length
|
||||
opt1 == [1, 2, 3]
|
||||
opt2 == [11, 12]
|
||||
=> ConfigError()
|
||||
=> calculate(<function func at 0x2384320>, [[1, 2, 3], [11, 12]], {})
|
||||
|
||||
- a multi option without value with a simple option
|
||||
opt1 == []
|
||||
opt2 == 11
|
||||
=> []
|
||||
=> calculate(<function func at 0xb65320>, [[], 12], {})
|
||||
|
||||
* if callback_params={'value': ((opt1, False), (opt2, False))}
|
||||
=> ConfigError()
|
||||
=> raises ValueError()
|
||||
|
||||
If index is not None, return a value, otherwise return:
|
||||
|
||||
@ -132,29 +146,36 @@ def carry_out_calculation(name, config, callback, callback_params,
|
||||
for callbk in callbacks:
|
||||
if isinstance(callbk, tuple):
|
||||
# callbk is something link (opt, True|False)
|
||||
option, force_permissive = callbk
|
||||
opt, force_permissive = callbk
|
||||
path = config.cfgimpl_get_description().impl_get_path_by_opt(
|
||||
option)
|
||||
opt)
|
||||
# get value
|
||||
try:
|
||||
value = config._getattr(path, force_permissive=True)
|
||||
value = config._getattr(path, force_permissive=True, validate=False)
|
||||
# convert to list, not modifie this multi
|
||||
if value.__class__.__name__ == 'Multi':
|
||||
value = list(value)
|
||||
except PropertiesOptionError as err:
|
||||
if force_permissive:
|
||||
continue
|
||||
raise ConfigError(_('unable to carry out a calculation, '
|
||||
'option {0} has properties: {1} for: '
|
||||
'{2}').format(option._name,
|
||||
'{2}').format(opt._name,
|
||||
err.proptype,
|
||||
name))
|
||||
is_multi = option.impl_is_multi()
|
||||
option._name))
|
||||
|
||||
is_multi = False
|
||||
if opt.impl_is_multi():
|
||||
#opt is master, search if option is a slave
|
||||
if opt.impl_get_multitype() == multitypes.master:
|
||||
if option in opt.impl_get_master_slaves():
|
||||
is_multi = True
|
||||
#opt is slave, search if option is an other slaves
|
||||
elif opt.impl_get_multitype() == multitypes.slave:
|
||||
if option in opt.impl_get_master_slaves().impl_get_master_slaves():
|
||||
is_multi = True
|
||||
if is_multi:
|
||||
len_value = len(value)
|
||||
if len_multi is not None and len_multi != len_value:
|
||||
raise ConfigError(_('unable to carry out a '
|
||||
'calculation, option value with'
|
||||
' multi types must have same '
|
||||
'length for: {0}').format(name))
|
||||
len_multi = len_value
|
||||
len_multi = len(value)
|
||||
one_is_multi = True
|
||||
tcparams.setdefault(key, []).append((value, is_multi))
|
||||
else:
|
||||
@ -167,16 +188,9 @@ def carry_out_calculation(name, config, callback, callback_params,
|
||||
if one_is_multi:
|
||||
ret = []
|
||||
if index:
|
||||
if index < len_multi:
|
||||
range_ = [index]
|
||||
else:
|
||||
range_ = []
|
||||
ret = None
|
||||
range_ = [index]
|
||||
else:
|
||||
if max_len and max_len < len_multi:
|
||||
range_ = range(max_len)
|
||||
else:
|
||||
range_ = range(len_multi)
|
||||
range_ = range(len_multi)
|
||||
for incr in range_:
|
||||
args = []
|
||||
kwargs = {}
|
||||
@ -195,10 +209,7 @@ def carry_out_calculation(name, config, callback, callback_params,
|
||||
if index:
|
||||
ret = calc
|
||||
else:
|
||||
if isinstance(calc, list):
|
||||
ret.extend(calc)
|
||||
else:
|
||||
ret.append(calc)
|
||||
ret.append(calc)
|
||||
return ret
|
||||
else:
|
||||
# no value is multi
|
||||
@ -212,7 +223,18 @@ def carry_out_calculation(name, config, callback, callback_params,
|
||||
args.append(couple[0])
|
||||
else:
|
||||
kwargs[key] = couple[0]
|
||||
return calculate(callback, args, kwargs)
|
||||
ret = calculate(callback, args, kwargs)
|
||||
if callback_params != {}:
|
||||
if isinstance(ret, list) and max_len:
|
||||
ret = ret[:max_len]
|
||||
if len(ret) < max_len:
|
||||
ret = ret + [None] * (max_len - len(ret))
|
||||
if isinstance(ret, list) and index:
|
||||
if len(ret) < index + 1:
|
||||
ret = None
|
||||
else:
|
||||
ret = ret[index]
|
||||
return ret
|
||||
|
||||
|
||||
def calculate(callback, args, kwargs):
|
||||
|
@ -47,7 +47,7 @@ class SubConfig(object):
|
||||
:type subpath: `str` with the path name
|
||||
"""
|
||||
# main option description
|
||||
if not isinstance(descr, OptionDescription):
|
||||
if descr is not None and not isinstance(descr, OptionDescription):
|
||||
raise TypeError(_('descr must be an optiondescription, not {0}'
|
||||
).format(type(descr)))
|
||||
self._impl_descr = descr
|
||||
@ -148,19 +148,25 @@ class SubConfig(object):
|
||||
except UnicodeEncodeError:
|
||||
lines.append("{0} = {1}".format(name,
|
||||
value.encode(default_encoding)))
|
||||
except PropertiesOptionError:
|
||||
pass
|
||||
return '\n'.join(lines)
|
||||
|
||||
__repr__ = __str__
|
||||
|
||||
def _cfgimpl_get_context(self):
|
||||
return self._impl_context()
|
||||
"""context could be None, we need to test it
|
||||
context is None only if all reference to `Config` object is deleted
|
||||
(for example we delete a `Config` and we manipulate a reference to
|
||||
old `SubConfig`, `Values`, `Multi` or `Settings`)
|
||||
"""
|
||||
context = self._impl_context()
|
||||
if context is None:
|
||||
raise ConfigError(_('the context does not exist anymore'))
|
||||
return context
|
||||
|
||||
def cfgimpl_get_description(self):
|
||||
if self._impl_descr is None:
|
||||
raise ConfigError(_('no option description found for this config'
|
||||
' (may be metaconfig without meta)'))
|
||||
' (may be GroupConfig)'))
|
||||
else:
|
||||
return self._impl_descr
|
||||
|
||||
@ -249,7 +255,8 @@ class SubConfig(object):
|
||||
force_properties=force_properties,
|
||||
force_permissive=force_permissive)
|
||||
|
||||
def find(self, bytype=None, byname=None, byvalue=None, type_='option'):
|
||||
def find(self, bytype=None, byname=None, byvalue=None, type_='option',
|
||||
check_properties=True):
|
||||
"""
|
||||
finds a list of options recursively in the config
|
||||
|
||||
@ -261,11 +268,11 @@ class SubConfig(object):
|
||||
return self._cfgimpl_get_context()._find(bytype, byname, byvalue,
|
||||
first=False,
|
||||
type_=type_,
|
||||
_subpath=self.cfgimpl_get_path()
|
||||
)
|
||||
_subpath=self.cfgimpl_get_path(),
|
||||
check_properties=check_properties)
|
||||
|
||||
def find_first(self, bytype=None, byname=None, byvalue=None,
|
||||
type_='option', display_error=True):
|
||||
type_='option', display_error=True, check_properties=True):
|
||||
"""
|
||||
finds an option recursively in the config
|
||||
|
||||
@ -276,7 +283,8 @@ class SubConfig(object):
|
||||
"""
|
||||
return self._cfgimpl_get_context()._find(
|
||||
bytype, byname, byvalue, first=True, type_=type_,
|
||||
_subpath=self.cfgimpl_get_path(), display_error=display_error)
|
||||
_subpath=self.cfgimpl_get_path(), display_error=display_error,
|
||||
check_properties=check_properties)
|
||||
|
||||
def _find(self, bytype, byname, byvalue, first, type_='option',
|
||||
_subpath=None, check_properties=True, display_error=True):
|
||||
@ -287,12 +295,9 @@ class SubConfig(object):
|
||||
:return: find list or an exception if nothing has been found
|
||||
"""
|
||||
def _filter_by_name():
|
||||
try:
|
||||
if byname is None or path == byname or \
|
||||
path.endswith('.' + byname):
|
||||
return True
|
||||
except IndexError:
|
||||
pass
|
||||
if byname is None or path == byname or \
|
||||
path.endswith('.' + byname):
|
||||
return True
|
||||
return False
|
||||
|
||||
def _filter_by_value():
|
||||
@ -442,23 +447,20 @@ class SubConfig(object):
|
||||
return pathsvalues
|
||||
|
||||
def _make_sub_dict(self, opt, path, pathsvalues, _currpath, flatten):
|
||||
if isinstance(opt, OptionDescription):
|
||||
try:
|
||||
try:
|
||||
if isinstance(opt, OptionDescription):
|
||||
pathsvalues += getattr(self, path).make_dict(flatten,
|
||||
_currpath +
|
||||
path.split('.'))
|
||||
except PropertiesOptionError:
|
||||
pass # this just a hidden or disabled option
|
||||
else:
|
||||
try:
|
||||
else:
|
||||
value = self._getattr(opt._name)
|
||||
if flatten:
|
||||
name = opt._name
|
||||
else:
|
||||
name = '.'.join(_currpath + [opt._name])
|
||||
pathsvalues.append((name, value))
|
||||
except PropertiesOptionError:
|
||||
pass # this just a hidden or disabled option
|
||||
except PropertiesOptionError:
|
||||
pass
|
||||
|
||||
def cfgimpl_get_path(self):
|
||||
descr = self.cfgimpl_get_description()
|
||||
@ -466,9 +468,9 @@ class SubConfig(object):
|
||||
return context_descr.impl_get_path_by_opt(descr)
|
||||
|
||||
|
||||
class CommonConfig(SubConfig):
|
||||
"abstract base class for the Config and the MetaConfig"
|
||||
__slots__ = ('_impl_values', '_impl_settings', '_impl_meta')
|
||||
class _CommonConfig(SubConfig):
|
||||
"abstract base class for the Config, GroupConfig and the MetaConfig"
|
||||
__slots__ = ('_impl_values', '_impl_settings', '_impl_meta', '_impl_test')
|
||||
|
||||
def _impl_build_all_paths(self):
|
||||
self.cfgimpl_get_description().impl_build_cache()
|
||||
@ -507,7 +509,8 @@ class CommonConfig(SubConfig):
|
||||
return None
|
||||
|
||||
def cfgimpl_get_meta(self):
|
||||
return self._impl_meta
|
||||
if self._impl_meta is not None:
|
||||
return self._impl_meta()
|
||||
|
||||
# information
|
||||
def impl_set_information(self, key, value):
|
||||
@ -525,11 +528,48 @@ class CommonConfig(SubConfig):
|
||||
"""
|
||||
return self._impl_values.get_information(key, default)
|
||||
|
||||
# ----- state
|
||||
def __getstate__(self):
|
||||
if self._impl_meta is not None:
|
||||
raise ConfigError(_('cannot serialize Config with MetaConfig'))
|
||||
slots = set()
|
||||
for subclass in self.__class__.__mro__:
|
||||
if subclass is not object:
|
||||
slots.update(subclass.__slots__)
|
||||
slots -= frozenset(['_impl_context', '_impl_meta', '__weakref__'])
|
||||
state = {}
|
||||
for slot in slots:
|
||||
try:
|
||||
state[slot] = getattr(self, slot)
|
||||
except AttributeError:
|
||||
pass
|
||||
storage = self._impl_values._p_._storage
|
||||
if not storage.serializable:
|
||||
raise ConfigError(_('this storage is not serialisable, could be a '
|
||||
'none persistent storage'))
|
||||
state['_storage'] = {'session_id': storage.session_id,
|
||||
'persistent': storage.persistent}
|
||||
state['_impl_setting'] = _impl_getstate_setting()
|
||||
return state
|
||||
|
||||
def __setstate__(self, state):
|
||||
for key, value in state.items():
|
||||
if key not in ['_storage', '_impl_setting']:
|
||||
setattr(self, key, value)
|
||||
set_storage(**state['_impl_setting'])
|
||||
self._impl_context = weakref.ref(self)
|
||||
self._impl_settings.context = weakref.ref(self)
|
||||
self._impl_values.context = weakref.ref(self)
|
||||
storage = get_storage(test=self._impl_test, **state['_storage'])
|
||||
self._impl_values._impl_setstate(storage)
|
||||
self._impl_settings._impl_setstate(storage)
|
||||
self._impl_meta = None
|
||||
|
||||
|
||||
# ____________________________________________________________
|
||||
class Config(CommonConfig):
|
||||
class Config(_CommonConfig):
|
||||
"main configuration management entry"
|
||||
__slots__ = ('__weakref__', '_impl_test')
|
||||
__slots__ = ('__weakref__',)
|
||||
|
||||
def __init__(self, descr, session_id=None, persistent=False):
|
||||
""" Configuration option management master class
|
||||
@ -553,41 +593,6 @@ class Config(CommonConfig):
|
||||
#undocumented option used only in test script
|
||||
self._impl_test = False
|
||||
|
||||
def __getstate__(self):
|
||||
if self._impl_meta is not None:
|
||||
raise ConfigError('cannot serialize Config with meta')
|
||||
slots = set()
|
||||
for subclass in self.__class__.__mro__:
|
||||
if subclass is not object:
|
||||
slots.update(subclass.__slots__)
|
||||
slots -= frozenset(['_impl_context', '__weakref__'])
|
||||
state = {}
|
||||
for slot in slots:
|
||||
try:
|
||||
state[slot] = getattr(self, slot)
|
||||
except AttributeError:
|
||||
pass
|
||||
storage = self._impl_values._p_._storage
|
||||
if not storage.serializable:
|
||||
raise ConfigError('this storage is not serialisable, could be a '
|
||||
'none persistent storage')
|
||||
state['_storage'] = {'session_id': storage.session_id,
|
||||
'persistent': storage.persistent}
|
||||
state['_impl_setting'] = _impl_getstate_setting()
|
||||
return state
|
||||
|
||||
def __setstate__(self, state):
|
||||
for key, value in state.items():
|
||||
if key not in ['_storage', '_impl_setting']:
|
||||
setattr(self, key, value)
|
||||
set_storage(**state['_impl_setting'])
|
||||
self._impl_context = weakref.ref(self)
|
||||
self._impl_settings.context = weakref.ref(self)
|
||||
self._impl_values.context = weakref.ref(self)
|
||||
storage = get_storage(test=self._impl_test, **state['_storage'])
|
||||
self._impl_values._impl_setstate(storage)
|
||||
self._impl_settings._impl_setstate(storage)
|
||||
|
||||
def cfgimpl_reset_cache(self,
|
||||
only_expired=False,
|
||||
only=('values', 'settings')):
|
||||
@ -597,115 +602,121 @@ class Config(CommonConfig):
|
||||
self.cfgimpl_get_settings().reset_cache(only_expired=only_expired)
|
||||
|
||||
|
||||
#class MetaConfig(CommonConfig):
|
||||
# __slots__ = ('_impl_children',)
|
||||
class GroupConfig(_CommonConfig):
|
||||
__slots__ = ('_impl_children', '__weakref__')
|
||||
|
||||
# def __init__(self, children, meta=True, session_id=None, persistent=False):
|
||||
# if not isinstance(children, list):
|
||||
# raise ValueError(_("metaconfig's children must be a list"))
|
||||
# self._impl_descr = None
|
||||
# self._impl_path = None
|
||||
# if meta:
|
||||
# for child in children:
|
||||
# if not isinstance(child, CommonConfig):
|
||||
# raise TypeError(_("metaconfig's children "
|
||||
# "must be config, not {0}"
|
||||
# ).format(type(child)))
|
||||
# if self._impl_descr is None:
|
||||
# self._impl_descr = child.cfgimpl_get_description()
|
||||
# elif not self._impl_descr is child.cfgimpl_get_description():
|
||||
# raise ValueError(_('all config in metaconfig must '
|
||||
# 'have the same optiondescription'))
|
||||
# if child.cfgimpl_get_meta() is not None:
|
||||
# raise ValueError(_("child has already a metaconfig's"))
|
||||
# child._impl_meta = self
|
||||
def __init__(self, children, session_id=None, persistent=False,
|
||||
_descr=None):
|
||||
if not isinstance(children, list):
|
||||
raise ValueError(_("metaconfig's children must be a list"))
|
||||
self._impl_children = children
|
||||
settings, values = get_storages(self, session_id, persistent)
|
||||
self._impl_settings = Settings(self, settings)
|
||||
self._impl_values = Values(self, values)
|
||||
super(GroupConfig, self).__init__(_descr, weakref.ref(self))
|
||||
self._impl_meta = None
|
||||
#undocumented option used only in test script
|
||||
self._impl_test = False
|
||||
|
||||
# self._impl_children = children
|
||||
# settings, values = get_storages(self, session_id, persistent)
|
||||
# self._impl_settings = Settings(self, settings)
|
||||
# self._impl_values = Values(self, values)
|
||||
# self._impl_meta = None
|
||||
def cfgimpl_get_children(self):
|
||||
return self._impl_children
|
||||
|
||||
# def cfgimpl_get_children(self):
|
||||
# return self._impl_children
|
||||
#def cfgimpl_get_context(self):
|
||||
# "a meta config is a config which has a setting, that is itself"
|
||||
# return self
|
||||
#
|
||||
def cfgimpl_reset_cache(self,
|
||||
only_expired=False,
|
||||
only=('values', 'settings')):
|
||||
if 'values' in only:
|
||||
self.cfgimpl_get_values().reset_cache(only_expired=only_expired)
|
||||
if 'settings' in only:
|
||||
self.cfgimpl_get_settings().reset_cache(only_expired=only_expired)
|
||||
for child in self._impl_children:
|
||||
child.cfgimpl_reset_cache(only_expired=only_expired, only=only)
|
||||
|
||||
# def cfgimpl_get_context(self):
|
||||
# "a meta config is a config wich has a setting, that is itself"
|
||||
# return self
|
||||
def setattrs(self, path, value):
|
||||
"""Setattr not in current GroupConfig, but in each children
|
||||
"""
|
||||
for child in self._impl_children:
|
||||
try:
|
||||
if not isinstance(child, GroupConfig):
|
||||
setattr(child, path, value)
|
||||
else:
|
||||
child.setattrs(path, value)
|
||||
except PropertiesOptionError:
|
||||
pass
|
||||
|
||||
# def cfgimpl_reset_cache(self,
|
||||
# only_expired=False,
|
||||
# only=('values', 'settings')):
|
||||
# if 'values' in only:
|
||||
# self.cfgimpl_get_values().reset_cache(only_expired=only_expired)
|
||||
# if 'settings' in only:
|
||||
# self.cfgimpl_get_settings().reset_cache(only_expired=only_expired)
|
||||
# for child in self._impl_children:
|
||||
# child.cfgimpl_reset_cache(only_expired=only_expired, only=only)
|
||||
def find_firsts(self, byname=None, bypath=None, byvalue=None,
|
||||
type_='path', display_error=True):
|
||||
"""Find first not in current GroupConfig, but in each children
|
||||
"""
|
||||
ret = []
|
||||
#if MetaConfig, all children have same OptionDescription as context
|
||||
#so search only one time for all children
|
||||
try:
|
||||
if bypath is None and byname is not None and \
|
||||
isinstance(self, MetaConfig):
|
||||
bypath = self._find(bytype=None, byvalue=None, byname=byname,
|
||||
first=True, type_='path',
|
||||
check_properties=False,
|
||||
display_error=display_error)
|
||||
byname = None
|
||||
except AttributeError:
|
||||
pass
|
||||
for child in self._impl_children:
|
||||
try:
|
||||
if not isinstance(child, MetaConfig):
|
||||
if bypath is not None:
|
||||
#if byvalue is None, try if not raise
|
||||
value = getattr(child, bypath)
|
||||
if byvalue is not None:
|
||||
if isinstance(value, Multi):
|
||||
if byvalue in value:
|
||||
ret.append(child)
|
||||
else:
|
||||
if value == byvalue:
|
||||
ret.append(child)
|
||||
else:
|
||||
ret.append(child)
|
||||
else:
|
||||
ret.append(child.find_first(byname=byname,
|
||||
byvalue=byvalue,
|
||||
type_=type_,
|
||||
display_error=False))
|
||||
else:
|
||||
ret.extend(child.find_firsts(byname=byname,
|
||||
bypath=bypath,
|
||||
byvalue=byvalue,
|
||||
type_=type_,
|
||||
display_error=False))
|
||||
except AttributeError:
|
||||
pass
|
||||
return self._find_return_results(ret, display_error)
|
||||
|
||||
# def set_contexts(self, path, value):
|
||||
# for child in self._impl_children:
|
||||
# try:
|
||||
# if not isinstance(child, MetaConfig):
|
||||
# setattr(child, path, value)
|
||||
# else:
|
||||
# child.set_contexts(path, value)
|
||||
# except PropertiesOptionError:
|
||||
# pass
|
||||
|
||||
# def find_first_contexts(self, byname=None, bypath=None, byvalue=None,
|
||||
# type_='path', display_error=True):
|
||||
# ret = []
|
||||
# try:
|
||||
# if bypath is None and byname is not None and \
|
||||
# self.cfgimpl_get_description() is not None:
|
||||
# bypath = self._find(bytype=None, byvalue=None, byname=byname,
|
||||
# first=True, type_='path',
|
||||
# check_properties=False,
|
||||
# display_error=display_error)
|
||||
# except ConfigError:
|
||||
# pass
|
||||
# for child in self._impl_children:
|
||||
# try:
|
||||
# if not isinstance(child, MetaConfig):
|
||||
# if bypath is not None:
|
||||
# if byvalue is not None:
|
||||
# if getattr(child, bypath) == byvalue:
|
||||
# ret.append(child)
|
||||
# else:
|
||||
# #not raise
|
||||
# getattr(child, bypath)
|
||||
# ret.append(child)
|
||||
# else:
|
||||
# ret.append(child.find_first(byname=byname,
|
||||
# byvalue=byvalue,
|
||||
# type_=type_,
|
||||
# display_error=False))
|
||||
# else:
|
||||
# ret.extend(child.find_first_contexts(byname=byname,
|
||||
# bypath=bypath,
|
||||
# byvalue=byvalue,
|
||||
# type_=type_,
|
||||
# display_error=False))
|
||||
# except AttributeError:
|
||||
# pass
|
||||
# return self._find_return_results(ret, display_error)
|
||||
class MetaConfig(GroupConfig):
|
||||
__slots__ = tuple()
|
||||
|
||||
def __init__(self, children, session_id=None, persistent=False):
|
||||
descr = None
|
||||
for child in children:
|
||||
if not isinstance(child, _CommonConfig):
|
||||
raise TypeError(_("metaconfig's children "
|
||||
"should be config, not {0}"
|
||||
).format(type(child)))
|
||||
if child.cfgimpl_get_meta() is not None:
|
||||
raise ValueError(_("child has already a metaconfig's"))
|
||||
if descr is None:
|
||||
descr = child.cfgimpl_get_description()
|
||||
elif not descr is child.cfgimpl_get_description():
|
||||
raise ValueError(_('all config in metaconfig must '
|
||||
'have the same optiondescription'))
|
||||
child._impl_meta = weakref.ref(self)
|
||||
|
||||
super(MetaConfig, self).__init__(children, session_id, persistent, descr)
|
||||
|
||||
|
||||
def mandatory_warnings(config):
|
||||
"""convenience function to trace Options that are mandatory and
|
||||
where no value has been set
|
||||
|
||||
:returns: generator of mandatory Option's path
|
||||
|
||||
"""
|
||||
#if value in cache, properties are not calculated
|
||||
config.cfgimpl_reset_cache(only=('values',))
|
||||
for path in config.cfgimpl_get_description().impl_getpaths(
|
||||
include_groups=True):
|
||||
try:
|
||||
config._getattr(path, force_properties=frozenset(('mandatory',)))
|
||||
except PropertiesOptionError as err:
|
||||
if err.proptype == ['mandatory']:
|
||||
yield path
|
||||
config.cfgimpl_reset_cache(only=('values',))
|
||||
#only for retro-compatibility
|
||||
config.cfgimpl_get_values().mandatory_warnings()
|
||||
|
@ -39,9 +39,7 @@ 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'"
|
||||
try:
|
||||
name = str(name)
|
||||
except:
|
||||
if not isinstance(name, str):
|
||||
return False
|
||||
if re.match(name_regexp, name) is None and not name.startswith('_') \
|
||||
and name not in forbidden_names \
|
||||
@ -337,7 +335,7 @@ class Option(BaseOption):
|
||||
self._consistencies = None
|
||||
|
||||
def _launch_consistency(self, func, option, value, context, index,
|
||||
all_cons_opts):
|
||||
all_cons_opts, warnings_only):
|
||||
"""Launch consistency now
|
||||
|
||||
:param func: function name, this name should start with _cons_
|
||||
@ -352,12 +350,11 @@ class Option(BaseOption):
|
||||
:type index: `int`
|
||||
:param all_cons_opts: all options concerne by this consistency
|
||||
:type all_cons_opts: `list` of `tiramisu.option.Option`
|
||||
:param warnings_only: specific raise error for warning
|
||||
:type warnings_only: `boolean`
|
||||
"""
|
||||
if context is not None:
|
||||
descr = context.cfgimpl_get_description()
|
||||
#option is also in all_cons_opts
|
||||
if option not in all_cons_opts:
|
||||
raise ConfigError(_('option not in all_cons_opts'))
|
||||
|
||||
all_cons_vals = []
|
||||
for opt in all_cons_opts:
|
||||
@ -368,7 +365,8 @@ class Option(BaseOption):
|
||||
#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)
|
||||
descr.impl_get_path_by_opt(opt), validate=False,
|
||||
force_permissive=True)
|
||||
else:
|
||||
opt_value = opt.impl_getdefault()
|
||||
|
||||
@ -382,7 +380,7 @@ class Option(BaseOption):
|
||||
except IndexError:
|
||||
#so return if no value
|
||||
return
|
||||
getattr(self, func)(all_cons_opts, all_cons_vals)
|
||||
getattr(self, func)(all_cons_opts, all_cons_vals, warnings_only)
|
||||
|
||||
def impl_validate(self, value, context=None, validate=True,
|
||||
force_index=None):
|
||||
@ -412,7 +410,7 @@ class Option(BaseOption):
|
||||
else:
|
||||
validator_params = {'': (val,)}
|
||||
# Raise ValueError if not valid
|
||||
carry_out_calculation(self._name, config=context,
|
||||
carry_out_calculation(self, config=context,
|
||||
callback=self._validator[0],
|
||||
callback_params=validator_params)
|
||||
|
||||
@ -420,23 +418,41 @@ class Option(BaseOption):
|
||||
if _value is None:
|
||||
return
|
||||
# option validation
|
||||
self._validate(_value)
|
||||
try:
|
||||
self._validate(_value)
|
||||
except ValueError as err:
|
||||
raise ValueError(_('invalid value for option {0}: {1}'
|
||||
'').format(self._name, err))
|
||||
error = None
|
||||
warning = None
|
||||
try:
|
||||
# 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)
|
||||
self._second_level_validation(_value)
|
||||
except ValueError as err:
|
||||
msg = _("invalid value {0} for option {1}: {2}").format(
|
||||
_value, self._name, err)
|
||||
self._second_level_validation(_value, self._warnings_only)
|
||||
except ValueError as error:
|
||||
if self._warnings_only:
|
||||
warnings.warn_explicit(ValueWarning(msg, self),
|
||||
ValueWarning,
|
||||
self.__class__.__name__, 0)
|
||||
else:
|
||||
raise ValueError(msg)
|
||||
warning = error
|
||||
error = None
|
||||
except ValueWarning as warning:
|
||||
pass
|
||||
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)
|
||||
except ValueError as error:
|
||||
pass
|
||||
except ValueWarning as warning:
|
||||
pass
|
||||
if warning:
|
||||
msg = _("warning on the value of the option {0}: {1}").format(
|
||||
self._name, warning)
|
||||
warnings.warn_explicit(ValueWarning(msg, self),
|
||||
ValueWarning,
|
||||
self.__class__.__name__, 0)
|
||||
elif error:
|
||||
raise ValueError(_("invalid value for option {0}: {1}").format(
|
||||
self._name, error))
|
||||
|
||||
# generic calculation
|
||||
if context is not None:
|
||||
@ -446,17 +462,13 @@ class Option(BaseOption):
|
||||
do_validation(value, force_index)
|
||||
else:
|
||||
if not isinstance(value, list):
|
||||
raise ValueError(_("which must be a list").format(value,
|
||||
self._name))
|
||||
raise ValueError(_("invalid value {0} for option {1} which must be a list").format(value, self._name))
|
||||
for index, val in enumerate(value):
|
||||
do_validation(val, index)
|
||||
|
||||
def impl_getdefault(self, default_multi=False):
|
||||
def impl_getdefault(self):
|
||||
"accessing the default value"
|
||||
if not default_multi or not self.impl_is_multi():
|
||||
return self._default
|
||||
else:
|
||||
return self.getdefault_multi()
|
||||
return self._default
|
||||
|
||||
def impl_getdefault_multi(self):
|
||||
"accessing the default value for a multi"
|
||||
@ -493,7 +505,7 @@ class Option(BaseOption):
|
||||
def impl_is_multi(self):
|
||||
return self._multi
|
||||
|
||||
def impl_add_consistency(self, func, *other_opts):
|
||||
def impl_add_consistency(self, func, *other_opts, **params):
|
||||
"""Add consistency means that value will be validate with other_opts
|
||||
option's values.
|
||||
|
||||
@ -501,16 +513,18 @@ class Option(BaseOption):
|
||||
:type func: `str`
|
||||
:param other_opts: options used to validate value
|
||||
:type other_opts: `list` of `tiramisu.option.Option`
|
||||
:param params: extra params (only warnings_only are allowed)
|
||||
"""
|
||||
if self._consistencies is None:
|
||||
self._consistencies = []
|
||||
warnings_only = params.get('warnings_only', False)
|
||||
for opt in other_opts:
|
||||
if not isinstance(opt, Option):
|
||||
raise ConfigError(_('consistency should be set with an option'))
|
||||
raise ConfigError(_('consistency must be set with an option'))
|
||||
if self is opt:
|
||||
raise ConfigError(_('cannot add consistency with itself'))
|
||||
if self.impl_is_multi() != opt.impl_is_multi():
|
||||
raise ConfigError(_('every options in consistency should be '
|
||||
raise ConfigError(_('every options in consistency must be '
|
||||
'multi or none'))
|
||||
func = '_cons_{0}'.format(func)
|
||||
all_cons_opts = tuple([self] + list(other_opts))
|
||||
@ -519,19 +533,23 @@ class Option(BaseOption):
|
||||
if self.impl_is_multi():
|
||||
for idx, val in enumerate(value):
|
||||
self._launch_consistency(func, self, val, None,
|
||||
idx, all_cons_opts)
|
||||
idx, all_cons_opts, warnings_only)
|
||||
else:
|
||||
self._launch_consistency(func, self, value, None,
|
||||
None, all_cons_opts)
|
||||
self._consistencies.append((func, all_cons_opts))
|
||||
None, all_cons_opts, warnings_only)
|
||||
self._consistencies.append((func, all_cons_opts, params))
|
||||
self.impl_validate(self.impl_getdefault())
|
||||
|
||||
def _cons_not_equal(self, opts, vals):
|
||||
def _cons_not_equal(self, opts, vals, warnings_only):
|
||||
for idx_inf, val_inf in enumerate(vals):
|
||||
for idx_sup, val_sup in enumerate(vals[idx_inf + 1:]):
|
||||
if val_inf == val_sup is not None:
|
||||
raise ValueError(_("same value for {0} and {1}").format(
|
||||
opts[idx_inf]._name, opts[idx_inf + idx_sup + 1]._name))
|
||||
if warnings_only:
|
||||
msg = _("same value for {0} and {1}, should be different")
|
||||
else:
|
||||
msg = _("same value for {0} and {1}, must be different")
|
||||
raise ValueError(msg.format(opts[idx_inf]._name,
|
||||
opts[idx_inf + idx_sup + 1]._name))
|
||||
|
||||
def _impl_convert_callbacks(self, descr, load=False):
|
||||
if not load and self._callback is None:
|
||||
@ -587,38 +605,22 @@ class Option(BaseOption):
|
||||
consistencies = self._state_consistencies
|
||||
else:
|
||||
consistencies = self._consistencies
|
||||
if isinstance(consistencies, list):
|
||||
new_value = []
|
||||
for consistency in consistencies:
|
||||
values = []
|
||||
for obj in consistency[1]:
|
||||
if load:
|
||||
values.append(descr.impl_get_opt_by_path(obj))
|
||||
else:
|
||||
values.append(descr.impl_get_path_by_opt(obj))
|
||||
new_value.append((consistency[0], tuple(values)))
|
||||
|
||||
else:
|
||||
new_value = {}
|
||||
for key, _consistencies in consistencies.items():
|
||||
new_value[key] = []
|
||||
for key_cons, _cons in _consistencies:
|
||||
_list_cons = []
|
||||
for _con in _cons:
|
||||
if load:
|
||||
_list_cons.append(
|
||||
descr.impl_get_opt_by_path(_con))
|
||||
else:
|
||||
_list_cons.append(
|
||||
descr.impl_get_path_by_opt(_con))
|
||||
new_value[key].append((key_cons, tuple(_list_cons)))
|
||||
new_value = []
|
||||
for consistency in consistencies:
|
||||
values = []
|
||||
for obj in consistency[1]:
|
||||
if load:
|
||||
values.append(descr.impl_get_opt_by_path(obj))
|
||||
else:
|
||||
values.append(descr.impl_get_path_by_opt(obj))
|
||||
new_value.append((consistency[0], tuple(values), consistency[2]))
|
||||
if load:
|
||||
del(self._state_consistencies)
|
||||
self._consistencies = new_value
|
||||
else:
|
||||
self._state_consistencies = new_value
|
||||
|
||||
def _second_level_validation(self, value):
|
||||
def _second_level_validation(self, value, warnings_only):
|
||||
pass
|
||||
|
||||
|
||||
@ -663,7 +665,7 @@ class ChoiceOption(Option):
|
||||
return self._open_values
|
||||
|
||||
def _validate(self, value):
|
||||
if not self._open_values and not value in self._values:
|
||||
if not self.impl_is_openvalues() and not value in self.impl_get_values():
|
||||
raise ValueError(_('value {0} is not permitted, '
|
||||
'only {1} is allowed'
|
||||
'').format(value, self._values))
|
||||
@ -676,7 +678,7 @@ class BoolOption(Option):
|
||||
|
||||
def _validate(self, value):
|
||||
if not isinstance(value, bool):
|
||||
raise ValueError(_('value must be a boolean'))
|
||||
raise ValueError(_('invalid boolean'))
|
||||
|
||||
|
||||
class IntOption(Option):
|
||||
@ -686,7 +688,7 @@ class IntOption(Option):
|
||||
|
||||
def _validate(self, value):
|
||||
if not isinstance(value, int):
|
||||
raise ValueError(_('value must be an integer'))
|
||||
raise ValueError(_('invalid integer'))
|
||||
|
||||
|
||||
class FloatOption(Option):
|
||||
@ -696,7 +698,7 @@ class FloatOption(Option):
|
||||
|
||||
def _validate(self, value):
|
||||
if not isinstance(value, float):
|
||||
raise ValueError(_('value must be a float'))
|
||||
raise ValueError(_('invalid float'))
|
||||
|
||||
|
||||
class StrOption(Option):
|
||||
@ -706,12 +708,11 @@ class StrOption(Option):
|
||||
|
||||
def _validate(self, value):
|
||||
if not isinstance(value, str):
|
||||
raise ValueError(_('value must be a string, not '
|
||||
'{0}').format(type(value)))
|
||||
raise ValueError(_('invalid string'))
|
||||
|
||||
|
||||
if sys.version_info[0] >= 3:
|
||||
#UnicodeOption is same has StrOption in python 3+
|
||||
#UnicodeOption is same as StrOption in python 3+
|
||||
class UnicodeOption(StrOption):
|
||||
__slots__ = tuple()
|
||||
pass
|
||||
@ -724,7 +725,7 @@ else:
|
||||
|
||||
def _validate(self, value):
|
||||
if not isinstance(value, unicode):
|
||||
raise ValueError(_('value must be an unicode'))
|
||||
raise ValueError(_('invalid unicode'))
|
||||
|
||||
|
||||
class SymLinkOption(BaseOption):
|
||||
@ -782,17 +783,51 @@ class IPOption(Option):
|
||||
warnings_only=warnings_only)
|
||||
|
||||
def _validate(self, value):
|
||||
# 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:
|
||||
#if integer for example
|
||||
raise ValueError(_('invalid IP'))
|
||||
# 'standard' validation
|
||||
try:
|
||||
IP('{0}/32'.format(value))
|
||||
except ValueError:
|
||||
raise ValueError(_('invalid IP {0}').format(self._name))
|
||||
raise ValueError(_('invalid IP'))
|
||||
|
||||
def _second_level_validation(self, value):
|
||||
def _second_level_validation(self, value, warnings_only):
|
||||
ip = IP('{0}/32'.format(value))
|
||||
if not self._allow_reserved and ip.iptype() == 'RESERVED':
|
||||
raise ValueError(_("IP mustn't not be in reserved class"))
|
||||
if warnings_only:
|
||||
msg = _("IP shouldn't be in reserved class")
|
||||
else:
|
||||
msg = _("invalid IP, mustn't be in reserved class")
|
||||
raise ValueError(msg)
|
||||
if self._private_only and not ip.iptype() == 'PRIVATE':
|
||||
raise ValueError(_("IP must be in private class"))
|
||||
if warnings_only:
|
||||
msg = _("IP should be in private class")
|
||||
else:
|
||||
msg = _("invalid IP, must be in private class")
|
||||
raise ValueError(msg)
|
||||
|
||||
def _cons_in_network(self, opts, vals, warnings_only):
|
||||
if len(vals) != 3:
|
||||
raise ConfigError(_('invalid len for vals'))
|
||||
if None in vals:
|
||||
return
|
||||
ip, network, netmask = vals
|
||||
if IP(ip) not in IP('{0}/{1}'.format(network, netmask)):
|
||||
if warnings_only:
|
||||
msg = _('IP {0} ({1}) not in network {2} ({3}) with netmask {4}'
|
||||
' ({5})')
|
||||
else:
|
||||
msg = _('invalid IP {0} ({1}) not in network {2} ({3}) with '
|
||||
'netmask {4} ({5})')
|
||||
raise ValueError(msg.format(ip, opts[0]._name, network,
|
||||
opts[1]._name, netmask, opts[2]._name))
|
||||
|
||||
|
||||
class PortOption(Option):
|
||||
@ -852,17 +887,23 @@ class PortOption(Option):
|
||||
if self._allow_range and ":" in str(value):
|
||||
value = str(value).split(':')
|
||||
if len(value) != 2:
|
||||
raise ValueError('range must have two values only')
|
||||
raise ValueError(_('invalid port, range must have two values '
|
||||
'only'))
|
||||
if not value[0] < value[1]:
|
||||
raise ValueError('first port in range must be'
|
||||
' smaller than the second one')
|
||||
raise ValueError(_('invalid port, first port in range must be'
|
||||
' smaller than the second one'))
|
||||
else:
|
||||
value = [value]
|
||||
|
||||
for val in value:
|
||||
try:
|
||||
int(val)
|
||||
except ValueError:
|
||||
raise ValueError(_('invalid port'))
|
||||
if not self._min_value <= int(val) <= self._max_value:
|
||||
raise ValueError('port must be an between {0} and {1}'
|
||||
''.format(self._min_value, self._max_value))
|
||||
raise ValueError(_('invalid port, must be an between {0} '
|
||||
'and {1}').format(self._min_value,
|
||||
self._max_value))
|
||||
|
||||
|
||||
class NetworkOption(Option):
|
||||
@ -874,12 +915,16 @@ class NetworkOption(Option):
|
||||
try:
|
||||
IP(value)
|
||||
except ValueError:
|
||||
raise ValueError(_('invalid network address {0}').format(self._name))
|
||||
raise ValueError(_('invalid network address'))
|
||||
|
||||
def _second_level_validation(self, value):
|
||||
def _second_level_validation(self, value, warnings_only):
|
||||
ip = IP(value)
|
||||
if ip.iptype() == 'RESERVED':
|
||||
raise ValueError(_("network shall not be in reserved class"))
|
||||
if warnings_only:
|
||||
msg = _("network address shouldn't be in reserved class")
|
||||
else:
|
||||
msg = _("invalid network address, mustn't be in reserved class")
|
||||
raise ValueError(msg)
|
||||
|
||||
|
||||
class NetmaskOption(Option):
|
||||
@ -891,21 +936,22 @@ class NetmaskOption(Option):
|
||||
try:
|
||||
IP('0.0.0.0/{0}'.format(value))
|
||||
except ValueError:
|
||||
raise ValueError(_('invalid netmask address {0}').format(self._name))
|
||||
raise ValueError(_('invalid netmask address'))
|
||||
|
||||
def _cons_network_netmask(self, opts, vals):
|
||||
def _cons_network_netmask(self, opts, vals, warnings_only):
|
||||
#opts must be (netmask, network) options
|
||||
if None in vals:
|
||||
return
|
||||
self.__cons_netmask(opts, vals[0], vals[1], False)
|
||||
self.__cons_netmask(opts, vals[0], vals[1], False, warnings_only)
|
||||
|
||||
def _cons_ip_netmask(self, opts, vals):
|
||||
def _cons_ip_netmask(self, opts, vals, warnings_only):
|
||||
#opts must be (netmask, ip) options
|
||||
if None in vals:
|
||||
return
|
||||
self.__cons_netmask(opts, vals[0], vals[1], True)
|
||||
self.__cons_netmask(opts, vals[0], vals[1], True, warnings_only)
|
||||
|
||||
def __cons_netmask(self, opts, val_netmask, val_ipnetwork, make_net):
|
||||
def __cons_netmask(self, opts, val_netmask, val_ipnetwork, make_net,
|
||||
warnings_only):
|
||||
if len(opts) != 2:
|
||||
raise ConfigError(_('invalid len for opts'))
|
||||
msg = None
|
||||
@ -918,23 +964,18 @@ class NetmaskOption(Option):
|
||||
IP('{0}/{1}'.format(val_ipnetwork, val_netmask),
|
||||
make_net=not make_net)
|
||||
except ValueError:
|
||||
if not make_net:
|
||||
msg = _("invalid network {0} ({1}) "
|
||||
"with netmask {2} ({3}),"
|
||||
" this network is an IP")
|
||||
pass
|
||||
else:
|
||||
if make_net:
|
||||
msg = _("invalid IP {0} ({1}) with netmask {2} ({3}),"
|
||||
msg = _("invalid IP {0} ({1}) with netmask {2},"
|
||||
" this IP is a network")
|
||||
|
||||
except ValueError:
|
||||
if make_net:
|
||||
msg = _("invalid IP {0} ({1}) with netmask {2} ({3})")
|
||||
else:
|
||||
msg = _("invalid network {0} ({1}) with netmask {2} ({3})")
|
||||
if not make_net:
|
||||
msg = _('invalid network {0} ({1}) with netmask {2}')
|
||||
if msg is not None:
|
||||
raise ValueError(msg.format(val_ipnetwork, opts[1]._name,
|
||||
val_netmask, self._name))
|
||||
val_netmask))
|
||||
|
||||
|
||||
class BroadcastOption(Option):
|
||||
@ -945,9 +986,9 @@ class BroadcastOption(Option):
|
||||
try:
|
||||
IP('{0}/32'.format(value))
|
||||
except ValueError:
|
||||
raise ValueError(_('invalid broadcast address {0}').format(self._name))
|
||||
raise ValueError(_('invalid broadcast address'))
|
||||
|
||||
def _cons_broadcast(self, opts, vals):
|
||||
def _cons_broadcast(self, opts, vals, warnings_only):
|
||||
if len(vals) != 3:
|
||||
raise ConfigError(_('invalid len for vals'))
|
||||
if None in vals:
|
||||
@ -967,20 +1008,44 @@ class DomainnameOption(Option):
|
||||
domainname:
|
||||
fqdn: with tld, not supported yet
|
||||
"""
|
||||
__slots__ = ('_type', '_allow_ip')
|
||||
__slots__ = ('_type', '_allow_ip', '_allow_without_dot', '_domain_re')
|
||||
_opt_type = 'domainname'
|
||||
|
||||
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, allow_ip=False, type_='domainname',
|
||||
warnings_only=False):
|
||||
warnings_only=False, allow_without_dot=False):
|
||||
if type_ not in ['netbios', 'hostname', 'domainname']:
|
||||
raise ValueError(_('unknown type_ {0} for hostname').format(type_))
|
||||
self._type = type_
|
||||
if allow_ip not in [True, False]:
|
||||
raise ValueError(_('allow_ip must be a boolean'))
|
||||
if allow_without_dot not in [True, False]:
|
||||
raise ValueError(_('allow_without_dot must be a boolean'))
|
||||
self._allow_ip = allow_ip
|
||||
self._allow_without_dot = allow_without_dot
|
||||
end = ''
|
||||
extrachar = ''
|
||||
extrachar_mandatory = ''
|
||||
if self._type != 'netbios':
|
||||
allow_number = '\d'
|
||||
else:
|
||||
allow_number = ''
|
||||
if self._type == 'netbios':
|
||||
length = 14
|
||||
elif self._type == 'hostname':
|
||||
length = 62
|
||||
elif self._type == 'domainname':
|
||||
length = 62
|
||||
if allow_without_dot is False:
|
||||
extrachar_mandatory = '\.'
|
||||
else:
|
||||
extrachar = '\.'
|
||||
end = '+[a-z]*'
|
||||
self._domain_re = re.compile(r'^(?:[a-z{0}][a-z\d\-{1}]{{,{2}}}{3}){4}$'
|
||||
''.format(allow_number, extrachar, length,
|
||||
extrachar_mandatory, end))
|
||||
super(DomainnameOption, self).__init__(name, doc, default=default,
|
||||
default_multi=default_multi,
|
||||
callback=callback,
|
||||
@ -999,29 +1064,94 @@ class DomainnameOption(Option):
|
||||
return
|
||||
except ValueError:
|
||||
pass
|
||||
if self._type == 'netbios':
|
||||
length = 15
|
||||
extrachar = ''
|
||||
elif self._type == 'hostname':
|
||||
length = 63
|
||||
extrachar = ''
|
||||
elif self._type == 'domainname':
|
||||
length = 255
|
||||
extrachar = '\.'
|
||||
if '.' not in value:
|
||||
raise ValueError(_("invalid value for {0}, must have dot"
|
||||
"").format(self._name))
|
||||
if len(value) > length:
|
||||
raise ValueError(_("invalid domainname's length for"
|
||||
" {0} (max {1})").format(self._name, length))
|
||||
if len(value) == 1:
|
||||
raise ValueError(_("invalid domainname's length for {0} (min 2)"
|
||||
"").format(self._name))
|
||||
regexp = r'^[a-z]([a-z\d{0}-])*[a-z\d]$'.format(extrachar)
|
||||
if re.match(regexp, value) is None:
|
||||
if self._type == 'domainname' and not self._allow_without_dot and \
|
||||
'.' not in value:
|
||||
raise ValueError(_("invalid domainname, must have dot"))
|
||||
if len(value) > 255:
|
||||
raise ValueError(_("invalid domainname's length (max 255)"))
|
||||
if len(value) < 2:
|
||||
raise ValueError(_("invalid domainname's length (min 2)"))
|
||||
if not self._domain_re.search(value):
|
||||
raise ValueError(_('invalid domainname'))
|
||||
|
||||
|
||||
class EmailOption(DomainnameOption):
|
||||
__slots__ = tuple()
|
||||
_opt_type = 'email'
|
||||
username_re = re.compile(r"^[\w!#$%&'*+\-/=?^`{|}~.]+$")
|
||||
|
||||
def _validate(self, value):
|
||||
splitted = value.split('@', 1)
|
||||
try:
|
||||
username, domain = splitted
|
||||
except ValueError:
|
||||
raise ValueError(_('invalid email address, must contains one @'
|
||||
))
|
||||
if not self.username_re.search(username):
|
||||
raise ValueError(_('invalid username in email address'))
|
||||
super(EmailOption, self)._validate(domain)
|
||||
|
||||
|
||||
class URLOption(DomainnameOption):
|
||||
__slots__ = tuple()
|
||||
_opt_type = 'url'
|
||||
proto_re = re.compile(r'(http|https)://')
|
||||
path_re = re.compile(r"^[a-z0-9\-\._~:/\?#\[\]@!%\$&\'\(\)\*\+,;=]+$")
|
||||
|
||||
def _validate(self, value):
|
||||
match = self.proto_re.search(value)
|
||||
if not match:
|
||||
raise ValueError(_('invalid url, must start with http:// or '
|
||||
'https://'))
|
||||
value = value[len(match.group(0)):]
|
||||
# get domain/files
|
||||
splitted = value.split('/', 1)
|
||||
try:
|
||||
domain, files = splitted
|
||||
except ValueError:
|
||||
domain = value
|
||||
files = None
|
||||
# if port in domain
|
||||
splitted = domain.split(':', 1)
|
||||
try:
|
||||
domain, port = splitted
|
||||
|
||||
except ValueError:
|
||||
domain = splitted[0]
|
||||
port = 0
|
||||
if not 0 <= int(port) <= 65535:
|
||||
raise ValueError(_('invalid url, port must be an between 0 and '
|
||||
'65536'))
|
||||
# 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'))
|
||||
|
||||
|
||||
class UsernameOption(Option):
|
||||
__slots__ = tuple()
|
||||
_opt_type = 'username'
|
||||
#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):
|
||||
match = self.username_re.search(value)
|
||||
if not match:
|
||||
raise ValueError(_('invalid username'))
|
||||
|
||||
|
||||
class FilenameOption(Option):
|
||||
__slots__ = tuple()
|
||||
_opt_type = 'file'
|
||||
path_re = re.compile(r"^[a-zA-Z0-9\-\._~/+]+$")
|
||||
|
||||
def _validate(self, value):
|
||||
match = self.path_re.search(value)
|
||||
if not match:
|
||||
raise ValueError(_('invalid filename'))
|
||||
|
||||
|
||||
class OptionDescription(BaseOption):
|
||||
"""Config's schema (organisation, group) and container of Options
|
||||
The `OptionsDescription` objects lives in the `tiramisu.config.Config`.
|
||||
@ -1125,11 +1255,12 @@ class OptionDescription(BaseOption):
|
||||
if not force_no_consistencies and \
|
||||
option._consistencies is not None:
|
||||
for consistency in option._consistencies:
|
||||
func, all_cons_opts = consistency
|
||||
func, all_cons_opts, params = consistency
|
||||
for opt in all_cons_opts:
|
||||
_consistencies.setdefault(opt,
|
||||
[]).append((func,
|
||||
all_cons_opts))
|
||||
all_cons_opts,
|
||||
params))
|
||||
else:
|
||||
_currpath.append(attr)
|
||||
option.impl_build_cache(cache_path,
|
||||
@ -1176,7 +1307,6 @@ class OptionDescription(BaseOption):
|
||||
self._group_type = group_type
|
||||
if isinstance(group_type, groups.MasterGroupType):
|
||||
#if master (same name has group) is set
|
||||
identical_master_child_name = False
|
||||
#for collect all slaves
|
||||
slaves = []
|
||||
master = None
|
||||
@ -1193,7 +1323,6 @@ class OptionDescription(BaseOption):
|
||||
": this option is not a multi"
|
||||
"").format(child._name, self._name))
|
||||
if child._name == self._name:
|
||||
identical_master_child_name = True
|
||||
child._multitype = multitypes.master
|
||||
master = child
|
||||
else:
|
||||
@ -1202,14 +1331,18 @@ class OptionDescription(BaseOption):
|
||||
raise ValueError(_('master group with wrong'
|
||||
' master name for {0}'
|
||||
).format(self._name))
|
||||
if master._callback is not None and master._callback[1] is not None:
|
||||
for key, callbacks in master._callback[1].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"))
|
||||
master._master_slaves = tuple(slaves)
|
||||
for child in self.impl_getchildren():
|
||||
if child != master:
|
||||
child._master_slaves = master
|
||||
child._multitype = multitypes.slave
|
||||
if not identical_master_child_name:
|
||||
raise ValueError(_("no child has same nom has master group"
|
||||
" for: {0}").format(self._name))
|
||||
else:
|
||||
raise ValueError(_('group_type: {0}'
|
||||
' not allowed').format(group_type))
|
||||
@ -1223,15 +1356,20 @@ class OptionDescription(BaseOption):
|
||||
#consistencies is something like [('_cons_not_equal', (opt1, opt2))]
|
||||
consistencies = self._cache_consistencies.get(option)
|
||||
if consistencies is not None:
|
||||
for func, all_cons_opts in consistencies:
|
||||
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
|
||||
ret = all_cons_opts[0]._launch_consistency(func, option,
|
||||
value,
|
||||
context, index,
|
||||
all_cons_opts)
|
||||
if ret is False:
|
||||
return False
|
||||
return True
|
||||
try:
|
||||
all_cons_opts[0]._launch_consistency(func, option,
|
||||
value,
|
||||
context, index,
|
||||
all_cons_opts,
|
||||
warnings_only)
|
||||
except ValueError as err:
|
||||
if warnings_only:
|
||||
raise ValueWarning(err.message, option)
|
||||
else:
|
||||
raise err
|
||||
|
||||
def _impl_getstate(self, descr=None):
|
||||
"""enables us to export into a dict
|
||||
@ -1341,7 +1479,7 @@ def validate_requires_arg(requires, name):
|
||||
'must be an option in option {0}').format(name))
|
||||
if option.impl_is_multi():
|
||||
raise ValueError(_('malformed requirements option {0} '
|
||||
'should not be a multi').format(name))
|
||||
'must not be a multi').format(name))
|
||||
if expected is not None:
|
||||
try:
|
||||
option._validate(expected)
|
||||
@ -1376,17 +1514,17 @@ def validate_requires_arg(requires, name):
|
||||
|
||||
def validate_callback(callback, callback_params, type_):
|
||||
if type(callback) != FunctionType:
|
||||
raise ValueError(_('{0} should be a function').format(type_))
|
||||
raise ValueError(_('{0} must be a function').format(type_))
|
||||
if callback_params is not None:
|
||||
if not isinstance(callback_params, dict):
|
||||
raise ValueError(_('{0}_params should be a dict').format(type_))
|
||||
raise ValueError(_('{0}_params must be a dict').format(type_))
|
||||
for key, callbacks in callback_params.items():
|
||||
if key != '' and len(callbacks) != 1:
|
||||
raise ValueError(_('{0}_params with key {1} should not have '
|
||||
'length different to 1').format(type_,
|
||||
raise ValueError(_("{0}_params with key {1} mustn't have "
|
||||
"length different to 1").format(type_,
|
||||
key))
|
||||
if not isinstance(callbacks, tuple):
|
||||
raise ValueError(_('{0}_params should be tuple for key "{1}"'
|
||||
raise ValueError(_('{0}_params must be tuple for key "{1}"'
|
||||
).format(type_, key))
|
||||
for callbk in callbacks:
|
||||
if isinstance(callbk, tuple):
|
||||
@ -1395,11 +1533,11 @@ def validate_callback(callback, callback_params, type_):
|
||||
raise ValueError(_('validator not support tuple'))
|
||||
if not isinstance(option, Option) and not \
|
||||
isinstance(option, SymLinkOption):
|
||||
raise ValueError(_('{0}_params should have an option '
|
||||
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]:
|
||||
raise ValueError(_('{0}_params should have a boolean'
|
||||
raise ValueError(_('{0}_params must have a boolean'
|
||||
' not a {0} for second argument'
|
||||
).format(type_, type(
|
||||
force_permissive)))
|
||||
|
@ -19,7 +19,7 @@ from time import time
|
||||
from copy import copy
|
||||
import weakref
|
||||
from tiramisu.error import (RequirementError, PropertiesOptionError,
|
||||
ConstError)
|
||||
ConstError, ConfigError)
|
||||
from tiramisu.i18n import _
|
||||
|
||||
|
||||
@ -237,6 +237,14 @@ multitypes = MultiTypeModule()
|
||||
populate_multitypes()
|
||||
|
||||
|
||||
# ____________________________________________________________
|
||||
class Undefined():
|
||||
pass
|
||||
|
||||
|
||||
undefined = Undefined()
|
||||
|
||||
|
||||
# ____________________________________________________________
|
||||
class Property(object):
|
||||
"a property is responsible of the option's value access rules"
|
||||
@ -249,6 +257,11 @@ class Property(object):
|
||||
self._properties = prop
|
||||
|
||||
def append(self, propname):
|
||||
"""Appends a property named propname
|
||||
|
||||
:param propname: a predefined or user defined property name
|
||||
:type propname: string
|
||||
"""
|
||||
if self._opt is not None and self._opt._calc_properties is not None \
|
||||
and propname in self._opt._calc_properties:
|
||||
raise ValueError(_('cannot append {0} property for option {1}: '
|
||||
@ -258,12 +271,29 @@ class Property(object):
|
||||
self._setting._setproperties(self._properties, self._opt, self._path)
|
||||
|
||||
def remove(self, propname):
|
||||
"""Removes a property named propname
|
||||
|
||||
:param propname: a predefined or user defined property name
|
||||
:type propname: string
|
||||
"""
|
||||
if propname in self._properties:
|
||||
self._properties.remove(propname)
|
||||
self._setting._setproperties(self._properties, self._opt,
|
||||
self._path)
|
||||
|
||||
def extend(self, propnames):
|
||||
"""Extends properties to the existing properties
|
||||
|
||||
:param propnames: an iterable made of property names
|
||||
:type propnames: iterable of string
|
||||
"""
|
||||
for propname in propnames:
|
||||
self.append(propname)
|
||||
|
||||
def reset(self):
|
||||
"""resets the properties (does not **clear** the properties,
|
||||
default properties are still present)
|
||||
"""
|
||||
self._setting.reset(_path=self._path)
|
||||
|
||||
def __contains__(self, propname):
|
||||
@ -275,7 +305,7 @@ class Property(object):
|
||||
|
||||
#____________________________________________________________
|
||||
class Settings(object):
|
||||
"``Config()``'s configuration options"
|
||||
"``config.Config()``'s configuration options settings"
|
||||
__slots__ = ('context', '_owner', '_p_', '__weakref__')
|
||||
|
||||
def __init__(self, context, storage):
|
||||
@ -293,6 +323,17 @@ class Settings(object):
|
||||
self.context = weakref.ref(context)
|
||||
self._p_ = storage
|
||||
|
||||
def _getcontext(self):
|
||||
"""context could be None, we need to test it
|
||||
context is None only if all reference to `Config` object is deleted
|
||||
(for example we delete a `Config` and we manipulate a reference to
|
||||
old `SubConfig`, `Values`, `Multi` or `Settings`)
|
||||
"""
|
||||
context = self.context()
|
||||
if context is None:
|
||||
raise ConfigError(_('the context does not exist anymore'))
|
||||
return context
|
||||
|
||||
#____________________________________________________________
|
||||
# properties methods
|
||||
def __contains__(self, propname):
|
||||
@ -317,16 +358,16 @@ class Settings(object):
|
||||
raise ValueError(_('opt and all_properties must not be set '
|
||||
'together in reset'))
|
||||
if all_properties:
|
||||
self._p_.reset_all_propertives()
|
||||
self._p_.reset_all_properties()
|
||||
else:
|
||||
if opt is not None and _path is None:
|
||||
_path = self._get_path_by_opt(opt)
|
||||
self._p_.reset_properties(_path)
|
||||
self.context().cfgimpl_reset_cache()
|
||||
self._getcontext().cfgimpl_reset_cache()
|
||||
|
||||
def _getproperties(self, opt=None, path=None, is_apply_req=True):
|
||||
if opt is None:
|
||||
props = self._p_.getproperties(path, default_properties)
|
||||
props = copy(self._p_.getproperties(path, default_properties))
|
||||
else:
|
||||
if path is None:
|
||||
raise ValueError(_('if opt is not None, path should not be'
|
||||
@ -337,8 +378,8 @@ class Settings(object):
|
||||
ntime = int(time())
|
||||
is_cached, props = self._p_.getcache(path, ntime)
|
||||
if is_cached:
|
||||
return props
|
||||
props = self._p_.getproperties(path, opt._properties)
|
||||
return copy(props)
|
||||
props = copy(self._p_.getproperties(path, opt._properties))
|
||||
if is_apply_req:
|
||||
props |= self.apply_requires(opt, path)
|
||||
if 'cache' in self:
|
||||
@ -346,7 +387,7 @@ class Settings(object):
|
||||
if ntime is None:
|
||||
ntime = int(time())
|
||||
ntime = ntime + expires_time
|
||||
self._p_.setcache(path, props, ntime)
|
||||
self._p_.setcache(path, copy(props), ntime)
|
||||
return props
|
||||
|
||||
def append(self, propname):
|
||||
@ -362,6 +403,10 @@ class Settings(object):
|
||||
props.remove(propname)
|
||||
self._setproperties(props, None, None)
|
||||
|
||||
def extend(self, propnames):
|
||||
for propname in propnames:
|
||||
self.append(propname)
|
||||
|
||||
def _setproperties(self, properties, opt, path):
|
||||
"""save properties for specified opt
|
||||
(never save properties if same has option properties)
|
||||
@ -375,7 +420,7 @@ class Settings(object):
|
||||
self._p_.reset_properties(path)
|
||||
else:
|
||||
self._p_.setproperties(path, properties)
|
||||
self.context().cfgimpl_reset_cache()
|
||||
self._getcontext().cfgimpl_reset_cache()
|
||||
|
||||
#____________________________________________________________
|
||||
def validate_properties(self, opt_or_descr, is_descr, is_write, path,
|
||||
@ -400,11 +445,13 @@ class Settings(object):
|
||||
(typically with the `frozen` property)
|
||||
"""
|
||||
# opt properties
|
||||
properties = copy(self._getproperties(opt_or_descr, path))
|
||||
properties = self._getproperties(opt_or_descr, path)
|
||||
self_properties = self._getproperties()
|
||||
# remove opt permissive
|
||||
# permissive affect option's permission with or without permissive
|
||||
# global property
|
||||
properties -= self._p_.getpermissive(path)
|
||||
# remove global permissive if need
|
||||
self_properties = copy(self._getproperties())
|
||||
if force_permissive is True or 'permissive' in self_properties:
|
||||
properties -= self._p_.getpermissive()
|
||||
if force_permissives is not None:
|
||||
@ -421,7 +468,7 @@ class Settings(object):
|
||||
properties -= frozenset(('mandatory', 'frozen'))
|
||||
else:
|
||||
if 'mandatory' in properties and \
|
||||
not self.context().cfgimpl_get_values()._isempty(
|
||||
not self._getcontext().cfgimpl_get_values()._isempty(
|
||||
opt_or_descr, value):
|
||||
properties.remove('mandatory')
|
||||
if is_write and 'everything_frozen' in self_properties:
|
||||
@ -544,6 +591,7 @@ class Settings(object):
|
||||
|
||||
# filters the callbacks
|
||||
calc_properties = set()
|
||||
context = self._getcontext()
|
||||
for requires in opt._requires:
|
||||
for require in requires:
|
||||
option, expected, action, inverse, \
|
||||
@ -555,8 +603,7 @@ class Settings(object):
|
||||
" '{0}' with requirement on: "
|
||||
"'{1}'").format(path, reqpath))
|
||||
try:
|
||||
value = self.context()._getattr(reqpath,
|
||||
force_permissive=True)
|
||||
value = context._getattr(reqpath, force_permissive=True)
|
||||
except PropertiesOptionError as err:
|
||||
if not transitive:
|
||||
continue
|
||||
@ -585,7 +632,7 @@ class Settings(object):
|
||||
:param opt: `Option`'s object
|
||||
:returns: path
|
||||
"""
|
||||
return self.context().cfgimpl_get_description().impl_get_path_by_opt(opt)
|
||||
return self._getcontext().cfgimpl_get_description().impl_get_path_by_opt(opt)
|
||||
|
||||
def get_modified_properties(self):
|
||||
return self._p_.get_modified_properties()
|
||||
|
@ -29,7 +29,7 @@ class Settings(Cache):
|
||||
self._permissives = {}
|
||||
super(Settings, self).__init__(storage)
|
||||
|
||||
# propertives
|
||||
# properties
|
||||
def setproperties(self, path, properties):
|
||||
self._properties[path] = properties
|
||||
|
||||
@ -39,7 +39,7 @@ class Settings(Cache):
|
||||
def hasproperties(self, path):
|
||||
return path in self._properties
|
||||
|
||||
def reset_all_propertives(self):
|
||||
def reset_all_properties(self):
|
||||
self._properties.clear()
|
||||
|
||||
def reset_properties(self, path):
|
||||
|
@ -31,7 +31,7 @@ class Settings(Sqlite3DB):
|
||||
self._storage.execute(settings_table, commit=False)
|
||||
self._storage.execute(permissives_table)
|
||||
|
||||
# propertives
|
||||
# properties
|
||||
def setproperties(self, path, properties):
|
||||
path = self._sqlite_encode_path(path)
|
||||
self._storage.execute("DELETE FROM property WHERE path = ?", (path,),
|
||||
@ -54,7 +54,7 @@ class Settings(Sqlite3DB):
|
||||
return self._storage.select("SELECT properties FROM property WHERE "
|
||||
"path = ?", (path,)) is not None
|
||||
|
||||
def reset_all_propertives(self):
|
||||
def reset_all_properties(self):
|
||||
self._storage.execute("DELETE FROM property")
|
||||
|
||||
def reset_properties(self, path):
|
||||
|
@ -19,8 +19,8 @@ from time import time
|
||||
from copy import copy
|
||||
import sys
|
||||
import weakref
|
||||
from tiramisu.error import ConfigError, SlaveError
|
||||
from tiramisu.setting import owners, multitypes, expires_time
|
||||
from tiramisu.error import ConfigError, SlaveError, PropertiesOptionError
|
||||
from tiramisu.setting import owners, multitypes, expires_time, undefined
|
||||
from tiramisu.autolib import carry_out_calculation
|
||||
from tiramisu.i18n import _
|
||||
from tiramisu.option import SymLinkOption
|
||||
@ -44,15 +44,28 @@ class Values(object):
|
||||
# the storage type is dictionary or sqlite3
|
||||
self._p_ = storage
|
||||
|
||||
def _getcontext(self):
|
||||
"""context could be None, we need to test it
|
||||
context is None only if all reference to `Config` object is deleted
|
||||
(for example we delete a `Config` and we manipulate a reference to
|
||||
old `SubConfig`, `Values`, `Multi` or `Settings`)
|
||||
"""
|
||||
context = self.context()
|
||||
if context is None:
|
||||
raise ConfigError(_('the context does not exist anymore'))
|
||||
return context
|
||||
|
||||
def _getdefault(self, opt):
|
||||
"""
|
||||
actually retrieves the default value
|
||||
|
||||
:param opt: the `option.Option()` object
|
||||
"""
|
||||
meta = self.context().cfgimpl_get_meta()
|
||||
meta = self._getcontext().cfgimpl_get_meta()
|
||||
if meta is not None:
|
||||
value = meta.cfgimpl_get_values()[opt]
|
||||
if isinstance(value, Multi):
|
||||
value = list(value)
|
||||
else:
|
||||
value = opt.impl_getdefault()
|
||||
if opt.impl_is_multi():
|
||||
@ -60,7 +73,7 @@ class Values(object):
|
||||
else:
|
||||
return value
|
||||
|
||||
def _getvalue(self, opt, path, validate=True):
|
||||
def _getvalue(self, opt, path):
|
||||
"""actually retrieves the value
|
||||
|
||||
:param opt: the `option.Option()` object
|
||||
@ -69,14 +82,9 @@ class Values(object):
|
||||
if not self._p_.hasvalue(path):
|
||||
# if there is no value
|
||||
value = self._getdefault(opt)
|
||||
if opt.impl_is_multi():
|
||||
value = Multi(value, self.context, opt, path, validate)
|
||||
else:
|
||||
# if there is a value
|
||||
value = self._p_.getvalue(path)
|
||||
if opt.impl_is_multi() and not isinstance(value, Multi):
|
||||
# load value so don't need to validate if is not a Multi
|
||||
value = Multi(value, self.context, opt, path, validate=False)
|
||||
return value
|
||||
|
||||
def get_modified_values(self):
|
||||
@ -103,11 +111,11 @@ class Values(object):
|
||||
if path is None:
|
||||
path = self._get_opt_path(opt)
|
||||
if self._p_.hasvalue(path):
|
||||
setting = self.context().cfgimpl_get_settings()
|
||||
context = self._getcontext()
|
||||
setting = context.cfgimpl_get_settings()
|
||||
opt.impl_validate(opt.impl_getdefault(),
|
||||
self.context(),
|
||||
'validator' in setting)
|
||||
self.context().cfgimpl_reset_cache()
|
||||
context, 'validator' in setting)
|
||||
context.cfgimpl_reset_cache()
|
||||
if (opt.impl_is_multi() and
|
||||
opt.impl_get_multitype() == multitypes.master):
|
||||
for slave in opt.impl_get_master_slaves():
|
||||
@ -135,7 +143,7 @@ class Values(object):
|
||||
callback, callback_params = opt._callback
|
||||
if callback_params is None:
|
||||
callback_params = {}
|
||||
return carry_out_calculation(opt._name, config=self.context(),
|
||||
return carry_out_calculation(opt, config=self._getcontext(),
|
||||
callback=callback,
|
||||
callback_params=callback_params,
|
||||
index=index, max_len=max_len)
|
||||
@ -149,7 +157,7 @@ class Values(object):
|
||||
if path is None:
|
||||
path = self._get_opt_path(opt)
|
||||
ntime = None
|
||||
setting = self.context().cfgimpl_get_settings()
|
||||
setting = self._getcontext().cfgimpl_get_settings()
|
||||
if 'cache' in setting and self._p_.hascache(path):
|
||||
if 'expire' in setting:
|
||||
ntime = int(time())
|
||||
@ -174,7 +182,8 @@ class Values(object):
|
||||
def _getitem(self, opt, path, validate, force_permissive, force_properties,
|
||||
validate_properties):
|
||||
# options with callbacks
|
||||
setting = self.context().cfgimpl_get_settings()
|
||||
context = self._getcontext()
|
||||
setting = context.cfgimpl_get_settings()
|
||||
is_frozen = 'frozen' in setting[opt]
|
||||
# For calculating properties, we need value (ie for mandatory value).
|
||||
# If value is calculating with a PropertiesOptionError's option
|
||||
@ -184,7 +193,7 @@ class Values(object):
|
||||
# ConfigError if properties did not raise.
|
||||
config_error = None
|
||||
force_permissives = None
|
||||
# if value is callback and is not set
|
||||
# if value has callback and is not set
|
||||
# or frozen with force_default_on_freeze
|
||||
if opt.impl_has_callback() and (
|
||||
self._is_default_owner(path) or
|
||||
@ -194,7 +203,7 @@ class Values(object):
|
||||
if (opt.impl_is_multi() and
|
||||
opt.impl_get_multitype() == multitypes.slave):
|
||||
masterp = self._get_opt_path(opt.impl_get_master_slaves())
|
||||
mastervalue = getattr(self.context(), masterp)
|
||||
mastervalue = context._getattr(masterp, validate=validate)
|
||||
lenmaster = len(mastervalue)
|
||||
if lenmaster == 0:
|
||||
value = []
|
||||
@ -226,9 +235,12 @@ class Values(object):
|
||||
if opt.impl_is_multi():
|
||||
value = Multi(value, self.context, opt, path, validate)
|
||||
else:
|
||||
value = self._getvalue(opt, path, validate)
|
||||
value = self._getvalue(opt, path)
|
||||
if opt.impl_is_multi():
|
||||
# load value so don't need to validate if is not a Multi
|
||||
value = Multi(value, self.context, opt, path, validate=validate)
|
||||
if config_error is None and validate:
|
||||
opt.impl_validate(value, self.context(), 'validator' in setting)
|
||||
opt.impl_validate(value, context, 'validator' in setting)
|
||||
if config_error is None and self._is_default_owner(path) and \
|
||||
'force_store_value' in setting[opt]:
|
||||
self.setitem(opt, value, path, is_write=False)
|
||||
@ -249,24 +261,45 @@ class Values(object):
|
||||
# is_write is, for example, used with "force_store_value"
|
||||
# user didn't change value, so not write
|
||||
# valid opt
|
||||
opt.impl_validate(value, self.context(),
|
||||
'validator' in self.context().cfgimpl_get_settings())
|
||||
if opt.impl_is_multi() and not isinstance(value, Multi):
|
||||
context = self._getcontext()
|
||||
opt.impl_validate(value, context,
|
||||
'validator' in context.cfgimpl_get_settings())
|
||||
if opt.impl_is_multi():
|
||||
value = Multi(value, self.context, opt, path, setitem=True)
|
||||
# Save old value
|
||||
if opt.impl_get_multitype() == multitypes.master and \
|
||||
self._p_.hasvalue(path):
|
||||
old_value = self._p_.getvalue(path)
|
||||
old_owner = self._p_.getowner(path, None)
|
||||
else:
|
||||
old_value = undefined
|
||||
old_owner = undefined
|
||||
self._setvalue(opt, path, value, force_permissive=force_permissive,
|
||||
is_write=is_write)
|
||||
if opt.impl_is_multi() and opt.impl_get_multitype() == multitypes.master:
|
||||
try:
|
||||
value._valid_master()
|
||||
except Exception, err:
|
||||
if old_value is not undefined:
|
||||
self._p_.setvalue(path, old_value, old_owner)
|
||||
else:
|
||||
self._p_.resetvalue(path)
|
||||
raise err
|
||||
|
||||
def _setvalue(self, opt, path, value, force_permissive=False,
|
||||
force_properties=None,
|
||||
is_write=True, validate_properties=True):
|
||||
self.context().cfgimpl_reset_cache()
|
||||
context = self._getcontext()
|
||||
context.cfgimpl_reset_cache()
|
||||
if validate_properties:
|
||||
setting = self.context().cfgimpl_get_settings()
|
||||
setting = context.cfgimpl_get_settings()
|
||||
setting.validate_properties(opt, False, is_write,
|
||||
value=value, path=path,
|
||||
force_permissive=force_permissive,
|
||||
force_properties=force_properties)
|
||||
owner = self.context().cfgimpl_get_settings().getowner()
|
||||
owner = context.cfgimpl_get_settings().getowner()
|
||||
if isinstance(value, Multi):
|
||||
value = list(value)
|
||||
self._p_.setvalue(path, value, owner)
|
||||
|
||||
def getowner(self, opt):
|
||||
@ -283,7 +316,7 @@ class Values(object):
|
||||
|
||||
def _getowner(self, path):
|
||||
owner = self._p_.getowner(path, owners.default)
|
||||
meta = self.context().cfgimpl_get_meta()
|
||||
meta = self._getcontext().cfgimpl_get_meta()
|
||||
if owner is owners.default and meta is not None:
|
||||
owner = meta.cfgimpl_get_values()._getowner(path)
|
||||
return owner
|
||||
@ -335,7 +368,7 @@ class Values(object):
|
||||
:param opt: the `option.Option` object
|
||||
:returns: a string with points like "gc.dummy.my_option"
|
||||
"""
|
||||
return self.context().cfgimpl_get_description().impl_get_path_by_opt(opt)
|
||||
return self._getcontext().cfgimpl_get_description().impl_get_path_by_opt(opt)
|
||||
|
||||
# information
|
||||
def set_information(self, key, value):
|
||||
@ -360,6 +393,42 @@ class Values(object):
|
||||
raise ValueError(_("information's item"
|
||||
" not found: {0}").format(key))
|
||||
|
||||
def mandatory_warnings(self):
|
||||
"""convenience function to trace Options that are mandatory and
|
||||
where no value has been set
|
||||
|
||||
:returns: generator of mandatory Option's path
|
||||
|
||||
"""
|
||||
#if value in cache, properties are not calculated
|
||||
self.reset_cache(False)
|
||||
context = self.context()
|
||||
for path in context.cfgimpl_get_description().impl_getpaths(
|
||||
include_groups=True):
|
||||
try:
|
||||
context._getattr(path,
|
||||
force_properties=frozenset(('mandatory',)))
|
||||
except PropertiesOptionError as err:
|
||||
if err.proptype == ['mandatory']:
|
||||
yield path
|
||||
self.reset_cache(False)
|
||||
|
||||
def force_cache(self):
|
||||
"""parse all option to force data in cache
|
||||
"""
|
||||
context = self.context()
|
||||
if not 'cache' in context.cfgimpl_get_settings():
|
||||
raise ConfigError(_('can force cache only if cache '
|
||||
'is actived in config'))
|
||||
#remove all cached properties and value to update "expired" time
|
||||
context.cfgimpl_reset_cache()
|
||||
for path in context.cfgimpl_get_description().impl_getpaths(
|
||||
include_groups=True):
|
||||
try:
|
||||
context._getattr(path)
|
||||
except PropertiesOptionError:
|
||||
pass
|
||||
|
||||
def __getstate__(self):
|
||||
return {'_p_': self._p_}
|
||||
|
||||
@ -387,6 +456,8 @@ class Multi(list):
|
||||
:param opt: the option object that have this Multi value
|
||||
:param setitem: only if set a value
|
||||
"""
|
||||
if isinstance(value, Multi):
|
||||
raise ValueError(_('{0} is already a Multi ').format(opt._name))
|
||||
self.opt = opt
|
||||
self.path = path
|
||||
if not isinstance(context, weakref.ReferenceType):
|
||||
@ -396,21 +467,32 @@ class Multi(list):
|
||||
value = [value]
|
||||
if validate and self.opt.impl_get_multitype() == multitypes.slave:
|
||||
value = self._valid_slave(value, setitem)
|
||||
elif validate and self.opt.impl_get_multitype() == multitypes.master:
|
||||
self._valid_master(value)
|
||||
elif not setitem and validate and \
|
||||
self.opt.impl_get_multitype() == multitypes.master:
|
||||
self._valid_master()
|
||||
super(Multi, self).__init__(value)
|
||||
|
||||
def _getcontext(self):
|
||||
"""context could be None, we need to test it
|
||||
context is None only if all reference to `Config` object is deleted
|
||||
(for example we delete a `Config` and we manipulate a reference to
|
||||
old `SubConfig`, `Values`, `Multi` or `Settings`)
|
||||
"""
|
||||
context = self.context()
|
||||
if context is None:
|
||||
raise ConfigError(_('the context does not exist anymore'))
|
||||
return context
|
||||
|
||||
def _valid_slave(self, value, setitem):
|
||||
#if slave, had values until master's one
|
||||
values = self.context().cfgimpl_get_values()
|
||||
masterp = self.context().cfgimpl_get_description().impl_get_path_by_opt(
|
||||
context = self._getcontext()
|
||||
values = context.cfgimpl_get_values()
|
||||
masterp = context.cfgimpl_get_description().impl_get_path_by_opt(
|
||||
self.opt.impl_get_master_slaves())
|
||||
mastervalue = getattr(self.context(), masterp)
|
||||
mastervalue = context._getattr(masterp, validate=False)
|
||||
masterlen = len(mastervalue)
|
||||
valuelen = len(value)
|
||||
is_default_owner = not values._is_default_owner(self.path) or setitem
|
||||
if valuelen > masterlen or (valuelen < masterlen and
|
||||
is_default_owner):
|
||||
if valuelen > masterlen or (valuelen < masterlen and setitem):
|
||||
raise SlaveError(_("invalid len for the slave: {0}"
|
||||
" which has {1} as master").format(
|
||||
self.opt._name, masterp))
|
||||
@ -427,59 +509,43 @@ class Multi(list):
|
||||
#else: same len so do nothing
|
||||
return value
|
||||
|
||||
def _valid_master(self, value):
|
||||
masterlen = len(value)
|
||||
values = self.context().cfgimpl_get_values()
|
||||
def _valid_master(self):
|
||||
#masterlen = len(value)
|
||||
values = self._getcontext().cfgimpl_get_values()
|
||||
for slave in self.opt._master_slaves:
|
||||
path = values._get_opt_path(slave)
|
||||
if not values._is_default_owner(path):
|
||||
value_slave = values._getvalue(slave, path)
|
||||
if len(value_slave) > masterlen:
|
||||
raise SlaveError(_("invalid len for the master: {0}"
|
||||
" which has {1} as slave with"
|
||||
" greater len").format(
|
||||
self.opt._name, slave._name))
|
||||
elif len(value_slave) < masterlen:
|
||||
for num in range(0, masterlen - len(value_slave)):
|
||||
if slave.impl_has_callback():
|
||||
# if callback add a value, but this value will not
|
||||
# change anymore automaticly (because this value
|
||||
# has owner)
|
||||
index = value_slave.__len__()
|
||||
value_slave.append(
|
||||
values._getcallback_value(slave, index=index),
|
||||
force=True)
|
||||
else:
|
||||
value_slave.append(slave.impl_getdefault_multi(),
|
||||
force=True)
|
||||
Multi(values._getvalue(slave, path), self.context, slave, path)
|
||||
|
||||
def __setitem__(self, index, value):
|
||||
self._validate(value, index)
|
||||
#assume not checking mandatory property
|
||||
super(Multi, self).__setitem__(index, value)
|
||||
self.context().cfgimpl_get_values()._setvalue(self.opt, self.path, self)
|
||||
self._getcontext().cfgimpl_get_values()._setvalue(self.opt, self.path, self)
|
||||
|
||||
def append(self, value, force=False):
|
||||
def append(self, value=undefined, force=False):
|
||||
"""the list value can be updated (appened)
|
||||
only if the option is a master
|
||||
"""
|
||||
context = self._getcontext()
|
||||
if not force:
|
||||
if self.opt.impl_get_multitype() == multitypes.slave:
|
||||
raise SlaveError(_("cannot append a value on a multi option {0}"
|
||||
" which is a slave").format(self.opt._name))
|
||||
elif self.opt.impl_get_multitype() == multitypes.master:
|
||||
values = self.context().cfgimpl_get_values()
|
||||
if value is None and self.opt.impl_has_callback():
|
||||
values = context.cfgimpl_get_values()
|
||||
if value is undefined and self.opt.impl_has_callback():
|
||||
value = values._getcallback_value(self.opt)
|
||||
#Force None il return a list
|
||||
if isinstance(value, list):
|
||||
value = None
|
||||
index = self.__len__()
|
||||
if value is undefined:
|
||||
value = self.opt.impl_getdefault_multi()
|
||||
self._validate(value, index)
|
||||
super(Multi, self).append(value)
|
||||
self.context().cfgimpl_get_values()._setvalue(self.opt, self.path,
|
||||
self,
|
||||
validate_properties=not force)
|
||||
context.cfgimpl_get_values()._setvalue(self.opt, self.path,
|
||||
self,
|
||||
validate_properties=not force)
|
||||
if not force and self.opt.impl_get_multitype() == multitypes.master:
|
||||
for slave in self.opt.impl_get_master_slaves():
|
||||
path = values._get_opt_path(slave)
|
||||
@ -488,16 +554,15 @@ class Multi(list):
|
||||
dvalue = values._getcallback_value(slave, index=index)
|
||||
else:
|
||||
dvalue = slave.impl_getdefault_multi()
|
||||
old_value = values.getitem(slave, path,
|
||||
old_value = values.getitem(slave, path, validate=False,
|
||||
validate_properties=False)
|
||||
if len(old_value) < self.__len__():
|
||||
values.getitem(slave, path,
|
||||
validate_properties=False).append(
|
||||
dvalue, force=True)
|
||||
else:
|
||||
values.getitem(slave, path,
|
||||
validate_properties=False)[
|
||||
index] = dvalue
|
||||
if len(old_value) + 1 != self.__len__():
|
||||
raise SlaveError(_("invalid len for the slave: {0}"
|
||||
" which has {1} as master").format(
|
||||
self.opt._name, self.__len__()))
|
||||
values.getitem(slave, path, validate=False,
|
||||
validate_properties=False).append(
|
||||
dvalue, force=True)
|
||||
|
||||
def sort(self, cmp=None, key=None, reverse=False):
|
||||
if self.opt.impl_get_multitype() in [multitypes.slave,
|
||||
@ -510,7 +575,7 @@ class Multi(list):
|
||||
super(Multi, self).sort(key=key, reverse=reverse)
|
||||
else:
|
||||
super(Multi, self).sort(cmp=cmp, key=key, reverse=reverse)
|
||||
self.context().cfgimpl_get_values()._setvalue(self.opt, self.path, self)
|
||||
self._getcontext().cfgimpl_get_values()._setvalue(self.opt, self.path, self)
|
||||
|
||||
def reverse(self):
|
||||
if self.opt.impl_get_multitype() in [multitypes.slave,
|
||||
@ -518,7 +583,7 @@ class Multi(list):
|
||||
raise SlaveError(_("cannot reverse multi option {0} if master or "
|
||||
"slave").format(self.opt._name))
|
||||
super(Multi, self).reverse()
|
||||
self.context().cfgimpl_get_values()._setvalue(self.opt, self.path, self)
|
||||
self._getcontext().cfgimpl_get_values()._setvalue(self.opt, self.path, self)
|
||||
|
||||
def insert(self, index, obj):
|
||||
if self.opt.impl_get_multitype() in [multitypes.slave,
|
||||
@ -526,7 +591,7 @@ class Multi(list):
|
||||
raise SlaveError(_("cannot insert multi option {0} if master or "
|
||||
"slave").format(self.opt._name))
|
||||
super(Multi, self).insert(index, obj)
|
||||
self.context().cfgimpl_get_values()._setvalue(self.opt, self.path, self)
|
||||
self._getcontext().cfgimpl_get_values()._setvalue(self.opt, self.path, self)
|
||||
|
||||
def extend(self, iterable):
|
||||
if self.opt.impl_get_multitype() in [multitypes.slave,
|
||||
@ -534,12 +599,12 @@ class Multi(list):
|
||||
raise SlaveError(_("cannot extend multi option {0} if master or "
|
||||
"slave").format(self.opt._name))
|
||||
super(Multi, self).extend(iterable)
|
||||
self.context().cfgimpl_get_values()._setvalue(self.opt, self.path, self)
|
||||
self._getcontext().cfgimpl_get_values()._setvalue(self.opt, self.path, self)
|
||||
|
||||
def _validate(self, value, force_index):
|
||||
if value is not None:
|
||||
try:
|
||||
self.opt.impl_validate(value, context=self.context(),
|
||||
self.opt.impl_validate(value, context=self._getcontext(),
|
||||
force_index=force_index)
|
||||
except ValueError as err:
|
||||
raise ValueError(_("invalid value {0} "
|
||||
@ -557,19 +622,20 @@ class Multi(list):
|
||||
:type force: boolean
|
||||
:returns: item at index
|
||||
"""
|
||||
context = self._getcontext()
|
||||
if not force:
|
||||
if self.opt.impl_get_multitype() == multitypes.slave:
|
||||
raise SlaveError(_("cannot pop a value on a multi option {0}"
|
||||
" which is a slave").format(self.opt._name))
|
||||
elif self.opt.impl_get_multitype() == multitypes.master:
|
||||
if self.opt.impl_get_multitype() == multitypes.master:
|
||||
for slave in self.opt.impl_get_master_slaves():
|
||||
values = self.context().cfgimpl_get_values()
|
||||
values = context.cfgimpl_get_values()
|
||||
if not values.is_default_owner(slave):
|
||||
#get multi without valid properties
|
||||
values.getitem(slave,
|
||||
values.getitem(slave, validate=False,
|
||||
validate_properties=False
|
||||
).pop(index, force=True)
|
||||
#set value without valid properties
|
||||
ret = super(Multi, self).pop(index)
|
||||
self.context().cfgimpl_get_values()._setvalue(self.opt, self.path, self, validate_properties=not force)
|
||||
context.cfgimpl_get_values()._setvalue(self.opt, self.path, self, validate_properties=not force)
|
||||
return ret
|
||||
|
Reference in New Issue
Block a user