Example #1
0
    def runCode(self, liveCoding=False):
        # get the code
        code = self.code()
        # save the code in the defaults, if something goes wrong
        setDefault("DrawBotCodeBackup", code)
        # get te path of the document (will be None for an untitled document)
        path = self.path()
        # reset the internal warning system
        warnings.resetWarnings()
        # reset the drawing tool
        _drawBotDrawingTool._reset()
        # create a namespace
        namespace = DrawBotNamespace(_drawBotDrawingTool, _drawBotDrawingTool._magicVariables)
        # add the tool callbacks in the name space
        _drawBotDrawingTool._addToNamespace(namespace)
        # when enabled clear the output text view
        if getDefault("DrawBotClearOutput", True):
            self.outPutView.clear()
        # create a new std output, catching all print statements and tracebacks
        self.output = []
        self.stdout = StdOutput(self.output)
        self.stderr = StdOutput(self.output, True)
        # warnings should show the warnings
        warnings.shouldShowWarnings = True
        # run the code
        ScriptRunner(code, path, namespace=namespace, stdout=self.stdout, stderr=self.stderr)
        # warnings should stop posting them
        warnings.shouldShowWarnings = False
        # set context, only when the panes are visible
        if self.w.split.isPaneVisible("drawView") or self.w.split.isPaneVisible("thumbnails"):
            def createContext(context):
                # draw the tool in to the context
                _drawBotDrawingTool._drawInContext(context)
            # create a context to draw in
            context = DrawBotContext()
            # savely run the callback and track all traceback back the the output
            CallbackRunner(createContext, stdout=self.stdout, stderr=self.stderr, args=[context])
            # get the pdf document and set in the draw view
            pdfDocument = context.getNSPDFDocument()
            if not liveCoding or (pdfDocument and pdfDocument.pageCount()):
                self.drawView.setPDFDocument(pdfDocument)
            # scroll down
            self.drawView.scrollDown()
        else:
            # if the panes are not visible, clear the draw view
            self.drawView.setPDFDocument(None)

        # set the catched print statements and tracebacks in the the output text view
        for text, isError in self.output:
            if liveCoding and isError:
                continue
            self.outPutView.append(text, isError)

        # reset the code backup if the script runs with any crashes
        setDefault("DrawBotCodeBackup", None)
        # clean up
        
        self.output = None
        self.stdout = None
        self.stderr = None
Example #2
0
    def executeScriptPath(self, path):
        # read content of py file and exec it
        from drawBot.misc import warnings

        with open(path) as f:
            source = f.read()
        code = compile(source, path, "exec")
        namespace = {"__name__": "__main__", "__file__": path}
        warnings.resetWarnings()  # so we can test DB warnings

        with StdOutCollector(captureStdErr=True) as output:
            cwd = os.getcwd()
            os.chdir(os.path.dirname(path))
            try:
                exec(code, namespace)
            except Exception:
                traceback.print_exc()
            os.chdir(cwd)
        return output.lines()
Example #3
0
    def runCode(self, liveCoding=False):
        # get the code
        code = self.code()
        # code = code.encode("utf-8")
        # save the code in the defaults, if something goes wrong
        setDefault("DrawBotCodeBackup", code)
        # get te path of the document (will be None for an untitled document)
        path = self.path()
        # reset the internal warning system
        warnings.resetWarnings()
        # reset the drawing tool
        _drawBotDrawingTool.newDrawing()
        # create a namespace
        namespace = DrawBotNamespace(_drawBotDrawingTool,
                                     _drawBotDrawingTool._magicVariables)
        # add the tool callbacks in the name space
        _drawBotDrawingTool._addToNamespace(namespace)
        # when enabled clear the output text view
        if getDefault("DrawBotClearOutput", True):
            self.outPutView.clear()
        # create a new std output, catching all print statements and tracebacks
        self.output = []

        liveOutput = None
        if getDefault("DrawButLiveUpdateStdoutStderr", False):
            liveOutput = self.outPutView

        self.stdout = StdOutput(self.output, outputView=liveOutput)
        self.stderr = StdOutput(self.output,
                                isError=True,
                                outputView=liveOutput)
        # warnings should show the warnings
        warnings.shouldShowWarnings = True
        # run the code
        ScriptRunner(code,
                     path,
                     namespace=namespace,
                     stdout=self.stdout,
                     stderr=self.stderr)
        # warnings should stop posting them
        warnings.shouldShowWarnings = False
        # set context, only when the panes are visible
        if self.w.split.isPaneVisible(
                "drawView") or self.w.split.isPaneVisible("thumbnails"):

            def createContext(context):
                # draw the tool in to the context
                _drawBotDrawingTool._drawInContext(context)

            # create a context to draw in
            context = DrawBotContext()
            # savely run the callback and track all traceback back the output
            CallbackRunner(createContext,
                           stdout=self.stdout,
                           stderr=self.stderr,
                           args=[context])
            # get the pdf document and set in the draw view
            pdfDocument = context.getNSPDFDocument()
            selectionIndex = self.thumbnails.getSelection()
            if not liveCoding or (pdfDocument and pdfDocument.pageCount()):
                self.drawView.setPDFDocument(pdfDocument)
            # scroll to the original position
            self.drawView.scrollToPageIndex(selectionIndex)
        else:
            # if the panes are not visible, clear the draw view
            self.drawView.setPDFDocument(None)
        # drawing is done
        _drawBotDrawingTool.endDrawing()
        # set the catched print statements and tracebacks in the the output text view
        for text, isError in self.output:
            if liveCoding and isError:
                continue
            self.outPutView.append(text, isError)

        # reset the code backup if the script runs with any crashes
        setDefault("DrawBotCodeBackup", None)
        # clean up

        self.output = None
        self.stdout = None
        self.stderr = None
    def runCode(self, liveCoding=False):
        # get the code
        try:
            code = self.document().text
            print "__runCode 1", code
            # get the path of the document (will be None for an untitled document)
            path = None
            try:
                path = self.document().fileURL().path()
            except:
                pass
            print "__runCode 2", path
            # reset the internal warning system
            warnings.resetWarnings()
            # reset the drawing tool
            _drawBotDrawingTool.newDrawing()
            # create a namespace
            namespace = DrawBotNamespace(_drawBotDrawingTool,
                                         _drawBotDrawingTool._magicVariables)
            # add the tool callbacks in the name space
            _drawBotDrawingTool._addToNamespace(namespace)
            # when enabled clear the output text view
            #if getDefault("PyDEClearOutput", True):
            #	self.outPutView.clear()
            # create a new std output, catching all print statements and tracebacks
            self.output = []
            stdout = StdOutput(self.output)
            stderr = StdOutput(self.output, True)
            sys.argv = [path]
            # warnings should show the warnings
            warnings.shouldShowWarnings = True
            # run the code
            ScriptRunner(code, path, stdout, stderr, namespace=namespace)
            # warnings should stop posting them
            warnings.shouldShowWarnings = False
            # set context, only when the panes are visible
            print "__set context"
            if self.w.split.isPaneVisible(
                    "drawView") or self.w.split.isPaneVisible("thumbnails"):
                print "__drawView"

                def createContext(context):
                    # draw the tool in to the context
                    _drawBotDrawingTool._drawInContext(context)

                # create a context to draw in
                context = DrawBotContext()
                # savely run the callback and track all traceback back the the output
                CallbackRunner(createContext,
                               stdout=None,
                               stderr=None,
                               args=[context])
                # get the pdf document and set in the draw view
                pdfDocument = context.getNSPDFDocument()
                if not liveCoding or (pdfDocument and pdfDocument.pageCount()):
                    self.drawView.setPDFDocument(pdfDocument)
                # scroll down
                self.drawView.scrollDown()
            else:
                print "__setPDF"
                # if the panes are not visible, clear the draw view
                self.drawView.setPDFDocument(None)

            # set the catched print statements and tracebacks in the the output text view
            for text, isError in self.output:
                if liveCoding and isError:
                    continue
                self.outPutView.append(text, isError)

            # reset the code backup if the script runs with any crashes
            #setDefault("pythonCodeBackup", None)
            # clean up

            self.output = None
            # self.stdout = None
            # self.stderr = None
        except Exception, e:
            print "-- Error", e
            print(traceback.format_exc())
            print "-- Error/"
	def runCode(self, liveCoding=False):
		# get the code
		try:
			code = self.document().text
			print "__runCode 1", code
			# get the path of the document (will be None for an untitled document)
			path = None
			try:
				path = self.document().fileURL().path()
			except:
				pass
			print "__runCode 2", path
			# reset the internal warning system
			warnings.resetWarnings()
			# reset the drawing tool
			_drawBotDrawingTool.newDrawing()
			# create a namespace
			namespace = DrawBotNamespace(_drawBotDrawingTool, _drawBotDrawingTool._magicVariables)
			# add the tool callbacks in the name space
			_drawBotDrawingTool._addToNamespace(namespace)
			# when enabled clear the output text view
			#if getDefault("PyDEClearOutput", True):
			#	self.outPutView.clear()
			# create a new std output, catching all print statements and tracebacks
			self.output = []
			stdout=StdOutput(self.output)
			stderr=StdOutput(self.output, True)
			sys.argv = [path]
			# warnings should show the warnings
			warnings.shouldShowWarnings = True
			# run the code
			ScriptRunner(code, path, stdout, stderr, namespace=namespace)
			# warnings should stop posting them
			warnings.shouldShowWarnings = False
			# set context, only when the panes are visible
			print "__set context"
			if self.w.split.isPaneVisible("drawView") or self.w.split.isPaneVisible("thumbnails"):
				print "__drawView"
				def createContext(context):
					# draw the tool in to the context
					_drawBotDrawingTool._drawInContext(context)
				# create a context to draw in
				context = DrawBotContext()
				# savely run the callback and track all traceback back the the output
				CallbackRunner(createContext, stdout=None, stderr=None, args=[context])
				# get the pdf document and set in the draw view
				pdfDocument = context.getNSPDFDocument()
				if not liveCoding or (pdfDocument and pdfDocument.pageCount()):
					self.drawView.setPDFDocument(pdfDocument)
				# scroll down
				self.drawView.scrollDown()
			else:
				print "__setPDF"
				# if the panes are not visible, clear the draw view
				self.drawView.setPDFDocument(None)

			# set the catched print statements and tracebacks in the the output text view
			for text, isError in self.output:
				if liveCoding and isError:
					continue
				self.outPutView.append(text, isError)

			# reset the code backup if the script runs with any crashes
			#setDefault("pythonCodeBackup", None)
			# clean up

			self.output = None
			# self.stdout = None
			# self.stderr = None
		except Exception, e:
			print "-- Error", e
			print(traceback.format_exc())
			print "-- Error/"