summaryrefslogtreecommitdiff
path: root/repoze/bfg/router.py
blob: ab8850110758a1d7e5ecbc16b3400cc5ed6feeaf (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
from zope.component import getAdapter
from zope.component.event import dispatch
from zope.interface import directlyProvides

from webob import Request
from webob.exc import HTTPNotFound
from webob.exc import HTTPUnauthorized

from repoze.bfg.events import NewRequest
from repoze.bfg.events import NewResponse
from repoze.bfg.events import WSGIApplicationCreatedEvent

from repoze.bfg.interfaces import ITraverserFactory
from repoze.bfg.interfaces import IRequest

from repoze.bfg.registry import registry_manager
from repoze.bfg.registry import makeRegistry

from repoze.bfg.security import Unauthorized

from repoze.bfg.view import is_response
from repoze.bfg.view import render_view_to_response

_marker = ()

class Router:
    """ WSGI application which routes requests to 'view' code based on
    a view registry"""
    def __init__(self, root_policy, registry):
        self.root_policy = root_policy
        self.registry = registry

    def __call__(self, environ, start_response):
        registry_manager.set(self.registry)
        request = Request(environ)
        directlyProvides(request, IRequest)
        dispatch(NewRequest(request))
        root = self.root_policy(environ)
        traverser = getAdapter(root, ITraverserFactory)
        context, name, subpath = traverser(environ)

        request.context = context
        request.view_name = name
        request.subpath = subpath

        try:
            response = render_view_to_response(context, request, name,
                                               secure=True)
        except Unauthorized, why:
            app = HTTPUnauthorized()
            app.explanation = str(why)
            return app(environ, start_response)

        if response is None:
            app = HTTPNotFound(request.url)
            return app(environ, start_response)

        if not is_response(response):
            raise ValueError('response did not implement IResponse: %r'
                             % response)

        dispatch(NewResponse(response))

        start_response(response.status, response.headerlist)
        return response.app_iter

def make_app(root_policy, package=None, filename='configure.zcml',
             options=None):
    """ Create a view registry based on the application's ZCML.  and
    return a Router object, representing a ``repoze.bfg`` WSGI
    application.  ``root_policy`` must be a callable that accepts a
    WSGI environment and returns a graph root object.  ``package`` is
    a Python module representing the application's package,
    ``filename`` is the filesystem path to a ZCML file (optionally
    relative to the package path) that should be parsed to create the
    view registry.  ``options``, if used, should be a dictionary
    containing bfg-specific runtime options, with each key
    representing the option and the key's value representing the
    specific option value, e.g. ``{'reload_templates':True}``"""
    registry = makeRegistry(filename, package, options)
    app = Router(root_policy, registry)

    try:
        registry_manager.set(registry)
        dispatch(WSGIApplicationCreatedEvent(app))
    finally:
        registry_manager.clear()

    return app