feat: better separation between mandatory and empty property + get true property type with mandatory function

This commit is contained in:
egarette@silique.fr 2026-02-19 20:44:24 +01:00
parent f68d56017c
commit dc1aba2ce9
5 changed files with 49 additions and 20 deletions

View file

@ -208,7 +208,9 @@ def test_mandatory_multi_none():
cfg.option('str3').value.get() cfg.option('str3').value.get()
except PropertiesOptionError as err: except PropertiesOptionError as err:
prop = err.proptype prop = err.proptype
assert 'mandatory' in prop assert 'empty' in prop
assert cfg.option("str3").value.mandatory()
assert cfg.option("str3").value.mandatory(return_type=True) == "empty"
cfg.property.read_write() cfg.property.read_write()
cfg.option('str3').value.set(['yes', None]) cfg.option('str3').value.set(['yes', None])
assert cfg.option('str3').owner.get() == 'user' assert cfg.option('str3').owner.get() == 'user'
@ -218,7 +220,7 @@ def test_mandatory_multi_none():
cfg.option('str3').value.get() cfg.option('str3').value.get()
except PropertiesOptionError as err: except PropertiesOptionError as err:
prop = err.proptype prop = err.proptype
assert 'mandatory' in prop assert 'empty' in prop
# assert not list_sessions() # assert not list_sessions()
@ -244,7 +246,7 @@ def test_mandatory_multi_empty():
cfg.option('str3').value.get() cfg.option('str3').value.get()
except PropertiesOptionError as err: except PropertiesOptionError as err:
prop = err.proptype prop = err.proptype
assert 'mandatory' in prop assert 'empty' in prop
# #
cfg.property.read_write() cfg.property.read_write()
cfg.option('str3').value.set(['yes', '']) cfg.option('str3').value.set(['yes', ''])
@ -255,7 +257,7 @@ def test_mandatory_multi_empty():
cfg.option('str3').value.get() cfg.option('str3').value.get()
except PropertiesOptionError as err: except PropertiesOptionError as err:
prop = err.proptype prop = err.proptype
assert 'mandatory' in prop assert 'empty' in prop
# assert not list_sessions() # assert not list_sessions()
@ -332,10 +334,26 @@ def test_mandatory_warnings_ro():
prop = err.proptype prop = err.proptype
assert 'mandatory' in prop assert 'mandatory' in prop
compare(cfg.value.mandatory(), ['str', 'str1', 'unicode2', 'str3']) compare(cfg.value.mandatory(), ['str', 'str1', 'unicode2', 'str3'])
assert cfg.option('str').value.mandatory()
assert cfg.option('str1').value.mandatory()
assert cfg.option('unicode2').value.mandatory()
assert cfg.option('str3').value.mandatory()
assert cfg.option('str').value.mandatory(return_type=True) == 'mandatory'
assert cfg.option('str1').value.mandatory(return_type=True) == 'mandatory'
assert cfg.option('unicode2').value.mandatory(return_type=True) == 'mandatory'
assert cfg.option('str3').value.mandatory(return_type=True) == 'mandatory'
cfg.property.read_write() cfg.property.read_write()
cfg.option('str').value.set('a') cfg.option('str').value.set('a')
cfg.property.read_only() cfg.property.read_only()
compare(cfg.value.mandatory(), ['str1', 'unicode2', 'str3']) compare(cfg.value.mandatory(), ['str1', 'unicode2', 'str3'])
assert not cfg.option('str').value.mandatory()
assert cfg.option('str1').value.mandatory()
assert cfg.option('unicode2').value.mandatory()
assert cfg.option('str3').value.mandatory()
assert cfg.option('str').value.mandatory(return_type=True) is False
assert cfg.option('str1').value.mandatory(return_type=True) == 'mandatory'
assert cfg.option('unicode2').value.mandatory(return_type=True) == 'mandatory'
assert cfg.option('str3').value.mandatory(return_type=True) == 'mandatory'
# assert not list_sessions() # assert not list_sessions()

View file

@ -8,7 +8,7 @@ import warnings
from tiramisu.setting import groups, owners from tiramisu.setting import groups, owners
from tiramisu import StrOption, IntOption, OptionDescription, submulti, Leadership, Config, \ from tiramisu import StrOption, IntOption, OptionDescription, submulti, Leadership, Config, \
MetaConfig, Params, ParamOption, Calculation MetaConfig, Params, ParamOption, Calculation
from tiramisu.error import LeadershipError, PropertiesOptionError from tiramisu.error import LeadershipError, PropertiesOptionError, ValueOptionError
def return_val(val=None): def return_val(val=None):
@ -317,10 +317,8 @@ def test_values_with_leader_and_followers_submulti_mandatory():
cfg.option('ip_admin_eth0.netmask_admin_eth0', 0).value.get() cfg.option('ip_admin_eth0.netmask_admin_eth0', 0).value.get()
# #
cfg.property.read_write() cfg.property.read_write()
cfg.option('ip_admin_eth0.netmask_admin_eth0', 0).value.set(["255.255.255.0", '', "255.255.255.0"]) with pytest.raises(ValueOptionError):
cfg.property.read_only() cfg.option('ip_admin_eth0.netmask_admin_eth0', 0).value.set(["255.255.255.0", "255.255.255.0"])
with pytest.raises(PropertiesOptionError):
cfg.option('ip_admin_eth0.netmask_admin_eth0', 0).value.get()
# assert not list_sessions() # assert not list_sessions()
@ -382,7 +380,6 @@ def test_values_with_leader_and_followers_follower_submulti():
cfg.option('ip_admin_eth0.netmask_admin_eth0', 0).value.set(['255.255.255.0']) cfg.option('ip_admin_eth0.netmask_admin_eth0', 0).value.set(['255.255.255.0'])
cfg.option('ip_admin_eth0.ip_admin_eth0').value.set(['192.168.230.145']) cfg.option('ip_admin_eth0.ip_admin_eth0').value.set(['192.168.230.145'])
cfg.option('ip_admin_eth0.netmask_admin_eth0', 0).value.set(['255.255.255.0']) cfg.option('ip_admin_eth0.netmask_admin_eth0', 0).value.set(['255.255.255.0'])
cfg.option('ip_admin_eth0.netmask_admin_eth0', 0).value.set(['255.255.255.0', '255.255.255.0'])
cfg.option('ip_admin_eth0.netmask_admin_eth0', 0).value.reset() cfg.option('ip_admin_eth0.netmask_admin_eth0', 0).value.reset()
cfg.option('ip_admin_eth0.netmask_admin_eth0', 0).value.set(['255.255.255.0']) cfg.option('ip_admin_eth0.netmask_admin_eth0', 0).value.set(['255.255.255.0'])
cfg.option('ip_admin_eth0.ip_admin_eth0').value.set(['192.168.230.145', '192.168.230.145']) cfg.option('ip_admin_eth0.ip_admin_eth0').value.set(['192.168.230.145', '192.168.230.145'])
@ -501,7 +498,7 @@ def test_callback_submulti():
def test_callback_submulti_follower(): def test_callback_submulti_follower():
multi = StrOption('multi', '', multi=True) multi = StrOption('multi', '', multi=True)
multi2 = StrOption('multi2', '', Calculation(return_list), multi=submulti) multi2 = StrOption('multi2', '', Calculation(return_list), multi=submulti, properties=('notunique',))
od = Leadership('multi', '', [multi, multi2]) od = Leadership('multi', '', [multi, multi2])
od1 = OptionDescription('multi', '', [od]) od1 = OptionDescription('multi', '', [od])
cfg = Config(od1) cfg = Config(od1)

View file

@ -1142,7 +1142,7 @@ class TiramisuOptionValue(CommonTiramisuOption, _TiramisuODGet):
"""Length for a leadership""" """Length for a leadership"""
return self._subconfig.parent.get_length_leadership() return self._subconfig.parent.get_length_leadership()
def mandatory(self): def mandatory(self, *, return_type=False):
"""Return path of options with mandatory property without any value""" """Return path of options with mandatory property without any value"""
subconfig = self._subconfig subconfig = self._subconfig
ori_config_bag = self._subconfig.config_bag ori_config_bag = self._subconfig.config_bag
@ -1152,6 +1152,10 @@ class TiramisuOptionValue(CommonTiramisuOption, _TiramisuODGet):
self._subconfig.config_bag = config_bag self._subconfig.config_bag = config_bag
try: try:
if subconfig.option.impl_is_optiondescription(): if subconfig.option.impl_is_optiondescription():
if return_type:
raise ConfigError(
_("return_type is not valid for a optiondescription")
)
options = [] options = []
for subconfig in config_bag.context.walk( for subconfig in config_bag.context.walk(
self._subconfig, self._subconfig,
@ -1172,6 +1176,8 @@ class TiramisuOptionValue(CommonTiramisuOption, _TiramisuODGet):
self._subconfig, only_mandatory=True self._subconfig, only_mandatory=True
) )
except PropertiesOptionError as err: except PropertiesOptionError as err:
if return_type:
return err.proptype[0]
return err.proptype == ["mandatory"] or err.proptype == ["empty"] return err.proptype == ["mandatory"] or err.proptype == ["empty"]
self._subconfig.config_bag = ori_config_bag self._subconfig.config_bag = ori_config_bag
return False return False

View file

@ -73,6 +73,7 @@ class Leadership(OptionDescription):
if __debug__: if __debug__:
self._check_default_value(child) self._check_default_value(child)
# remove empty property for follower # remove empty property for follower
if not child.impl_is_submulti():
child._properties = frozenset(child._properties - {"empty", "unique"}) child._properties = frozenset(child._properties - {"empty", "unique"})
followers.append(child) followers.append(child)
child._add_dependency(self, "leadership") child._add_dependency(self, "leadership")

View file

@ -268,8 +268,8 @@ class Values:
force_allow_empty_list: bool, force_allow_empty_list: bool,
) -> bool: ) -> bool:
"""convenience method to know if an option is empty""" """convenience method to know if an option is empty"""
index = subconfig.index
option = subconfig.option option = subconfig.option
index = subconfig.index
if index is None and option.impl_is_submulti(): if index is None and option.impl_is_submulti():
# index is not set # index is not set
isempty = True isempty = True
@ -277,9 +277,16 @@ class Values:
isempty = self._isempty_multi(val, force_allow_empty_list) isempty = self._isempty_multi(val, force_allow_empty_list)
if isempty: if isempty:
break break
elif ( elif option.impl_is_submulti():
index is None or (index is not None and option.impl_is_submulti()) if index is None:
) and option.impl_is_multi(): isempty = False
for v in value:
if self._isempty_multi(v, force_allow_empty_list):
isempty = True
break
else:
isempty = self._isempty_multi(value, force_allow_empty_list)
elif index is None and option.impl_is_multi():
# it's a single list # it's a single list
isempty = self._isempty_multi(value, force_allow_empty_list) isempty = self._isempty_multi(value, force_allow_empty_list)
else: else:
@ -293,9 +300,9 @@ class Values:
) -> bool: ) -> bool:
if not isinstance(value, list): if not isinstance(value, list):
return False return False
return ( if not force_allow_empty_list:
(not force_allow_empty_list and value == []) or None in value or "" in value return value == []
) return None in value or "" in value
# ______________________________________________________________________ # ______________________________________________________________________
# set value # set value