tiramisu/tiramisu/function.py

609 lines
27 KiB
Python
Raw Normal View History

2024-07-06 14:33:25 +02:00
# Copyright (C) 2018-2024 Team tiramisu (see AUTHORS for all contributors)
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published by the
# Free Software Foundation, either version 3 of the License, or (at your
# option) any later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
# details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
2023-05-11 15:44:48 +02:00
"""some functions to validates or calculates value
"""
2019-03-09 13:28:15 +01:00
from typing import Any, List, Optional
from operator import add, mul, sub, truediv
2019-10-27 11:09:15 +01:00
from ipaddress import ip_address, ip_interface, ip_network
from .i18n import _
2019-09-01 09:41:53 +02:00
from .setting import undefined
from .error import display_list
2018-12-24 09:30:58 +01:00
2023-05-11 15:44:48 +02:00
FUNCTION_WAITING_FOR_DICT = []
2024-10-25 22:15:45 +02:00
FUNCTION_WAITING_FOR_ERROR = []
2023-05-11 15:44:48 +02:00
def function_waiting_for_dict(function):
"""functions (calculation or validation) receive by default only the value of other options
all functions declared with this function recieve a dict with option informations
(value, name, ...)
2019-10-27 11:09:15 +01:00
"""
2023-05-11 15:44:48 +02:00
name = function.__name__
if name not in FUNCTION_WAITING_FOR_DICT:
FUNCTION_WAITING_FOR_DICT.append(name)
return function
2024-10-25 22:15:45 +02:00
def function_waiting_for_error(function):
"""functions (calculation or validation) receive by default only the value of other options
set PropertyError too
"""
name = function.__name__
if name not in FUNCTION_WAITING_FOR_ERROR:
FUNCTION_WAITING_FOR_ERROR.append(name)
return function
2023-05-11 15:44:48 +02:00
@function_waiting_for_dict
def valid_network_netmask(network: dict,
netmask: dict,
):
"""
validates if network and netmask are coherent
this validator must be set to netmask option
"""
if None in [network['value'], netmask['value']]:
2019-10-27 11:09:15 +01:00
return
try:
2023-05-11 15:44:48 +02:00
ip_network(f'{network["value"]}/{netmask["value"]}')
except ValueError as err:
raise ValueError(_(f'network "{network["value"]}" ({network["name"]}) does not match '
'with this netmask')) from err
@function_waiting_for_dict
def valid_ip_netmask(ip: dict, # pylint: disable=invalid-name
netmask: dict,
):
"""validates if ip and netmask are coherent
this validator must be set to netmask option
"""
if None in [ip['value'], netmask['value']]:
2019-10-27 11:09:15 +01:00
return
2023-05-11 15:44:48 +02:00
ip_netmask = ip_interface(f'{ip["value"]}/{netmask["value"]}')
2019-10-27 11:09:15 +01:00
if ip_netmask.ip == ip_netmask.network.network_address:
2023-05-11 15:44:48 +02:00
msg = _(f'IP "{ip["value"]}" ({ip["name"]}) with this netmask is '
'in fact a network address')
raise ValueError(msg)
if ip_netmask.ip == ip_netmask.network.broadcast_address:
msg = _(f'IP "{ip["value"]}" ({ip["name"]}) with this netmask is '
'in fact a broacast address')
raise ValueError(msg)
2019-10-27 11:09:15 +01:00
2023-05-11 15:44:48 +02:00
@function_waiting_for_dict
def valid_broadcast(network: dict,
netmask: dict,
broadcast: dict,
):
"""validates if the broadcast is coherent with network and netmask
"""
if None in [network['value'], netmask['value'], broadcast['value']]:
2020-02-18 22:08:52 +01:00
return
2023-05-11 15:44:48 +02:00
if ip_network(f'{network["value"]}/{netmask["value"]}').broadcast_address != \
ip_address(broadcast['value']):
msg = _(f'broadcast invalid with network {network["value"]} ({network["name"]}) '
f'and netmask {netmask["value"]} ({netmask["name"]})')
raise ValueError(msg)
@function_waiting_for_dict
def valid_in_network(ip: dict, # pylint: disable=invalid-name
network: dict,
netmask=Optional[dict],
):
"""validates if an IP is in a network
this validator must be set to ip option
"""
if None in [ip['value'], network['value']]:
return
if '/' in network['value']:
# it's a CIDR network
network_value = network['value']
2019-10-27 11:09:15 +01:00
else:
2023-05-11 15:44:48 +02:00
if netmask is None or netmask['value'] is None:
2020-02-18 22:08:52 +01:00
return
2023-05-11 15:44:48 +02:00
network_value = f'{network["value"]}/{netmask["value"]}'
network_obj = ip_network(network_value)
ip_netmask = ip_interface(f'{ip["value"]}/{network_obj.netmask}')
if ip_netmask not in network_obj:
2019-10-27 11:09:15 +01:00
if netmask is None:
2023-05-11 15:44:48 +02:00
msg = _('this IP is not in network {network["value"]} ({network["name"]})')
2019-10-27 11:09:15 +01:00
else:
2023-05-11 15:44:48 +02:00
msg = _('this IP is not in network {network["value"]} ({network["name"]}) '
'with netmask {netmask["value"]} ({netmask["name"]})')
2019-10-27 11:09:15 +01:00
raise ValueError(msg)
# test if ip is not network/broadcast IP
if ip_netmask.ip == ip_netmask.network.network_address:
2023-05-11 15:44:48 +02:00
msg = _(f'this IP with the network {network["value"]} ({network["value"]} '
'is in fact a network address')
2019-10-27 11:09:15 +01:00
raise ValueError(msg)
2023-05-11 15:44:48 +02:00
if ip_netmask.ip == ip_netmask.network.broadcast_address:
msg = _(f'this IP with the network {network["value"]} ({network["value"]} '
'is in fact a broadcast address')
2019-10-27 11:09:15 +01:00
raise ValueError(msg)
2023-05-11 15:44:48 +02:00
@function_waiting_for_dict
2019-10-27 11:09:15 +01:00
def valid_not_equal(*values):
2023-05-11 15:44:48 +02:00
"""valid that two options have not same value
"""
2019-10-27 11:09:15 +01:00
equal = set()
2023-05-11 15:44:48 +02:00
for val in values[1:]:
if 'propertyerror' in val:
continue
if values[0]['value'] == val['value'] is not None:
equal.add(val['name'])
if not equal:
return
msg = _(f'value is identical to {display_list(list(equal), add_quote=True)}')
raise ValueError(msg)
2019-03-09 13:28:15 +01:00
2019-09-01 09:41:53 +02:00
class CalcValue:
2023-05-11 15:44:48 +02:00
"""class to calc_value with different functions
"""
# pylint: disable=too-many-instance-attributes
2019-09-01 09:41:53 +02:00
def __call__(self,
*args: List[Any],
multi: bool=False,
default: Any=undefined,
condition: Any=undefined,
no_condition_is_invalid: bool=False,
2019-09-01 09:41:53 +02:00
expected: Any=undefined,
condition_operator: str='AND',
reverse_condition: bool=False,
2019-09-01 09:41:53 +02:00
allow_none: bool=False,
remove_duplicate_value: bool=False,
join: Optional[str]=None,
min_args_len: Optional[int]=None,
operator: Optional[str]=None,
index: Optional[int]=None,
**kwargs) -> Any:
2023-05-11 15:44:48 +02:00
# pylint: disable=too-many-statements,too-many-branches,too-many-nested-blocks,too-many-locals
2019-09-01 09:41:53 +02:00
"""calculate value
:param args: list of value
:param multi: value returns must be a list of value
:param default: default value if condition is not matched or if args is empty
if there is more than one default value, set default_0, default_1, ...
:param condition: test if condition is equal to expected value
if there is more than one condition, set condition_0, condition_1, ...
:param expected: value expected for all conditions
2023-05-11 15:44:48 +02:00
if expected value is different between condition, set expected_0,
expected_1, ...
2019-09-01 09:41:53 +02:00
:param no_condition_is_invalid: if no condition and not condition_0, condition_1, ... (for
2023-05-11 15:44:48 +02:00
example if option is disabled) consider that condition not
matching
2019-09-01 09:41:53 +02:00
:param condition_operator: OR or AND operator for condition
:param allow_none: if False, do not return list in None is present in list
:param remove_duplicate_value: if True, remote duplicated value
:param join: join all args with specified characters
:param min_args_len: if number of arguments is smaller than this value, return default value
:param operator: 'add', 'mul', 'div' or 'sub' all args (args must be integer value)
:param index: index for follower
2019-03-09 13:28:15 +01:00
2019-09-01 09:41:53 +02:00
examples:
* you want to copy value from an option to an other option:
2023-05-11 15:44:48 +02:00
>>> from tiramisu import calc_value, StrOption, OptionDescription, Config, \
... Params, ParamOption
2019-09-01 09:41:53 +02:00
>>> val1 = StrOption('val1', '', 'val1')
2023-05-11 15:44:48 +02:00
>>> val2 = StrOption('val2', '', callback=calc_value,
... callback_params=Params(ParamOption(val1)))
2019-09-01 09:41:53 +02:00
>>> od = OptionDescription('root', '', [val1, val2])
>>> cfg = Config(od)
>>> cfg.value.dict()
{'val1': 'val1', 'val2': 'val1'}
2019-03-09 13:28:15 +01:00
2019-09-01 09:41:53 +02:00
* you want to copy values from two options in one multi option
2023-05-11 15:44:48 +02:00
>>> from tiramisu import calc_value, StrOption, OptionDescription, Config, Params, \
... ParamOption, ParamValue
2019-09-01 09:41:53 +02:00
>>> val1 = StrOption('val1', "", 'val1')
>>> val2 = StrOption('val2', "", 'val2')
2023-05-11 15:44:48 +02:00
>>> val3 = StrOption('val3', "", multi=True, callback=calc_value,
... callback_params=Params((ParamOption(val1), ParamOption(val2)),
... multi=ParamValue(True)))
2019-09-01 09:41:53 +02:00
>>> od = OptionDescription('root', '', [val1, val2, val3])
>>> cfg = Config(od)
>>> cfg.value.dict()
{'val1': 'val1', 'val2': 'val2', 'val3': ['val1', 'val2']}
2019-03-09 13:28:15 +01:00
2019-09-01 09:41:53 +02:00
* you want to copy a value from an option if it not disabled, otherwise set 'default_value'
2023-05-11 15:44:48 +02:00
>>> from tiramisu import calc_value, StrOption, OptionDescription, Config, Params, \
... ParamOption, ParamValue
2019-09-01 09:41:53 +02:00
>>> val1 = StrOption('val1', '', 'val1')
2023-05-11 15:44:48 +02:00
>>> val2 = StrOption('val2', '', callback=calc_value,
... callback_params=Params(ParamOption(val1, True),
... default=ParamValue('default_value')))
2019-09-01 09:41:53 +02:00
>>> od = OptionDescription('root', '', [val1, val2])
>>> cfg = Config(od)
>>> cfg.property.read_write()
>>> cfg.value.dict()
{'val1': 'val1', 'val2': 'val1'}
>>> cfg.option('val1').property.add('disabled')
>>> cfg.value.dict()
{'val2': 'default_value'}
2019-03-09 13:28:15 +01:00
2019-09-01 09:41:53 +02:00
* you want to copy value from an option if an other is True, otherwise set 'default_value'
2023-05-11 15:44:48 +02:00
>>> from tiramisu import calc_value, BoolOption, StrOption, OptionDescription, Config, \
... Params, ParamOption, ParamValue
2019-09-01 09:41:53 +02:00
>>> boolean = BoolOption('boolean', '', True)
>>> val1 = StrOption('val1', '', 'val1')
2023-05-11 15:44:48 +02:00
>>> val2 = StrOption('val2', '', callback=calc_value,
... callback_params=Params(ParamOption(val1, True),
... default=ParamValue('default_value'),
... condition=ParamOption(boolean),
... expected=ParamValue(True)))
2019-09-01 09:41:53 +02:00
>>> od = OptionDescription('root', '', [boolean, val1, val2])
>>> cfg = Config(od)
>>> cfg.property.read_write()
>>> cfg.value.dict()
{'boolean': True, 'val1': 'val1', 'val2': 'val1'}
>>> cfg.option('boolean').value.set(False)
>>> cfg.value.dict()
{'boolean': False, 'val1': 'val1', 'val2': 'default_value'}
2019-03-09 13:28:15 +01:00
2019-09-01 09:41:53 +02:00
* you want to copy option even if None is present
2023-05-11 15:44:48 +02:00
>>> from tiramisu import calc_value, StrOption, OptionDescription, Config, Params, \
... ParamOption, ParamValue
2019-09-01 09:41:53 +02:00
>>> val1 = StrOption('val1', "", 'val1')
>>> val2 = StrOption('val2', "")
2023-05-11 15:44:48 +02:00
>>> val3 = StrOption('val3', "", multi=True, callback=calc_value,
... callback_params=Params((ParamOption(val1), ParamOption(val2)),
... multi=ParamValue(True), allow_none=ParamValue(True)))
2019-09-01 09:41:53 +02:00
>>> od = OptionDescription('root', '', [val1, val2, val3])
>>> cfg = Config(od)
>>> cfg.value.dict()
{'val1': 'val1', 'val2': None, 'val3': ['val1', None]}
2019-03-09 13:28:15 +01:00
2019-09-01 09:41:53 +02:00
* you want uniq value
2023-05-11 15:44:48 +02:00
>>> from tiramisu import calc_value, StrOption, OptionDescription, Config, Params, \
... ParamOption, ParamValue
2019-09-01 09:41:53 +02:00
>>> val1 = StrOption('val1', "", 'val1')
>>> val2 = StrOption('val2', "", 'val1')
2023-05-11 15:44:48 +02:00
>>> val3 = StrOption('val3', "", multi=True, callback=calc_value,
... callback_params=Params((ParamOption(val1), ParamOption(val2)),
... multi=ParamValue(True), remove_duplicate_value=ParamValue(True)))
2019-09-01 09:41:53 +02:00
>>> od = OptionDescription('root', '', [val1, val2, val3])
>>> cfg = Config(od)
>>> cfg.value.dict()
{'val1': 'val1', 'val2': 'val1', 'val3': ['val1']}
2019-03-09 13:28:15 +01:00
2019-09-01 09:41:53 +02:00
* you want to join two values with '.'
2023-05-11 15:44:48 +02:00
>>> from tiramisu import calc_value, StrOption, OptionDescription, Config, Params, \
... ParamOption, ParamValue
2019-09-01 09:41:53 +02:00
>>> val1 = StrOption('val1', "", 'val1')
>>> val2 = StrOption('val2', "", 'val2')
2023-05-11 15:44:48 +02:00
>>> val3 = StrOption('val3', "", callback=calc_value,
... callback_params=Params((ParamOption(val1),
... ParamOption(val2)), join=ParamValue('.')))
2019-09-01 09:41:53 +02:00
>>> od = OptionDescription('root', '', [val1, val2, val3])
>>> cfg = Config(od)
>>> cfg.value.dict()
{'val1': 'val1', 'val2': 'val2', 'val3': 'val1.val2'}
2019-03-09 13:28:15 +01:00
2019-09-01 09:41:53 +02:00
* you want join three values, only if almost three values are set
2023-05-11 15:44:48 +02:00
>>> from tiramisu import calc_value, StrOption, OptionDescription, Config, Params, \
... ParamOption, ParamValue
2019-09-01 09:41:53 +02:00
>>> val1 = StrOption('val1', "", 'val1')
>>> val2 = StrOption('val2', "", 'val2')
>>> val3 = StrOption('val3', "", 'val3')
2023-05-11 15:44:48 +02:00
>>> val4 = StrOption('val4', "", callback=calc_value,
... callback_params=Params((ParamOption(val1),
... ParamOption(val2),
... ParamOption(val3, True)),
... join=ParamValue('.'), min_args_len=ParamValue(3)))
2019-09-01 09:41:53 +02:00
>>> od = OptionDescription('root', '', [val1, val2, val3, val4])
>>> cfg = Config(od)
>>> cfg.property.read_write()
>>> cfg.value.dict()
{'val1': 'val1', 'val2': 'val2', 'val3': 'val3', 'val4': 'val1.val2.val3'}
>>> cfg.option('val3').property.add('disabled')
>>> cfg.value.dict()
{'val1': 'val1', 'val2': 'val2', 'val4': ''}
2019-03-09 13:28:15 +01:00
2019-09-01 09:41:53 +02:00
* you want to add all values
2023-05-11 15:44:48 +02:00
>>> from tiramisu import calc_value, IntOption, OptionDescription, Config, Params, \
... ParamOption, ParamValue
2019-09-01 09:41:53 +02:00
>>> val1 = IntOption('val1', "", 1)
>>> val2 = IntOption('val2', "", 2)
2023-05-11 15:44:48 +02:00
>>> val3 = IntOption('val3', "", callback=calc_value,
... callback_params=Params((ParamOption(val1),
ParamOption(val2)),
... operator=ParamValue('add')))
2019-09-01 09:41:53 +02:00
>>> od = OptionDescription('root', '', [val1, val2, val3])
>>> cfg = Config(od)
>>> cfg.value.dict()
{'val1': 1, 'val2': 2, 'val3': 3}
2019-03-09 13:28:15 +01:00
2019-09-01 09:41:53 +02:00
"""
2023-05-11 15:44:48 +02:00
# pylint: disable=attribute-defined-outside-init
2019-09-01 09:41:53 +02:00
self.args = args
self.condition = condition
self.expected = expected
self.condition_operator = condition_operator
self.reverse_condition = reverse_condition
2019-09-01 09:41:53 +02:00
self.kwargs = kwargs
2023-05-11 15:44:48 +02:00
self.no_condition_is_invalid = no_condition_is_invalid # pylint: disable=attribute-defined-outside-init
2019-09-01 09:41:53 +02:00
value = self.get_value(default,
2023-05-11 15:44:48 +02:00
min_args_len,
)
2019-09-01 09:41:53 +02:00
if not multi:
if join is not None:
if None not in value:
value = join.join(value)
else:
value = None
2019-09-01 09:41:53 +02:00
elif value and operator:
new_value = value[0]
2023-05-11 15:44:48 +02:00
oper = {'mul': mul,
'add': add,
'div': truediv,
'sub': sub,
}[operator]
2019-09-01 09:41:53 +02:00
for val in value[1:]:
2023-05-11 15:44:48 +02:00
new_value = oper(new_value, val)
2019-09-01 09:41:53 +02:00
value = new_value
elif value == []:
value = None
else:
value = value[0]
if isinstance(value, list) and index is not None:
if len(value) > index:
value = value[index]
else:
value = None
2021-05-16 06:51:33 +02:00
else:
if join is not None:
if None not in value:
length_val = None
for val in value:
if isinstance(val, list):
if None in val:
length_val = None
break
lval = len(val)
if length_val is not None and length_val != lval:
2023-05-11 15:44:48 +02:00
msg = _('unexpected value in calc_value with join attribute '
f'"{val}" with invalid length "{length_val}"')
raise ValueError(msg)
2021-05-16 06:51:33 +02:00
length_val = lval
new_value = []
2022-01-09 20:36:04 +01:00
if length_val is not None:
for idx in range(length_val):
idx_val = []
for val in value:
if isinstance(val, list):
idx_val.append(val[idx])
else:
idx_val.append(val)
new_value.append(join.join(idx_val))
2021-05-16 06:51:33 +02:00
value = new_value
else:
value = []
elif None in value and not allow_none:
value = []
if remove_duplicate_value:
2021-05-16 06:51:33 +02:00
new_value = []
for val in value:
if val not in new_value:
new_value.append(val)
value = new_value
2019-09-01 09:41:53 +02:00
return value
def value_from_kwargs(self,
value: Any,
pattern: str,
to_dict: bool=False,
empty_test=undefined) -> Any:
2023-05-11 15:44:48 +02:00
"""get value from kwargs
"""
# pylint: disable=too-many-branches
2019-03-09 13:28:15 +01:00
# if value attribute exist return it's value
# otherwise pattern_0, pattern_1, ...
# otherwise undefined
2019-09-01 09:41:53 +02:00
if value is not empty_test:
2019-03-09 13:28:15 +01:00
if to_dict == 'all':
2019-09-01 09:41:53 +02:00
returns = {None: value}
2019-03-09 13:28:15 +01:00
else:
returns = value
else:
kwargs_matches = {}
len_pattern = len(pattern)
2023-05-11 15:44:48 +02:00
for key, pattern_value in self.kwargs.items():
2019-03-09 13:28:15 +01:00
if key.startswith(pattern):
index = int(key[len_pattern:])
2019-09-01 09:41:53 +02:00
if isinstance(pattern_value, dict):
pattern_value = pattern_value['value']
kwargs_matches[index] = pattern_value
2019-03-09 13:28:15 +01:00
if not kwargs_matches:
2019-09-01 09:41:53 +02:00
returns = undefined
2019-03-09 13:28:15 +01:00
else:
2019-09-01 09:41:53 +02:00
keys = sorted(kwargs_matches)
2019-03-09 13:28:15 +01:00
if to_dict:
2019-09-01 09:41:53 +02:00
returns = {}
2019-03-09 13:28:15 +01:00
else:
2019-09-01 09:41:53 +02:00
returns = []
for key in keys:
if to_dict:
returns[key] = kwargs_matches[key]
else:
returns.append(kwargs_matches[key])
2019-03-09 13:28:15 +01:00
return returns
2019-09-01 09:41:53 +02:00
def is_condition_matches(self,
2023-05-11 15:44:48 +02:00
condition_value,
):
"""verify the condition
"""
# pylint: disable=too-many-branches
2019-09-01 09:41:53 +02:00
calculated_conditions = self.value_from_kwargs(condition_value,
'condition_',
2023-05-11 15:44:48 +02:00
to_dict='all',
)
2019-09-01 09:41:53 +02:00
if calculated_conditions is undefined:
is_matches = not self.no_condition_is_invalid
else:
2019-03-09 13:28:15 +01:00
is_matches = None
2019-09-01 09:41:53 +02:00
calculated_expected = self.value_from_kwargs(self.expected,
'expected_',
2023-05-11 15:44:48 +02:00
to_dict=True,
)
calculated_reverse = self.value_from_kwargs(self.reverse_condition,
'reverse_condition_',
2019-09-01 09:41:53 +02:00
to_dict=True,
2023-05-11 15:44:48 +02:00
empty_test=False,
)
2019-03-09 13:28:15 +01:00
for idx, calculated_condition in calculated_conditions.items():
if isinstance(calculated_expected, dict):
2019-09-01 09:41:53 +02:00
if idx is not None:
2021-02-06 15:18:28 +01:00
if isinstance(calculated_expected[idx], list):
current_matches = calculated_condition in calculated_expected[idx]
else:
current_matches = calculated_condition == calculated_expected[idx]
2019-09-01 09:41:53 +02:00
else:
current_matches = calculated_condition in calculated_expected.values()
2019-03-09 13:28:15 +01:00
else:
current_matches = calculated_condition == calculated_expected
if isinstance(calculated_reverse, dict) and idx in calculated_reverse:
reverse_condition = calculated_reverse[idx]
2019-09-01 09:41:53 +02:00
else:
reverse_condition = False
2019-03-09 13:28:15 +01:00
if is_matches is None:
is_matches = current_matches
2019-09-01 09:41:53 +02:00
if self.condition_operator == 'AND':
2019-03-09 13:28:15 +01:00
is_matches = is_matches and current_matches
if reverse_condition:
2019-09-01 09:41:53 +02:00
is_matches = not is_matches
if not is_matches:
break
elif self.condition_operator == 'OR':
2019-03-09 13:28:15 +01:00
is_matches = is_matches or current_matches
if reverse_condition:
2019-09-01 09:41:53 +02:00
is_matches = not is_matches
if is_matches:
break
2019-03-09 13:28:15 +01:00
else:
2023-05-11 15:44:48 +02:00
msg = _(f'unexpected {self.condition_operator} condition_operator '
'in calc_value')
raise ValueError(msg)
is_matches = is_matches and not self.reverse_condition \
or not is_matches and self.reverse_condition
2019-03-09 13:28:15 +01:00
return is_matches
2019-09-01 09:41:53 +02:00
def get_value(self,
default,
2023-05-11 15:44:48 +02:00
min_args_len,
):
"""get the value from arguments
"""
# retrieve the condition
2019-09-01 09:41:53 +02:00
if isinstance(self.condition, dict):
if 'value' in self.condition:
condition_value = self.condition['value']
else:
condition_value = undefined
else:
condition_value = self.condition
2023-05-11 15:44:48 +02:00
# value is empty if condition doesn't match
# otherwise value is arg
if not self.is_condition_matches(condition_value):
2019-03-09 13:28:15 +01:00
value = []
else:
2019-09-01 09:41:53 +02:00
value = self.get_args()
2019-03-09 13:28:15 +01:00
if min_args_len and not len(value) >= min_args_len:
value = []
2023-05-11 15:44:48 +02:00
if not value:
2019-03-09 13:28:15 +01:00
# default value
2019-09-01 09:41:53 +02:00
new_default = self.value_from_kwargs(default,
2023-05-11 15:44:48 +02:00
'default_',
)
2019-03-09 13:28:15 +01:00
if new_default is not undefined:
if not isinstance(new_default, list):
value = [new_default]
else:
value = new_default
return value
2019-09-01 09:41:53 +02:00
def get_args(self):
2023-05-11 15:44:48 +02:00
"""get all arguments
"""
2019-09-01 09:41:53 +02:00
return list(self.args)
class CalcValuePropertyHelp(CalcValue):
2023-05-11 15:44:48 +02:00
"""special class to display property error
"""
2019-09-01 09:41:53 +02:00
def get_name(self):
2023-05-11 15:44:48 +02:00
"""get the condition name
"""
2019-09-01 09:41:53 +02:00
return self.condition['name']
2023-05-11 15:44:48 +02:00
def get_indexed_name(self, index: int) -> str:
"""get name for a specified index
"""
condition_index = self.kwargs.get(f'condition_{index}')
if condition_index is not None and not isinstance(condition_index, dict):
raise ValueError(_(f'unexpected condition_{index} must have "todict" argument'))
return condition_index['name']
def build_property_message(self,
name: str,
value: Any,
) -> str:
"""prepare message to display error message if needed
"""
if not self.reverse_condition:
2019-09-01 09:41:53 +02:00
msg = _('the value of "{0}" is {1}').format(name, value)
2019-03-09 13:28:15 +01:00
else:
2019-09-01 09:41:53 +02:00
msg = _('the value of "{0}" is not {1}').format(name, value)
return msg
def get_args(self):
args = super().get_args()
2023-05-11 15:44:48 +02:00
action = args[0]
calculated_expected = self.value_from_kwargs(self.expected,
'expected_',
to_dict=True)
if self.condition is not undefined:
if 'propertyerror' in self.condition:
msg = self.condition['propertyerror']
2019-09-01 09:41:53 +02:00
else:
2023-05-11 15:44:48 +02:00
name = self.get_name()
if isinstance(calculated_expected, dict):
calc_values = calculated_expected.values()
else:
calc_values = [calculated_expected]
display_value = display_list([str(val) for val in calc_values],
2024-08-07 08:55:43 +02:00
separator='or',
2023-05-11 15:44:48 +02:00
add_quote=True)
msg = self.build_property_message(name, display_value)
else:
msgs = []
for key, value in calculated_expected.items():
name = self.get_indexed_name(key)
msgs.append(self.build_property_message(name, f'"{value}"'))
2024-08-07 08:55:43 +02:00
msg = display_list(msgs, separator=self.condition_operator.lower())
2023-05-11 15:44:48 +02:00
return [(action, f'"{action}" ({msg})')]
2019-09-01 09:41:53 +02:00
calc_value = CalcValue()
2023-05-11 15:44:48 +02:00
calc_value.__name__ = 'calc_value' # pylint: disable=attribute-defined-outside-init
# function_waiting_for_dict(calc_value)
2019-09-01 09:41:53 +02:00
calc_value_property_help = CalcValuePropertyHelp()
2023-05-11 15:44:48 +02:00
calc_value_property_help.__name__ = 'calc_value_property_help' # pylint: disable=attribute-defined-outside-init
function_waiting_for_dict(calc_value_property_help)