[Checkins] SVN: z3c.pt/trunk/src/z3c/pt/t Fixed byte template ghost class and added preliminary test for integrity.

Malthe Borch mborch at gmail.com
Tue Sep 9 05:20:41 EDT 2008


Log message for revision 90987:
  Fixed byte template ghost class and added preliminary test for integrity.

Changed:
  U   z3c.pt/trunk/src/z3c/pt/testing.py
  U   z3c.pt/trunk/src/z3c/pt/translation.py
  U   z3c.pt/trunk/src/z3c/pt/translation.txt

-=-
Modified: z3c.pt/trunk/src/z3c/pt/testing.py
===================================================================
--- z3c.pt/trunk/src/z3c/pt/testing.py	2008-09-09 09:18:03 UTC (rev 90986)
+++ z3c.pt/trunk/src/z3c/pt/testing.py	2008-09-09 09:20:41 UTC (rev 90987)
@@ -25,10 +25,13 @@
     stream = generation.CodeIO(symbols)
     return out, write, stream
 
-def render_xhtml(body, **kwargs):
+def compile_xhtml(body, **kwargs):
     compiler = translation.Compiler(
         body, mock_parser, implicit_doctype=doctypes.xhtml)
-    template = compiler(params=sorted(kwargs.keys()))
+    return compiler(params=sorted(kwargs.keys()))
+
+def render_xhtml(body, **kwargs):
+    template = compile_xhtml(body, **kwargs)
     return template.render(**kwargs)    
     
 def render_text(body, **kwargs):

Modified: z3c.pt/trunk/src/z3c/pt/translation.py
===================================================================
--- z3c.pt/trunk/src/z3c/pt/translation.py	2008-09-09 09:18:03 UTC (rev 90986)
+++ z3c.pt/trunk/src/z3c/pt/translation.py	2008-09-09 09:20:41 UTC (rev 90987)
@@ -454,7 +454,6 @@
     doctype regardless of what the template defines."""
 
     doctype = None
-    implicit_doctype = None
     
     def __init__(self, body, parser, implicit_doctype=None, explicit_doctype=None):
         # if no doctype is defined, prepend the implicit doctype to
@@ -469,8 +468,8 @@
 
             # prepend to body
             body = implicit_doctype + "\n" + body
-            self.implicit_doctype = implicit_doctype
-        
+            
+        self.xmldoc = body
         self.root, parsed_doctype = parser.parse(body)
 
         if explicit_doctype is not None:
@@ -579,16 +578,19 @@
         if 'xmlns' in self.root.attrib:
             del self.root.attrib['xmlns']
         
-        return ByteCodeTemplate(render, stream, self)
+        return ByteCodeTemplate(
+            render, stream, self.xmldoc, self.parser, self.root)
 
 class ByteCodeTemplate(object):
     """Template compiled to byte-code."""
 
-    def __init__(self, func, stream, compiler):
+    def __init__(self, func, stream, xmldoc, parser, tree):
         self.func = func
         self.stream = stream
-        self.compiler = compiler
-
+        self.xmldoc = xmldoc
+        self.parser = parser
+        self.tree = tree
+        
     def __reduce__(self):
         reconstructor, (cls, base, state), kwargs = \
                        GhostedByteCodeTemplate(self).__reduce__()
@@ -597,13 +599,17 @@
     def __setstate__(self, state):
         self.__dict__.update(GhostedByteCodeTemplate.rebuild(state))
 
+    def __repr__(self):
+        return '<%s parser="%s">' % \
+               (type(self).__name__, str(type(self.parser)).split("'")[1])
+
     def render(self, *args, **kwargs):
         return self.func(generation, *args, **kwargs)
-    
+
     @property
     def source(self):
         return self.stream.getvalue()
-    
+
     @property
     def selectors(self):
         selectors = getattr(self, '_selectors', None)
@@ -611,7 +617,7 @@
             return selectors
 
         self._selectors = selectors = {}
-        for element in self.compiler.root.xpath(
+        for element in self.tree.xpath(
             './/*[@meta:select]', namespaces={'meta': config.META_NS}):
             name = element.attrib[utils.meta_attr('select')]
             selectors[name] = element.xpath
@@ -625,14 +631,8 @@
         self.code = marshal.dumps(template.func.func_code)
         self.defaults = len(template.func.func_defaults or ())
         self.stream = template.stream
-
-        compiler = template.compiler
-        xmldoc = compiler.root.tostring()        
-        if compiler.implicit_doctype is not None:
-            xmldoc = compiler.implicit_doctype + "\n" + xmldoc
-            
-        self.parser = compiler.parser
-        self.xmldoc = xmldoc
+        self.xmldoc = template.xmldoc
+        self.parser = template.parser
         
     @classmethod
     def rebuild(cls, state):
@@ -641,12 +641,17 @@
         func = _locals['render']
         func.func_defaults = ((None,)*state['defaults']) or None
         func.func_code = marshal.loads(state['code'])
+
+        stream = state['stream']
+        xmldoc = state['xmldoc']
         parser = state['parser']
-        root, doctype = state['parser'].parse(state['xmldoc'])
-        stream = state['stream']
+        tree, doctype = parser.parse(xmldoc)
+        
         return dict(
             func=func,
+            stream=stream,
+            xmldoc=xmldoc,
             parser=parser,
-            root=root,
-            stream=stream)
+            tree=tree)
+
             

Modified: z3c.pt/trunk/src/z3c/pt/translation.txt
===================================================================
--- z3c.pt/trunk/src/z3c/pt/translation.txt	2008-09-09 09:18:03 UTC (rev 90986)
+++ z3c.pt/trunk/src/z3c/pt/translation.txt	2008-09-09 09:20:41 UTC (rev 90987)
@@ -137,6 +137,34 @@
   >>> print render_text(js)
   print '<div class="description">Hello World!</div>';
 
+Ghosted templates
+-----------------
+
+To facilitate disk-caching, template instances can be pickled.
+
+  >>> from z3c.pt.testing import compile_xhtml
+  >>> template = compile_xhtml("""\
+  ... <div xmlns="http://www.w3.org/1999/xhtml">
+  ...   Hello World!
+  ... </div>""")
+
+  >>> from pickle import dumps, loads
+  >>> pickle = dumps(template)
+  >>> template = loads(pickle)
+
+Check that we're getting a template object back.
+  
+  >>> template
+  <ByteCodeTemplate parser="z3c.pt.testing.MockParser">
+
+Let's try and render the template.
+ 
+  >>> print template.render()
+  <div xmlns="http://www.w3.org/1999/xhtml">
+    Hello World!
+  </div>
+
+  
 Error handling
 --------------
 



More information about the Checkins mailing list