[Zope-Checkins] CVS: Zope/lib/python/Products/PageTemplates - PageTemplate.py:1.21.2.2

Shane Hathaway shane@cvs.zope.org
Mon, 4 Mar 2002 18:20:12 -0500


Update of /cvs-repository/Zope/lib/python/Products/PageTemplates
In directory cvs.zope.org:/tmp/cvs-serv20207

Modified Files:
      Tag: shane-better-tracebacks-branch
	PageTemplate.py 
Log Message:
Removed the assumption that a Page Template must always be cooked.  Now
there is a flag attribute called _v_cooked.  Much simpler than requiring
__setstate__(), and now compiling is always performed with acquisition context.

Added an overrideable pt_source_file(), which returns a URL-like string
or None.  _cook() calls it and passes the source filename to the TALGenerator.


=== Zope/lib/python/Products/PageTemplates/PageTemplate.py 1.21.2.1 => 1.21.2.2 ===
     _v_errors = ()
     _v_warnings = ()
+    _v_program = None
+    _v_macros = None
+    _v_cooked = 0
     id = '(unknown)'
     _text = ''
     _error_start = '<!-- Page Template Diagnostics'
@@ -73,6 +76,8 @@
     
     def pt_render(self, source=0, extra_context={}):
         """Render this Page Template"""
+        if not self._v_cooked:
+            self._cook()
         if self._v_errors:
             raise PTRuntimeError, 'Page Template %s has errors.' % self.id
         output = StringIO()
@@ -93,6 +98,8 @@
         return self.pt_render(extra_context={'options': kwargs})
 
     def pt_errors(self):
+        if not self._v_cooked:
+            self._cook()
         err = self._v_errors
         if err:
             return err
@@ -103,13 +110,20 @@
             return ('Macro expansion failed', '%s: %s' % sys.exc_info()[:2])
         
     def pt_warnings(self):
+        if not self._v_cooked:
+            self._cook()
         return self._v_warnings
 
     def pt_macros(self):
+        if not self._v_cooked:
+            self._cook()
         if self._v_errors:
             raise PTRuntimeError, 'Page Template %s has errors.' % self.id
         return self._v_macros
 
+    def pt_source_file(self):
+        return None  # Unknown.
+
     def write(self, text):
         assert type(text) is type('')
         if text[:len(self._error_start)] == self._error_start:
@@ -121,6 +135,8 @@
         self._cook()
 
     def read(self):
+        if not self._v_cooked:
+            self._cook()
         if not self._v_errors:
             if not self.expand:
                 return self._text
@@ -138,14 +154,14 @@
     def _cook(self):
         """Compile the TAL and METAL statments.
 
-        A Page Template must always be cooked, and cooking must not
-        fail due to user input.
+        Cooking must not fail due to compilation errors in templates.
         """
+        source_file = self.pt_source_file()
         if self.html():
-            gen = TALGenerator(getEngine(), xml=0)
+            gen = TALGenerator(getEngine(), xml=0, source_file=source_file)
             parser = HTMLTALParser(gen)
         else:
-            gen = TALGenerator(getEngine())
+            gen = TALGenerator(getEngine(), source_file=source_file)
             parser = TALParser(gen)
 
         self._v_errors = ()
@@ -156,6 +172,7 @@
             self._v_errors = ["Compilation failed",
                               "%s: %s" % sys.exc_info()[:2]]
         self._v_warnings = parser.getWarnings()
+        self._v_cooked = 1
 
     def html(self):
         if not hasattr(getattr(self, 'aq_base', self), 'is_html'):