##############################################################################
#
# Copyright (c) 2001, 2002 Zope Corporation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""Interfaces for the publisher.

$Id: __init__.py 26792 2004-07-27 14:33:50Z jim $
"""

from zope.interface import Interface
from zope.interface import Attribute
from zope.security.interfaces import Unauthorized
from zope.exceptions import NotFoundError, INotFoundError
from zope.component.interfaces import IPresentationRequest
from zope.interface import implements
from zope.interface.common.mapping import IEnumerableMapping
from zope.interface.common.interfaces import IException
from zope.security.interfaces import IParticipation

class IPublishingException(IException):
    pass

class PublishingException(Exception):
    implements(IPublishingException)

class ITraversalException(IPublishingException):
    pass

class TraversalException(PublishingException):
    implements(ITraversalException)

class INotFound(INotFoundError, ITraversalException):
    def getObject():
        'Returns the object that was being traversed.'

    def getName():
        'Returns the name that was being traversed.'

class NotFound(NotFoundError, TraversalException):
    implements(INotFound)

    def __init__(self, ob, name, request=None):
        self.ob = ob
        self.name = name

    def getObject(self):
        return self.ob

    def getName(self):
        return self.name

    def __str__(self):
        try:
            ob = `self.ob`
        except:
            ob = 'unprintable object'
        return 'Object: %s, name: %s' % (ob, `self.name`)

class IDebugError(ITraversalException):
    def getObject():
        'Returns the object being traversed.'

    def getMessage():
        'Returns the debug message.'

class DebugError(TraversalException):
    implements(IDebugError)

    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 IBadRequest(IPublishingException):
    def __str__():
        'Returns the error message.'

class BadRequest(PublishingException):

    implements(IBadRequest)

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

    def __str__(self):
        return self.message

class IRedirect(IPublishingException):
    def getLocation():
        'Returns the location.'

class Redirect(PublishingException):

    implements(IRedirect)

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

    def getLocation(self):
        return self.location

    def __str__(self):
        return 'Location: %s' % self.location

class IRetry(IPublishingException):
    def getOriginalException():
        'Returns the original exception object.'

class Retry(PublishingException):
    """Raise this to retry a request.
    """

    implements(IRetry)

    def __init__(self, orig_exc=None):
        self.orig_exc = orig_exc

    def getOriginalException(self):
        return self.orig_exc

    def __str__(self):
        return repr(self.orig_exc)


class IExceptionSideEffects(Interface):
    '''An exception caught by the publisher is adapted to this so that
    it can have persistent side-effects.'''

    def __call__(obj, request, exc_info):
        '''Effect persistent side-effects.

        Arguments are:
          obj                 context-wrapped object that was published
          request             the request
          exc_info            the exception info being handled
        '''


class IPublishTraverse(Interface):

    def publishTraverse(request, name):
        """Lookup a name

        The request argument is the publisher request object.
        """


class IPublisher(Interface):

    def publish(request):
        """Publish a request

        The request must be an IPublisherRequest.
        """


class IPublisherResponse(Interface):
    """Interface used by the publsher
    """

    def setBody(result):
        """Sets the response result value.
        """

    def reset():
        """Resets response state on exceptions.
        """

    def handleException(exc_info):
        """Handles an otherwise unhandled exception.

        The publication object gets the first chance to handle an exception,
        and if it doesn't have a good way to do it, it defers to the
        response.  Implementations should set the reponse body.
        """

    def internalError():
        """Called when the exception handler bombs.

        Should report back to the client that an internal error occurred.
        """

    def outputBody():
        """Outputs the response to the client
        """

    def retry():
        """Returns a retry response

        Returns a response suitable for repeating the publication attempt.
        """


class IPublication(Interface):
    """Object publication framework.

    The responsibility of publication objects is to provide
    application hooks for the publishing process. This allows
    application-specific tasks, such as connecting to databases,
    managing transactions, and setting security contexts to be invoked
    during the publishing process.
    """
    # The order of the hooks mostly corresponds with the order in which
    # they are invoked.

    def beforeTraversal(request):
        """Pre-traversal hook.

        This is called *once* before any traversal has been done.
        """

    def getApplication(request):
        """Returns the object where traversal should commence.
        """

    def callTraversalHooks(request, ob):
        """Invokes any traversal hooks associated with the object.
        """

    def traverseName(request, ob, name, check_auth=True):
        """Traverses to the next object.

        If check_auth is set,
        performs idenitification, authentication, and authorization.
        Returns the subobject.
        """

    def afterTraversal(request, ob):
        """Post-traversal hook.
        """

    def callObject(request, ob):
        """Call the object, returning the result.

        For GET/POST this means calling it, but for other methods
        (including those of WebDAV and FTP) this might mean invoking
        a method of an adapter.
        """

    def afterCall(request, ob):
        """Post-callObject hook (if it was successful).
        """

    def handleException(object, request, exc_info, retry_allowed=1):
        """Handle an exception

        Either:
        - sets the body of the response, request.response, or
        - raises a Retry exception, or
        - throws another exception, which is a Bad Thing.

        Note that this method should not leak, which means that
        exc_info must be set to some other value before exiting the method.
        """

    def endRequest(request, ob):
        """Do any end-of-request cleanup
        """


class IApplicationResponse(Interface):
    """Features that support application logic
    """

    def write(string):
        """Output a string to the response body.
        """


class IPublicationRequest(IPresentationRequest, IParticipation):
    """Interface provided by requests to IPublication objects
    """

    response = Attribute("""The request's response object

        Return an IPublisherResponse for the request.
        """)

    def close():
        """Release resources held by the request.
        """

    def hold(object):
        """Hold a reference to an object until the request is closed
        """

    def getTraversalStack():
        """Return the request traversal stack

        This is a sequence of steps to traverse in reverse order. They
        will be traversed from last to first.
        """

    def setTraversalStack(stack):
        """Change the traversal stack.

        See getTraversalStack.
        """

    def getPositionalArguments():
        """Return the positional arguments given to the request.
        """

    def setPresentationSkin(skin):
        """Set the skin to be used for the request.

        It's up to the publication object to decide this.
        """

    def setPrincipal(principal):
        """Set the principal attribute.

        It should be IPrincipal wrapped in it's AuthenticationService's context.
        """


class IPublisherRequest(IPublicationRequest):
    """Request interface use by the publisher

    The responsibility of requests is to encapsulate protocol
    specific details, especially wrt request inputs.

    Request objects also serve as "context" objects, providing
    construction of and access to responses and storage of publication
    objects.
    """

    def supportsRetry():
        """Check whether the request supports retry

        Return a boolean value indicating whether the request can be retried.
        """

    def retry():
        """Return a retry request

        Return a request suitable for repeating the publication attempt.
        """

    publication = Attribute("""The request's publication object

        The publication object, an IRequestPublication provides
        application-specific functionality hooks.
        """)

    def setPublication(publication):
        """Set the request's publication object
        """

    def traverse(object):
        """Traverse from the given object to the published object

        The published object is returned.

        The following hook methods on the publication will be called:

          - callTraversalHooks is called before each step and after
            the last step.

          - traverseName to actually do a single traversal

        """

    def processInputs():
        """Do any input processing that needs to be done before traversing

        This is done after construction to allow the publisher to
        handle errors that arise.
        """


class IApplicationRequest(IEnumerableMapping):
    """Features that support application logic
    """

    principal = Attribute("""Principal object associated with the request
                             This is a read-only attribute.
                          """)

    body = Attribute("""The body of the request as a string""")

    bodyFile = Attribute("""The body of the request as a file""")

    def __getitem__(key):
        """Return request data

        The only request data are environment variables.
        """

    environment = Attribute(
        """Request environment data

        This is a read-only mapping from variable name to value.
        """)


class IResponse(IPublisherResponse, IApplicationResponse):
    """The basic response contract
    """


class IRequest(IPublisherRequest, IPublicationRequest, IApplicationRequest):
    """The basic request contract
    """
