Skip to content

Commit 346d78d

Browse files
committed
Adapt unittests to deal with graph saving
1 parent cd219fd commit 346d78d

4 files changed

Lines changed: 45 additions & 31 deletions

File tree

meshroom/core/graph.py

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,19 @@ def inner(self, *args, **kwargs):
167167
return inner
168168

169169

170+
def generateTempProjectFilepath(tmpFolder=None):
171+
"""
172+
Generate a temporary project filepath.
173+
This method is used to generate a temporary project file for the current graph.
174+
"""
175+
from datetime import datetime
176+
if tmpFolder is None:
177+
from meshroom.env import EnvVar
178+
tmpFolder = EnvVar.get(EnvVar.MESHROOM_TEMP_PATH)
179+
timestamp = datetime.now().strftime("%Y-%m-%d_%H-%M")
180+
return os.path.join(tmpFolder, f"meshroom_{timestamp}.mg")
181+
182+
170183
class Graph(BaseObject):
171184
"""
172185
_________________ _________________ _________________
@@ -1316,7 +1329,7 @@ def save(self, filepath=None, setupProjectFile=True, template=False):
13161329
def _save(self, filepath=None, setupProjectFile=True, template=False):
13171330
path = filepath or self._filepath
13181331
if not path:
1319-
raise ValueError("filepath must be specified for unsaved files.")
1332+
path = generateTempProjectFilepath()
13201333

13211334
data = self.serialize(template)
13221335

@@ -1329,6 +1342,21 @@ def _save(self, filepath=None, setupProjectFile=True, template=False):
13291342
# update the file date version
13301343
self._fileDateVersion = os.path.getmtime(path)
13311344

1345+
def saveAsTemp(self, tmpFolder=None):
1346+
"""
1347+
Save the current Meshroom graph as a temporary project file.
1348+
"""
1349+
# Update the saving flag indicating that the current graph is being saved
1350+
self._saving = True
1351+
try:
1352+
self._saveAsTemp(tmpFolder)
1353+
finally:
1354+
self._saving = False
1355+
1356+
def _saveAsTemp(self, tmpFolder=None):
1357+
projectPath = generateTempProjectFilepath(tmpFolder)
1358+
self._save(projectPath)
1359+
13321360
def _setFilepath(self, filepath):
13331361
"""
13341362
Set the internal filepath of this Graph.
@@ -1594,6 +1622,8 @@ def executeGraph(graph, toNodes=None, forceCompute=False, forceStatus=False):
15941622

15951623
print('Nodes to execute: ', str([n.name for n in nodes]))
15961624

1625+
graph.save()
1626+
15971627
for node in nodes:
15981628
node.beginSequence(forceCompute)
15991629

meshroom/ui/graph.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
from meshroom.core import sessionUid
2525
from meshroom.common.qt import QObjectListModel
2626
from meshroom.core.attribute import Attribute, ListAttribute
27-
from meshroom.core.graph import Graph, Edge
27+
from meshroom.core.graph import Graph, Edge, generateTempProjectFilepath
2828
from meshroom.core.graphIO import GraphIO
2929

3030
from meshroom.core.taskManager import TaskManager
@@ -516,11 +516,8 @@ def _saveAs(self, url, setupProjectFile=True, template=False):
516516

517517
@Slot()
518518
def saveAsTemp(self):
519-
from meshroom.env import EnvVar
520-
from datetime import datetime
521-
tempFolder = EnvVar.get(EnvVar.MESHROOM_TEMP_PATH)
522-
timestamp = datetime.now().strftime("%Y-%m-%d_%H:%M")
523-
self._saveAs(os.path.join(tempFolder, f"meshroom_{timestamp}.mg"))
519+
projectPath = generateTempProjectFilepath()
520+
self._saveAs(projectPath)
524521

525522
@Slot()
526523
def save(self):

tests/conftest.py

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,6 @@
66
from meshroom.core.graph import Graph
77

88

9-
@pytest.fixture
10-
def graphWithIsolatedCache():
11-
"""
12-
Yield a Graph instance using a unique temporary cache directory.
13-
14-
Can be used for testing graph computation in isolation, without having to save the graph to disk.
15-
"""
16-
with tempfile.TemporaryDirectory() as cacheDir:
17-
graph = Graph("")
18-
graph.cacheDir = cacheDir
19-
yield graph
20-
21-
229
@pytest.fixture
2310
def graphSavedOnDisk():
2411
"""
@@ -27,6 +14,6 @@ def graphSavedOnDisk():
2714
Can be used for testing graph IO and computation in isolation.
2815
"""
2916
with tempfile.TemporaryDirectory() as cacheDir:
30-
graph = Graph("")
31-
graph.save(Path(cacheDir) / "test_graph.mg")
17+
graph = Graph()
18+
graph.saveAsTemp(cacheDir)
3219
yield graph

tests/test_nodeAttributeChangedCallback.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from meshroom.core.node import Node
66

77

8-
class NodeWithAttributeChangedCallback(desc.Node):
8+
class NodeWithAttributeChangedCallback(desc.BaseNode):
99
"""
1010
A Node containing an input Attribute with an 'on{Attribute}Changed' method,
1111
called whenever the value of this attribute is changed explicitly.
@@ -182,7 +182,7 @@ def test_loadingGraphDoesNotTriggerCallbackForConnectedAttributes(
182182
assert loadedNodeB.affectedInput.value == 2
183183

184184

185-
class NodeWithCompoundAttributes(desc.Node):
185+
class NodeWithCompoundAttributes(desc.BaseNode):
186186
"""
187187
A Node containing a variation of compound attributes (List/Groups),
188188
called whenever the value of this attribute is changed explicitly.
@@ -311,7 +311,7 @@ def test_connectionToListElementInGroup(self):
311311
assert nodeB.affectedInput.value == 20
312312

313313

314-
class NodeWithDynamicOutputValue(desc.Node):
314+
class NodeWithDynamicOutputValue(desc.BaseNode):
315315
"""
316316
A Node containing an output attribute which value is computed dynamically during graph execution.
317317
"""
@@ -363,9 +363,9 @@ def test_connectingUncomputedDynamicOutputDoesNotTriggerDownstreamAttributeChang
363363
assert nodeB.affectedInput.value == 0
364364

365365
def test_connectingComputedDynamicOutputTriggersDownstreamAttributeChangedCallback(
366-
self, graphWithIsolatedCache
366+
self, graphSavedOnDisk
367367
):
368-
graph: Graph = graphWithIsolatedCache
368+
graph: Graph = graphSavedOnDisk
369369
nodeA = graph.addNewNode(NodeWithDynamicOutputValue.__name__)
370370
nodeB = graph.addNewNode(NodeWithAttributeChangedCallback.__name__)
371371

@@ -377,9 +377,9 @@ def test_connectingComputedDynamicOutputTriggersDownstreamAttributeChangedCallba
377377
assert nodeB.affectedInput.value == 40
378378

379379
def test_dynamicOutputValueComputeDoesNotTriggerDownstreamAttributeChangedCallback(
380-
self, graphWithIsolatedCache
380+
self, graphSavedOnDisk
381381
):
382-
graph: Graph = graphWithIsolatedCache
382+
graph: Graph = graphSavedOnDisk
383383
nodeA = graph.addNewNode(NodeWithDynamicOutputValue.__name__)
384384
nodeB = graph.addNewNode(NodeWithAttributeChangedCallback.__name__)
385385

@@ -392,9 +392,9 @@ def test_dynamicOutputValueComputeDoesNotTriggerDownstreamAttributeChangedCallba
392392

393393

394394
def test_clearingDynamicOutputValueDoesNotTriggerDownstreamAttributeChangedCallback(
395-
self, graphWithIsolatedCache
395+
self, graphSavedOnDisk
396396
):
397-
graph: Graph = graphWithIsolatedCache
397+
graph: Graph = graphSavedOnDisk
398398
nodeA = graph.addNewNode(NodeWithDynamicOutputValue.__name__)
399399
nodeB = graph.addNewNode(NodeWithAttributeChangedCallback.__name__)
400400

0 commit comments

Comments
 (0)