comparison dogdish/dispatcher.py @ 0:b3fd8f98df32

initial stub
author Jeff Hammel <jhammel@mozilla.com>
date Wed, 10 Oct 2012 16:01:56 -0700
parents
children 2cb8b06d64df
comparison
equal deleted inserted replaced
-1:000000000000 0:b3fd8f98df32
1 """
2 request dispatcher
3 """
4
5 from webob import Request, exc
6
7 from cgi import escape
8 from urlparse import urlparse
9 from webob import Response, exc
10
11 class Handler(object):
12 def __init__(self, request):
13 self.request = request
14 self.application_path = urlparse(request.application_url)[2]
15
16 def link(self, path=(), permanant=False):
17 if isinstance(path, basestring):
18 path = [ path ]
19 path = [ i.strip('/') for i in path ]
20 if permanant:
21 application_url = [ self.request.application_url ]
22 else:
23 application_url = [ self.application_path ]
24 path = application_url + path
25 return '/'.join(path)
26
27 def redirect(self, location):
28 raise exc.HTTPSeeOther(location=location)
29
30 class Get(Handler):
31
32 body = """<?xml version="1.0"?>
33 <updates>
34 <update type="minor" appVersion="19.0a1" version="19.0a1" extensionVersion="19.0a1" buildID="20121010114416" licenseURL="http://www.mozilla.com/test/sample-eula.html" detailsURL="http://www.mozilla.com/test/sample-details.html">
35 <patch type="complete" URL="http://update.boot2gecko.org/nightly/b2g_update_2012-10-10_114416.mar" hashFunction="SHA512" hashValue="84edb1f53891cf983bc0f6066d31492f43e2d063aaceb05e1c51876f4fde81635afeb7ce3203dee6f65dd59be0cae5c73c49093b625c99acd4118000ad72dda8" size="42924805"/>
36 </update>
37 </updates>"""
38
39 @classmethod
40 def match(cls, request):
41 return request.method == 'GET'
42
43 def __call__(self):
44 name = self.request.GET.get('name', 'world')
45 retval = """<html><body><form method="post">Hello,
46 <input type="text" name="name" value="%s"/></form></body></html>"""
47 return Response(content_type='text/html',
48 body=retval % name)
49
50 class Dispatcher(object):
51
52 ### class level variables
53 defaults = {}
54
55 def __init__(self, **kw):
56 for key in self.defaults:
57 setattr(self, key, kw.get(key, self.defaults[key]))
58 self.handlers = [ Get ]
59 if self.app:
60 assert hasattr(self.app, '__call__')
61
62 ### methods dealing with HTTP
63 def __call__(self, environ, start_response):
64 request = Request(environ)
65 for h in self.handlers:
66 if h.match(request):
67 handler = h(request)
68 break
69 else:
70 if self.app:
71 return self.app(environ, start_response)
72 handler = exc.HTTPNotFound
73 res = handler()
74 return res(environ, start_response)
75
76 def main(args=sys.argv[1:]):
77 """CLI entry point"""
78
79 import optparse
80 from wsgiref import simple_server
81
82 parser = optparse.OptionParser()
83
84 parser.add_option('-p', '--port', dest='port',
85 default=8080, type='int',
86 help="port to serve on")
87 options, args = parser.parse_args()
88
89 dispatcher = Dispatcher()
90
91 server = simple_server.make_server(host='0.0.0.0', port=options.port, app=app)
92 server.serve_forever()
93
94 if __name__ == '__main__':
95 main()