annotate simpypi/dispatcher.py @ 12:1cdb25cef7ee

print helpful message when running test app
author Jeff Hammel <jhammel@mozilla.com>
date Tue, 28 Feb 2012 14:37:21 -0800
parents 2bfcba075db0
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
93e830409685 initial stub commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1 """
93e830409685 initial stub commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
2 request dispatcher:
93e830409685 initial stub commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
3 data persisting across requests should go here
93e830409685 initial stub commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
4 """
93e830409685 initial stub commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
5
93e830409685 initial stub commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
6 import os
10
3e8597489ea3 start moving this to a single file
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
7 #from handlers import Index
2
b03602153de2 removing more cruft
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
8 from webob import Request, Response, exc
0
93e830409685 initial stub commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
9
2
b03602153de2 removing more cruft
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
10 here = os.path.dirname(os.path.abspath(__file__))
0
93e830409685 initial stub commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
11
93e830409685 initial stub commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
12 class Dispatcher(object):
93e830409685 initial stub commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
13
93e830409685 initial stub commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
14 ### class level variables
93e830409685 initial stub commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
15 defaults = {'auto_reload': 'False',
93e830409685 initial stub commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
16 'template_dirs': '',
93e830409685 initial stub commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
17 }
93e830409685 initial stub commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
18
12
1cdb25cef7ee print helpful message when running test app
Jeff Hammel <jhammel@mozilla.com>
parents: 11
diff changeset
19 def __init__(self, directory, index=None):
2
b03602153de2 removing more cruft
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
20
b03602153de2 removing more cruft
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
21 self.directory = directory
b03602153de2 removing more cruft
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
22 assert os.path.exists(directory)
0
93e830409685 initial stub commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
23
93e830409685 initial stub commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
24 # set instance parameters from kw and defaults
2
b03602153de2 removing more cruft
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
25 # XXX unneeded for now
b03602153de2 removing more cruft
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
26 # for key in self.defaults:
b03602153de2 removing more cruft
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
27 # setattr(self, key, kw.get(key, self.defaults[key]))
b03602153de2 removing more cruft
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
28 # self.auto_reload = self.auto_reload.lower() == 'true'
0
93e830409685 initial stub commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
29
93e830409685 initial stub commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
30 # request handlers
10
3e8597489ea3 start moving this to a single file
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
31 self.handlers = dict([(method, getattr(self, method))
11
Jeff Hammel <jhammel@mozilla.com>
parents: 10
diff changeset
32 for method in ('GET', 'POST')])
10
3e8597489ea3 start moving this to a single file
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
33 # XXX unneeded for now
3e8597489ea3 start moving this to a single file
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
34 # self.handlers = [ Index ]
3e8597489ea3 start moving this to a single file
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
35
3e8597489ea3 start moving this to a single file
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
36 # cache index HTML
12
1cdb25cef7ee print helpful message when running test app
Jeff Hammel <jhammel@mozilla.com>
parents: 11
diff changeset
37 self.index = index or os.path.join(here, 'templates', 'index.html')
10
3e8597489ea3 start moving this to a single file
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
38 assert os.path.exists(self.index)
3e8597489ea3 start moving this to a single file
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
39 self.index = file(self.index).read()
0
93e830409685 initial stub commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
40
93e830409685 initial stub commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
41 # template directories
2
b03602153de2 removing more cruft
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
42 # self.template_dirs = self.template_dirs.split()
10
3e8597489ea3 start moving this to a single file
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
43 # self.template_dirs = [os.path.join(here, 'templates')]
0
93e830409685 initial stub commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
44
93e830409685 initial stub commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
45 def __call__(self, environ, start_response):
93e830409685 initial stub commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
46
93e830409685 initial stub commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
47 # get a request object
93e830409685 initial stub commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
48 request = Request(environ)
93e830409685 initial stub commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
49
2
b03602153de2 removing more cruft
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
50 # get the path
10
3e8597489ea3 start moving this to a single file
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
51 # path = request.path_info.strip('/').split('/')
3e8597489ea3 start moving this to a single file
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
52 # if path == ['']:
3e8597489ea3 start moving this to a single file
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
53 # path = []
3e8597489ea3 start moving this to a single file
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
54 # request.environ['path'] = path
0
93e830409685 initial stub commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
55
93e830409685 initial stub commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
56 # match the request to a handler
10
3e8597489ea3 start moving this to a single file
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
57 handler = self.handlers.get(request.method)
3e8597489ea3 start moving this to a single file
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
58 if handler:
3e8597489ea3 start moving this to a single file
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
59 res = handler(request)
0
93e830409685 initial stub commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
60 else:
10
3e8597489ea3 start moving this to a single file
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
61 res = exc.HTTPNotFound()
3e8597489ea3 start moving this to a single file
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
62 # for h in self.handlers:
3e8597489ea3 start moving this to a single file
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
63 # handler = h.match(self, request)
3e8597489ea3 start moving this to a single file
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
64 # if handler is not None:
3e8597489ea3 start moving this to a single file
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
65 # break
3e8597489ea3 start moving this to a single file
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
66 # else:
3e8597489ea3 start moving this to a single file
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
67 # handler = exc.HTTPNotFound
0
93e830409685 initial stub commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
68
93e830409685 initial stub commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
69 # get response
10
3e8597489ea3 start moving this to a single file
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
70 # res = handler()
0
93e830409685 initial stub commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
71 return res(environ, start_response)
10
3e8597489ea3 start moving this to a single file
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
72
3e8597489ea3 start moving this to a single file
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
73 def GET(self, request):
3e8597489ea3 start moving this to a single file
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
74 return Response(body=self.index, content_type='text/html')
3e8597489ea3 start moving this to a single file
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
75
3e8597489ea3 start moving this to a single file
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
76 def POST(self, request):
3e8597489ea3 start moving this to a single file
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
77 """handle posting a package"""
3e8597489ea3 start moving this to a single file
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
78
3e8597489ea3 start moving this to a single file
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
79 # get the package
3e8597489ea3 start moving this to a single file
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
80 package = self.request.POST.get('package')
3e8597489ea3 start moving this to a single file
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
81
3e8597489ea3 start moving this to a single file
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
82 # redirect to the main page
3e8597489ea3 start moving this to a single file
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
83 # TODO
3e8597489ea3 start moving this to a single file
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
84 # self.redirect(self.link(self.handler_path))