[core] Add new desc parameters to flag attributes with erroneous values

This commit adds two new description parameters:

- `validValue`, which is true if the value of the attribute is not
erroneous (i.e. valid for the type of the attribute but invalid in the
context of that specific attribute) and false otherwise. This holds a
different significance than the "validateValue" method, which only checks
if the parameter's value is expected with respect to the attribute's type.
For example, we could want an IntParam that flags any value that is not
even: an odd integer value would successfully go through the
"validateValue" method, but would then be flagged as an invalid value
at the attribute's level.

- `errorMessage`, an attribute-specific string that contains a message
explaining why the attribute's `validValue` parameter might be set to
false.

By default, all the attributes' values are valid, and they have no error
message.
This commit is contained in:
Candice Bentéjac 2023-08-02 14:37:46 +02:00 committed by Loïc Vital
parent 04388eff5d
commit 63bd6d61cd
2 changed files with 37 additions and 9 deletions

View file

@ -62,6 +62,7 @@ class Attribute(BaseObject):
self._value = copy.copy(attributeDesc.value)
self._label = attributeDesc.label
self._enabled = True
self._validValue = True
# invalidation value for output attributes
self._invalidationValue = ""
@ -147,6 +148,24 @@ class Attribute(BaseObject):
""" Value for which the attribute should be ignored during the UID computation. """
return self.attributeDesc.uidIgnoreValue
def getValidValue(self):
"""
Get the status of _validValue:
- If it is a function, execute it and return the result
- Otherwise, simply return its value
"""
if isinstance(self.desc.validValue, types.FunctionType):
try:
return self.desc.validValue(self.node)
except Exception:
return True
return self._validValue
def setValidValue(self, value):
if self._validValue == value:
return
self._validValue = value
def _get_value(self):
if self.isLink:
return self.getLinkParam().value
@ -338,6 +357,8 @@ class Attribute(BaseObject):
enabledChanged = Signal()
enabled = Property(bool, getEnabled, setEnabled, notify=enabledChanged)
uidIgnoreValue = Property(Variant, getUidIgnoreValue, constant=True)
validValueChanged = Signal()
validValue = Property(bool, getValidValue, setValidValue, notify=validValueChanged)
def raiseIfLink(func):