annotate wsgraph/web.py @ 3:42f484880808

wire this up
author Jeff Hammel <jhammel@mozilla.com>
date Sun, 09 Dec 2012 14:51:23 -0800
parents 7a3b21cafc00
children c7170cab1184
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1 #!/usr/bin/env python
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
2
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
3 """
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
4 web handler for WSGraph
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
5 """
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
6
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
7 import json
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
8 from webob import Request, Response, exc
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
9
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
10 def JSONFormatter(**kwargs):
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
11 return json.dumps(kwargs, sort_keys=True)
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
12
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
13 def JSONGraphFormatter(graph):
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
14 return json.dumps({'nodes': graph.nodes(),
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
15 'edges': graph.edges()},
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
16 sort_keys=True)
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
17
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
18 class Dispatcher(object):
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
19
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
20 def __init__(self, graph,
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
21 graph_formatters=None, # TODO
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
22 node_formatters=None, # TODO
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
23 edge_formatters=None, # TODO
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
24 require_auth=False):
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
25 self.graph = graph
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
26 self.require_auth = require_auth
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
27 self.formatters = {0: JSONGraphFormatter,
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
28 1: JSONFormatter,
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
29 2: JSONFormatter}
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
30
3
42f484880808 wire this up
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
31 self.methods = dict([(i, getattr(self, i))
42f484880808 wire this up
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
32 for i in dir(self)
42f484880808 wire this up
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
33 if i.isupper()])
42f484880808 wire this up
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
34
0
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
35 def __call__(self, environ, start_response):
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
36 request = Request(environ)
3
42f484880808 wire this up
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
37 method = self.methods.get(request.method)
42f484880808 wire this up
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
38 if method is None:
42f484880808 wire this up
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
39 return exc.HTTPMethodNotAllowed()(environ, start_response)
42f484880808 wire this up
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
40 response = method(request)
0
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
41 return response(environ, start_response)
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
42
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
43 @staticmethod
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
44 def path_segments(request):
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
45 import pdb; pdb.set_trace()
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
46
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
47 # HTTP methods
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
48
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
49 def GET(self, request):
2
7a3b21cafc00 start to sketch out REST API
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
50 """
7a3b21cafc00 start to sketch out REST API
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
51 respond to a GET request
7a3b21cafc00 start to sketch out REST API
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
52
7a3b21cafc00 start to sketch out REST API
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
53 Formatters are keyed off of
7a3b21cafc00 start to sketch out REST API
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
54
7a3b21cafc00 start to sketch out REST API
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
55 formatters = {0: {
7a3b21cafc00 start to sketch out REST API
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
56 }
7a3b21cafc00 start to sketch out REST API
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
57
7a3b21cafc00 start to sketch out REST API
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
58 API:
7a3b21cafc00 start to sketch out REST API
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
59
7a3b21cafc00 start to sketch out REST API
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
60 ?format=<format>
7a3b21cafc00 start to sketch out REST API
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
61
7a3b21cafc00 start to sketch out REST API
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
62 """
0
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
63
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
64 segments = self.path_segments(request)
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
65
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
66 # formatter
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
67 formatter = self.formatters[len(segments)]
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
68 return Response(content_type='text/plain',
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
69 body="WSGraph")
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
70
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
71 def POST(self, request):
2
7a3b21cafc00 start to sketch out REST API
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
72 """
7a3b21cafc00 start to sketch out REST API
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
73 respond to a POST request
7a3b21cafc00 start to sketch out REST API
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
74
7a3b21cafc00 start to sketch out REST API
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
75 API:
7a3b21cafc00 start to sketch out REST API
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
76
7a3b21cafc00 start to sketch out REST API
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
77 ?update :
7a3b21cafc00 start to sketch out REST API
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
78 """
7a3b21cafc00 start to sketch out REST API
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
79
7a3b21cafc00 start to sketch out REST API
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
80 return exc.HTTPSeeOther('/') # TODO: /path/to/self
0
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
81
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
82 def HEAD(self, request):
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
83 """respond to a HEAD request"""
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
84 raise NotImplementedError
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
85
3
42f484880808 wire this up
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
86 def OPTIONS(self, request):
42f484880808 wire this up
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
87 raise NotImplementedError
42f484880808 wire this up
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
88
0
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
89 if __name__ == '__main__':
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
90
3
42f484880808 wire this up
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
91 ### example web server
42f484880808 wire this up
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
92
0
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
93 # imports
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
94 from wsgiref import simple_server
3
42f484880808 wire this up
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
95 from model import MemoryCache
42f484880808 wire this up
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
96 from optparse import OptionParser
0
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
97
3
42f484880808 wire this up
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
98 # parse command line options
42f484880808 wire this up
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
99 parser = OptionParser()
42f484880808 wire this up
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
100 parser.add_option('-p', '--port', type='int', default=8080,
42f484880808 wire this up
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
101 help="port to serve on")
42f484880808 wire this up
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
102 options, args = parser.parse_args()
42f484880808 wire this up
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
103
42f484880808 wire this up
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
104 # example model
0
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
105 graph = MemoryCache()
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
106
3
42f484880808 wire this up
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
107 # WSGI app
0
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
108 app = Dispatcher(graph=graph)
3
42f484880808 wire this up
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
109 server = simple_server.make_server(host='0.0.0.0', port=options.port, app=app)
42f484880808 wire this up
Jeff Hammel <jhammel@mozilla.com>
parents: 2
diff changeset
110 print 'http://localhost:%s/' % options.port
0
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
111 server.serve_forever()
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
112