requirement are now a dictionary (not anymore a tuple)

This commit is contained in:
2013-06-29 18:41:14 +02:00
parent e501c6d12d
commit 9b134c3aa7
8 changed files with 252 additions and 142 deletions

View File

@ -125,7 +125,7 @@ class Option(BaseInformation):
self._name = name
self._impl_informations = {}
self.impl_set_information('doc', doc)
validate_requires_arg(requires, self._name)
requires = validate_requires_arg(requires, self._name)
self._requires = requires
self._multi = multi
self._consistencies = None
@ -645,7 +645,7 @@ class OptionDescription(BaseInformation):
'{0}').format(child))
old = child
self._children = (tuple(child_names), tuple(children))
validate_requires_arg(requires, self._name)
requires = validate_requires_arg(requires, self._name)
self._requires = requires
self._cache_paths = None
self._consistencies = None
@ -812,35 +812,63 @@ class OptionDescription(BaseInformation):
def validate_requires_arg(requires, name):
"check malformed requirements"
if requires is not None:
config_action = {}
for req in requires:
if not type(req) == tuple:
raise ValueError(_("malformed requirements type for option:"
" {0}, must be a tuple").format(name))
if not isinstance(req[0], Option):
raise ValueError(_('malformed requirements first argument '
'must be an option in option {0}').format(name))
if req[0].impl_is_multi():
raise ValueError(_('malformed requirements option {0} '
'should not be a multi').format(name))
if req[1] is not None and not req[0]._validate(req[1]):
raise ValueError(_('malformed requirements second argument '
'must be valid for option {0}').format(name))
if len(req) == 3:
action = req[2]
inverse = False
elif len(req) in [4, 5, 6]:
action = req[2]
inverse = req[3]
else:
raise ValueError(_("malformed requirements for option: {0}"
" invalid len").format(name))
if action in config_action:
if inverse != config_action[action]:
raise ValueError(_("inconsistency in action types for option: {0}"
" action: {1}").format(name, action))
else:
config_action[action] = inverse
return requires
"""check malformed requirements
and tranform dict to tuple"""
if requires is None:
return None
ret_requires = []
config_action = {}
for require in requires:
if not type(require) == dict:
raise ValueError(_("malformed requirements type for option:"
" {0}, must be a dict").format(name))
valid_keys = ('option', 'expected', 'action', 'inverse', 'transitive',
'same_action')
unknown_keys = frozenset(require.keys()) - frozenset(valid_keys)
if unknown_keys != frozenset():
raise ValueError('malformed requirements for option: {0}'
' unknown keys {1}, must only '
'{2}'.format(name,
unknown_keys,
valid_keys))
try:
option = require['option']
expected = require['expected']
action = require['action']
except KeyError:
raise ValueError(_("malformed requirements for option: {0}"
" require must have option, expected and"
" action keys").format(name))
inverse = require.get('inverse', False)
if inverse not in [True, False]:
raise ValueError(_('malformed requirements for option: {0}'
' inverse must be boolean'))
transitive = require.get('transitive', True)
if transitive not in [True, False]:
raise ValueError(_('malformed requirements for option: {0}'
' transitive must be boolean'))
same_action = require.get('same_action', True)
if same_action not in [True, False]:
raise ValueError(_('malformed requirements for option: {0}'
' same_action must be boolean'))
if not isinstance(option, Option):
raise ValueError(_('malformed requirements first argument '
'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))
if expected is not None and not option._validate(expected):
raise ValueError(_('malformed requirements second argument '
'must be valid for option {0}').format(name))
if action in config_action:
if inverse != config_action[action]:
raise ValueError(_("inconsistency in action types for option: {0}"
" action: {1}").format(name, action))
else:
config_action[action] = inverse
ret_requires.append((option, expected, action, inverse, transitive,
same_action))
return tuple(ret_requires)

View File

@ -334,6 +334,9 @@ class Setting(object):
def apply_requires(opt, config):
"carries out the jit (just in time requirements between options"
if opt._requires is None:
return
def build_actions(requires):
"action are hide, show, enable, disable..."
trigger_actions = {}
@ -341,74 +344,42 @@ def apply_requires(opt, config):
action = require[2]
trigger_actions.setdefault(action, []).append(require)
return trigger_actions
#for symlink
if hasattr(opt, '_requires') and opt._requires is not None:
# filters the callbacks
settings = config.cfgimpl_get_settings()
setting = Property(settings, settings._get_properties(opt, False), opt)
trigger_actions = build_actions(opt._requires)
descr = config.cfgimpl_get_context().cfgimpl_get_description()
optpath = descr.impl_get_path_by_opt(opt)
for requires in trigger_actions.values():
matches = False
for require in requires:
if len(require) == 3:
option, expected, action = require
inverse = False
transitive = True
same_action = True
elif len(require) == 4:
option, expected, action, inverse = require
transitive = True
same_action = True
elif len(require) == 5:
option, expected, action, inverse, transitive = require
same_action = True
elif len(require) == 6:
option, expected, action, inverse, transitive, same_action = require
path = descr.impl_get_path_by_opt(option)
if path == optpath or path.startswith(optpath + '.'):
raise RequirementError(_("malformed requirements "
"imbrication detected for option: '{0}' "
"with requirement on: '{1}'").format(optpath, path))
try:
value = config.cfgimpl_get_context()._getattr(path, force_permissive=True)
except PropertiesOptionError, err:
if not transitive:
continue
properties = err.proptype
if same_action and action not in properties:
raise RequirementError(_("option '{0}' has requirement's property error: "
"{1} {2}").format(opt._name, path, properties))
#transitive action, force expected
value = expected
inverse = False
except AttributeError:
raise AttributeError(_("required option not found: "
"{0}").format(path))
if value == expected:
matches = True
if inverse:
#temporary remove, definitive if no value != expected
#matches
setting.remove(action)
else:
setting.append(action)
## the calculation cannot be carried out
break
if value != expected:
matches = True
if inverse:
setting.append(action)
# the calculation cannot be carried out
break
else:
#temporary remove, definitive if no value != expected
#matches
setting.remove(action)
# no requirement has been triggered, then just reverse the action
if not matches:
if inverse:
setting.append(action)
else:
setting.remove(action)
# filters the callbacks
settings = config.cfgimpl_get_settings()
setting = Property(settings, settings._get_properties(opt, False), opt)
trigger_actions = build_actions(opt._requires)
descr = config.cfgimpl_get_context().cfgimpl_get_description()
optpath = descr.impl_get_path_by_opt(opt)
for requires in trigger_actions.values():
matches = False
for require in requires:
option, expected, action, inverse, transitive, same_action = require
path = descr.impl_get_path_by_opt(option)
if path == optpath or path.startswith(optpath + '.'):
raise RequirementError(_("malformed requirements "
"imbrication detected for option: '{0}' "
"with requirement on: '{1}'").format(optpath, path))
try:
value = config.cfgimpl_get_context()._getattr(path, force_permissive=True)
except PropertiesOptionError, err:
if not transitive:
continue
properties = err.proptype
if same_action and action not in properties:
raise RequirementError(_("option '{0}' has requirement's property error: "
"{1} {2}").format(opt._name, path, properties))
#transitive action, force expected
value = expected
inverse = False
except AttributeError:
raise AttributeError(_("required option not found: "
"{0}").format(path))
if not inverse and value == expected or inverse and value != expected:
matches = True
setting.append(action)
## the calculation cannot be carried out
break
# no requirement has been triggered, then just reverse the action
if not matches:
setting.remove(action)