-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
[ui/core] Add Validators paradigm #3088
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| from typing import TYPE_CHECKING | ||
|
|
||
| if TYPE_CHECKING: | ||
| from meshroom.core.attribute import Attribute | ||
| from meshroom.core.node import Node | ||
|
|
||
|
|
||
| def success() -> tuple[bool, list[str]]: | ||
| return (True, []) | ||
|
|
||
| def error(*messages: str) -> tuple[bool, list[str]]: | ||
| return (False, list(messages)) | ||
|
|
||
| class AttributeValidator(object): | ||
| """ Interface for an attribute validation | ||
| You can inherit from this class and override the __call__ methods to implement your own attribute validation logic | ||
|
|
||
| Because it's a callable class, you can also create your own validators on the fly | ||
|
|
||
| .. code-block: python | ||
|
|
||
| lambda node, attribute: success() if attribute.value and attribute.value != "" else error("attribute have no value") | ||
| """ | ||
|
|
||
| def __call__(self, node: "Node", attribute: "Attribute") -> tuple[bool, list[str]]: | ||
| """ | ||
| Override this method to implement your custom validation logic. | ||
| You can use the success() and error() helpers that encapsulate the returning response. | ||
|
|
||
| :param node: The node that holds the attribute to validate | ||
| :param attribute: The atribute to validate | ||
|
|
||
| :returns: The validtion response: (True, []) if it's valid, (False, [errorMessage1, errorMessage2, ...]) if error exists | ||
|
|
||
| """ | ||
| raise NotImplementedError() | ||
|
|
||
|
|
||
| class NotEmptyValidator(AttributeValidator): | ||
| """ The attribute value should not be empty | ||
| This class is used to determine if an attribute label should be considered as mandatory/required | ||
| """ | ||
|
|
||
| def __call__(self, node: "Node", attribute: "Attribute") -> tuple[bool, list[str]]: | ||
|
|
||
| if attribute.value is None or attribute.value == "": | ||
| return error("Empty value is not allowed") | ||
|
|
||
| return success() | ||
|
|
||
|
|
||
| class RangeValidator(AttributeValidator): | ||
| """ Check if the attribute value is in the given range | ||
| """ | ||
|
|
||
| def __init__(self, min, max): | ||
| self._min = min | ||
| self._max = max | ||
|
|
||
| def __call__(self, node:"Node", attribute: "Attribute") -> tuple[bool, list[str]]: | ||
|
|
||
| if attribute.value < self._min or attribute.value > self._max: | ||
| return error(f"Value should be greater than {self._min} and less than {self._max}", | ||
| f"({self._min} < {attribute.value} < {self._max})") | ||
|
|
||
| return success() |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1522,6 +1522,8 @@ def _onAttributeChanged(self, attr: Attribute): | |
| if callback: | ||
| callback(self) | ||
|
|
||
| self.hasInvalidAttributeChanged.emit() | ||
|
|
||
| if self.graph: | ||
| # If we are in a graph, propagate the notification to the connected output attributes | ||
| for edge in self.graph.outEdges(attr): | ||
|
|
@@ -1792,7 +1794,7 @@ def loadOutputAttr(self): | |
| # This does not apply to non dynamic output | ||
| if not self.nodeDesc.hasDynamicOutputAttribute: | ||
| return | ||
|
|
||
| # Check existence of values.json file | ||
| valuesFile = self.valuesFile | ||
| if not os.path.exists(valuesFile): | ||
|
|
@@ -2157,6 +2159,12 @@ def hasTextOutputAttribute(self) -> bool: | |
| """ | ||
| return next((attr for attr in self._attributes if attr.enabled and attr.isOutput and attr.isTextDisplayable), None) is not None | ||
|
|
||
| def _hasInvalidAttribute(self): | ||
| for attribute in self._attributes: | ||
| if len(attribute.errorMessages) > 0: | ||
| return True | ||
| return False | ||
|
|
||
| def _hasDisplayableShape(self): | ||
| """ | ||
|
Comment on lines
2168
to
2169
|
||
| Return True if at least one attribute is a ShapeAttribute, a ShapeListAttribute or a shape File. | ||
|
|
@@ -2236,6 +2244,9 @@ def _hasDisplayableShape(self): | |
| # Whether the node contains a ShapeAttribute, a ShapeListAttribute or a shape File. | ||
| hasDisplayableShape = Property(bool, _hasDisplayableShape, constant=True) | ||
|
|
||
| hasInvalidAttributeChanged = Signal() | ||
| hasInvalidAttribute = Property(bool, _hasInvalidAttribute, notify=hasInvalidAttributeChanged) | ||
|
|
||
|
|
||
| class Node(BaseNode): | ||
| """ | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.