Skip to content

Commit 412103a

Browse files
committed
[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.
1 parent 35914bd commit 412103a

File tree

3 files changed

+45
-3
lines changed

3 files changed

+45
-3
lines changed

meshroom/core/exception.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1-
#!/usr/bin/env python
21
# coding:utf-8
2+
"""
3+
Module defining exceptions for Meshroom.
4+
"""
5+
6+
from meshroom.core.typing import PathLike
37

48

59
class MeshroomException(Exception):
@@ -12,6 +16,21 @@ class GraphException(MeshroomException):
1216
pass
1317

1418

19+
class GraphCompatibilityError(GraphException):
20+
"""
21+
Raised when node compatibility issues occurs when loading a graph.
22+
23+
Args:
24+
filepath: The path to the file that caused the error.
25+
issues: A dictionnary of node names and their respective compatibility issues.
26+
"""
27+
def __init__(self, filepath: PathLike, issues: dict[str, str]) -> None:
28+
self.filepath = filepath
29+
self.issues = issues
30+
msg = f"Compatibility issues found when loading {self.filepath}: {self.issues}"
31+
super().__init__(msg)
32+
33+
1534
class UnknownNodeTypeError(GraphException):
1635
"""
1736
Raised when asked to create a unknown node type.

meshroom/core/graph.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@
1515
from meshroom.common import BaseObject, DictModel, Slot, Signal, Property
1616
from meshroom.core import Version
1717
from meshroom.core.attribute import Attribute, ListAttribute, GroupAttribute
18-
from meshroom.core.exception import StopGraphVisit, StopBranchVisit
18+
from meshroom.core.exception import GraphCompatibilityError, StopGraphVisit, StopBranchVisit
1919
from meshroom.core.node import nodeFactory, Status, Node, CompatibilityNode
20+
from meshroom.core.typing import PathLike
2021

2122
# Replace default encoder to support Enums
2223

@@ -1633,11 +1634,27 @@ def setVerbose(self, v):
16331634
canComputeLeaves = Property(bool, lambda self: self._canComputeLeaves, notify=canComputeLeavesChanged)
16341635

16351636

1636-
def loadGraph(filepath):
1637+
def loadGraph(filepath: PathLike, strictCompatibility: bool = False) -> Graph:
16371638
"""
1639+
Load a Graph from a Meshroom Graph (.mg) file.
1640+
1641+
Args:
1642+
filepath: The path to the Meshroom Graph file.
1643+
strictCompatibility: If True, raise a GraphCompatibilityError if the loaded Graph has node compatibility issues.
1644+
1645+
Returns:
1646+
Graph: The loaded Graph instance.
1647+
1648+
Raises:
1649+
CompatibilityError: If the Graph has node compatibility issues and `strictCompatibility` is False.
16381650
"""
16391651
graph = Graph("")
16401652
graph.load(filepath)
1653+
1654+
compatibilityIssues = len(graph.compatibilityNodes) > 0
1655+
if compatibilityIssues and strictCompatibility:
1656+
raise GraphCompatibilityError(filepath, {n.name: str(n.issue) for n in graph.compatibilityNodes})
1657+
16411658
graph.update()
16421659
return graph
16431660

meshroom/core/typing.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import os
2+
from typing import Union
3+
4+
5+
# PathLike: A type that can be used to indicate a file system path.
6+
PathLike = Union[os.PathLike, bytes, str]

0 commit comments

Comments
 (0)