def test_errorPropogation(self): # A generator that raises an error def gen(ctx, data): yield 1 pop yield 2 # Hacky imports from cStringIO import StringIO from nevow.flat import twist # Simple context with None IData ctx = context.WovenContext() ctx.remember(None, inevow.IData) # The actual test notquiteglobals = {} io = StringIO() def finished(spam): print 'FINISHED' def error(failure): notquiteglobals['exception'] = failure.value def checker(result): if not isinstance(notquiteglobals['exception'], NameError): self.fail('deferflatten did not errback with the correct failure') return result d = twist.deferflatten(gen, ctx, io.write) d.addCallback(finished) d.addErrback(error) d.addBoth(checker) return d
def render_skin(ctx, data): content = inevow.IQ(ctx.tag).onePattern('skincontent') gather = tags.invisible() d = deferflatten(content, ctx, gather.__getitem__) def gotContent(s): self.content = gather return skin d.addCallback(gotContent) return d
def flatten(self, obj): """ Flatten the given object using L{twist.deferflatten} and a simple context. Return the Deferred returned by L{twist.deferflatten}. it. """ # Simple context with None IData ctx = context.WovenContext() ctx.remember(None, inevow.IData) return twist.deferflatten(obj, ctx, lambda bytes: None)
def render(self, tag, precompile=False, data=None, setupRequest=lambda r: r, setupContext=lambda c:c, wantDeferred=False): ctx = self.setupContext(precompile, setupRequest) ctx = setupContext(ctx) if precompile: return flat.precompile(tag, ctx) else: if wantDeferred: L = [] D = twist.deferflatten(tag, ctx, L.append) D.addCallback(lambda igresult: ''.join(L)) return D else: return flat.flatten(tag, ctx)
def render(self, tag, precompile=False, data=None, setupRequest=lambda r: r, setupContext=lambda c:c): ctx = self.setupContext(precompile, setupRequest) ctx = setupContext(ctx) if precompile: return flat.precompile(tag, ctx) else: try: from twisted.trial import util from nevow.flat import twist except ImportError: return flat.flatten(tag, ctx) else: L = [] util.deferredResult(twist.deferflatten(tag, ctx, L.append)) return ''.join(L)
def test_renderNestedDeferredCallables(self): """ Test flattening of a renderer which returns a Deferred which fires with a renderer which returns a Deferred. """ def render_inner(ctx, data): return defer.succeed('') def render_outer(ctx, data): return defer.succeed(render_inner) ctx = context.WovenContext() ctx.remember(None, inevow.IData) out = [] d = twist.deferflatten(render_outer, ctx, out.append) def flattened(ign): self.assertEqual(out, ['']) d.addCallback(flattened) return d
def render(self, tag, precompile=False, data=None, setupRequest=lambda r: r, setupContext=lambda c: c): ctx = self.setupContext(precompile, setupRequest) ctx = setupContext(ctx) if precompile: return flat.precompile(tag, ctx) else: try: from twisted.trial import util from nevow.flat import twist except ImportError: return flat.flatten(tag, ctx) else: L = [] util.deferredResult(twist.deferflatten(tag, ctx, L.append)) return ''.join(L)
def test_renderNestedDeferredErrorHandling(self): """ Test that flattening a renderer which returns a Deferred which fires with a renderer which raises an exception causes the outermost Deferred to errback. """ class NestedException(Exception): pass def render_inner(ctx, data): raise NestedException() def render_outer(ctx, data): return defer.succeed(render_inner) ctx = context.WovenContext() ctx.remember(None, inevow.IData) out = [] d = twist.deferflatten(render_outer, ctx, out.append) return self.assertFailure(d, NestedException)
def test_errorPropogation(self): # A generator that raises an error def gen(ctx, data): yield 1 raise Exception('This is an exception!') yield 2 # Hacky imports from cStringIO import StringIO from nevow.flat import twist # Simple context with None IData ctx = context.WovenContext() ctx.remember(None, inevow.IData) # The actual test notquiteglobals = {} io = StringIO() def finished(spam): print 'FINISHED' def error(failure): notquiteglobals['exception'] = failure.value def checker(result): if not isinstance(notquiteglobals['exception'], Exception): self.fail( 'deferflatten did not errback with the correct failure') return result d = twist.deferflatten(gen, ctx, io.write) d.addCallback(finished) d.addErrback(error) d.addBoth(checker) return d
def flattenFactory(stan, ctx, writer, finisher): from nevow.flat.twist import deferflatten return deferflatten(stan, ctx, writer).addCallback(finisher)