[Zope-Checkins] CVS: Zope3/lib/python/Zope/Publisher - BaseRequest.py:1.1.2.23.4.4 BaseResponse.py:1.1.2.8.6.4

Jim Fulton jim@zope.com
Mon, 25 Mar 2002 18:31:12 -0500


Update of /cvs-repository/Zope3/lib/python/Zope/Publisher
In directory cvs.zope.org:/tmp/cvs-serv31034/Zope/Publisher

Modified Files:
      Tag: Zope3-publisher-refactor-branch
	BaseRequest.py BaseResponse.py 
Log Message:
Refactord BaseRequest/BaseResponse and HTTPRequest/Response some more:

- Use slots so I can keep track of what's going on. ;)
- Use properties for environment, cookies, headers, URLs.
- Put URL responsibilities in HTTPRequest.
- Have HTTP traverse delegate to and augment Base traverse.



=== Zope3/lib/python/Zope/Publisher/BaseRequest.py 1.1.2.23.4.3 => 1.1.2.23.4.4 ===
 from IPublisherRequest import IPublisherRequest
 from IPublicationRequest import IPublicationRequest
+from RequestDataProperty import RequestDataProperty, RequestDataMapper
 
 class IRequest(IPublisherRequest, IPublicationRequest, IApplicationRequest):
     """The basic request contract
@@ -30,7 +31,10 @@
 
 _marker = object()
 
-class BaseRequest:
+class RequestEnvironment(RequestDataMapper):
+    _mapname = '_environ'
+
+class BaseRequest(object):
     """Represents a publishing request.
     
     This object provides access to request data. Request data may
@@ -44,17 +48,31 @@
     """
 
     __implements__ = IRequest
-    _common = {} # Variables common to all request instances
-    _held = ()   # Objects held until the request is closed
 
-    def __init__(self, body_instream, outstream, environ):
-        self.__traversal_stack = ()
-        self._other = {} 
+    __slots__ = (
+        '_held',             # Objects held until the request is closed
+        '_traversed_names',  # The names that have been traversed
+        '_traversal_stack',  # Names to be traversed, in reverse order
+        '_environ',          # The request environment variables
+        '_response',         # The response
+        '_args',             # positional arguments
+        '_body_instream',    # input stream
+        '_body',             # The request body as a string
+        '_publication',      # publication object
+        '_viewskin',         # View skin
+        )
+
+    environment = RequestDataProperty(RequestEnvironment)
+
+    def __init__(self, body_instream, outstream, environ, positional=()):
+        self._traversal_stack = []
+        self._traversed_names = []
         self._environ = environ
-        self.__args = ()
-        self.__response = self._createResponse(outstream)
-        self.__body_instream = body_instream
-        self.__held = ()
+
+        self._args = positional
+        self._response = self._createResponse(outstream)
+        self._body_instream = body_instream
+        self._held = ()
 
 
     ############################################################
@@ -66,7 +84,7 @@
 
     def getPublication(self):
         'See Zope.Publisher.IPublisherRequest.IPublisherRequest'
-        return self.__publication
+        return getattr(self, '_publication', None)
 
     def processInputs(self):
         'See Zope.Publisher.IPublisherRequest.IPublisherRequest'
@@ -78,7 +96,7 @@
 
     def setPublication(self, pub):
         'See Zope.Publisher.IPublisherRequest.IPublisherRequest'
-        self.__publication = pub
+        self._publication = pub
 
     def supportsRetry(self):
         'See Zope.Publisher.IPublisherRequest.IPublisherRequest'
@@ -89,21 +107,23 @@
 
         publication = self.getPublication()
 
-        to_traverse = self.__traversal_stack
+        traversal_stack = self._traversal_stack
+        traversed_names = self._traversed_names
+        
         prev_object = None
         while 1:
             if object is not prev_object:
                 # Invoke hooks (but not more than once).
                 publication.callTraversalHooks(self, object)
-                # A hook may have called changeTraversalStack().
-                to_traverse = self.__traversal_stack
+
             prev_object = object
 
-            if to_traverse:
+            if traversal_stack:
                 # Traverse to the next step.
-                entry_name = to_traverse.pop()
+                entry_name = traversal_stack.pop()
                 subobject = publication.traverseName(
                     self, object, entry_name)
+                traversed_names.append(entry_name)
                 object = subobject
             else:
                 # Finished traversal.
@@ -116,20 +136,22 @@
 
     def close(self):
         'See Zope.Publisher.IPublicationRequest.IPublicationRequest'
-        self._other.clear()
-        self.__dict__.clear()
+        self._held = None
+        self._response = None
+        self._body_instream = None
+        self._publication = None
 
     def getPositionalArguments(self):
         'See Zope.Publisher.IPublicationRequest.IPublicationRequest'
-        return self.__args
+        return self._args
 
     def getResponse(self):
         'See Zope.Publisher.IPublicationRequest.IPublicationRequest'
-        return self.__response
+        return self._response
 
     def getTraversalStack(self):
         'See Zope.Publisher.IPublicationRequest.IPublicationRequest'
-        return self.__traversal_stack
+        return list(self._traversal_stack) # Return a copy
 
     def hold(self, object):
         'See Zope.Publisher.IPublicationRequest.IPublicationRequest'
@@ -137,9 +159,7 @@
 
     def setTraversalStack(self, stack):
         'See Zope.Publisher.IPublicationRequest.IPublicationRequest'
-        self.__traversal_stack = stack
-
-    _viewskin = ''
+        self._traversal_stack[:] = list(stack)
 
     def setViewSkin(self, skin):
         'See Zope.Publisher.IPublicationRequest.IPublicationRequest'
@@ -150,13 +170,11 @@
 
     def getViewSkin(self):
         'See Zope.ComponentArchitecture.IViewService.IViewRequest'
-        return self._viewskin
-
-    _viewtype = None
+        return getattr(self, '_viewskin', '')
 
     def getViewType(self):
         'See Zope.ComponentArchitecture.IViewService.IViewRequest'
-        return self._viewtype
+        return getattr(self, '_viewtype', None)
 
     # This is not part of the interface:
     def setViewType(self, viewtype):
@@ -173,24 +191,23 @@
     ######################################
     # from: Zope.Publisher.IApplicationRequest.IApplicationRequest
 
-    __body = None
     def getBody(self):
         'See Zope.Publisher.IApplicationRequest.IApplicationRequest'
-        body = self.__body
+        body = getattr(self, '_body', None)
         if body is None:
-            s = self.__body_instream
+            s = self._body_instream
             if s is None:
                 return default
             p = s.tell()
             s.seek(0)
             body = s.read()
             s.seek(p)
-            self.__body = body
+            self._body = body
         return body
 
     def getBodyFile(self):
         'See Zope.Publisher.IApplicationRequest.IApplicationRequest'
-        return self.__body_instream
+        return self._body_instream
 
     ######################################
     # from: Interface.Common.Mapping.IEnumerableMapping
@@ -209,11 +226,7 @@
 
     def keys(self):
         'See Interface.Common.Mapping.IEnumerableMapping'
-        keys = {}
-        keys.update(self._common)
-        keys.update(self._other)
-        keys.update(self._environ)
-        return keys.keys()
+        return self._environ.keys()
 
     def values(self):
         'See Interface.Common.Mapping.IEnumerableMapping'
@@ -236,11 +249,6 @@
 
     def get(self, key, default=None):
         'See Interface.Common.Mapping.IReadMapping'
-        result = self._other.get(key, self)
-        if result is not self: return result 
-
-        result = self._common.get(key, self)
-        if result is not self: return result 
 
         result = self._environ.get(key, self)
         if result is not self: return result


=== Zope3/lib/python/Zope/Publisher/BaseResponse.py 1.1.2.8.6.3 => 1.1.2.8.6.4 ===
 class BaseResponse(object):
     """Base Response Class
-
-    What should be here?
     """
 
-    __implements__ = IResponse
+    __slots__ = (
+        '_body',      # The response body
+        '_outstream', # The output stream
+        )
 
-    _body = ''
+    __implements__ = IResponse
 
     
     def __init__(self, outstream):
+        self._body = ''
         self._outstream = outstream
 
     ############################################################
@@ -52,7 +54,7 @@
         'See Zope.Publisher.IPublisherResponse.IPublisherResponse'
         self._outstream.write(self._getBody())
 
-    def setBody(self, result):
+    def setBody(self, body):
         'See Zope.Publisher.IPublisherResponse.IPublisherResponse'
         self._body = body