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