def testPreferencesPermissionsErrors( self ) : a = Gaffer.ApplicationRoot( "testApp" ) a.savePreferences( self.__preferencesFile ) os.chmod( self.__preferencesFile, 0 ) self.assertRaises( RuntimeError, a.savePreferences, self.__preferencesFile )
def testAddRecent(self): a = Gaffer.ApplicationRoot("testApp") b = GafferUI.Bookmarks.acquire(a, Gaffer.FileSystemPath, category=None) self.assertEqual(b.names(), []) b.addRecent("/a") self.assertEqual(b.names(), []) self.assertEqual(b.recents(), ["/a"]) b.addRecent("/b") b.addRecent("/c") self.assertEqual(b.names(), []) self.assertEqual(b.recents(), ["/a", "/b", "/c"]) b.addRecent("/a") self.assertEqual(b.names(), []) self.assertEqual(b.recents(), ["/b", "/c", "/a"]) b.addRecent("/d") b.addRecent("/e") b.addRecent("/f") self.assertEqual(b.names(), []) self.assertEqual(b.recents(), ["/b", "/c", "/a", "/d", "/e", "/f"]) b.addRecent("/g") self.assertEqual(b.names(), []) self.assertEqual(b.recents(), ["/c", "/a", "/d", "/e", "/f", "/g"])
def testSetNodeSetDriverRestore(self): s = Gaffer.ScriptNode() c = GafferUI.CompoundEditor(s) GafferUI.NodeSetEditor.registerNodeSetDriverMode( "testMode", lambda e, t: t.getNodeSet()) editors = list((GafferUI.NodeEditor(s), GafferUI.NodeEditor(s), GafferUI.AnimationEditor(s), GafferUI.NodeEditor(s))) editors[0].setNodeSetDriver(editors[1]) editors[2].setNodeSetDriver(editors[3], "testMode") for e in editors: c.addEditor(e) a = Gaffer.ApplicationRoot("testApp") l = GafferUI.Layouts.acquire(a) l.add("ReprDriverTest", repr(c), persistent=False) cc = l.create("ReprDriverTest", s) editors = cc.editors() driver, mode = editors[0].getNodeSetDriver() self.assertTrue(driver is editors[1]) self.assertTrue(mode is GafferUI.NodeSetEditor.DriverModeNodeSet) driver, mode = editors[2].getNodeSetDriver() self.assertTrue(driver is editors[3]) self.assertTrue(mode is "testMode") driver, mode = editors[3].getNodeSetDriver() self.assertIsNone(driver)
def testPreferencesLocation(self): a = Gaffer.ApplicationRoot("testApp") self.assertEqual(a.preferencesLocation(), os.path.dirname(self.__defaultPreferencesFile)) self.failUnless(os.path.isdir(a.preferencesLocation()))
def testRestore(self): s = Gaffer.ScriptNode() c = GafferUI.CompoundEditor(s) editors = list((GafferUI.NodeEditor(s), GafferUI.AnimationEditor(s), GafferUI.GraphEditor(s), GafferUI.PythonEditor(s))) editorTypes = [type(e) for e in editors] for e in editors[:2]: c.addEditor(e) p = c._createDetachedPanel() for e in editors[2:]: p.addEditor(e) self.assertEqual(len(c._detachedPanels()), 1) self.assertEqual(c.editors(), editors) a = Gaffer.ApplicationRoot("testApp") l = GafferUI.Layouts.acquire(a) l.add("ReprTest", repr(c), persistent=False) cc = l.create("ReprTest", s) self.assertEqual(len(cc._detachedPanels()), 1) ct = [type(e) for e in cc.editors()] self.assertEqual(ct, editorTypes) self.assertEqual(repr(cc.editors()), repr(editors))
def testPasteWithContinueOnError( self ) : app = Gaffer.ApplicationRoot() script = Gaffer.ScriptNode() app["scripts"]["s"] = script app.setClipboardContents( IECore.StringData( inspect.cleandoc( """ iAmAnError parent.addChild( Gaffer.Node() ) """ ) ) ) six.assertRaisesRegex( self, RuntimeError, "iAmAnError", script.paste ) self.assertEqual( len( script.children( Gaffer.Node ) ), 0 ) with IECore.CapturingMessageHandler() as mh : script.paste( continueOnError = True ) self.assertEqual( len( mh.messages ), 1 ) self.assertEqual( mh.messages[0].level, IECore.Msg.Level.Error ) self.assertTrue( "iAmAnError" in mh.messages[0].message ) self.assertEqual( len( script.children( Gaffer.Node ) ), 1 )
def testCopyIgnoresNestedSelections(self): a = Gaffer.ApplicationRoot() s = Gaffer.ScriptNode() a["scripts"].addChild(s) s["n1"] = GafferTest.AddNode() s["n2"] = GafferTest.AddNode() s["b"] = Gaffer.Box() s["b"]["n1"] = GafferTest.AddNode() s.selection().add(s["n1"]) s.selection().add(s["b"]["n1"]) s.copy(filter=s.selection()) s2 = Gaffer.ScriptNode() a["scripts"].addChild(s2) s2.paste() self.assertTrue("n1" in s2) self.assertTrue("b" not in s2) s.selection().clear() s.selection().add(s["b"]["n1"]) s.copy(filter=s.selection()) s2 = Gaffer.ScriptNode() a["scripts"].addChild(s2) s2.paste() self.assertTrue("b" not in s2) self.assertTrue("n1" not in s2)
def testSingleBackup(self): a = Gaffer.ApplicationRoot() b = GafferUI.Backups.acquire(a) b.settings()["fileName"].setValue(self.temporaryDirectory() + "/backups/${script:name}.gfr") s = Gaffer.ScriptNode() s["fileName"].setValue(self.temporaryDirectory() + "/test.gfr") a["scripts"].addChild(s) s.save() backupFileName = self.temporaryDirectory() + "/backups/test.gfr" self.assertFalse(os.path.exists(backupFileName)) self.assertEqual(b.backups(s), []) self.assertEqual(b.backup(s), backupFileName) self.assertTrue(os.path.exists(backupFileName)) self.assertEqual(b.backups(s), [backupFileName]) self.__assertFilesEqual(s["fileName"].getValue(), backupFileName) s.addChild(Gaffer.Node()) s.save() self.assertEqual(b.backup(s), backupFileName) self.assertEqual(b.backups(s), [backupFileName]) self.__assertFilesEqual(s["fileName"].getValue(), backupFileName)
def testCutWithSpecificSourceParent(self): a = Gaffer.ApplicationRoot() s = Gaffer.ScriptNode() a["scripts"].addChild(s) s["n1"] = GafferTest.AddNode() s["n2"] = GafferTest.AddNode() s["b"] = Gaffer.Box() s["b"]["n3"] = GafferTest.AddNode() s["b"]["n4"] = GafferTest.AddNode() s.selection().add(s["n1"]) s.selection().add(s["b"]["n3"]) s.cut(parent=s["b"], filter=s.selection()) self.assertTrue("n1" in s) self.assertTrue("n2" in s) self.assertTrue("b" in s) self.assertTrue("n3" not in s["b"]) self.assertTrue("n4" in s["b"]) s2 = Gaffer.ScriptNode() a["scripts"].addChild(s2) s2.paste() self.assertTrue("n1" not in s2) self.assertTrue("n2" not in s2) self.assertTrue("b" not in s2) self.assertTrue("n3" in s2) self.assertTrue("n4" not in s2)
def testClipboard( self ) : a = Gaffer.ApplicationRoot() d = [] def f( app ) : self.assertTrue( app.isSame( a ) ) d.append( app.getClipboardContents() ) c = a.clipboardContentsChangedSignal().connect( f ) self.assertEqual( len( d ), 0 ) self.assertEqual( a.getClipboardContents(), None ) a.setClipboardContents( IECore.IntData( 10 ) ) self.assertEqual( len( d ), 1 ) self.assertEqual( a.getClipboardContents(), IECore.IntData( 10 ) ) a.setClipboardContents( IECore.IntData( 20 ) ) self.assertEqual( len( d ), 2 ) self.assertEqual( a.getClipboardContents(), IECore.IntData( 20 ) ) a.setClipboardContents( IECore.IntData( 20 ) ) self.assertEqual( len( d ), 2 ) self.assertEqual( a.getClipboardContents(), IECore.IntData( 20 ) )
def testApplicationRoot(self): s = Gaffer.ScriptNode() self.failUnless(s.applicationRoot() is None) a = Gaffer.ApplicationRoot() a["scripts"]["one"] = s self.failUnless(s.applicationRoot().isSame(a))
def testApplicationRoot( self ) : s = Gaffer.ScriptNode() self.assertIsNone( s.applicationRoot() ) a = Gaffer.ApplicationRoot() a["scripts"]["one"] = s self.assertTrue( s.applicationRoot().isSame( a ) )
def testRecentsUseFullPaths(self): a = Gaffer.ApplicationRoot("testApp") b = GafferUI.Bookmarks.acquire(a, Gaffer.FileSystemPath, category=None) self.assertEqual(b.recents(), []) b.addRecent("/a/b") b.addRecent("/b/b") self.assertEqual(b.recents(), ["/a/b", "/b/b"])
def testAncestor(self): a = Gaffer.ApplicationRoot() s = Gaffer.ScriptNode() a["scripts"]["one"] = s n = GafferTest.AddNode() s["node"] = n self.assert_(n.ancestor(Gaffer.ScriptNode).isSame(s)) self.assert_(n.ancestor(Gaffer.ApplicationRoot).isSame(a))
def testRecoveryFile(self): a = Gaffer.ApplicationRoot() b = GafferUI.Backups.acquire(a) b.settings()["fileName"].setValue( "${script:directory}/${script:name}-backup${backup:number}.gfr") b.settings()["files"].setValue(3) s = Gaffer.ScriptNode() s["fileName"].setValue(self.temporaryDirectory() + "/test.gfr") self.assertEqual(b.recoveryFile(s), None) timeBetweenBackups = 0.01 if sys.platform != "darwin" else 1.1 # Script hasn't even been saved - always choose the recovery file b.backup(s) self.assertEqual(b.recoveryFile(s), self.temporaryDirectory() + "/test-backup0.gfr") time.sleep(timeBetweenBackups) b.backup(s) self.assertEqual(b.recoveryFile(s), self.temporaryDirectory() + "/test-backup1.gfr") # Script has been saved, and backups are identical. No need for recovery. s.save() self.assertEqual(b.recoveryFile(s), None) time.sleep(timeBetweenBackups) b.backup(s) self.assertEqual(b.recoveryFile(s), None) # Script has node added, but has not been saved. We need the recovery file. time.sleep(timeBetweenBackups) s.addChild(Gaffer.Node()) b.backup(s) self.assertEqual(b.recoveryFile(s), self.temporaryDirectory() + "/test-backup0.gfr") # Script saved again, no need for recovery. s.save() self.assertEqual(b.recoveryFile(s), None) # Node deleted, script not saved. We need recovery. del s["Node"] b.backup(s) self.assertEqual(b.recoveryFile(s), self.temporaryDirectory() + "/test-backup1.gfr")
def testCommonAncestor( self ) : a = Gaffer.ApplicationRoot() s = Gaffer.ScriptNode() a["scripts"]["one"] = s s["n1"] = Gaffer.Node() s["n2"] = Gaffer.Node() self.assert_( s["n1"].commonAncestor( s["n2"], Gaffer.ScriptNode ).isSame( s ) ) self.assert_( s["n2"].commonAncestor( s["n1"], Gaffer.ScriptNode ).isSame( s ) )
def testPersistence(self): a = Gaffer.ApplicationRoot("testApp") b = GafferUI.Bookmarks.acquire(a, Gaffer.FileSystemPath, category=None) b.add("a", "/a", persistent=True) b.add("b", "/b", persistent=False) self.assertEqual(b.names(), ["a", "b"]) self.assertEqual(b.names(persistent=True), ["a"]) self.assertEqual(b.names(persistent=False), ["b"])
def testScriptNodeRemovalCancellation(self): def f(canceller): while True: IECore.Canceller.check(canceller) a = Gaffer.ApplicationRoot() a["scripts"]["s"] = Gaffer.ScriptNode() a["scripts"]["s"]["n"] = GafferTest.AddNode() # If not cancelled, this task will spin forever. t = Gaffer.BackgroundTask(a["scripts"]["s"]["n"]["sum"], f) # But removing the script from the application should cancel it. del a["scripts"]["s"] t.wait()
def __init__(self, description=""): IECore.Parameterised.__init__(self, description) self.parameters().addParameters([ IECore.FileNameParameter( name="profileFileName", description="If this is specified, then the application " "is run using the cProfile profiling module, and the " "results saved to the file for later examination.", defaultValue="", allowEmptyString=True), ]) self.__root = Gaffer.ApplicationRoot(self.__class__.__name__)
def testMultipleBackups(self): a = Gaffer.ApplicationRoot() b = GafferUI.Backups.acquire(a) b.settings()["fileName"].setValue( "${script:directory}/${script:name}-backup${backup:number}.gfr") b.settings()["files"].setValue(3) s = Gaffer.ScriptNode() s["fileName"].setValue(self.temporaryDirectory() + "/test.gfr") s["add"] = GafferTest.AddNode() a["scripts"].addChild(s) numBackups = 50 timeBetweenBackups = 0.01 if sys.platform == "darwin": # HFS+ only has second resolution, so # we have to wait longer between backups # otherwise all the backups have the # same modification time and we can't # tell which is the latest. timeBetweenBackups = 1.1 # We also need to do fewer tests because # otherwise it takes an age. numBackups = 5 expectedBackups = [] for i in range(0, numBackups): s["add"]["op1"].setValue(i) s.save() backupFileName = "{0}/test-backup{1}.gfr".format( self.temporaryDirectory(), i % 3) if i < 3: self.assertFalse(os.path.exists(backupFileName)) self.assertEqual(b.backup(s), backupFileName) self.assertTrue(os.path.exists(backupFileName)) self.__assertFilesEqual(s["fileName"].getValue(), backupFileName) expectedBackups.append(backupFileName) if len(expectedBackups) > 3: del expectedBackups[0] self.assertEqual(b.backups(s), expectedBackups) time.sleep(timeBetweenBackups)
def testAcquire(self): a = Gaffer.ApplicationRoot() self.assertIsNone(GafferUI.Backups.acquire(a, createIfNecessary=False)) b = GafferUI.Backups.acquire(a) self.assertIsInstance(b, GafferUI.Backups) self.assertIs(b, GafferUI.Backups.acquire(a)) wa = weakref.ref(a) wb = weakref.ref(b) del a self.assertIsNone(wa()) del b self.assertIsNone(wb())
def testDynamic(self): w = GafferUI.TextWidget("/some/value") a = Gaffer.ApplicationRoot("testApp") b = GafferUI.Bookmarks.acquire(a, Gaffer.FileSystemPath, category=None) def f(forWidget): if isinstance(forWidget, GafferUI.TextWidget): return forWidget.getText() else: return "/" b.add("t", f) self.assertEqual(b.get("t", w), "/some/value")
def testReadOnly(self): a = Gaffer.ApplicationRoot() b = GafferUI.Backups.acquire(a) b.settings()["fileName"].setValue(self.temporaryDirectory() + "/backups/${script:name}.gfr") s = Gaffer.ScriptNode() s["fileName"].setValue(self.temporaryDirectory() + "/test.gfr") def assertBackupsReadOnly(): for f in b.backups(s): self.assertFalse(os.access(f, os.W_OK)) b.backup(s) assertBackupsReadOnly()
def testDefault(self): a = Gaffer.ApplicationRoot("testApp") b = GafferUI.Bookmarks.acquire(a) self.assertEqual(b.getDefault(), "/") self.assertEqual(b.names(), []) b.setDefault("/somewhere") self.assertEqual(b.getDefault(), "/somewhere") self.assertEqual(b.names(), []) b.setDefault("/somewhere/else") self.assertEqual(b.getDefault(), "/somewhere/else") self.assertEqual(b.names(), [])
def testPersistence(self): a = Gaffer.ApplicationRoot("testApp") l = GafferUI.Layouts.acquire(a) self.assertEqual(l.names(), []) l.add("JustTheGraphEditor", "GafferUI.GraphEditor( script )") self.assertEqual(l.names(), ["JustTheGraphEditor"]) self.assertEqual(l.names(persistent=False), ["JustTheGraphEditor"]) self.assertEqual(l.names(persistent=True), []) l.add("JustTheNodeEditor", "GafferUI.NodeEditor( script )", persistent=True) self.assertEqual(l.names(), ["JustTheGraphEditor", "JustTheNodeEditor"]) self.assertEqual(l.names(persistent=False), ["JustTheGraphEditor"]) self.assertEqual(l.names(persistent=True), ["JustTheNodeEditor"])
def testAddAndRemove(self): a = Gaffer.ApplicationRoot("testApp") l = GafferUI.Layouts.acquire(a) self.assertEqual(l.names(), []) l.add("JustTheGraphEditor", "GafferUI.GraphEditor( script )") self.assertEqual(l.names(), ["JustTheGraphEditor"]) l.add("JustTheNodeEditor", "GafferUI.NodeEditor( script )") self.assertEqual(l.names(), ["JustTheGraphEditor", "JustTheNodeEditor"]) l.remove("JustTheGraphEditor") self.assertEqual(l.names(), ["JustTheNodeEditor"]) l.remove("JustTheNodeEditor") self.assertEqual(l.names(), [])
def testCopyPaste(self): app = Gaffer.ApplicationRoot() s1 = Gaffer.ScriptNode() s2 = Gaffer.ScriptNode() app["scripts"]["s1"] = s1 app["scripts"]["s2"] = s2 n1 = GafferTest.AddNode() s1["n1"] = n1 s1.copy() s2.paste() self.assert_(s1["n1"].isInstanceOf(GafferTest.AddNode.staticTypeId())) self.assert_(s2["n1"].isInstanceOf(GafferTest.AddNode.staticTypeId()))
def testOverrideDefaultCategory(self): a = Gaffer.ApplicationRoot("testApp") bd = GafferUI.Bookmarks.acquire(a, Gaffer.FileSystemPath, category=None) bc = GafferUI.Bookmarks.acquire(a, Gaffer.FileSystemPath, category="myCategory") bd.add("a", "/a") bc.add("a", "/aa") self.assertEqual(bc.names(), ["a"]) self.assertEqual(bd.names(), ["a"]) self.assertEqual(bd.get("a"), "/a") self.assertEqual(bc.get("a"), "/aa")
def test(self): a = Gaffer.ApplicationRoot("testApp") bd = GafferUI.Bookmarks.acquire(a, Gaffer.FileSystemPath, category=None) bc = GafferUI.Bookmarks.acquire(a, Gaffer.FileSystemPath, category="myCategory") self.assertTrue(isinstance(bd, GafferUI.Bookmarks)) self.assertTrue(isinstance(bc, GafferUI.Bookmarks)) self.assertEqual(bd.names(), []) self.assertEqual(bc.names(), []) bd.add("test", "/test") self.assertEqual(bd.names(), ["test"]) self.assertEqual(bc.names(), ["test"]) self.assertEqual(bd.get("test"), "/test") self.assertEqual(bc.get("test"), "/test") bc.add("c", "/c") self.assertEqual(bd.names(), ["test"]) self.assertEqual(bc.names(), ["test", "c"]) self.assertEqual(bc.get("c"), "/c") self.assertRaises(KeyError, bd.get, "c") bc.remove("c") self.assertEqual(bd.names(), ["test"]) self.assertEqual(bc.names(), ["test"]) bc.remove("test") self.assertEqual(bd.names(), []) self.assertEqual(bc.names(), [])
def testLifeTimeAfterExecution( self ) : # the ScriptNode used to keep an internal dictionary # as the context for all script execution. this created the # danger of circular references keeping it alive forever. # that is no longer the case, but this test remains to ensure # that the same problem doesn't crop up in the future. a = Gaffer.ApplicationRoot() a["scripts"]["s"] = Gaffer.ScriptNode() a["scripts"]["s"].execute( "script.addChild( Gaffer.Node( \"a\" ) )" ) a["scripts"]["s"].execute( "circularRef = script.getChild( \"a\" ).parent()" ) w = weakref.ref( a["scripts"]["s"] ) del a["scripts"]["s"] IECore.RefCounted.collectGarbage() self.assertEqual( w(), None )