summaryrefslogtreecommitdiff
path: root/interfaces/__init__.py
blob: 0d18ebc653d775df5c53feb7ea013d54f34578fd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
##############################################################################
#
# 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$
"""

import zope.deprecation

from zope.interface import Interface
from zope.interface import Attribute
from zope.security.interfaces import Unauthorized
from zope.component.interfaces import IPresentationRequest
from zope.interface import implements
from zope.interface.interfaces import IInterface
from zope.interface.common.mapping import IEnumerableMapping
from zope.interface.common.interfaces import IException
from zope.security.interfaces import IParticipation

# BBB : can be remove in 3.3
zope.deprecation.__show__.off()
from zope.exceptions import NotFoundError, INotFoundError
zope.deprecation.__show__.on()

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.

        If a lookup is not possible, raise a NotFound error.

        This method should return an object having the specified name and
        `self` as parent. The method can use the request to determine the
        correct object.
        """


class IPublisher(Interface):

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

        The request must be an IPublisherRequest.
        """

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

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

    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 reset():
        """Reset the output result.

        Reset the response by nullifying already set variables.
        """

    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.

        This is called before traversing each object.  The ob argument
        is the object that is about to be traversed.
        """

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

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

        This is called after all traversal.
        """

    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 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(held):
        """Hold a reference to an object until the request is closed.

        The object should be an IHeld.  If it is an IHeld, it's
        release method will be called when it is released.
        """

    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 setPrincipal(principal):
        """Set the principal attribute.

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

class IHeld(Interface):
    """Object to be held and explicitly released by a request
    """

    def release():
        """Release the held object

        This is called by a request that holds the IHeld when the
        request is closed

        """

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 IDebugFlags(Interface):
    """Features that support debugging."""

    sourceAnnotations = Attribute("""Enable ZPT source annotations""")
    showTAL = Attribute("""Leave TAL markup in rendered page templates""")


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

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

    bodyStream = Attribute(
        """The stream that provides the data of the request.

        The data returned by the stream will not include any possible header
        information, which should have been stripped by the server (or
        previous layer) before.

        Also, the body stream might already be read and not return any
        data. This is commonly done when retrieving the data for the ``body``
        attribute.

        If you access this stream directly to retrieve data, it will not be
        possible by other parts of the framework to access the data of the
        request via the ``body`` attribute.""")

    debug = Attribute("""Debug flags (see IDebugFlags).""")

    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.
        """)

    annotations = Attribute(
        """Stores arbitrary application data under package-unique keys.

        By "package-unique keys", we mean keys that are are unique by
        virtue of including the dotted name of a package as a prefex.  A
        package name is used to limit the authority for picking names for
        a package to the people using that package.

        For example, when implementing annotations for hypothetical
        request-persistent adapters in a hypothetical zope.persistentadapter
        package, the key would be (or at least begin with) the following::

          "zope.persistentadapter"
        """)


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


class ILayer(IInterface):
    """A grouping of related views for a request."""