if an option has configerror for mandatory property but is disabled too, do not raise

This commit is contained in:
egarette@silique.fr 2023-11-04 08:27:22 +01:00
parent e7b174f28f
commit a3261abc94
2 changed files with 63 additions and 37 deletions

View file

@ -5,7 +5,7 @@ do_autopath()
import pytest import pytest
from tiramisu import Config from tiramisu import Config
from tiramisu import IntOption, StrOption, OptionDescription, DynOptionDescription, \ from tiramisu import IntOption, StrOption, OptionDescription, DynOptionDescription, PasswordOption, UsernameOption, \
SymLinkOption, Leadership, undefined, Calculation, Params, \ SymLinkOption, Leadership, undefined, Calculation, Params, \
ParamOption, ParamValue, ParamIndex, calc_value ParamOption, ParamValue, ParamIndex, calc_value
from tiramisu.error import PropertiesOptionError, ConfigError from tiramisu.error import PropertiesOptionError, ConfigError
@ -14,6 +14,8 @@ from tiramisu.setting import groups
#def teardown_function(function): #def teardown_function(function):
# assert list_sessions() == [], 'session list is not empty when leaving "{}"'.format(function.__name__) # assert list_sessions() == [], 'session list is not empty when leaving "{}"'.format(function.__name__)
def is_mandatory(variable):
return True
def make_description(): def make_description():
@ -723,3 +725,12 @@ def test_mandatory_callback_leader_and_followers_leader():
# FIXME cfg = get_config(cfg, config_type) # FIXME cfg = get_config(cfg, config_type)
compare(cfg.value.mandatory(), ['val1.val1']) compare(cfg.value.mandatory(), ['val1.val1'])
# assert not list_sessions() # assert not list_sessions()
def test_mandatory_and_disabled():
password = PasswordOption(name="password", doc="Password", properties=frozenset({"disabled"}))
username = UsernameOption(name="username", doc="Username", properties=frozenset({"normal", Calculation(is_mandatory, Params((ParamOption(password)))), "disabled"}))
od1 = OptionDescription('rootconfig', '', [username, password])
cfg = Config(od1)
cfg.property.read_write()
cfg.value.dict()

View file

@ -196,9 +196,12 @@ class OptionBag:
self.properties = None self.properties = None
elif properties is undefined: elif properties is undefined:
settings = context.get_settings() settings = context.get_settings()
self.properties = settings.getproperties(self, try:
apply_requires=apply_requires, self.properties = settings.getproperties(self,
) apply_requires=apply_requires,
)
except ConfigError:
self.properties = None
if properties is not undefined: if properties is not undefined:
self.properties = properties self.properties = properties
@ -454,6 +457,7 @@ class Settings:
apply_requires=True, apply_requires=True,
uncalculated=False, uncalculated=False,
help_property=False, help_property=False,
transitive_raise=True,
): ):
"""get properties """get properties
""" """
@ -489,17 +493,22 @@ class Settings:
else: else:
props.add((prop, prop)) props.add((prop, prop))
elif apply_requires: elif apply_requires:
if not help_property: try:
new_prop = prop.execute(option_bag, if not help_property:
for_settings=True, new_prop = prop.execute(option_bag,
) for_settings=True,
else: )
new_prop = prop.help(option_bag, else:
for_settings=True, new_prop = prop.help(option_bag,
) for_settings=True,
if isinstance(new_prop, str): )
new_prop = (new_prop, new_prop) if isinstance(new_prop, str):
if new_prop is None: new_prop = (new_prop, new_prop)
if new_prop is None:
continue
except ConfigError as err:
if transitive_raise:
raise err from err
continue continue
if (not help_property and not isinstance(new_prop, str)) or \ if (not help_property and not isinstance(new_prop, str)) or \
(help_property and not isinstance(new_prop, tuple)): (help_property and not isinstance(new_prop, tuple)):
@ -514,7 +523,7 @@ class Settings:
props -= self.getpermissives(option_bag) props -= self.getpermissives(option_bag)
if not uncalculated and apply_requires and \ if not uncalculated and apply_requires and \
not option_bag.config_bag.is_unrestraint and \ not option_bag.config_bag.is_unrestraint and \
not help_property: not help_property and transitive_raise:
cache.setcache(option_bag, cache.setcache(option_bag,
props, props,
type_='properties', type_='properties',
@ -683,15 +692,17 @@ class Settings:
option_bag, option_bag,
apply_requires=True, apply_requires=True,
uncalculated=False, uncalculated=False,
transitive_raise=True,
): ):
"""raise if needed """raise if needed
""" """
if not uncalculated and apply_requires: if not uncalculated and apply_requires and option_bag.properties is not None:
option_properties = option_bag.properties option_properties = option_bag.properties
else: else:
option_properties = self.getproperties(option_bag, option_properties = self.getproperties(option_bag,
apply_requires=apply_requires, apply_requires=apply_requires,
uncalculated=uncalculated, uncalculated=uncalculated,
transitive_raise=transitive_raise,
) )
return self._calc_raises_properties(option_bag.config_bag.properties, return self._calc_raises_properties(option_bag.config_bag.properties,
option_bag.config_bag.permissives, option_bag.config_bag.permissives,
@ -721,26 +732,30 @@ class Settings:
if not config_properties or config_properties == frozenset(['cache']): if not config_properties or config_properties == frozenset(['cache']):
# if no global property # if no global property
return return
properties = self.calc_raises_properties(option_bag) for transitive_raise in [False, True]:
if properties != frozenset(): properties = self.calc_raises_properties(option_bag,
if need_help: transitive_raise=transitive_raise,
help_properties = dict(self.getproperties(option_bag, )
help_property=True, if properties != frozenset():
)) if need_help:
calc_properties = [] help_properties = dict(self.getproperties(option_bag,
for property_ in self._calc_raises_properties(option_bag.config_bag.properties, help_property=True,
option_bag.config_bag.permissives, transitive_raise=transitive_raise,
set(help_properties.keys()), ))
): calc_properties = []
calc_properties.append(help_properties[property_]) for property_ in self._calc_raises_properties(option_bag.config_bag.properties,
calc_properties = frozenset(calc_properties) option_bag.config_bag.permissives,
else: set(help_properties.keys()),
calc_properties = properties ):
raise PropertiesOptionError(option_bag, calc_properties.append(help_properties[property_])
properties, calc_properties = frozenset(calc_properties)
self, else:
help_properties=calc_properties, calc_properties = properties
) raise PropertiesOptionError(option_bag,
properties,
self,
help_properties=calc_properties,
)
def validate_mandatory(self, def validate_mandatory(self,
value, value,