[core] Add 'strictCompatibility' mode for loadGraph function

Provide a way to control how to handle node compatibility issues
when loading a Graph with the `strictCompatibility` parameter.
Introduce a new error type, GraphCompatibilityError, to be raised
when loading a Graph with compatibility issues with strictCompatibility enabled.
This commit is contained in:
Yann Lanthony 2024-11-14 19:11:08 +01:00
parent c3bb55a414
commit c09a0d2327
2 changed files with 33 additions and 2 deletions

View file

@ -12,6 +12,21 @@ class GraphException(MeshroomException):
pass
class GraphCompatibilityError(GraphException):
"""
Raised when node compatibility issues occur when loading a graph.
Args:
filepath: The path to the file that caused the error.
issues: A dictionnary of node names and their respective compatibility issues.
"""
def __init__(self, filepath, issues: dict[str, str]) -> None:
self.filepath = filepath
self.issues = issues
msg = f"Compatibility issues found when loading {self.filepath}: {self.issues}"
super().__init__(msg)
class UnknownNodeTypeError(GraphException):
"""
Raised when asked to create a unknown node type.

View file

@ -15,7 +15,7 @@ import meshroom.core
from meshroom.common import BaseObject, DictModel, Slot, Signal, Property
from meshroom.core import Version
from meshroom.core.attribute import Attribute, ListAttribute, GroupAttribute
from meshroom.core.exception import StopGraphVisit, StopBranchVisit
from meshroom.core.exception import GraphCompatibilityError, StopGraphVisit, StopBranchVisit
from meshroom.core.node import nodeFactory, Status, Node, CompatibilityNode
# Replace default encoder to support Enums
@ -1633,11 +1633,27 @@ class Graph(BaseObject):
canComputeLeaves = Property(bool, lambda self: self._canComputeLeaves, notify=canComputeLeavesChanged)
def loadGraph(filepath):
def loadGraph(filepath, strictCompatibility: bool = False) -> Graph:
"""
Load a Graph from a Meshroom Graph (.mg) file.
Args:
filepath: The path to the Meshroom Graph file.
strictCompatibility: If True, raise a GraphCompatibilityError if the loaded Graph has node compatibility issues.
Returns:
Graph: The loaded Graph instance.
Raises:
GraphCompatibilityError: If the Graph has node compatibility issues and `strictCompatibility` is False.
"""
graph = Graph("")
graph.load(filepath)
compatibilityIssues = len(graph.compatibilityNodes) > 0
if compatibilityIssues and strictCompatibility:
raise GraphCompatibilityError(filepath, {n.name: str(n.issue) for n in graph.compatibilityNodes})
graph.update()
return graph