From c1945c942929c821df4cb0be314c2c8d97ae07bc Mon Sep 17 00:00:00 2001 From: mdipierro Date: Fri, 24 Aug 2012 22:40:18 -0500 Subject: [PATCH] template render can recycle a Response(), thanks Anthony --- VERSION | 2 +- gluon/template.py | 71 ++++++++++++++++++++++++++++------------------- 2 files changed, 44 insertions(+), 29 deletions(-) diff --git a/VERSION b/VERSION index c517cf07..20895a41 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -Version 2.00.0 (2012-08-24 22:17:44) dev +Version 2.00.0 (2012-08-24 22:40:15) dev diff --git a/gluon/template.py b/gluon/template.py index 0127f1e2..72a75555 100644 --- a/gluon/template.py +++ b/gluon/template.py @@ -841,6 +841,32 @@ def get_parsed(text): """ return str(TemplateParser(text)) +class DummyResponse(): + def __init__(self): + self.body = cStringIO.StringIO() + def write(self, data, escape=True): + if not escape: + self.body.write(str(data)) + elif hasattr(data,'xml') and callable(data.xml): + self.body.write(data.xml()) + else: + # make it a string + if not isinstance(data, (str, unicode)): + data = str(data) + elif isinstance(data, unicode): + data = data.encode('utf8', 'xmlcharrefreplace') + data = cgi.escape(data, True).replace("'","'") + self.body.write(data) + +class NOESCAPE(): + """ + A little helper to avoid escaping. + """ + def __init__(self, text): + self.text = text + def xml(self): + return self.text + # And this is a generic render function. # Here for integration with gluon. def render(content = "hello world", @@ -877,42 +903,29 @@ def render(content = "hello world", >>> render(content='{{for i in range(3):\\n=i\\npass}}') '012' """ - # Here to avoid circular Imports + # save current response class + if context and 'response' in context: + old_response = context['response'] + else: + old_response = None + + # here to avoid circular Imports try: from globals import Response - except: + except ImportError: # Working standalone. Build a mock Response object. - class Response(): - def __init__(self): - self.body = cStringIO.StringIO() - def write(self, data, escape=True): - if not escape: - self.body.write(str(data)) - elif hasattr(data,'xml') and callable(data.xml): - self.body.write(data.xml()) - else: - # make it a string - if not isinstance(data, (str, unicode)): - data = str(data) - elif isinstance(data, unicode): - data = data.encode('utf8', 'xmlcharrefreplace') - data = cgi.escape(data, True).replace("'","'") - self.body.write(data) + Response = DummyResponse - # A little helper to avoid escaping. - class NOESCAPE(): - def __init__(self, text): - self.text = text - def xml(self): - return self.text # Add it to the context so we can use it. - context['NOESCAPE'] = NOESCAPE - + if not 'NOESCAPE' in context: + context['NOESCAPE'] = XML + # If we don't have anything to render, why bother? if not content and not stream and not filename: raise SyntaxError, "Must specify a stream or filename or content" - # Here for legacy purposes, probably can be reduced to something more simple. + # Here for legacy purposes, probably can be reduced to + # something more simple. close_stream = False if not stream: if filename: @@ -936,7 +949,9 @@ def render(content = "hello world", stream.close() # Returned the rendered content. - return context['response'].body.getvalue() + text = context['response'].body.getvalue() + if old_response is not None: context['response'] = old_response + return text if __name__ == '__main__':