Merge branch 'master' of ssh://git.labs.libre-entreprise.org/gitroot/tiramisu
This commit is contained in:
@@ -152,7 +152,10 @@ def carry_out_calculation(option, config, callback, callback_params,
|
||||
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
|
||||
|
@@ -48,7 +48,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
|
||||
@@ -149,8 +149,6 @@ class SubConfig(object):
|
||||
except UnicodeEncodeError:
|
||||
lines.append("{0} = {1}".format(name,
|
||||
value.encode(default_encoding)))
|
||||
except PropertiesOptionError:
|
||||
pass
|
||||
return '\n'.join(lines)
|
||||
|
||||
__repr__ = __str__
|
||||
@@ -169,7 +167,7 @@ class SubConfig(object):
|
||||
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
|
||||
|
||||
@@ -298,12 +296,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():
|
||||
@@ -454,22 +449,16 @@ class SubConfig(object):
|
||||
|
||||
def _make_sub_dict(self, opt, path, pathsvalues, _currpath, flatten):
|
||||
if isinstance(opt, OptionDescription):
|
||||
try:
|
||||
pathsvalues += getattr(self, path).make_dict(flatten,
|
||||
_currpath +
|
||||
path.split('.'))
|
||||
except PropertiesOptionError:
|
||||
pass # this just a hidden or disabled option
|
||||
pathsvalues += getattr(self, path).make_dict(flatten,
|
||||
_currpath +
|
||||
path.split('.'))
|
||||
else:
|
||||
try:
|
||||
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
|
||||
value = self._getattr(opt._name)
|
||||
if flatten:
|
||||
name = opt._name
|
||||
else:
|
||||
name = '.'.join(_currpath + [opt._name])
|
||||
pathsvalues.append((name, value))
|
||||
|
||||
def cfgimpl_get_path(self):
|
||||
descr = self.cfgimpl_get_description()
|
||||
@@ -477,9 +466,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()
|
||||
@@ -518,7 +507,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):
|
||||
@@ -536,11 +526,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
|
||||
@@ -564,41 +591,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')):
|
||||
@@ -608,99 +600,119 @@ 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):
|
||||
@@ -719,6 +731,4 @@ def mandatory_warnings(config):
|
||||
except PropertiesOptionError as err:
|
||||
if err.proptype == ['mandatory']:
|
||||
yield path
|
||||
except ConfigError:
|
||||
pass
|
||||
config.cfgimpl_reset_cache(only=('values',))
|
||||
|
@@ -354,9 +354,6 @@ class Option(BaseOption):
|
||||
"""
|
||||
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:
|
||||
@@ -587,31 +584,15 @@ 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)))
|
||||
if load:
|
||||
del(self._state_consistencies)
|
||||
self._consistencies = new_value
|
||||
@@ -663,7 +644,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))
|
||||
@@ -783,9 +764,13 @@ class IPOption(Option):
|
||||
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
|
||||
for val in value.split('.'):
|
||||
if val.startswith("0") and len(val) > 1:
|
||||
raise ValueError(_('invalid IP'))
|
||||
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))
|
||||
@@ -857,18 +842,23 @@ class PortOption(Option):
|
||||
if self._allow_range and ":" in str(value):
|
||||
value = str(value).split(':')
|
||||
if len(value) != 2:
|
||||
raise ValueError('invalid part, range must have two values '
|
||||
'only')
|
||||
raise ValueError(_('invalid part, range must have two values '
|
||||
'only'))
|
||||
if not value[0] < value[1]:
|
||||
raise ValueError('invalid port, 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('invalid 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):
|
||||
@@ -924,19 +914,14 @@ 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},"
|
||||
" this network is an IP")
|
||||
pass
|
||||
else:
|
||||
if make_net:
|
||||
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}')
|
||||
else:
|
||||
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,
|
||||
@@ -1032,8 +1017,8 @@ class DomainnameOption(Option):
|
||||
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) > 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):
|
||||
@@ -1310,13 +1295,10 @@ class OptionDescription(BaseOption):
|
||||
if consistencies is not None:
|
||||
for func, all_cons_opts in consistencies:
|
||||
#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
|
||||
all_cons_opts[0]._launch_consistency(func, option,
|
||||
value,
|
||||
context, index,
|
||||
all_cons_opts)
|
||||
|
||||
def _impl_getstate(self, descr=None):
|
||||
"""enables us to export into a dict
|
||||
|
@@ -66,6 +66,8 @@ class Values(object):
|
||||
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():
|
||||
@@ -73,7 +75,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
|
||||
@@ -82,14 +84,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):
|
||||
@@ -198,7 +195,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
|
||||
@@ -208,7 +205,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(context, masterp)
|
||||
mastervalue = context._getattr(masterp, validate=validate)
|
||||
lenmaster = len(mastervalue)
|
||||
if lenmaster == 0:
|
||||
value = []
|
||||
@@ -240,7 +237,10 @@ 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, context, 'validator' in setting)
|
||||
if config_error is None and self._is_default_owner(path) and \
|
||||
@@ -266,10 +266,27 @@ class Values(object):
|
||||
context = self._getcontext()
|
||||
opt.impl_validate(value, context,
|
||||
'validator' in context.cfgimpl_get_settings())
|
||||
if opt.impl_is_multi() and not isinstance(value, Multi):
|
||||
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,
|
||||
@@ -283,6 +300,8 @@ class Values(object):
|
||||
force_permissive=force_permissive,
|
||||
force_properties=force_properties)
|
||||
owner = context.cfgimpl_get_settings().getowner()
|
||||
if isinstance(value, Multi):
|
||||
value = list(value)
|
||||
self._p_.setvalue(path, value, owner)
|
||||
|
||||
def getowner(self, opt):
|
||||
@@ -403,6 +422,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):
|
||||
@@ -412,8 +433,9 @@ 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):
|
||||
@@ -433,12 +455,10 @@ class Multi(list):
|
||||
values = context.cfgimpl_get_values()
|
||||
masterp = context.cfgimpl_get_description().impl_get_path_by_opt(
|
||||
self.opt.impl_get_master_slaves())
|
||||
mastervalue = getattr(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))
|
||||
@@ -455,30 +475,12 @@ class Multi(list):
|
||||
#else: same len so do nothing
|
||||
return value
|
||||
|
||||
def _valid_master(self, value):
|
||||
masterlen = len(value)
|
||||
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(undefined, force=True)
|
||||
Multi(values._getvalue(slave, path), self.context, slave, path)
|
||||
|
||||
def __setitem__(self, index, value):
|
||||
self._validate(value, index)
|
||||
@@ -518,16 +520,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,
|
||||
@@ -592,12 +593,12 @@ class Multi(list):
|
||||
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 = 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
|
||||
|
Reference in New Issue
Block a user