summaryrefslogtreecommitdiff
path: root/repoze/bfg/urldispatch.py
blob: 0281b52b36d0a18e641e05c57a03bbc0c59546f9 (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
from routes import Mapper
from routes import request_config

_marker = object()

class RoutesRootFactory(Mapper):
    def __init__(self, default_root_factory, **kw):
        self.default_root_factory = default_root_factory
        kw['controller_scan'] = None
        kw['always_scan'] = False
        kw['directory'] = None
        kw['explicit'] = True
        Mapper.__init__(self, **kw)
        self._regs_created = False

    def has_routes(self):
        return bool(self.matchlist)

    def connect(self, *arg, **kw):
        result = Mapper.connect(self, *arg, **kw)
        route = self.matchlist[-1]
        route._factory = None # overridden by ZCML
        return result

    def __call__(self, environ):
        if not self._regs_created:
            self.create_regs([])
            self._regs_created = True
        path = environ.get('PATH_INFO', '/')
        self.environ = environ # sets the thread local
        match = self.routematch(path)
        if match:
            args, route = match
        else:
            args = None
        if isinstance(args, dict): # might be an empty dict
            args = args.copy()
            config = request_config()
            config.mapper = self
            config.mapper_dict = args
            config.host = environ.get('HTTP_HOST', environ['SERVER_NAME'])
            config.protocol = environ['wsgi.url_scheme']
            config.redirect = None
            environ['wsgiorg.routing_args'] = ((), args)
            environ['bfg.routes.route'] = route
            environ['bfg.routes.matchdict'] = args
            adhoc_attrs = environ.setdefault('webob.adhoc_attrs', {})
            adhoc_attrs['matchdict'] = args
            factory = route._factory or self.default_root_factory
            return factory(environ)

        return self.default_root_factory(environ)