[Checkins] SVN: zope.pagetemplate/branches/engine-as-component/ This branch is a refactor of the cook- and render implementation which adds a formal interface to the parser and interpreter. In addition, a utility lookup is made for the interface such that an alternative implementation may be configured.

Malthe Borch mborch at gmail.com
Wed Jul 20 11:17:20 EDT 2011


Log message for revision 122300:
  This branch is a refactor of the cook- and render implementation which adds a formal interface to the parser and interpreter. In addition, a utility lookup is made for the interface such that an alternative implementation may be configured.

Changed:
  A   zope.pagetemplate/branches/engine-as-component/
  U   zope.pagetemplate/branches/engine-as-component/CHANGES.txt
  U   zope.pagetemplate/branches/engine-as-component/src/zope/pagetemplate/interfaces.py
  U   zope.pagetemplate/branches/engine-as-component/src/zope/pagetemplate/pagetemplate.py
  U   zope.pagetemplate/branches/engine-as-component/src/zope/pagetemplate/tests/test_basictemplate.py

-=-
Modified: zope.pagetemplate/branches/engine-as-component/CHANGES.txt
===================================================================
--- zope.pagetemplate/trunk/CHANGES.txt	2011-07-20 13:32:00 UTC (rev 122299)
+++ zope.pagetemplate/branches/engine-as-component/CHANGES.txt	2011-07-20 15:17:19 UTC (rev 122300)
@@ -2,6 +2,12 @@
 CHANGES
 =======
 
+3.6.0 (unreleased)
+
+- Refactor of the cook- and render implementation such that an
+  alternative implementation may be configured via utility component
+  registration.
+
 3.5.3 (unreleased)
 ------------------
 

Modified: zope.pagetemplate/branches/engine-as-component/src/zope/pagetemplate/interfaces.py
===================================================================
--- zope.pagetemplate/trunk/src/zope/pagetemplate/interfaces.py	2011-07-20 13:32:00 UTC (rev 122299)
+++ zope.pagetemplate/branches/engine-as-component/src/zope/pagetemplate/interfaces.py	2011-07-20 15:17:19 UTC (rev 122300)
@@ -98,8 +98,21 @@
         Subclasses might override this to influence the decision about
         whether compilation is necessary.
         """
-        
+
     content_type = Attribute("The content-type of the generated output")
 
     expand = Attribute(
         "Flag indicating whether the read method should expand macros")
+
+
+class IPageTemplateEngine(Interface):
+    def cook(source_file, text, engine, content_type):
+        """Parse text and return template program."""
+
+
+class IPageTemplateProgram(Interface):
+    macros = Attribute(
+        "Template macros.")
+
+    def __call__(context, tal=1, showtal=-1, sourceAnnotations=0):
+        """Render template in the provided context."""

Modified: zope.pagetemplate/branches/engine-as-component/src/zope/pagetemplate/pagetemplate.py
===================================================================
--- zope.pagetemplate/trunk/src/zope/pagetemplate/pagetemplate.py	2011-07-20 13:32:00 UTC (rev 122299)
+++ zope.pagetemplate/branches/engine-as-component/src/zope/pagetemplate/pagetemplate.py	2011-07-20 15:17:19 UTC (rev 122300)
@@ -21,16 +21,20 @@
 from zope.tal.talgenerator import TALGenerator
 from zope.tal.talinterpreter import TALInterpreter
 from zope.tales.engine import Engine
+from zope.component import queryUtility
 # Don't use cStringIO here!  It's not unicode aware.
 from StringIO import StringIO
 
 from zope.pagetemplate.interfaces import IPageTemplateSubclassing
+from zope.pagetemplate.interfaces import IPageTemplateEngine
+from zope.pagetemplate.interfaces import IPageTemplateProgram
 from zope.interface import implements
+from zope.interface import classProvides
 
-
 _default_options = {}
 _error_start = '<!-- Page Template Diagnostics'
 
+
 class PageTemplate(object):
     """Page Templates using TAL, TALES, and METAL.
 
@@ -61,14 +65,13 @@
     content_type = 'text/html'
     expand = 1
     _v_errors = ()
+    _v_cooked = 0
     _v_program = None
-    _v_macros = None
-    _v_cooked = 0
     _text = ''
 
     def macros(self):
         self._cook_check()
-        return self._v_macros
+        return self._v_program.macros
 
     macros = property(macros)
 
@@ -106,13 +109,13 @@
         if self._v_errors:
             raise PTRuntimeError(str(self._v_errors))
 
-        output = StringIO(u'')
         context = self.pt_getEngineContext(namespace)
-        TALInterpreter(self._v_program, self._v_macros,
-                       context, output, tal=not source, showtal=showtal,
-                       strictinsert=0, sourceAnnotations=sourceAnnotations)()
-        return output.getvalue()
 
+        return self._v_program(
+            context, tal=not source, showtal=showtal,
+            sourceAnnotations=sourceAnnotations
+            )
+
     def pt_errors(self, namespace):
         self._cook_check()
         err = self._v_errors
@@ -175,23 +178,23 @@
 
         Cooking must not fail due to compilation errors in templates.
         """
-        engine = self.pt_getEngine()
+
+        pt_engine = self.pt_getEngine()
         source_file = self.pt_source_file()
-        if self.content_type == 'text/html':
-            gen = TALGenerator(engine, xml=0, source_file=source_file)
-            parser = HTMLTALParser(gen)
-        else:
-            gen = TALGenerator(engine, source_file=source_file)
-            parser = TALParser(gen)
 
         self._v_errors = ()
+
         try:
-            parser.parseString(self._text)
-            self._v_program, self._v_macros = parser.getCode()
+            engine = queryUtility(
+                IPageTemplateEngine, default=PageTemplateEngine
+                )
+            self._v_program = engine.cook(
+                source_file, self._text, pt_engine, self.content_type)
         except:
             etype, e = sys.exc_info()[:2]
             self._v_errors = ["Compilation failed",
                               "%s.%s: %s" % (etype.__module__, etype.__name__, e)]
+
         self._v_cooked = 1
 
 
@@ -200,6 +203,42 @@
     pass
 
 
+class PageTemplateEngine(object):
+    """Page template engine that uses the TAL interpreter to render."""
+
+    implements(IPageTemplateProgram)
+    classProvides(IPageTemplateEngine)
+
+    def __init__(self, program, macros):
+        self.macros = macros
+
+        # Internal
+        self._program = program
+
+    def __call__(self, context, **options):
+        output = StringIO(u'')
+        interpreter = TALInterpreter(
+            self._program, self.macros, context,
+            stream=output, strictinsert=0, **options
+            )
+        interpreter()
+        return output.getvalue()
+
+    @classmethod
+    def cook(cls, source_file, text, engine, content_type):
+        if content_type == 'text/html':
+            gen = TALGenerator(engine, xml=0, source_file=source_file)
+            parser = HTMLTALParser(gen)
+        else:
+            gen = TALGenerator(engine, source_file=source_file)
+            parser = TALParser(gen)
+
+        parser.parseString(text)
+        program, macros = parser.getCode()
+
+        return cls(program, macros)
+
+
 class PageTemplateTracebackSupplement(object):
     #implements(ITracebackSupplement)
 

Modified: zope.pagetemplate/branches/engine-as-component/src/zope/pagetemplate/tests/test_basictemplate.py
===================================================================
--- zope.pagetemplate/trunk/src/zope/pagetemplate/tests/test_basictemplate.py	2011-07-20 13:32:00 UTC (rev 122299)
+++ zope.pagetemplate/branches/engine-as-component/src/zope/pagetemplate/tests/test_basictemplate.py	2011-07-20 15:17:19 UTC (rev 122300)
@@ -17,13 +17,17 @@
 
 from zope.pagetemplate.tests import util
 import zope.pagetemplate.pagetemplate
+import zope.component.testing
 
-
 class BasicTemplateTests(unittest.TestCase):
 
     def setUp(self):
+        zope.component.testing.setUp(self)
         self.t = zope.pagetemplate.pagetemplate.PageTemplate()
 
+    def tearDown(self):
+        zope.component.testing.tearDown(self)
+
     def test_if_in_var(self):
         # DTML test 1: if, in, and var:
         pass # for unittest
@@ -73,6 +77,43 @@
         else:
             self.fail("expected PTRuntimeError")
 
+    def test_engine_utility_registration(self):
+        self.t.write("foo")
+        output = self.t.pt_render({})
+        self.assertEqual(output, 'foo')
+
+        from zope.pagetemplate.interfaces import IPageTemplateEngine
+        from zope.component import provideUtility
+
+        class DummyProgram(object):
+            def __init__(*args):
+                self.args = args
+
+            def __call__(*args, **kwargs):
+                return self.args, args, kwargs
+
+        class DummyEngine(object):
+            cook = DummyProgram
+
+        provideUtility(DummyEngine, IPageTemplateEngine)
+        self.t._cook()
+
+        # "Render" and unpack arguments passed for verification
+        ((cls, source_file, text, engine, content_type),
+         (program, context),
+         options) = \
+         self.t.pt_render({})
+
+        self.assertEqual(source_file, None)
+        self.assertEqual(text, 'foo')
+        self.assertEqual(content_type, 'text/html')
+        self.assertTrue(isinstance(program, DummyProgram))
+        self.assertEqual(options, {
+            'tal': True,
+            'showtal': False,
+            'sourceAnnotations': False
+            })
+
     def test_batches_and_formatting(self):
         # DTML test 3: batches and formatting:
         pass # for unittest



More information about the checkins mailing list