annotate wsgraph/web.py @ 15:ee45f44394a0

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