def gotResp(resp, num): def print_(n): print "DATA %s: %r" % (num, n) def printdone(n): print "DONE %s" % num print "GOT RESPONSE %s: %s" % (num, resp) stream_mod.readStream(resp.stream, print_).addCallback(printdone)
def test_errouput(self): p = self.runCode("import sys\nfor i in range(100): sys.stderr.write('x' * 1000)") l = [] d = stream.readStream(p.errStream, l.append) def verify(_): self.assertEquals("".join(l), ("x" * 1000) * 100) p.run() return d.addCallback(verify)
def test_output(self): p = self.runCode("import sys\nfor i in range(100): sys.stdout.write('x' * 1000)") l = [] d = stream.readStream(p.outStream, l.append) def verify(_): self.assertEquals("".join(l), ("x" * 1000) * 100) d2 = p.run() return d.addCallback(verify).addCallback(lambda _: d2)
def http_POST(self, request): parser, unmarshaller = xmlrpclib.getparser() deferred = stream.readStream(request.stream, parser.feed) deferred.addCallback( lambda x: self._cbDispatch(request, parser, unmarshaller)) deferred.addErrback(self._ebRender) deferred.addCallback(self._cbRender, request) return deferred
def test_processclosedinput(self): p = self.runCode("import sys; sys.stdout.write(sys.stdin.read(3));" + "sys.stdin.close(); sys.stdout.write('def')", "abc123") l = [] d = stream.readStream(p.outStream, l.append) def verify(_): self.assertEquals("".join(l), "abcdef") d2 = p.run() return d.addCallback(verify).addCallback(lambda _: d2)
def test_inputerror(self): p = self.runCode("import sys\nsys.stdout.write(sys.stdin.read())", TestStreamer(["hello", defer.fail(ZeroDivisionError())])) l = [] d = stream.readStream(p.outStream, l.append) d2 = p.run() def verify(_): self.assertEquals("".join(l), "hello") return d2 return d.addCallback(verify).addCallback(lambda _: log.flushErrors(ZeroDivisionError))
def test_input(self): p = self.runCode("import sys\nsys.stdout.write(sys.stdin.read())", "hello world") l = [] d = stream.readStream(p.outStream, l.append) d2 = p.run() def verify(_): self.assertEquals("".join(l), "hello world") return d2 return d.addCallback(verify)
def _getQuery(self, result): self.query = '' if self.request.method == 'GET': self.query = urllib.unquote(self.request.querystring) elif self.request.method == 'POST': # Returning deferred needed for chaining d = stream.readStream(self.request.stream, self._handlePostData) return d else: raise ValueError(u"Unhandled method '%s'" % self.request.method) return result
def allDataFromStream(stream, filter=None): data = [] def gotAllData(_): if not data: return None result = "".join([str(x) for x in data]) if filter is None: return result else: return filter(result) return readStream(stream, data.append).addCallback(gotAllData)
def readStreamToString(s): """ Read all data from a stream into a string. @param s: a L{OPSI.web2.stream.IByteStream} to read from. @return: a L{Deferred} results in a str """ allData = [] def gotData(data): allData.append(data) d = stream.readStream(s, gotData) d.addCallback(lambda ign: ''.join(allData)) return d
def test_render(self): """ Test that the result from Data.render is acceptable, including the response code, the content-type header, and the actual response body itself. """ response = iweb.IResponse(self.data.render(None)) self.assertEqual(response.code, 200) self.assert_(response.headers.hasHeader("content-type")) self.assertEqual(response.headers.getHeader("content-type"), http_headers.MimeType("text", "plain")) def checkStream(data): self.assertEquals(str(data), self.text) return stream.readStream( iweb.IResponse(self.data.render(None)).stream, checkStream)
def readIntoFile(stream, outFile, maxlen): """Read the stream into a file, but not if it's longer than maxlen. Returns Deferred which will be triggered on finish. """ curlen = [0] def done(_): return _ def write(data): curlen[0] += len(data) if curlen[0] > maxlen: raise MimeFormatError("Maximum length of %d bytes exceeded." % maxlen) outFile.write(data) return readStream(stream, write).addBoth(done)
def noDataFromStream(stream): def gotData(data): if data: raise ValueError("Stream contains unexpected data.") return readStream(stream, gotData)