Mercurial > hg > MakeItSo
annotate makeitso/dispatcher.py @ 45:3bf29175907c
actually introduce a variable for the script
author | Jeff Hammel <jhammel@mozilla.com> |
---|---|
date | Wed, 05 Jan 2011 18:09:32 -0800 |
parents | 6d1c703c5ffc |
children | 1b9573832f33 |
rev | line source |
---|---|
14
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
1 """ |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
2 request dispatcher: |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
3 data persisting across requests should go here |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
4 """ |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
5 |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
6 from handlers import Index |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
7 from webob import Request, Response, exc |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
8 |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
9 class Dispatcher(object): |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
10 |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
11 ### class level variables |
20
6d1c703c5ffc
use tempita instead of genshi since we have it already (and dont need a really complex templating system)
Jeff Hammel <jhammel@mozilla.com>
parents:
18
diff
changeset
|
12 defaults = { 'app': None, } |
14
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
13 |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
14 def __init__(self, **kw): |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
15 |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
16 # set instance parameters from kw and defaults |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
17 for key in self.defaults: |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
18 setattr(self, key, kw.get(key, self.defaults[key])) |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
19 |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
20 # request handlers |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
21 self.handlers = [ Index ] |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
22 |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
23 # endpoint app if used as middleware |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
24 if self.app: |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
25 assert hasattr(self.app, '__call__') |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
26 |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
27 def __call__(self, environ, start_response): |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
28 |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
29 # get a request object |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
30 request = Request(environ) |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
31 |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
32 # get the path |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
33 path = request.path_info.strip('/').split('/') |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
34 if path == ['']: |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
35 path = [] |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
36 request.environ['path'] = path |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
37 |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
38 # match the request to a handler |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
39 for h in self.handlers: |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
40 handler = h.match(self, request) |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
41 if handler is not None: |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
42 break |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
43 else: |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
44 if self.app: |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
45 return self.app(environ, start_response) |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
46 handler = exc.HTTPNotFound |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
47 |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
48 # get response |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
49 res = handler() |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
50 return res(environ, start_response) |