Mercurial > mozilla > hg > dogfood
annotate dogdish/dispatcher.py @ 10:235738f81a3d
adding an example directory
author | Jeff Hammel <jhammel@mozilla.com> |
---|---|
date | Wed, 17 Oct 2012 10:08:14 -0700 |
parents | f517d80bafe0 |
children | 5babc2ae6c27 |
rev | line source |
---|---|
1 | 1 #!/usr/bin/env python |
2 | |
0 | 3 """ |
1 | 4 dogdish |
7
65d0a1153864
a stub that sorta does something
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
5 https://bugzilla.mozilla.org/show_bug.cgi?id=800118 |
0 | 6 """ |
7 | |
8 | 8 import os |
4 | 9 import sys |
0 | 10 from urlparse import urlparse |
4 | 11 from webob import Request |
0 | 12 from webob import Response, exc |
13 | |
8 | 14 here = os.path.dirname(os.path.abspath(__file__)) |
15 | |
0 | 16 class Handler(object): |
9 | 17 """abstract handler object for a request""" |
1 | 18 |
0 | 19 def __init__(self, request): |
20 self.request = request | |
21 self.application_path = urlparse(request.application_url)[2] | |
22 | |
23 def link(self, path=(), permanant=False): | |
24 if isinstance(path, basestring): | |
25 path = [ path ] | |
26 path = [ i.strip('/') for i in path ] | |
27 if permanant: | |
28 application_url = [ self.request.application_url ] | |
29 else: | |
30 application_url = [ self.application_path ] | |
31 path = application_url + path | |
32 return '/'.join(path) | |
33 | |
34 def redirect(self, location): | |
35 raise exc.HTTPSeeOther(location=location) | |
36 | |
37 class Get(Handler): | |
9 | 38 """handle GET requests""" |
0 | 39 |
9 | 40 # template for response body |
0 | 41 body = """<?xml version="1.0"?> |
42 <updates> | |
43 <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"> | |
9 | 44 <patch type="complete" URL="http://update.boot2gecko.org/nightly/b2g_update_2012-10-10_114416.mar%(query)s" hashFunction="SHA512" hashValue="84edb1f53891cf983bc0f6066d31492f43e2d063aaceb05e1c51876f4fde81635afeb7ce3203dee6f65dd59be0cae5c73c49093b625c99acd4118000ad72dda8" size="42924805"/> |
0 | 45 </update> |
46 </updates>""" | |
47 | |
9 | 48 ### methods for request handler |
49 | |
0 | 50 @classmethod |
51 def match(cls, request): | |
52 return request.method == 'GET' | |
53 | |
54 def __call__(self): | |
7
65d0a1153864
a stub that sorta does something
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
55 body = self.body |
65d0a1153864
a stub that sorta does something
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
56 query = {} |
65d0a1153864
a stub that sorta does something
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
57 dogfood_id = self.request.GET.get('dogfood_id') |
65d0a1153864
a stub that sorta does something
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
58 if dogfood_id: |
65d0a1153864
a stub that sorta does something
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
59 query['dogfood_id'] = dogfood_id |
65d0a1153864
a stub that sorta does something
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
60 |
65d0a1153864
a stub that sorta does something
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
61 # build query string |
65d0a1153864
a stub that sorta does something
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
62 if query: |
65d0a1153864
a stub that sorta does something
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
63 query = '?' + '&'.join(['%s=%s' % (key, value) for key, value in query.items()]) |
65d0a1153864
a stub that sorta does something
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
64 else: |
65d0a1153864
a stub that sorta does something
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
65 query = '' |
65d0a1153864
a stub that sorta does something
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
66 |
1 | 67 return Response(content_type='text/xml', |
7
65d0a1153864
a stub that sorta does something
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
68 body=body % query) |
0 | 69 |
70 class Dispatcher(object): | |
9 | 71 """web application""" |
72 | |
0 | 73 |
74 ### class level variables | |
8 | 75 defaults = {'directory': here} |
0 | 76 |
77 def __init__(self, **kw): | |
78 for key in self.defaults: | |
79 setattr(self, key, kw.get(key, self.defaults[key])) | |
80 self.handlers = [ Get ] | |
81 | |
82 ### methods dealing with HTTP | |
83 def __call__(self, environ, start_response): | |
84 request = Request(environ) | |
85 for h in self.handlers: | |
86 if h.match(request): | |
87 handler = h(request) | |
88 break | |
89 else: | |
90 handler = exc.HTTPNotFound | |
91 res = handler() | |
92 return res(environ, start_response) | |
93 | |
94 def main(args=sys.argv[1:]): | |
95 """CLI entry point""" | |
96 | |
97 import optparse | |
98 from wsgiref import simple_server | |
99 | |
100 parser = optparse.OptionParser() | |
101 | |
102 parser.add_option('-p', '--port', dest='port', | |
103 default=8080, type='int', | |
104 help="port to serve on") | |
105 options, args = parser.parse_args() | |
106 | |
6 | 107 app = Dispatcher() |
0 | 108 |
7
65d0a1153864
a stub that sorta does something
Jeff Hammel <jhammel@mozilla.com>
parents:
6
diff
changeset
|
109 print "http://localhost:%s/" % options.port |
0 | 110 server = simple_server.make_server(host='0.0.0.0', port=options.port, app=app) |
111 server.serve_forever() | |
112 | |
113 if __name__ == '__main__': | |
114 main() |