annotate uploader/dispatcher.py @ 0:827f7577f940

initial commit of file upload widget
author k0s <k0scist@gmail.com>
date Sat, 21 Nov 2009 15:29:03 -0500
parents
children a02c4fcd7001
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
827f7577f940 initial commit of file upload widget
k0s <k0scist@gmail.com>
parents:
diff changeset
1 """
827f7577f940 initial commit of file upload widget
k0s <k0scist@gmail.com>
parents:
diff changeset
2 request dispatcher
827f7577f940 initial commit of file upload widget
k0s <k0scist@gmail.com>
parents:
diff changeset
3 """
827f7577f940 initial commit of file upload widget
k0s <k0scist@gmail.com>
parents:
diff changeset
4
827f7577f940 initial commit of file upload widget
k0s <k0scist@gmail.com>
parents:
diff changeset
5 import os
827f7577f940 initial commit of file upload widget
k0s <k0scist@gmail.com>
parents:
diff changeset
6 from handlers import Get, Post
827f7577f940 initial commit of file upload widget
k0s <k0scist@gmail.com>
parents:
diff changeset
7 from webob import Request, exc
827f7577f940 initial commit of file upload widget
k0s <k0scist@gmail.com>
parents:
diff changeset
8
827f7577f940 initial commit of file upload widget
k0s <k0scist@gmail.com>
parents:
diff changeset
9 class Dispatcher(object):
827f7577f940 initial commit of file upload widget
k0s <k0scist@gmail.com>
parents:
diff changeset
10
827f7577f940 initial commit of file upload widget
k0s <k0scist@gmail.com>
parents:
diff changeset
11 ### class level variables
827f7577f940 initial commit of file upload widget
k0s <k0scist@gmail.com>
parents:
diff changeset
12 defaults = { 'directory': None}
827f7577f940 initial commit of file upload widget
k0s <k0scist@gmail.com>
parents:
diff changeset
13
827f7577f940 initial commit of file upload widget
k0s <k0scist@gmail.com>
parents:
diff changeset
14 def __init__(self, **kw):
827f7577f940 initial commit of file upload widget
k0s <k0scist@gmail.com>
parents:
diff changeset
15 for key in self.defaults:
827f7577f940 initial commit of file upload widget
k0s <k0scist@gmail.com>
parents:
diff changeset
16 setattr(self, key, kw.get(key, self.defaults[key]))
827f7577f940 initial commit of file upload widget
k0s <k0scist@gmail.com>
parents:
diff changeset
17 self.handlers = [ Get, Post ]
827f7577f940 initial commit of file upload widget
k0s <k0scist@gmail.com>
parents:
diff changeset
18 assert os.path.exists(self.directory)
827f7577f940 initial commit of file upload widget
k0s <k0scist@gmail.com>
parents:
diff changeset
19
827f7577f940 initial commit of file upload widget
k0s <k0scist@gmail.com>
parents:
diff changeset
20 ### methods dealing with HTTP
827f7577f940 initial commit of file upload widget
k0s <k0scist@gmail.com>
parents:
diff changeset
21 def __call__(self, environ, start_response):
827f7577f940 initial commit of file upload widget
k0s <k0scist@gmail.com>
parents:
diff changeset
22 request = Request(environ)
827f7577f940 initial commit of file upload widget
k0s <k0scist@gmail.com>
parents:
diff changeset
23 for h in self.handlers:
827f7577f940 initial commit of file upload widget
k0s <k0scist@gmail.com>
parents:
diff changeset
24 if h.match(request):
827f7577f940 initial commit of file upload widget
k0s <k0scist@gmail.com>
parents:
diff changeset
25 handler = h(self, request)
827f7577f940 initial commit of file upload widget
k0s <k0scist@gmail.com>
parents:
diff changeset
26 break
827f7577f940 initial commit of file upload widget
k0s <k0scist@gmail.com>
parents:
diff changeset
27 else:
827f7577f940 initial commit of file upload widget
k0s <k0scist@gmail.com>
parents:
diff changeset
28 handler = exc.HTTPNotFound
827f7577f940 initial commit of file upload widget
k0s <k0scist@gmail.com>
parents:
diff changeset
29 res = handler()
827f7577f940 initial commit of file upload widget
k0s <k0scist@gmail.com>
parents:
diff changeset
30 return res(environ, start_response)