use python-coverage to delete dead part of code

This commit is contained in:
2015-11-24 10:58:19 +01:00
parent 54ca54e505
commit ab555966f7
16 changed files with 409 additions and 269 deletions

View File

@ -140,6 +140,7 @@ class Base(StorageBase):
default = []
self.impl_validate(default)
self._set_default_values(default, default_multi)
#callback is False in optiondescription
if callback is not False:
self.impl_set_callback(callback, callback_params)
self.commit()
@ -184,7 +185,7 @@ class BaseOption(Base):
:param load: `True` if we are at the init of the option description
:type load: bool
"""
if not load and self.impl_getrequires() is None:
if not load and self.impl_getrequires() == []:
self._state_requires = None
elif load and self._state_requires is None:
del(self._state_requires)
@ -215,7 +216,7 @@ class BaseOption(Base):
if self.__class__.__name__ == 'OptionDescription' or \
isinstance(self, SymLinkOption):
return
if not load and self.impl_get_callback() is None:
if not load and self.impl_get_callback() == (None, {}):
self._state_callback = None
self._state_callback_params = {}
elif load and self._state_callback is None:
@ -228,22 +229,19 @@ class BaseOption(Base):
else:
callback, callback_params = self.impl_get_callback()
self._state_callback_params = {}
if callback_params is not None:
cllbck_prms = {}
for key, values in callback_params.items():
vls = []
for value in values:
if isinstance(value, tuple) and value[0] is not None:
if load:
value = (descr.impl_get_opt_by_path(value[0]),
value[1])
else:
value = (descr.impl_get_path_by_opt(value[0]),
value[1])
vls.append(value)
cllbck_prms[key] = tuple(vls)
else:
cllbck_prms = None
cllbck_prms = {}
for key, values in callback_params.items():
vls = []
for value in values:
if isinstance(value, tuple) and value[0] is not None:
if load:
value = (descr.impl_get_opt_by_path(value[0]),
value[1])
else:
value = (descr.impl_get_path_by_opt(value[0]),
value[1])
vls.append(value)
cllbck_prms[key] = tuple(vls)
if load:
del(self._state_callback)
@ -354,12 +352,7 @@ class BaseOption(Base):
except (KeyError, AttributeError):
pass
elif name == '_opt':
try:
if self._impl_getopt() is not None:
#so _opt is already set
is_readonly = True
except (KeyError, AttributeError):
pass
pass
elif name != '_readonly':
is_readonly = self.impl_is_readonly()
if is_readonly: # pragma: optional cover
@ -470,21 +463,10 @@ class Option(OnlyOption):
try:
all_cons_vals.append(opt_value[index])
except IndexError, err:
log.debug('indexerror in _launch_consistency: {0}'.format(err))
#value is not already set, could be higher index
#so return if no value and not default_value
if context is not undefined:
if isinstance(opt, DynSymLinkOption):
path = opt.impl_getpath(context)
else:
path = descr.impl_get_path_by_opt(opt)
default_value = context.cfgimpl_get_values()._getvalue(opt, path, True, index=index)
else:
default_value = opt.impl_getdefault_multi()
if default_value is not None:
all_cons_vals.append(default_value)
else:
return
log.debug('indexerror in _launch_consistency: {0}'.format(err))
return
except PropertiesOptionError as err:
log.debug('propertyerror in _launch_consistency: {0}'.format(err))
if transitive:
@ -563,16 +545,12 @@ class Option(OnlyOption):
if self._is_warnings_only():
warning = error
error = None
except ValueWarning as warning:
log.debug(_('do_validation for {0}: warning in value').format(
self.impl_getname()), exc_info=True)
pass
if error is None and warning is None:
try:
# if context launch consistency validation
if context is not undefined:
descr._valid_consistency(current_opt, _value, context,
_index, submulti_index)
#if context is not undefined:
self._valid_consistency(current_opt, _value, context,
_index, submulti_index)
except ValueError as error:
log.debug(_('do_validation for {0}: error in consistency').format(
self.impl_getname()), exc_info=True)
@ -584,7 +562,7 @@ class Option(OnlyOption):
if warning:
msg = _("warning on the value of the option {0}: {1}").format(
self.impl_getname(), warning)
if context is None or 'warnings' in \
if context is undefined or 'warnings' in \
context.cfgimpl_get_settings():
warnings.warn_explicit(ValueWarning(msg, self),
ValueWarning,
@ -594,8 +572,8 @@ class Option(OnlyOption):
self.impl_getname(), error))
# generic calculation
if context is not undefined:
descr = context.cfgimpl_get_description()
#if context is not undefined:
# descr = context.cfgimpl_get_description()
if not self.impl_is_multi():
do_validation(value, None, None)
@ -656,33 +634,7 @@ class Option(OnlyOption):
"accesses the Option's doc"
return self.impl_get_information('doc')
#def impl_getkey(self, value):
# return value
def impl_add_consistency(self, func, *other_opts, **params):
"""Add consistency means that value will be validate with other_opts
option's values.
:param func: function's name
:type func: `str`
:param other_opts: options used to validate value
:type other_opts: `list` of `tiramisu.option.Option`
:param params: extra params (warnings_only and transitive are allowed)
"""
if self.impl_is_readonly(): # pragma: optional cover
raise AttributeError(_("'{0}' ({1}) cannot add consistency, option is"
" read-only").format(
self.__class__.__name__,
self.impl_getname()))
warnings_only = False
transitive = True
for key, value in params.items():
if key == 'warnings_only':
warnings_only = value
elif key == 'transitive':
transitive = value
else:
raise ValueError(_('unknow parameter {0} in consistency').format(key))
def _valid_consistencies(self, other_opts):
if self._is_subdyn():
dynod = self._impl_getsubdyn()
else:
@ -706,31 +658,75 @@ class Option(OnlyOption):
if self.impl_is_multi() != opt.impl_is_multi(): # pragma: optional cover
raise ConfigError(_('every options in consistency must be '
'multi or none'))
def impl_add_consistency(self, func, *other_opts, **params):
"""Add consistency means that value will be validate with other_opts
option's values.
:param func: function's name
:type func: `str`
:param other_opts: options used to validate value
:type other_opts: `list` of `tiramisu.option.Option`
:param params: extra params (warnings_only and transitive are allowed)
"""
if self.impl_is_readonly(): # pragma: optional cover
raise AttributeError(_("'{0}' ({1}) cannot add consistency, option is"
" read-only").format(
self.__class__.__name__,
self.impl_getname()))
self._valid_consistencies(other_opts)
func = '_cons_{0}'.format(func)
if func not in dir(self):
raise ConfigError(_('consistency {0} not available for this option').format(func))
all_cons_opts = tuple([self] + list(other_opts))
value = self.impl_getdefault()
if value is not None:
if self.impl_is_multi():
for idx, val in enumerate(value):
if not self.impl_is_submulti():
self._launch_consistency(func, self, val, undefined, idx,
None, all_cons_opts,
warnings_only, transitive)
else:
for slave_idx, val in enumerate(value):
self._launch_consistency(func, self, val, None,
idx, slave_idx,
all_cons_opts,
warnings_only, transitive)
else:
self._launch_consistency(func, self, value, undefined, None,
None, all_cons_opts, warnings_only,
transitive)
unknown_params = set(params.keys()) - set(['warnings_only', 'transitive'])
if unknown_params != set():
raise ValueError(_('unknow parameter {0} in consistency').format(unknown_params))
self._add_consistency(func, all_cons_opts, params)
#re validate default value when add consistency
self.impl_validate(self.impl_getdefault())
#validate default value when add consistency
try:
self.impl_validate(self.impl_getdefault())
except ValueError, err:
self._del_consistency()
raise err
def _valid_consistency(self, option, value, context, index, submulti_idx):
if context is not undefined:
descr = context.cfgimpl_get_description()
if descr._cache_consistencies is None:
return True
#consistencies is something like [('_cons_not_equal', (opt1, opt2))]
if isinstance(option, DynSymLinkOption):
consistencies = descr._cache_consistencies.get(option._impl_getopt())
else:
consistencies = descr._cache_consistencies.get(option)
else:
consistencies = option._get_consistencies()
if consistencies is not None:
for func, all_cons_opts, params in consistencies:
warnings_only = params.get('warnings_only', False)
transitive = params.get('transitive', True)
#all_cons_opts[0] is the option where func is set
if isinstance(option, DynSymLinkOption):
subpath = '.'.join(option._dyn.split('.')[:-1])
namelen = len(option._impl_getopt().impl_getname())
suffix = option.impl_getname()[namelen:]
opts = []
for opt in all_cons_opts:
name = opt.impl_getname() + suffix
path = subpath + '.' + name
opts.append(opt._impl_to_dyn(name, path))
else:
opts = all_cons_opts
try:
opts[0]._launch_consistency(func, option, value, context,
index, submulti_idx, opts,
warnings_only, transitive)
except ValueError as err:
if warnings_only:
raise ValueWarning(err.message, option)
else:
raise err
def _cons_not_equal(self, opts, vals, warnings_only):
for idx_inf, val_inf in enumerate(vals):
@ -740,6 +736,7 @@ class Option(OnlyOption):
msg = _("same value for {0} and {1}, should be different")
else:
msg = _("same value for {0} and {1}, must be different")
log.debug('_cons_not_equal: {0} and {1} are not different'.format(val_inf, val_sup))
raise ValueError(msg.format(opts[idx_inf].impl_getname(),
opts[idx_inf + idx_sup + 1].impl_getname()))
@ -753,7 +750,7 @@ class Option(OnlyOption):
:param load: `True` if we are at the init of the option description
:type load: bool
"""
if not load and self._get_consistencies() is None:
if not load and self._get_consistencies() == ():
self._state_consistencies = None
elif load and self._state_consistencies is None:
del(self._state_consistencies)
@ -787,10 +784,7 @@ class Option(OnlyOption):
def _validate_callback(self, callback, callback_params):
if callback is None:
return
try:
default_multi = self.impl_getdefault_multi()
except AttributeError:
default_multi = None
default_multi = self.impl_getdefault_multi()
if (not self.impl_is_multi() and (self.impl_getdefault() is not None or
default_multi is not None)) or \
(self.impl_is_multi() and (self.impl_getdefault() != [] or
@ -808,8 +802,6 @@ def validate_requires_arg(requires, name):
know more about
the description of the requires dictionary
"""
if requires is None:
return None, None
ret_requires = {}
config_action = {}

View File

@ -173,13 +173,9 @@ class IPOption(Option):
# sometimes an ip term starts with a zero
# but this does not fit in some case, for example bind does not like it
self._impl_valid_unicode(value)
try:
for val in value.split('.'):
if val.startswith("0") and len(val) > 1:
raise ValueError(_('invalid IP')) # pragma: optional cover
except AttributeError: # pragma: optional cover
#if integer for example
raise ValueError(_('invalid IP'))
for val in value.split('.'):
if val.startswith("0") and len(val) > 1:
raise ValueError(_('invalid IP')) # pragma: optional cover
# 'standard' validation
try:
IP('{0}/32'.format(value))
@ -432,6 +428,7 @@ class DomainnameOption(Option):
def _validate(self, value, context=undefined):
self._impl_valid_unicode(value)
def _valid_length(val):
if len(val) < 1:
raise ValueError(_("invalid domainname's length (min 1)"))

View File

@ -24,10 +24,9 @@ import re
from tiramisu.i18n import _
from tiramisu.setting import groups, undefined # , log
from .baseoption import BaseOption, DynSymLinkOption, SymLinkOption, \
allowed_character
from .baseoption import BaseOption, SymLinkOption, allowed_character
from . import MasterSlaves
from tiramisu.error import ConfigError, ConflictError, ValueWarning
from tiramisu.error import ConfigError, ConflictError
from tiramisu.storage import get_storages_option
from tiramisu.autolib import carry_out_calculation
@ -103,6 +102,7 @@ class OptionDescription(BaseOption, StorageOptionDescription):
cache_option.append(option._get_id())
if not isinstance(option, OptionDescription):
for func, all_cons_opts, params in option._get_consistencies():
all_cons_opts[0]._valid_consistencies(all_cons_opts[1:])
for opt in all_cons_opts:
_consistencies.setdefault(opt,
[]).append((func,
@ -162,40 +162,6 @@ class OptionDescription(BaseOption, StorageOptionDescription):
raise ValueError(_('group_type: {0}'
' not allowed').format(group_type))
def _valid_consistency(self, option, value, context, index, submulti_idx):
if self._cache_consistencies is None:
return True
#consistencies is something like [('_cons_not_equal', (opt1, opt2))]
if isinstance(option, DynSymLinkOption):
consistencies = self._cache_consistencies.get(option._impl_getopt())
else:
consistencies = self._cache_consistencies.get(option)
if consistencies is not None:
for func, all_cons_opts, params in consistencies:
warnings_only = params.get('warnings_only', False)
transitive = params.get('transitive', True)
#all_cons_opts[0] is the option where func is set
if isinstance(option, DynSymLinkOption):
subpath = '.'.join(option._dyn.split('.')[:-1])
namelen = len(option._impl_getopt().impl_getname())
suffix = option.impl_getname()[namelen:]
opts = []
for opt in all_cons_opts:
name = opt.impl_getname() + suffix
path = subpath + '.' + name
opts.append(opt._impl_to_dyn(name, path))
else:
opts = all_cons_opts
try:
opts[0]._launch_consistency(func, option, value, context,
index, submulti_idx, opts,
warnings_only, transitive)
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
:param descr: parent :class:`tiramisu.option.OptionDescription`
@ -317,12 +283,15 @@ class DynOptionDescription(OptionDescription):
for child in children:
if isinstance(child, OptionDescription):
if child.impl_get_group_type() != groups.master:
raise ConfigError(_('cannot set optiondescription in an '
raise ConfigError(_('cannot set optiondescription in a '
'dynoptiondescription'))
for chld in child._impl_getchildren():
chld._impl_setsubdyn(self)
if isinstance(child, SymLinkOption):
raise ConfigError(_('cannot set symlinkoption in an '
raise ConfigError(_('cannot set symlinkoption in a '
'dynoptiondescription'))
if isinstance(child, SymLinkOption):
raise ConfigError(_('cannot set symlinkoption in a '
'dynoptiondescription'))
child._impl_setsubdyn(self)
self.impl_set_callback(callback, callback_params)