Mercurial > hg > MakeItSo
annotate makeitso/dispatcher.py @ 16:1818cc524cde
change API making substitute return the substituted quantity
author | Jeff Hammel <jhammel@mozilla.com> |
---|---|
date | Mon, 15 Nov 2010 18:28:10 -0800 |
parents | bf1ce840d0f0 |
children | 02498337c747 |
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 import os |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
7 |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
8 from handlers import Index |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
9 |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
10 from genshi.template import TemplateLoader |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
11 from paste.fileapp import FileApp |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
12 from pkg_resources import resource_filename |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
13 from webob import Request, Response, exc |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
14 |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
15 class Dispatcher(object): |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
16 |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
17 ### class level variables |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
18 defaults = { 'auto_reload': 'False', |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
19 'template_dirs': '', |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
20 'app': None, |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
21 'name': 'anonymous' } |
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 def __init__(self, **kw): |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
24 |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
25 # 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
|
26 for key in self.defaults: |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
27 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
|
28 self.auto_reload = self.auto_reload.lower() == 'true' |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
29 |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
30 # request handlers |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
31 self.handlers = [ Index ] |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
32 |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
33 # endpoint app if used as middleware |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
34 if self.app: |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
35 assert hasattr(self.app, '__call__') |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
36 |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
37 # template loader |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
38 self.template_dirs = self.template_dirs.split() |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
39 self.template_dirs.append(resource_filename(__name__, 'templates')) |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
40 self.loader = TemplateLoader(self.template_dirs, |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
41 auto_reload=self.auto_reload) |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
42 |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
43 def __call__(self, environ, start_response): |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
44 |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
45 # get a request object |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
46 request = Request(environ) |
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 the path |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
49 path = request.path_info.strip('/').split('/') |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
50 if path == ['']: |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
51 path = [] |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
52 request.environ['path'] = path |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
53 |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
54 # match the request to a handler |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
55 for h in self.handlers: |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
56 handler = h.match(self, request) |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
57 if handler is not None: |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
58 break |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
59 else: |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
60 if self.app: |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
61 return self.app(environ, start_response) |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
62 handler = exc.HTTPNotFound |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
63 |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
64 # add navigation links to handler [example] |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
65 if hasattr(handler, 'data'): |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
66 handler.data.setdefault('links', []) |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
67 for h in self.handlers: |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
68 handler.data['links'].append((handler.link(h.handler_path), |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
69 h.__name__)) |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
70 |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
71 # get response |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
72 res = handler() |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
73 return res(environ, start_response) |