def test_ScriptRunner_encoding(self): out = StdOutCollector() ScriptRunner("# -*- coding: utf-8 -*-\nprint(1/2)", stdout=out, stderr=out) self.assertEqual(out.lines(), ["0.5"]) out = StdOutCollector() ScriptRunner(u"# -*- coding: utf-8 -*-\nprint(1/2)", stdout=out, stderr=out) self.assertEqual(out.lines(), ["0.5"])
def test_ScriptRunner_io(self): out = io.StringIO() ScriptRunner("print('hey!')", stdout=out, stderr=out) self.assertEqual(out.getvalue(), u'hey!\n') out = io.StringIO() ScriptRunner("print(u'hey!')", stdout=out, stderr=out) self.assertEqual(out.getvalue(), u'hey!\n') out = io.StringIO() ScriptRunner(u"print('hey!')", stdout=out, stderr=out) self.assertEqual(out.getvalue(), u'hey!\n') out = io.StringIO() ScriptRunner(u"print(u'hey!')", stdout=out, stderr=out) self.assertEqual(out.getvalue(), u'hey!\n')
def test_ScriptRunner_checkSyntaxOnly(self): out = StdOutCollector() ScriptRunner("print(aaaa)", stdout=out, stderr=out, checkSyntaxOnly=True) self.assertEqual(out.lines(), []) out = StdOutCollector() ScriptRunner("print('hello world!')", stdout=out, stderr=out, checkSyntaxOnly=False) self.assertEqual(out.lines(), ['hello world!']) out = StdOutCollector() ScriptRunner("print('hello world!')", stdout=out, stderr=out, checkSyntaxOnly=True) self.assertEqual(out.lines(), []) out = StdOutCollector() ScriptRunner("aaa bbb", stdout=out, stderr=out, checkSyntaxOnly=True) self.assertEqual(out.lines()[-1], 'SyntaxError: invalid syntax')
def test_ScriptRunner_namespace(self): out = StdOutCollector() ScriptRunner("print(aaaa)", stdout=out, stderr=out, namespace=dict(aaaa=123)) self.assertEqual(out.lines(), ["123"])
def test_ScriptRunner_print_function(self): out = StdOutCollector() ScriptRunner("print 'hey!'", stdout=out, stderr=out) self.assertEqual( out.lines()[-1], "SyntaxError: Missing parentheses in call to 'print'. Did you mean print('hey!')?" )
def runCode(self): """ Runs a PageBot script. """ path = self.getPath() if path is None: return _drawBotDrawingTool.newDrawing() namespace = {} _drawBotDrawingTool._addToNamespace(namespace) # Creates a new standard output, catching all print statements and tracebacks. self.output = [] self.stdout = StdOutput(self.output, outputView=self.outputWindow.outputView) self.stderr = StdOutput(self.output, isError=True, outputView=self.outputWindow.outputView) # Calls DrawBot's ScriptRunner with above parameters. ScriptRunner(None, path, namespace=namespace, stdout=self.stdout, stderr=self.stderr) self.printErrors()
def checkSyntax(self, sender=None): # get the code code = self.code() # get te path of the document (will be None for an untitled document) path = self.path() # when enabled clear the output text view if getDefault("DrawBotClearOutput", True): self.outPutView.set("") # create a new std output, catching all print statements and tracebacks self.output = [] self.stdout = StdOutput(self.output) self.stderr = StdOutput(self.output, True) # run the code, but with the optional flag checkSyntaxOnly so it will just compile the code ScriptRunner(code, path, stdout=self.stdout, stderr=self.stderr, checkSyntaxOnly=True) # set the catched print statements and tracebacks in the the output text view for text, isError in self.output: self.outPutView.append(text, isError) # clean up self.output = None self.stdout = None self.stderr = None
def hitCallback(sender): script = 'fill(random(), random(), random())\nrect(10+random()*100, 10+random()*100, 200, 300)' _drawBotDrawingTool.newDrawing() namespace = DrawBotNamespace(_drawBotDrawingTool, _drawBotDrawingTool._magicVariables) _drawBotDrawingTool._addToNamespace(namespace) # Creates a new standard output, catching all print statements and tracebacks. output = [] stdout = StdOutput(output, outputView=outputWindow.outputView) stderr = StdOutput(output, isError=True, outputView=outputWindow.outputView) # Calls DrawBot's ScriptRunner with above parameters. ScriptRunner(script, None, namespace=namespace, stdout=stdout, stderr=stderr) context = getContextForFileExt('pdf') _drawBotDrawingTool._drawInContext(context) pdfDocument = _drawBotDrawingTool.pdfImage() w.drawView.setPDFDocument(pdfDocument)
def test_ScriptRunner_file(self): out = StdOutCollector() path = os.path.join( testDataDir, "scriptRunnerTest.py" ) # use an actual file, no not confuse coverage testing ScriptRunner("print(__file__)\nprint(__name__)", stdout=out, stderr=out, path=path) self.assertEqual(out.lines(), [path, "__main__"])
def test_ScriptRunner_oldDivision(self): realGetDefault = drawBot.scriptTools.getDefault def mockedGetDefault(*args): return False drawBot.scriptTools.getDefault = mockedGetDefault try: out = StdOutCollector() ScriptRunner("print(1/2)", stdout=out, stderr=out) self.assertEqual(out.lines(), ["0.5"]) finally: drawBot.scriptTools.getDefault = realGetDefault
def test_ScriptRunner_io(self): if PY3: MyStringIO = io.StringIO else: class MyStringIO(io.StringIO): def write(self, value): if not isinstance(value, unicode): value = value.decode("utf8") super(MyStringIO, self).write(value) out = MyStringIO() ScriptRunner("print('hey!')", stdout=out, stderr=out) self.assertEqual(out.getvalue(), u'hey!\n') out = MyStringIO() ScriptRunner("print(u'hey!')", stdout=out, stderr=out) self.assertEqual(out.getvalue(), u'hey!\n') out = MyStringIO() ScriptRunner(u"print('hey!')", stdout=out, stderr=out) self.assertEqual(out.getvalue(), u'hey!\n') out = MyStringIO() ScriptRunner(u"print(u'hey!')", stdout=out, stderr=out) self.assertEqual(out.getvalue(), u'hey!\n')
def run(self): """ Execute the .drawBot package. Return if executing was succesfull with a report on failure. """ # check if the package can run in this version of DrawBot if LooseVersion(self.info.requiresVersion) > drawBotVersion: return False, "Requires a newer version of DrawBot (%s)." % self.info.requiresVersion # get the main scriptin path path = self.mainScriptPath() if path is None: return False, "Cannot execute an empty package." if not os.path.exists(path): return False, "Cannot find '%s'." % path # run the script ScriptRunner(path=path) return True, ""
def test_ScriptRunner_fromPath(self): out = StdOutCollector() path = os.path.join(testDataDir, "scriptRunnerTest.py") ScriptRunner(path=path, stdout=out, stderr=out) self.assertEqual(out.lines(), [path, "__main__", u'\xc5benr\xe5'])
def test_ScriptRunner_division(self): out = StdOutCollector() ScriptRunner("print(1/2)", stdout=out, stderr=out) self.assertEqual(out.lines(), ["0.5"])
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 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 test_ScriptRunner_StdOutCollector(self): out = StdOutCollector() ScriptRunner("print('hey!')", stdout=out, stderr=out) self.assertEqual(out.lines(), ["hey!"])