From b442d522890d1b797c71657f56c9250aab0dd837 Mon Sep 17 00:00:00 2001 From: Emmanuel Garette Date: Wed, 25 Apr 2018 19:12:54 +0200 Subject: [PATCH] can use requires with self option --- test/test_requires.py | 18 ++++++++++++++++++ tiramisu/autolib.py | 2 ++ tiramisu/error.py | 2 +- tiramisu/option/baseoption.py | 2 ++ tiramisu/setting.py | 9 +++++++-- tiramisu/value.py | 20 ++++++++++++++------ 6 files changed, 44 insertions(+), 9 deletions(-) diff --git a/test/test_requires.py b/test/test_requires.py index c563e7a..811f59d 100644 --- a/test/test_requires.py +++ b/test/test_requires.py @@ -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', '', diff --git a/tiramisu/autolib.py b/tiramisu/autolib.py index 79f44d4..2fda0dc 100644 --- a/tiramisu/autolib.py +++ b/tiramisu/autolib.py @@ -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 diff --git a/tiramisu/error.py b/tiramisu/error.py index b62f29f..ed371a0 100644 --- a/tiramisu/error.py +++ b/tiramisu/error.py @@ -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 diff --git a/tiramisu/option/baseoption.py b/tiramisu/option/baseoption.py index 6c1c3f0..0135ef9 100644 --- a/tiramisu/option/baseoption.py +++ b/tiramisu/option/baseoption.py @@ -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)) diff --git a/tiramisu/setting.py b/tiramisu/setting.py index e8ab1e4..9040d20 100644 --- a/tiramisu/setting.py +++ b/tiramisu/setting.py @@ -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, diff --git a/tiramisu/value.py b/tiramisu/value.py index 2240dcd..e02dfd9 100644 --- a/tiramisu/value.py +++ b/tiramisu/value.py @@ -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: