1) Is template block processing done top to bottom sequentially? I've
included example.py - it's interesting that by having a blocking call first
the second request is blocked, but having the blocking call after a
non-blocking call the second request is not. I would've thought it would
behave as a deferred list, but looking at _flattenTree and guessing not.
Maybe related to wait_for_it example?
2) Is it possible for a Resource to act as an Element too? I've included a
non-working elementresource.py. I'm a total hack, but I would think that
if an instance had a loader attr it could be processable.
3) Is it possible to include xmlns:t="
http://twistedmatrix.com/ns/twisted.web.template/0.1" not in a tag itself?
Or perhaps have a tag like 'render-block' that could be transparent-like?
4) Is it possible to have xmlns:t="
http://twistedmatrix.com/ns/twisted.web.template/0.1" in multiple places in
a template? I have some cases with inline scripts that bonk out with > or
< characters. This is very much related to #3.
5) Is it possible for a render element to return something like "<~sometag
t:render>..." and process recursively?
6) Is there any examples of connection keep-alive long polling?
7) Examples of request based scoping would be great. All the examples on
http://twistedmatrix.com/documents/13.0.0/web/howto/twisted-templates.html
have flatten(None... - I've included request_scope.py
8) The wait_for_it example, is that meant as a chunked transfer example?
It would be cool to have an example I could open in a browser. Trying to
wrap my head around this and subviews in the meantime.
Happy holidays.
from twisted.web import server, resource, template, client
from twisted.internet import reactor, defer, threads
import time
import pprint
blocking_first = """<html xmlns:t="http://twistedmatrix.com/ns/twisted.web.template/0.1"> Blocking:<t:transparent t:render="blocking"/>Non-blocking:<t:transparent t:render="non_blocking"/></html>"""
blocking_second = """<html xmlns:t="http://twistedmatrix.com/ns/twisted.web.template/0.1"> Non-blocking:<t:transparent t:render="non_blocking"/>Blocking:<t:transparent t:render="blocking"/></html>"""
class ExampleTemplate(template.Element):
loader = template.XMLString(blocking_second)
def non_blocking_call(self):
print 'called non-blocking'
d = defer.Deferred()
d.addCallback(lambda ign: str(int(time.time())))
reactor.callLater(2, d.callback, None)
return d
def blocking_call(self):
print 'called blocking'
time.sleep(2)
return str(int(time.time()))
@template.renderer
def blocking(self, request, tag):
'''
switch to
yield threads.deferToThread(self.blocking_call)
to unblock
'''
return self.blocking_call()
@template.renderer
def non_blocking(self, request, tag):
yield self.non_blocking_call()
class ExampleResource(resource.Resource):
isLeaf = True
def render_GET(self, request):
print 'get:', int(time.time())
d = template.flattenString(request, ExampleTemplate())
d.addCallback(request.write)
d.addCallback(lambda ign: request.finish())
return server.NOT_DONE_YET
site = server.Site(ExampleResource())
def simulataneous():
dl = defer.DeferredList([client.getPage('http://localhost:6789'), client.getPage('http://localhost:6789')])
dl.addCallback(lambda res: [pprint.pprint(ans[1]) for ans in res ])
dl.addErrback(lambda err: pprint.pprint(err))
if __name__ == '__main__':
reactor.listenTCP(6789, site)
reactor.callWhenRunning(simulataneous)
reactor.run()
from twisted.web import server, resource, template, client
from twisted.internet import reactor
import pprint
class ElementResource(resource.Resource, template.Element):
isLeaf = True
loader = template.XMLString("""<html xmlns:t="http://twistedmatrix.com/ns/twisted.web.template/0.1"><t:transparent t:render="whatever"/></html>""")
@template.renderer
def whatever(self, request, tag):
return 'whatever'
def render_GET(self, request):
d = template.flattenString(request, self)
d.addCallback(request.write)
d.addCallback(lambda ign: request.finish())
return server.NOT_DONE_YET
site = server.Site(ElementResource())
def test():
d = client.getPage('http://localhost:6789')
d.addCallback(lambda res: pprint.pprint(res))
d.addErrback(lambda err: pprint.pprint(err.getErrorMessage()))
if __name__ == '__main__':
reactor.listenTCP(6789, site)
reactor.callWhenRunning(test)
reactor.run()
from twisted.web import server, resource, template, client
from twisted.internet import reactor, defer
import pprint
class ExampleTemplate(template.Element):
loader = template.XMLString("""<html xmlns:t="http://twistedmatrix.com/ns/twisted.web.template/0.1">result:<t:transparent t:render="result"/></html>""")
@template.renderer
def result(self, request, tag):
return request.result
class ExampleResource(resource.Resource):
inventory = { 'item' : 1 }
isLeaf = True
def shop(self, act, item, request):
if act == 'buy':
if item in self.inventory.keys() and self.inventory[item] > 0:
self.inventory[item] += -1
request.result = "Bought"
else:
request.result = "Unavailable"
elif act == 'sell':
if item in self.inventory.keys():
self.inventory[item] += 1
else:
self.inventory[item] = 1
request.result = "Sold"
else:
request.result = "Can't " + act + " here"
def render_GET(self, request):
d = defer.DeferredLock().run(self.shop, request.args['action'][0], request.args['action'][1], request)
d.addCallback(lambda ign: template.flattenString(request, ExampleTemplate()))
d.addCallback(request.write)
d.addCallback(lambda ign: request.finish())
return server.NOT_DONE_YET
site = server.Site(ExampleResource())
def simulataneous():
dl = defer.DeferredList([client.getPage('http://localhost:6789?action=buy&action=item'),client.getPage('http://localhost:6789?action=buy&action=item'),client.getPage('http://localhost:6789?action=sell&action=item'),client.getPage('http://localhost:6789?action=buy&action=item')])
dl.addCallback(lambda res: [pprint.pprint(ans[1]) for ans in res])
dl.addErrback(lambda err: pprint.pprint(err))
if __name__ == '__main__':
reactor.listenTCP(6789, site)
reactor.callWhenRunning(simulataneous)
reactor.run()
_______________________________________________
Twisted-Python mailing list
Twisted-Python@twistedmatrix.com
http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python