[Zope3-checkins] SVN: Zope3/trunk/src/zope/app/zptpage/ Implemented ++debug++source, tal for ZPT pages.

Marius Gedminas marius at pov.lt
Sat Jun 12 10:07:47 EDT 2004


Log message for revision 25392:
Implemented ++debug++source,tal for ZPT pages.




-=-
Modified: Zope3/trunk/src/zope/app/zptpage/tests/test_zptpage.py
===================================================================
--- Zope3/trunk/src/zope/app/zptpage/tests/test_zptpage.py	2004-06-12 14:06:58 UTC (rev 25391)
+++ Zope3/trunk/src/zope/app/zptpage/tests/test_zptpage.py	2004-06-12 14:07:47 UTC (rev 25392)
@@ -18,6 +18,7 @@
 
 import unittest
 
+from zope.interface import directlyProvides
 from zope.interface.verify import verifyClass
 from zope.exceptions import Forbidden
 
@@ -30,9 +31,13 @@
 from zope.app.tests.placelesssetup import PlacelessSetup
 from zope.app.traversing.adapters import Traverser, DefaultTraversable
 from zope.app.traversing.interfaces import ITraverser, ITraversable
+from zope.app.traversing.interfaces import IPhysicallyLocatable
+from zope.app.traversing.interfaces import IContainmentRoot
 from zope.app.tests import ztapi
 from zope.security.checker import NamesChecker, defineChecker
 from zope.app.container.contained import contained
+from zope.app.location.traversing import LocationPhysicallyLocatable
+from zope.app.traversing.adapters import RootPhysicallyLocatable
 
 from zope.app.zptpage.interfaces import IZPTPage
 from zope.app.zptpage.zptpage import ZPTPage, ZPTSourceView,\
@@ -51,6 +56,10 @@
         super(ZPTPageTests, self).setUp()
         ztapi.provideAdapter(None, ITraverser, Traverser)
         ztapi.provideAdapter(None, ITraversable, DefaultTraversable)
+        ztapi.provideAdapter(
+              None, IPhysicallyLocatable, LocationPhysicallyLocatable)
+        ztapi.provideAdapter(
+              IContainmentRoot, IPhysicallyLocatable, RootPhysicallyLocatable)
         defineChecker(Data, NamesChecker(['URL', 'name']))
         defineChecker(TestRequest, NamesChecker(['getPresentationSkin']))
 
@@ -68,8 +77,9 @@
 
         page = contained(page, Data(name='zope'))
 
-        out = page.render(Data(URL={'1': 'http://foo.com/'}),
-                          title="Zope rules")
+        request = Data(URL={'1': 'http://foo.com/'},
+                       debug=Data(showTAL=False, sourceAnnotations=False))
+        out = page.render(request, title="Zope rules")
         out = ' '.join(out.split())
 
         self.assertEqual(
@@ -88,7 +98,8 @@
 
         page = contained(page, Data(name='zope'))
 
-        self.assertRaises(Forbidden, page.render, Data())
+        request = Data(debug=Data(showTAL=False, sourceAnnotations=False))
+        self.assertRaises(Forbidden, page.render, request)
 
     def test_template_context_wrapping(self):
 
@@ -111,7 +122,41 @@
         request = TestRequest()
         self.assertEquals(page.render(request), 'zpt\n')
 
+    def test_source_file(self):
+        page = ZPTPage()
+        self.assert_(page.pt_source_file() is None)
 
+        page = self.pageInContext(page)
+        self.assertEquals(page.pt_source_file(), '/folder/zpt')
+
+    def pageInContext(self, page):
+        root = Data()
+        directlyProvides(root, IContainmentRoot)
+        folder = contained(Data(), root, name='folder')
+        return contained(page, folder, name='zpt')
+
+    def test_debug_flags(self):
+        page = ZPTPage()
+        page = self.pageInContext(page)
+        page.setSource(u'<tal:x>Foo</tal:x>')
+
+        request = TestRequest()
+        self.assertEquals(page.render(request), 'Foo\n')
+
+        request.debug.showTAL = True
+        self.assertEquals(page.render(request), '<tal:x>Foo</tal:x>\n')
+
+        request.debug.showTAL = False
+        request.debug.sourceAnnotations = True
+        self.assertEquals(page.pt_source_file(), '/folder/zpt')
+        self.assertEquals(page.render(request),
+            '<!--\n' +
+            '=' * 78 + '\n' +
+            '/folder/zpt (line 1)\n' +
+            '=' * 78 + '\n' +
+            '-->Foo\n')
+
+
 class DummyZPT:
 
     def __init__(self, source):
@@ -150,7 +195,7 @@
     def test_ReadFile(self):
         page = ZPTPage()
         content = u"<p></p>"
-        page.setSource(content)        
+        page.setSource(content)
         f = ZPTReadFile(page)
         self.assertEqual(f.read(), content)
         self.assertEqual(f.size(), len(content))

Modified: Zope3/trunk/src/zope/app/zptpage/zptpage.py
===================================================================
--- Zope3/trunk/src/zope/app/zptpage/zptpage.py	2004-06-12 14:06:58 UTC (rev 25391)
+++ Zope3/trunk/src/zope/app/zptpage/zptpage.py	2004-06-12 14:07:47 UTC (rev 25392)
@@ -28,6 +28,7 @@
 from zope.app.filerepresentation.interfaces import IReadFile, IWriteFile
 from zope.app.filerepresentation.interfaces import IFileFactory
 from zope.app.container.contained import Contained
+from zope.app import zapi
 
 from interfaces import IZPTPage, IRenderZPTPage
 
@@ -71,9 +72,16 @@
         namespace['container'] = namespace['context'] = instance
         return namespace
 
+    def pt_source_file(self):
+        try:
+            return zapi.getPath(self)
+        except TypeError:
+            return None
+
     def render(self, request, *args, **keywords):
         instance = self.__parent__
 
+        debug_flags = request.debug
         request = ProxyFactory(request)
         instance = ProxyFactory(instance)
         if args: args = ProxyFactory(args)
@@ -82,7 +90,8 @@
         namespace = self.pt_getContext(instance, request,
                                        args=args, options=kw)
 
-        return self.pt_render(namespace)
+        return self.pt_render(namespace, showtal=debug_flags.showTAL,
+                              sourceAnnotations=debug_flags.sourceAnnotations)
 
     source = property(getSource, setSource, None,
                       """Source of the Page Template.""")




More information about the Zope3-Checkins mailing list