annotate simplewiki/dispatcher.py @ 0:d5102c881cb5

initial commit
author Jeff Hammel <jhammel@mozilla.com>
date Tue, 07 Sep 2010 17:50:04 -0700
parents
children 4c83f7715993
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1 """
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
2 request dispatcher:
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
3 data persisting across requests should go here
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
4 """
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
5
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
6 import os
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
7
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
8 from handlers import Index
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
9
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
10 from genshi.template import TemplateLoader
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
11 from paste.fileapp import FileApp
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
12 from pkg_resources import resource_filename
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
13 from webob import Request, Response, exc
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
14
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
15 class Dispatcher(object):
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
16
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
17 ### class level variables
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
18 defaults = { 'auto_reload': 'False',
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
19 'template_dirs': '',
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
20 'app': None,
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
21 'name': 'anonymous' }
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
22
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
23 def __init__(self, **kw):
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
24
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
25 # set instance parameters from kw and defaults
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
26 for key in self.defaults:
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
27 setattr(self, key, kw.get(key, self.defaults[key]))
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
28 self.auto_reload = self.auto_reload.lower() == 'true'
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
29
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
30 # request handlers
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
31 self.handlers = [ Index ]
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
32
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
33 # endpoint app if used as middleware
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
34 if self.app:
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
35 assert hasattr(self.app, '__call__')
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
36
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
37 # template loader
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
38 self.template_dirs = self.template_dirs.split()
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
39 self.template_dirs.append(resource_filename(__name__, 'templates'))
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
40 self.loader = TemplateLoader(self.template_dirs,
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
41 auto_reload=self.auto_reload)
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
42
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
43 def __call__(self, environ, start_response):
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
44
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
45 # get a request object
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
46 request = Request(environ)
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
47
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
48 # get the path
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
49 path = request.path_info.strip('/').split('/')
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
50 if path == ['']:
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
51 path = []
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
52 request.environ['path'] = path
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
53
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
54 # match the request to a handler
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
55 for h in self.handlers:
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
56 handler = h.match(self, request)
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
57 if handler is not None:
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
58 break
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
59 else:
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
60 if self.app:
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
61 return self.app(environ, start_response)
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
62 handler = exc.HTTPNotFound
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
63
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
64 # add navigation links to handler [example]
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
65 if hasattr(handler, 'data'):
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
66 handler.data.setdefault('links', [])
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
67 for h in self.handlers:
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
68 handler.data['links'].append((handler.link(h.handler_path),
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
69 h.__name__))
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
70
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
71 # get response
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
72 res = handler()
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
73 return res(environ, start_response)