annotate discusssions/dispatcher.py @ 2:910fa7e781b9

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