annotate wsgraph/web.py @ 5:9d5a8c90c482

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