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