annotate dogdish/dispatcher.py @ 8:ebb65758d725

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