Mercurial > hg > MakeItSo
annotate makeitso/dispatcher.py @ 251:b4a3e8bbe095
py 3 + version bump
author | Jeff Hammel <k0scist@gmail.com> |
---|---|
date | Fri, 31 Mar 2017 18:16:36 -0700 |
parents | 1b9573832f33 |
children |
rev | line source |
---|---|
14
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
1 """ |
48
1b9573832f33
clean up the dispatcher code
Jeff Hammel <jhammel@mozilla.com>
parents:
20
diff
changeset
|
2 request dispatcher for MakeItSo! |
14
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
3 """ |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
4 |
48
1b9573832f33
clean up the dispatcher code
Jeff Hammel <jhammel@mozilla.com>
parents:
20
diff
changeset
|
5 import sys |
14
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
6 from handlers import Index |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
7 from webob import Request, Response, exc |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
8 |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
9 class Dispatcher(object): |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
10 |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
11 ### class level variables |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
12 |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
13 def __init__(self, **kw): |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
14 |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
15 # request handlers |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
16 self.handlers = [ Index ] |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
17 |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
18 def __call__(self, environ, start_response): |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
19 |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
20 # get a request object |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
21 request = Request(environ) |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
22 |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
23 # get the path |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
24 path = request.path_info.strip('/').split('/') |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
25 if path == ['']: |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
26 path = [] |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
27 request.environ['path'] = path |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
28 |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
29 # match the request to a handler |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
30 for h in self.handlers: |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
31 handler = h.match(self, request) |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
32 if handler is not None: |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
33 break |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
34 else: |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
35 handler = exc.HTTPNotFound |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
36 |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
37 # get response |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
38 res = handler() |
bf1ce840d0f0
make this a genshi view (should be: tempita view)
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
39 return res(environ, start_response) |
48
1b9573832f33
clean up the dispatcher code
Jeff Hammel <jhammel@mozilla.com>
parents:
20
diff
changeset
|
40 |
1b9573832f33
clean up the dispatcher code
Jeff Hammel <jhammel@mozilla.com>
parents:
20
diff
changeset
|
41 if __name__ == '__main__': |
1b9573832f33
clean up the dispatcher code
Jeff Hammel <jhammel@mozilla.com>
parents:
20
diff
changeset
|
42 from optparse import OptionParser |
1b9573832f33
clean up the dispatcher code
Jeff Hammel <jhammel@mozilla.com>
parents:
20
diff
changeset
|
43 from wsgiref import simple_server |
1b9573832f33
clean up the dispatcher code
Jeff Hammel <jhammel@mozilla.com>
parents:
20
diff
changeset
|
44 |
1b9573832f33
clean up the dispatcher code
Jeff Hammel <jhammel@mozilla.com>
parents:
20
diff
changeset
|
45 # parse command line options |
1b9573832f33
clean up the dispatcher code
Jeff Hammel <jhammel@mozilla.com>
parents:
20
diff
changeset
|
46 parser = OptionParser() |
1b9573832f33
clean up the dispatcher code
Jeff Hammel <jhammel@mozilla.com>
parents:
20
diff
changeset
|
47 parser.add_option('-p', '--port', dest='port', default=8080, type='int', |
1b9573832f33
clean up the dispatcher code
Jeff Hammel <jhammel@mozilla.com>
parents:
20
diff
changeset
|
48 help='port to serve on') |
1b9573832f33
clean up the dispatcher code
Jeff Hammel <jhammel@mozilla.com>
parents:
20
diff
changeset
|
49 options, args = parser.parse_args() |
1b9573832f33
clean up the dispatcher code
Jeff Hammel <jhammel@mozilla.com>
parents:
20
diff
changeset
|
50 |
1b9573832f33
clean up the dispatcher code
Jeff Hammel <jhammel@mozilla.com>
parents:
20
diff
changeset
|
51 # create an app |
1b9573832f33
clean up the dispatcher code
Jeff Hammel <jhammel@mozilla.com>
parents:
20
diff
changeset
|
52 app = Dispatcher() |
1b9573832f33
clean up the dispatcher code
Jeff Hammel <jhammel@mozilla.com>
parents:
20
diff
changeset
|
53 |
1b9573832f33
clean up the dispatcher code
Jeff Hammel <jhammel@mozilla.com>
parents:
20
diff
changeset
|
54 # serve it! |
1b9573832f33
clean up the dispatcher code
Jeff Hammel <jhammel@mozilla.com>
parents:
20
diff
changeset
|
55 server = simple_server.make_server(host=host, port=options.port, app=app) |
1b9573832f33
clean up the dispatcher code
Jeff Hammel <jhammel@mozilla.com>
parents:
20
diff
changeset
|
56 server.serve_forever() |