annotate simplewiki/dispatcher.py @ 5:b2fbb4f982da default tip

[mq]: edit
author Jeff Hammel <jhammel@mozilla.com>
date Tue, 07 Sep 2010 22:58:11 -0700
parents dd1c4916cbcd
children
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
5
b2fbb4f982da [mq]: edit
Jeff Hammel <jhammel@mozilla.com>
parents: 4
diff changeset
8 from handlers import GenshiRenderer, Index, Post, FileServer, EditView
0
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': '',
1
4c83f7715993 [mq]: renderer
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
20 'name': 'anonymous',
4c83f7715993 [mq]: renderer
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
21 'directory': None }
0
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
1
4c83f7715993 [mq]: renderer
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
30 assert self.directory and os.path.exists(self.directory), "Must specify an existing directory"
0
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
31
1
4c83f7715993 [mq]: renderer
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
32 # request handlers
5
b2fbb4f982da [mq]: edit
Jeff Hammel <jhammel@mozilla.com>
parents: 4
diff changeset
33 self.handlers = [ Post, EditView, GenshiRenderer, Index, FileServer ]
0
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
34
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
35 # template loader
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
36 self.template_dirs = self.template_dirs.split()
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
37 self.template_dirs.append(resource_filename(__name__, 'templates'))
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
38 self.loader = TemplateLoader(self.template_dirs,
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
39 auto_reload=self.auto_reload)
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
40
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
41 def __call__(self, environ, start_response):
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
42
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
43 # get a request object
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
44 request = Request(environ)
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
45
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
46 # get the path
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
47 path = request.path_info.strip('/').split('/')
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
48 if path == ['']:
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
49 path = []
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
50 request.environ['path'] = path
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
51
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
52 # match the request to a handler
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
53 for h in self.handlers:
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
54 handler = h.match(self, request)
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
55 if handler is not None:
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
56 break
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
57 else:
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
58 handler = exc.HTTPNotFound
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
59
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
60 # get response
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
61 res = handler()
d5102c881cb5 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
62 return res(environ, start_response)