annotate wsgraph/web.py @ 4:c7170cab1184

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