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