Mercurial > hg > WSGraph
annotate wsgraph/web.py @ 3:42f484880808
wire this up
author | Jeff Hammel <jhammel@mozilla.com> |
---|---|
date | Sun, 09 Dec 2012 14:51:23 -0800 |
parents | 7a3b21cafc00 |
children | c7170cab1184 |
rev | line source |
---|---|
0 | 1 #!/usr/bin/env python |
2 | |
3 """ | |
4 web handler for WSGraph | |
5 """ | |
6 | |
7 import json | |
8 from webob import Request, Response, exc | |
9 | |
10 def JSONFormatter(**kwargs): | |
11 return json.dumps(kwargs, sort_keys=True) | |
12 | |
13 def JSONGraphFormatter(graph): | |
14 return json.dumps({'nodes': graph.nodes(), | |
15 'edges': graph.edges()}, | |
16 sort_keys=True) | |
17 | |
18 class Dispatcher(object): | |
19 | |
20 def __init__(self, graph, | |
21 graph_formatters=None, # TODO | |
22 node_formatters=None, # TODO | |
23 edge_formatters=None, # TODO | |
24 require_auth=False): | |
25 self.graph = graph | |
26 self.require_auth = require_auth | |
27 self.formatters = {0: JSONGraphFormatter, | |
28 1: JSONFormatter, | |
29 2: JSONFormatter} | |
30 | |
3 | 31 self.methods = dict([(i, getattr(self, i)) |
32 for i in dir(self) | |
33 if i.isupper()]) | |
34 | |
0 | 35 def __call__(self, environ, start_response): |
36 request = Request(environ) | |
3 | 37 method = self.methods.get(request.method) |
38 if method is None: | |
39 return exc.HTTPMethodNotAllowed()(environ, start_response) | |
40 response = method(request) | |
0 | 41 return response(environ, start_response) |
42 | |
43 @staticmethod | |
44 def path_segments(request): | |
45 import pdb; pdb.set_trace() | |
46 | |
47 # HTTP methods | |
48 | |
49 def GET(self, request): | |
2
7a3b21cafc00
start to sketch out REST API
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
50 """ |
7a3b21cafc00
start to sketch out REST API
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
51 respond to a GET request |
7a3b21cafc00
start to sketch out REST API
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
52 |
7a3b21cafc00
start to sketch out REST API
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
53 Formatters are keyed off of |
7a3b21cafc00
start to sketch out REST API
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
54 |
7a3b21cafc00
start to sketch out REST API
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
55 formatters = {0: { |
7a3b21cafc00
start to sketch out REST API
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
56 } |
7a3b21cafc00
start to sketch out REST API
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
57 |
7a3b21cafc00
start to sketch out REST API
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
58 API: |
7a3b21cafc00
start to sketch out REST API
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
59 |
7a3b21cafc00
start to sketch out REST API
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
60 ?format=<format> |
7a3b21cafc00
start to sketch out REST API
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
61 |
7a3b21cafc00
start to sketch out REST API
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
62 """ |
0 | 63 |
64 segments = self.path_segments(request) | |
65 | |
66 # formatter | |
67 formatter = self.formatters[len(segments)] | |
68 return Response(content_type='text/plain', | |
69 body="WSGraph") | |
70 | |
71 def POST(self, request): | |
2
7a3b21cafc00
start to sketch out REST API
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
72 """ |
7a3b21cafc00
start to sketch out REST API
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
73 respond to a POST request |
7a3b21cafc00
start to sketch out REST API
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
74 |
7a3b21cafc00
start to sketch out REST API
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
75 API: |
7a3b21cafc00
start to sketch out REST API
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
76 |
7a3b21cafc00
start to sketch out REST API
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
77 ?update : |
7a3b21cafc00
start to sketch out REST API
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
78 """ |
7a3b21cafc00
start to sketch out REST API
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
79 |
7a3b21cafc00
start to sketch out REST API
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
80 return exc.HTTPSeeOther('/') # TODO: /path/to/self |
0 | 81 |
82 def HEAD(self, request): | |
83 """respond to a HEAD request""" | |
84 raise NotImplementedError | |
85 | |
3 | 86 def OPTIONS(self, request): |
87 raise NotImplementedError | |
88 | |
0 | 89 if __name__ == '__main__': |
90 | |
3 | 91 ### example web server |
92 | |
0 | 93 # imports |
94 from wsgiref import simple_server | |
3 | 95 from model import MemoryCache |
96 from optparse import OptionParser | |
0 | 97 |
3 | 98 # parse command line options |
99 parser = OptionParser() | |
100 parser.add_option('-p', '--port', type='int', default=8080, | |
101 help="port to serve on") | |
102 options, args = parser.parse_args() | |
103 | |
104 # example model | |
0 | 105 graph = MemoryCache() |
106 | |
3 | 107 # WSGI app |
0 | 108 app = Dispatcher(graph=graph) |
3 | 109 server = simple_server.make_server(host='0.0.0.0', port=options.port, app=app) |
110 print 'http://localhost:%s/' % options.port | |
0 | 111 server.serve_forever() |
112 |