annotate webob_view/template/+package+/dispatcher.py @ 5:f70d8f3aee51

allow app argument to allow use as middleware OOTB
author k0s <k0scist@gmail.com>
date Sun, 27 Dec 2009 16:38:50 -0500
parents 4dcb932065e4
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
72cb6dc31e3f initial commit of webob_view from https://svn.openplans.org/svn/standalone/templates/webob_view/trunk/
k0s <k0scist@gmail.com>
parents:
diff changeset
1 """
2
4ebaf68f2272 moving to dispatcher as that's what it is now
k0s <k0scist@gmail.com>
parents: 1
diff changeset
2 request dispatcher
0
72cb6dc31e3f initial commit of webob_view from https://svn.openplans.org/svn/standalone/templates/webob_view/trunk/
k0s <k0scist@gmail.com>
parents:
diff changeset
3 """
72cb6dc31e3f initial commit of webob_view from https://svn.openplans.org/svn/standalone/templates/webob_view/trunk/
k0s <k0scist@gmail.com>
parents:
diff changeset
4
1
eb2cb7ebc849 moving to a handler architecture
k0s <k0scist@gmail.com>
parents: 0
diff changeset
5 from handlers import Get, Post
eb2cb7ebc849 moving to a handler architecture
k0s <k0scist@gmail.com>
parents: 0
diff changeset
6 from webob import Request, exc
0
72cb6dc31e3f initial commit of webob_view from https://svn.openplans.org/svn/standalone/templates/webob_view/trunk/
k0s <k0scist@gmail.com>
parents:
diff changeset
7
1
eb2cb7ebc849 moving to a handler architecture
k0s <k0scist@gmail.com>
parents: 0
diff changeset
8 class Dispatcher(object):
0
72cb6dc31e3f initial commit of webob_view from https://svn.openplans.org/svn/standalone/templates/webob_view/trunk/
k0s <k0scist@gmail.com>
parents:
diff changeset
9
72cb6dc31e3f initial commit of webob_view from https://svn.openplans.org/svn/standalone/templates/webob_view/trunk/
k0s <k0scist@gmail.com>
parents:
diff changeset
10 ### class level variables
5
f70d8f3aee51 allow app argument to allow use as middleware OOTB
k0s <k0scist@gmail.com>
parents: 3
diff changeset
11 defaults = { 'app': None}
0
72cb6dc31e3f initial commit of webob_view from https://svn.openplans.org/svn/standalone/templates/webob_view/trunk/
k0s <k0scist@gmail.com>
parents:
diff changeset
12
72cb6dc31e3f initial commit of webob_view from https://svn.openplans.org/svn/standalone/templates/webob_view/trunk/
k0s <k0scist@gmail.com>
parents:
diff changeset
13 def __init__(self, **kw):
72cb6dc31e3f initial commit of webob_view from https://svn.openplans.org/svn/standalone/templates/webob_view/trunk/
k0s <k0scist@gmail.com>
parents:
diff changeset
14 for key in self.defaults:
72cb6dc31e3f initial commit of webob_view from https://svn.openplans.org/svn/standalone/templates/webob_view/trunk/
k0s <k0scist@gmail.com>
parents:
diff changeset
15 setattr(self, key, kw.get(key, self.defaults[key]))
1
eb2cb7ebc849 moving to a handler architecture
k0s <k0scist@gmail.com>
parents: 0
diff changeset
16 self.handlers = [ Get, Post ]
5
f70d8f3aee51 allow app argument to allow use as middleware OOTB
k0s <k0scist@gmail.com>
parents: 3
diff changeset
17 if self.app:
f70d8f3aee51 allow app argument to allow use as middleware OOTB
k0s <k0scist@gmail.com>
parents: 3
diff changeset
18 assert hasattr(self.app, '__call__')
0
72cb6dc31e3f initial commit of webob_view from https://svn.openplans.org/svn/standalone/templates/webob_view/trunk/
k0s <k0scist@gmail.com>
parents:
diff changeset
19
72cb6dc31e3f initial commit of webob_view from https://svn.openplans.org/svn/standalone/templates/webob_view/trunk/
k0s <k0scist@gmail.com>
parents:
diff changeset
20 ### methods dealing with HTTP
72cb6dc31e3f initial commit of webob_view from https://svn.openplans.org/svn/standalone/templates/webob_view/trunk/
k0s <k0scist@gmail.com>
parents:
diff changeset
21 def __call__(self, environ, start_response):
72cb6dc31e3f initial commit of webob_view from https://svn.openplans.org/svn/standalone/templates/webob_view/trunk/
k0s <k0scist@gmail.com>
parents:
diff changeset
22 request = Request(environ)
1
eb2cb7ebc849 moving to a handler architecture
k0s <k0scist@gmail.com>
parents: 0
diff changeset
23 for h in self.handlers:
eb2cb7ebc849 moving to a handler architecture
k0s <k0scist@gmail.com>
parents: 0
diff changeset
24 if h.match(request):
eb2cb7ebc849 moving to a handler architecture
k0s <k0scist@gmail.com>
parents: 0
diff changeset
25 handler = h(request)
eb2cb7ebc849 moving to a handler architecture
k0s <k0scist@gmail.com>
parents: 0
diff changeset
26 break
3
4dcb932065e4 finish dispatching version of webob_view
k0s <k0scist@gmail.com>
parents: 2
diff changeset
27 else:
5
f70d8f3aee51 allow app argument to allow use as middleware OOTB
k0s <k0scist@gmail.com>
parents: 3
diff changeset
28 if self.app:
f70d8f3aee51 allow app argument to allow use as middleware OOTB
k0s <k0scist@gmail.com>
parents: 3
diff changeset
29 return self.app(environ, start_response)
3
4dcb932065e4 finish dispatching version of webob_view
k0s <k0scist@gmail.com>
parents: 2
diff changeset
30 handler = exc.HTTPNotFound
1
eb2cb7ebc849 moving to a handler architecture
k0s <k0scist@gmail.com>
parents: 0
diff changeset
31 res = handler()
0
72cb6dc31e3f initial commit of webob_view from https://svn.openplans.org/svn/standalone/templates/webob_view/trunk/
k0s <k0scist@gmail.com>
parents:
diff changeset
32 return res(environ, start_response)