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