示例#1
0
		def f( threadIndex ) :

			with Gaffer.OutputRedirection( stdOut = perThreadOuts[threadIndex].append, stdErr = perThreadErrs[threadIndex].append ) :
				for i in range( 0, 100 ) :
					sys.stdout.write( "OUT %d %d" % ( threadIndex, i ) )
					sys.stderr.write( "ERR %d %d" % ( threadIndex, i ) )
			time.sleep( 0.001 )
    def testRedirection(self):

        out = []
        err = []
        with Gaffer.OutputRedirection(stdOut=out.append, stdErr=err.append):

            sys.stdout.write("OUT")
            print "PRINT",
            sys.stderr.write("ERR")

        self.assertEqual(out, ["OUT", "PRINT"])
        self.assertEqual(err, ["ERR"])

        sys.stdout.write("")
        sys.stderr.write("")

        self.assertEqual(out, ["OUT", "PRINT"])
        self.assertEqual(err, ["ERR"])
示例#3
0
    def execute(self):

        # decide what to execute
        haveSelection = True
        toExecute = self.__inputWidget.selectedText()
        if not toExecute:
            haveSelection = False
            toExecute = self.__inputWidget.getText()

        # parse it first. this lets us give better error formatting
        # for syntax errors, and also figure out whether we can eval()
        # and display the result or must exec() only.
        try:
            parsed = ast.parse(toExecute)
        except SyntaxError as e:
            self.__outputWidget.appendHTML(self.__syntaxErrorToHTML(e))
            return

        # execute it

        self.__outputWidget.appendHTML(self.__codeToHTML(toExecute))

        with Gaffer.OutputRedirection(
                stdOut=Gaffer.WeakMethod(self.__redirectOutput),
                stdErr=Gaffer.WeakMethod(self.__redirectOutput)):
            with _MessageHandler(self.__outputWidget):
                with Gaffer.UndoScope(self.scriptNode()):
                    with self.getContext():
                        try:
                            if len(parsed.body) == 1 and isinstance(
                                    parsed.body[0], ast.Expr):
                                result = eval(toExecute, self.__executionDict,
                                              self.__executionDict)
                                if result is not None:
                                    self.__outputWidget.appendText(str(result))
                            else:
                                exec(toExecute, self.__executionDict,
                                     self.__executionDict)
                            if not haveSelection:
                                self.__inputWidget.setText("")
                        except Exception as e:
                            self.__outputWidget.appendHTML(
                                self.__exceptionToHTML())
示例#4
0
class PythonEditor(GafferUI.Editor):
    def __init__(self, scriptNode, **kw):

        self.__splittable = GafferUI.SplitContainer()

        GafferUI.Editor.__init__(self, self.__splittable, scriptNode, **kw)

        self.__outputWidget = GafferUI.MultiLineTextWidget(
            editable=False,
            wrapMode=GafferUI.MultiLineTextWidget.WrapMode.None,
            role=GafferUI.MultiLineTextWidget.Role.Code,
        )
        self.__inputWidget = GafferUI.MultiLineTextWidget(
            wrapMode=GafferUI.MultiLineTextWidget.WrapMode.None,
            role=GafferUI.MultiLineTextWidget.Role.Code,
        )

        self.__splittable.append(self.__outputWidget)
        self.__splittable.append(self.__inputWidget)

        self.__inputWidgetActivatedConnection = self.__inputWidget.activatedSignal(
        ).connect(Gaffer.WeakMethod(self.__activated))
        self.__inputWidgetDropTextConnection = self.__inputWidget.dropTextSignal(
        ).connect(Gaffer.WeakMethod(self.__dropText))

        self.__executionDict = {
            "imath": imath,
            "IECore": IECore,
            "Gaffer": Gaffer,
            "GafferUI": GafferUI,
            "root": scriptNode,
        }

    def inputWidget(self):

        return self.__inputWidget

    def outputWidget(self):

        return self.__outputWidget

    def execute(self):

        # decide what to execute
        haveSelection = True
        toExecute = self.__inputWidget.selectedText()
        if not toExecute:
            haveSelection = False
            toExecute = self.__inputWidget.getText()

        # parse it first. this lets us give better error formatting
        # for syntax errors, and also figure out whether we can eval()
        # and display the result or must exec() only.
        try:
            parsed = ast.parse(toExecute)
        except SyntaxError, e:
            self.__outputWidget.appendHTML(self.__syntaxErrorToHTML(e))
            return

        # execute it

        self.__outputWidget.appendHTML(self.__codeToHTML(toExecute))

        with Gaffer.OutputRedirection(
                stdOut=Gaffer.WeakMethod(self.__redirectOutput),
                stdErr=Gaffer.WeakMethod(self.__redirectOutput)):
            with _MessageHandler(self.__outputWidget):
                with Gaffer.UndoScope(self.scriptNode()):
                    with self.getContext():
                        try:
                            if len(parsed.body) == 1 and isinstance(
                                    parsed.body[0], ast.Expr):
                                result = eval(toExecute, self.__executionDict,
                                              self.__executionDict)
                                if result is not None:
                                    self.__outputWidget.appendText(str(result))
                            else:
                                exec(toExecute, self.__executionDict,
                                     self.__executionDict)
                            if not haveSelection:
                                self.__inputWidget.setText("")
                        except Exception, e:
                            self.__outputWidget.appendHTML(
                                self.__exceptionToHTML())