can use requires with self option

This commit is contained in:
Emmanuel Garette 2018-04-25 19:12:54 +02:00
parent 2018a92b66
commit b442d52289
6 changed files with 44 additions and 9 deletions

View File

@ -58,6 +58,24 @@ def test_requires():
api.option('ip_address_service').value.get()
def test_requires_self():
a = StrOption('ip_address_service', '',
requires=[{'option': 'self', 'expected': 'b', 'action': 'disabled'}])
od = OptionDescription('service', '', [a])
api = getapi(Config(od))
api.property.read_write()
assert api.option('ip_address_service').value.get() == None
api.option('ip_address_service').value.set('a')
assert api.option('ip_address_service').value.get() == 'a'
api.option('ip_address_service').value.set('b')
props = []
try:
api.option('ip_address_service').value.get()
except PropertiesOptionError as err:
props = err.proptype
assert frozenset(props) == frozenset(['disabled'])
def test_requires_with_requires():
a = BoolOption('activate_service', '', True)
b = IPOption('ip_address_service', '',

View File

@ -74,6 +74,8 @@ def manager_callback(callbk,
(not opt.impl_is_master_slaves('slave') or index is None):
return orig_value
if opt == option:
sconfig_bag.setting_properties = None
sconfig_bag.force_unrestraint= False
sconfig_bag.validate = False
try:
# get value

View File

@ -77,7 +77,7 @@ class PropertiesOptionError(AttributeError):
self._requires = config_bag.option.impl_getrequires()
self._name = config_bag.option.impl_get_display_name()
self._orig_opt = None
self._config_bag = config_bag.copy('nooption')
self._config_bag = config_bag.copy()
self.proptype = proptype
self._settings = settings
self.msg = None

View File

@ -469,6 +469,8 @@ def validate_requires_arg(new_option,
"""
def get_option(require):
option = require['option']
if option == 'self':
option = new_option
if not isinstance(option, BaseOption):
raise ValueError(_('malformed requirements '
'must be an option in option {0}').format(name))

View File

@ -481,7 +481,7 @@ class Settings(object):
for option, expected in exps:
reqpath = option.impl_getpath(context)
#FIXME c'est un peut tard !
if reqpath == path or reqpath.startswith(path + '.'):
if reqpath.startswith(path + '.'):
raise RequirementError(_("malformed requirements "
"imbrication detected for option:"
" '{0}' with requirement on: "
@ -493,8 +493,13 @@ class Settings(object):
elif option.impl_is_multi():
is_indexed = True
sconfig_bag = config_bag.copy('nooption')
if config_bag.option == option:
sconfig_bag.setting_properties = None
sconfig_bag.force_unrestraint= False
sconfig_bag.validate = False
else:
sconfig_bag.force_permissive = True
sconfig_bag.option = option
sconfig_bag.force_permissive = True
try:
value = context.getattr(reqpath,
idx,

View File

@ -152,15 +152,23 @@ class Values(object):
with_value=True)
if owner != owners.default:
# if a value is store in storage, check if not frozen + force_default_on_freeze
# if frozen + force_default_on_freeze => force default value
self_properties = config_bag.properties
if self_properties is None:
if config_bag.setting_properties is None:
# get property without apply requires
settings = self._getcontext().cfgimpl_get_settings()
self_properties = settings.getproperties(path,
index,
config_bag)
config_bag.properties = self_properties
config_bag,
apply_requires=False)
else:
# if a value is store in storage, check if not frozen + force_default_on_freeze
# if frozen + force_default_on_freeze => force default value
self_properties = config_bag.properties
if self_properties is None:
settings = self._getcontext().cfgimpl_get_settings()
self_properties = settings.getproperties(path,
index,
config_bag)
config_bag.properties = self_properties
if not ('frozen' in self_properties and \
'force_default_on_freeze' in self_properties):
if index is not None and not is_slave: