[Zope-Checkins] CVS: Zope3/lib/python/Zope/Publisher - DefaultPublication.py:1.1.2.1 BaseResponse.py:1.1.2.2 Exceptions.py:1.1.2.3 IPublication.py:1.1.2.3 Publish.py:1.1.2.3

Shane Hathaway shane@digicool.com
Thu, 15 Nov 2001 13:07:05 -0500


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

Modified Files:
      Tag: Zope-3x-branch
	BaseResponse.py Exceptions.py IPublication.py Publish.py 
Added Files:
      Tag: Zope-3x-branch
	DefaultPublication.py 
Log Message:
- Refined exception handling.
- Added HTTPResponse and DefaultPublication object.
(all untested as yet)


=== Added File Zope3/lib/python/Zope/Publisher/DefaultPublication.py ===

from IPublication import IPublication
from Exceptions import NotFound, Unauthorized
from mapply import mapply

class DefaultPublication:

    __implements__ = IPublication

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

    def preTraversal(self, request):
        pass

    def getApplication(self, request):
        return self.app

    def invokeHooks(self, request, ob):
        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)
        if not getattr(subob, '__doc__', None):
            raise DebugError(subob, 'Missing or empty doc string at: %s' %
                             request.getURL())
        return subob

    def getDefault(self, request, ob):
        return ob, None

    def postTraversal(self, request, ob):
        pass

    def publish(self, request, ob):
        return mapply(ob, request.args, request)

    def postPublish(self, request):
        pass

    def handleException(request, exc):
        # Let the response handle it as best it can.
        request.response.handleException(exc)



=== Zope3/lib/python/Zope/Publisher/BaseResponse.py 1.1.2.1 => 1.1.2.2 ===
                  status=None, cookies=None):
         self.outstream = outstream
-        self.body = body
         if headers is None:
             headers = {}
         self.headers = headers
-        self.status = status
+        self.setStatus(status)
+        if body:
+            self.setBody(body)
         if cookies is None:
             cookies = {}
         self.cookies = cookies
@@ -97,7 +98,7 @@
 
     def write(self,data):
         """
-        Implements the notional stream output interface.
+        Implements the stream output interface.
 
         HTML data may be returned using a stream-oriented interface.
         This allows the browser to display partial results while
@@ -111,36 +112,8 @@
         """
         self.body = self.body + data
 
-##    def exception(self, fatal=0, info=None):
-##        """Handle an exception.
-
-##        The fatal argument indicates whether the error is fatal.
-
-##        The info argument, if given should be a tuple with an
-##        error type, value, and traceback.
-##        """
-
-##    def notFoundError(self, v=''):
-##        """Generate an error indicating that an object was not found.
-##        """
-##        raise 'Not Found', v
-
-##    def debugError(self, v=''):
-##        """Raise an error with debigging info and in debugging mode"""
-##        raise 'Debug Error', v
-
-##    def badRequestError(self, v=''):
-##        """Raise an error indicating something wrong with the request"""
-##        raise 'Bad Request', v
-
-##    def forbiddenError(self, v=''):
-##        """Raise an error indicating that the request cannot be done"""
-##        raise 'Forbidden', v
-
-##    def unauthorized(self):
-##        """Raise an eror indicating that the user was not authizated
-
-##        Make sure to generate an appropriate challenge, as appropriate.
-##        """
-##        raise Unauthorized
+    def handleException(self, exc_info):
+        import traceback
+        traceback.print_exception(
+            exc_info[0], exc_info[1], exc_info[2], 100, self)
 


=== Zope3/lib/python/Zope/Publisher/Exceptions.py 1.1.2.2 => 1.1.2.3 ===
     """
     """
-    def __init__(self, ob, s):
-        self.ob = ob
-        self.s = s
 
 
 class NotFound (TraversalException):
     """
     """
+    def __init__(self, ob, name):
+        self.ob = ob
+        self.name = name
+
+    def getObject(self):
+        return self.ob
+
+    def getName(self):
+        return self.name
+
+    def __str__(self):
+        return 'Object: %s, name: %s' % (`self.ob`, `self.name`)
 
 
 class DebugError (TraversalException):
     """
     """
+    def __init__(self, ob, message):
+        self.ob = ob
+        self.message = message
+
+    def getObject(self):
+        return self.ob
+
+    def getMessage(self):
+        return self.message
+
+    def __str__(self):
+        return self.message
 
 
 class BadRequest (PublishingException):
 
-    def __init__(self, s):
-        self.s = s
+    def __init__(self, message):
+        self.message = message
+
+    def __str__(self):
+        return self.message
 
 
 class Redirect (PublishingException):
 
     def __init__(self, location):
         self.location = location
+
+    def getLocation(self):
+        return self.location
+
+    def __str__(self):
+        return 'Location: %s' % self.location
 
 
 class Retry (PublishingException):


=== Zope3/lib/python/Zope/Publisher/IPublication.py 1.1.2.2 => 1.1.2.3 ===
         """
 
-    def handleException(request, t, v, tb):
+    def handleException(request, exc_info, retry_allowed=1):
         """
         Either:
         - sets the body of request.response,


=== Zope3/lib/python/Zope/Publisher/Publish.py 1.1.2.2 => 1.1.2.3 ===
 
 
-def traverseAndPublish(publication, request):
-
-##    (bobo_before, bobo_after, object, realm, debug_mode, err_hook,
-##     validated_hook, transactions_manager)= get_module_info(module_name)
+def executeRequest(publication, request):
 
     parents = None
 
@@ -20,9 +17,6 @@
         request.processInputs()
         response = request.response
     
-        if debug_mode:
-            response.setDebugMode(debug_mode)
-
         publication.preTraversal(request)
         #if transactions_manager: transactions_manager.begin()
     
@@ -31,6 +25,7 @@
         object = request.traverse(publication, object, path_str)
     
         publication.postTraversal(request, object)
+
         result = publication.publish(request, object)
 ##        if transactions_manager:
 ##            transactions_manager.recordMetaData(object, request)
@@ -50,38 +45,41 @@
         return response
     except:
         try:
-            t, v, tb = sys.exc_info()
+            exc = sys.exc_info()
             try:
-                publication.handleException(request, t, v, tb)
+                publication.handleException(request, exc)
             except Retry:
                 # The exception handler requested a retry.
                 if not request.supports_retry():
                     # Can't retry.  Restore the original exception.
-                    raise t, v, tb
+                    raise exc[0], exc[1], exc[2]
                 newrequest = request.retry()
                 request.close()  # Free resources held by the request.
                 try:
-                    tb = None
+                    exc = None
                     return traverseAndPublish(publication, newrequest)
                 finally:
                     newrequest.close()
         finally:
-            del tb  # Avoid circular ref.
+            del exc  # Avoid circular ref.
 
 
-def publishRequest(publication, request):
+def publish(publication, request):
     to_raise = None
     try:
         try:
-            response = traverseAndPublish(publication, request)
+            response = executeRequest(publication, request)
         except SystemExit, v:
+            # Output and re-raise
             to_raise = sys.exc_info()
-            publication.handleException(
-                request, to_raise[0], to_raise[1], to_raise[2])
+            publication.handleException(request, to_raise, 0)
         except:
-            to_raise = sys.exc_info()
-            publication.handleException(
-                request, to_raise[0], to_raise[1], to_raise[2])
+            # Output but don't re-raise
+            exc = sys.exc_info()
+            try:
+                publication.handleException(request, exc, 0)
+            finally:
+                exc = None  # Avoid circ. ref
 
         response.outputBody()
 
@@ -89,225 +87,12 @@
         if request is not None:
             request.close()
 
-    if to_raise:
+    if to_raise is not None:
         try:
             raise to_raise[0], to_raise[1], to_raise[2]
         finally:
             to_raise = None
 
     return response.getStatus()
-    
-
-
 
-##def publish_module(module_name,
-##                   stdin=sys.stdin, stdout=sys.stdout, stderr=sys.stderr,
-##                   environ=os.environ, debug=0, request=None, response=None):
-##    must_die=0
-##    status=200
-##    after_list=[None]
-##    try:
-##        try:
-##            if response is None:
-##                response=Response(stdout=stdout, stderr=stderr)
-##            else:
-##                stdout=response.stdout
-
-##            if request is None:
-##                request=Request(stdin, environ, response)
-
-##            response = publish(request, module_name, after_list, debug=debug)
-##        except SystemExit, v:
-##            must_die=sys.exc_info()
-##            request.response.exception(must_die)
-##        except ImportError, v:
-##            if type(v) is type(()) and len(v)==3: must_die=v
-##            elif hasattr(sys, 'exc_info'): must_die=sys.exc_info()
-##            else: must_die = SystemExit, v, sys.exc_info()[2]
-##            request.response.exception(1, v)
-##        except:
-##            request.response.exception()
-##            status=response.getStatus()
-
-##        if response:
-##            outputBody=getattr(response, 'outputBody', None)
-##            if outputBody is not None:
-##                outputBody()
-##            else:
-##                response=str(response)
-##                if response: stdout.write(response)
-
-##        # The module defined a post-access function, call it
-##        if after_list[0] is not None: after_list[0]()
-
-##    finally:
-##        if request is not None: request.close()
-
-##    if must_die:
-##        try: raise must_die[0], must_die[1], must_die[2]
-##        finally: must_die=None
-
-##    return status
-
-
-##_l=allocate_lock()
-##def get_module_info(module_name, modules={},
-##                    acquire=_l.acquire,
-##                    release=_l.release,
-##                    ):
-
-##    if modules.has_key(module_name): return modules[module_name]
-
-##    if module_name[-4:]=='.cgi': module_name=module_name[:-4]
-
-##    acquire()
-##    tb=None
-##    try:
-##        try:
-##            module=__import__(module_name, globals(), globals(), ('__doc__',))
-
-##            realm=module_name
-
-##            # Let the app specify a realm
-##            if hasattr(module,'__bobo_realm__'):
-##                realm=module.__bobo_realm__
-##            elif os.environ.has_key('Z_REALM'):
-##                realm=os.environ['Z_REALM']
-##            elif os.environ.has_key('BOBO_REALM'):
-##                realm=os.environ['BOBO_REALM']
-##            else: realm=module_name
-
-##            # Check for debug mode
-##            debug_mode=None
-##            if hasattr(module,'__bobo_debug_mode__'):
-##                debug_mode=not not module.__bobo_debug_mode__
-##            else:
-
-##                z1 = os.environ.get('Z_DEBUG_MODE','')
-##                z2 = os.environ.get('BOBO_DEBUG_MODE','')
-            
-##                if z1.lower() in ('yes','y') or z1.isdigit():
-##                    debug_mode = 1
-##                elif z2.lower() in ('yes','y') or z2.isdigit():
-##                    debug_mode = 1
-
-
-##            if hasattr(module,'__bobo_before__'):
-##                bobo_before=module.__bobo_before__
-##            else: bobo_before=None
-
-##            if hasattr(module,'__bobo_after__'): bobo_after=module.__bobo_after__
-##            else: bobo_after=None
-
-##            if hasattr(module,'bobo_application'):
-##                object=module.bobo_application
-##            elif hasattr(module,'web_objects'):
-##                object=module.web_objects
-##            else: object=module
-
-##            error_hook=getattr(module,'zpublisher_exception_hook', None)
-##            validated_hook=getattr(module,'zpublisher_validated_hook', None)
-
-##            transactions_manager=getattr(
-##                module,'zpublisher_transactions_manager', None)
-##            if not transactions_manager:
-##                try: get_transaction()
-##                except: pass
-##                else:
-##                    # Create a default transactions manager for use
-##                    # by software that uses ZPublisher and ZODB but
-##                    # not the rest of Zope.
-##                    transactions_manager = DefaultTransactionsManager()
-
-##            info= (bobo_before, bobo_after, object, realm, debug_mode,
-##                   error_hook, validated_hook, transactions_manager)
-
-##            modules[module_name]=modules[module_name+'.cgi']=info
-            
-##            return info
-##        except:
-##            t,v,tb=sys.exc_info()
-##            v=str(v)
-##            raise ImportError, (t, v), tb
-##    finally:
-##        tb=None
-##        release()
-
-
-##class DefaultTransactionsManager:
-##    def begin(self): get_transaction().begin()
-##    def commit(self): get_transaction().commit()
-##    def abort(self): get_transaction().abort()
-##    def recordMetaData(self, object, request):
-##        # Is this code needed?
-##        request_get = request.get
-##        T=get_transaction()
-##        T.note(request_get('PATH_INFO'))
-##        auth_user=request_get('AUTHENTICATED_USER',None)
-##        if auth_user is not None:
-##            T.setUser(auth_user, request_get('AUTHENTICATION_PATH'))
-        
-
-### ZPublisher profiler support
-### ---------------------------
-
-##if os.environ.get('PROFILE_PUBLISHER', None):
-
-##    import profile, pstats
-
-##    _pfile=os.environ['PROFILE_PUBLISHER']
-##    _plock=allocate_lock()
-##    _pfunc=publish_module
-##    _pstat=None
-
-##    def pm(module_name, stdin, stdout, stderr, 
-##           environ, debug, request, response):
-##        try:
-##            r=_pfunc(module_name, stdin=stdin, stdout=stdout, 
-##                     stderr=stderr, environ=environ, debug=debug, 
-##                     request=request, response=response)
-##        except: r=None
-##        sys._pr_=r
-
-##    def publish_module(module_name, stdin=sys.stdin, stdout=sys.stdout, 
-##                       stderr=sys.stderr, environ=os.environ, debug=0, 
-##                       request=None, response=None):
-##        global _pstat
-##        _plock.acquire()
-##        try:
-##            if request is not None:
-##                path_info=request.get('PATH_INFO')
-##            else: path_info=environ.get('PATH_INFO')
-##            if path_info[-14:]=='manage_profile':
-##                return _pfunc(module_name, stdin=stdin, stdout=stdout, 
-##                              stderr=stderr, environ=environ, debug=debug, 
-##                              request=request, response=response)
-##            pobj=profile.Profile()
-##            pobj.runcall(pm, module_name, stdin, stdout, stderr, 
-##                         environ, debug, request, response)
-##            result=sys._pr_
-##            pobj.create_stats()
-##            if _pstat is None:
-##                _pstat=sys._ps_=pstats.Stats(pobj)
-##            else: _pstat.add(pobj)
-##        finally:
-##            _plock.release()
-
-##        if result is None:
-##            try:
-##                error=sys.exc_info()
-##                file=open(_pfile, 'w')
-##                file.write(
-##                "See the url "
-##                "http://www.python.org/doc/current/lib/module-profile.html"
-##                "\n for information on interpreting profiler statistics.\n\n"
-##                    )
-##                sys.stdout=file
-##                _pstat.strip_dirs().sort_stats('cumulative').print_stats(250)
-##                _pstat.strip_dirs().sort_stats('time').print_stats(250)
-##                file.flush()
-##                file.close()
-##            except: pass
-##            raise error[0], error[1], error[2]
-##        return result