[Zope-Checkins] CVS: Zope3/lib/python/Zope/App/ZopePublication - Traversers.py:1.1.2.1 ZopePublication.py:1.1.2.4

Brian Lloyd brian@digicool.com
Mon, 19 Nov 2001 15:20:55 -0500


Update of /cvs-repository/Zope3/lib/python/Zope/App/ZopePublication
In directory cvs.zope.org:/tmp/cvs-serv20257

Modified Files:
      Tag: Zope-3x-branch
	ZopePublication.py 
Added Files:
      Tag: Zope-3x-branch
	Traversers.py 
Log Message:
Added default publication and default traversal adapter.


=== Added File Zope3/lib/python/Zope/App/ZopePublication/Traversers.py ===
from Zope.Publisher.Browser.IBrowserPublisher import IBrowserPublisher
from Zope.Publisher.Exceptions import Unauthorized, NotFound, DebugError

# Need to:
# - register component
# - decide on browser_traverse return vals


class DefaultTraverser:
    """ """
    __implements__ = IBrowserPublisher

    def __init__(self, target):
        self.target = target

    def browser_default(self, request):
        if hasattr(ob, 'index_html'):
            ob.index_html, ('index_html',)
        return self.target, None

    def browser_traverse(self, request, name):
        """ """
        ob = self.target
        if name[:1] == '_':
            raise Unauthorized("Name %s begins with an underscore" % `name`)
        if hasattr(ob, name):
            subob = getattr(ob, name)
        else:
            try:
                subob = ob[name]
            except (KeyError, IndexError,
                    TypeError, AttributeError):
                raise NotFound(ob, name, request.getURL())
        if not getattr(subob, '__doc__', None):
            raise DebugError(subob, 'Missing or empty doc string at: %s' %
                             request.getURL())
        return subob


# TODO: put this in the config file
Zope.ComponentArchitecture.providePresentation(
    None, '_traverse', IBrowserPublisher, DefaultTraverser
    )


=== Zope3/lib/python/Zope/App/ZopePublication/ZopePublication.py 1.1.2.3 => 1.1.2.4 ===
 #from ZODB.POSException import ConflictError
 from zLOG import LOG, ERROR, INFO
-
+from Zope.Publisher.Exceptions import NotFound, DebugError
+from Zope.ComponentArchitecture import getPresentation
 from Zope.Publisher.DefaultPublication import DefaultPublication
 from Zope.Publisher.mapply import mapply
 from Zope.Publisher.Exceptions import Retry
@@ -31,9 +32,7 @@
 
 
 class ZopePublication (DefaultPublication):
-    """
-    Zope publication specification.
-    """
+    """Base Zope publication specification."""
 
     version_cookie = 'Zope-Version'
     root_name = 'Application'
@@ -77,23 +76,14 @@
         pass
 
     def traverseName(self, request, ob, name, check_auth=1):
-        if name[:1] == '_':
-            raise Unauthorized("Name %s begins with an underscore" % `name`)
-        if hasattr(ob, name):
-            subob = getattr(ob, name)
-        else:
-            try:
-                subob = ob[name]
-            except (KeyError, IndexError,
-                    TypeError, AttributeError):
-                raise NotFound(ob, name, request.getURL())
-        if not getattr(subob, '__doc__', None):
-            raise DebugError(subob, 'Missing or empty doc string at: %s' %
-                             request.getURL())
-        return subob
+        raise NotImplemented(
+            'This method must be overridden.'
+            )
 
     def getDefaultTraversal(self, request, ob):
-        return ob, None
+        raise NotImplemented(
+            'This method must be overridden.'
+            )
 
     def afterTraversal(self, request, ob):
         #recordMetaData(object, request)
@@ -135,3 +125,27 @@
         return response
 
 
+from Zope.Publisher.Browser import IBrowserPublisher
+
+class BrowserPublication(ZopePublication):
+    """Web browser (HTTP) publication handling."""
+
+    def traverseName(self, request, ob, name, check_auth=1):
+        if IBrowserPublisher.isImplementBy(ob):
+            return ob.browser_traverse(request, name)
+            
+        adapter = getPresentation(ob, '_traverse', IBrowserPublisher, None)
+        if adapter is not None:
+            return adapter.browser_traverse(request, name)
+
+        raise NotFound(ob, name, request.getURL())
+
+    def getDefaultTraversal(self, request, ob):
+        if IBrowserPublisher.isImplementBy(ob):
+            return ob.browser_default(request)
+            
+        adapter = getPresentation(ob, '_traverse', IBrowserPublisher, None)
+        if adapter is not None:
+            return adapter.browser_default(request)
+
+        return ob, None