[ZPT] CVS: Zope/lib/python/Products/PageTemplates - PageTemplate.py:1.21.2.3 TALES.py:1.28.2.4

Shane Hathaway shane@cvs.zope.org
Tue, 5 Mar 2002 17:57:36 -0500


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

Modified Files:
      Tag: shane-better-tracebacks-branch
	PageTemplate.py TALES.py 
Log Message:
Simplified the ITracebackSupplement interface.

=== Zope/lib/python/Products/PageTemplates/PageTemplate.py 1.21.2.2 => 1.21.2.3 ===
 __version__='$Revision$'[11:-2]
 
-import sys, pprint
-from cgi import escape
+import sys
 
 from TAL.TALParser import TALParser
 from TAL.HTMLTALParser import HTMLTALParser
@@ -28,12 +27,9 @@
 from string import join, strip, rstrip, split, replace, lower, find
 from cStringIO import StringIO
 from ExtensionClass import Base
+from ComputedAttribute import ComputedAttribute
 
 
-class MacroCollection(Base):
-    def __of__(self, parent):
-        return parent.pt_macros()
-
 class PageTemplate(Base):
     "Page Templates using TAL, TALES, and METAL"
      
@@ -48,7 +44,9 @@
     _text = ''
     _error_start = '<!-- Page Template Diagnostics'
 
-    macros = MacroCollection()
+    def macros(self):
+        return self.pt_macros()
+    macros = ComputedAttribute(macros, 1)
 
     def pt_edit(self, text, content_type):
         if content_type:
@@ -78,14 +76,15 @@
         """Render this Page Template"""
         if not self._v_cooked:
             self._cook()
+
+        __traceback_supplement__ = (PageTemplateTracebackSupplement, self)
+
         if self._v_errors:
             raise PTRuntimeError, 'Page Template %s has errors.' % self.id
         output = StringIO()
         c = self.pt_getContext()
         c.update(extra_context)
 
-        __traceback_supplement__ = (PageTemplateTracebackSupplement, self, c)
-
         TALInterpreter(self._v_program, self._v_macros,
                        getEngine().getContext(c),
                        output,
@@ -118,6 +117,7 @@
         if not self._v_cooked:
             self._cook()
         if self._v_errors:
+            __traceback_supplement__ = (PageTemplateTracebackSupplement, self)
             raise PTRuntimeError, 'Page Template %s has errors.' % self.id
         return self._v_macros
 
@@ -195,28 +195,22 @@
 
 
 class PageTemplateTracebackSupplement:
-    """Implementation of Products.ErrorReporter.ITracebackSupplement
-
-    Shows the context in which an expression is executed.
-    """
-
-    def __init__(self, pt, context):
-        c = context.copy()
-        if c.has_key('request'):
-            c['request'] = '(not shown)'
-        self.context = c
-        self.pt = pt
+    """Implementation of Products.ErrorReporter.ITracebackSupplement"""
+    def __init__(self, pt):
+        self.manageable_object = pt
+        try:
+            errors = pt.pt_errors()
+        except: # We're already trying to report an error, don't make another.
+            errors = None
+        self.errors = errors
 
     def getInfo(self, as_html=0):
-        s = pprint.pformat(self.context)
+        errors = self.errors
+        if not errors:
+            return None
         if not as_html:
-            return '   - Context:\n      %s' % s.replace('\n', '\n      ')
+            return '   - Errors:\n      %s' % '\n      '.join(errors)
         else:
-            return '<b>Context:</b><pre>%s</pre>' % (
-                escape(pprint.pformat(self.context)))
-
-    def getManageableObject(self):
-        # N/A
-        return None
-    getLine = getColumn = getExpression = getManageableObject
-
+            from cgi import escape
+            s = '<br />'.join(map(escape, errors))
+            return '<b>Errors:</b><br />%s' % s


=== Zope/lib/python/Products/PageTemplates/TALES.py 1.28.2.3 => 1.28.2.4 ===
     def __init__(self, context, expression):
         self.context = context
-        self.expression = expression
+        self.expression = repr(expression)
+        self.line = context.position[0]
+        self.column = context.position[1]
 
-    def getManageableObject(self):
-        source_file = self.context.source_file
+        source_file = context.source_file
         if (isinstance(source_file, StringType) and
             source_file.startswith('traversal:')):
             p = source_file[10:]
@@ -269,22 +270,22 @@
                 try:
                     object = root.unrestrictedTraverse(p)
                 except:
-                    # Hmm, it didn't work out.
-                    return None
+                    # Hmm, couldn't find the script??
+                    pass
                 else:
-                    return object
-        return None
-
-    def getLine(self):
-        return self.context.position[0]
-
-    def getColumn(self):
-        return self.context.position[1]
-
-    def getExpression(self):
-        return repr(self.expression)
-
+                    self.manageable_object = object
+        
     def getInfo(self, as_html=0):
+        import pprint
+        data = self.context.contexts.copy()
+        if data.has_key('request'):
+            data['request'] = '(not shown)'
+        s = pprint.pformat(data)
+        if not as_html:
+            return '   - Names:\n      %s' % s.replace('\n', '\n      ')
+        else:
+            from cgi import escape
+            return '<b>Names:</b><pre>%s</pre>' % (escape(s))
         return None