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