0
|
1 #!/usr/bin/env python
|
|
2
|
|
3 """
|
|
4 web handler for WSGraph
|
|
5
|
|
6 Formatters are keyed off of:
|
|
7 - (TODO)
|
|
8
|
|
9 formatters = {0: {
|
|
10 }
|
|
11 """
|
|
12
|
|
13 import json
|
|
14 from webob import Request, Response, exc
|
|
15
|
|
16 def JSONFormatter(**kwargs):
|
|
17 return json.dumps(kwargs, sort_keys=True)
|
|
18
|
|
19 def JSONGraphFormatter(graph):
|
|
20 return json.dumps({'nodes': graph.nodes(),
|
|
21 'edges': graph.edges()},
|
|
22 sort_keys=True)
|
|
23
|
|
24 class Dispatcher(object):
|
|
25
|
|
26 def __init__(self, graph,
|
|
27 graph_formatters=None, # TODO
|
|
28 node_formatters=None, # TODO
|
|
29 edge_formatters=None, # TODO
|
|
30 require_auth=False):
|
|
31 self.graph = graph
|
|
32 self.require_auth = require_auth
|
|
33 self.formatters = {0: JSONGraphFormatter,
|
|
34 1: JSONFormatter,
|
|
35 2: JSONFormatter}
|
|
36
|
|
37 def __call__(self, environ, start_response):
|
|
38 request = Request(environ)
|
|
39 import pdb; pdb.set_trace()
|
|
40 return response(environ, start_response)
|
|
41
|
|
42 @staticmethod
|
|
43 def path_segments(request):
|
|
44 import pdb; pdb.set_trace()
|
|
45
|
|
46 # HTTP methods
|
|
47
|
|
48 def GET(self, request):
|
|
49 """respond to a GET request"""
|
|
50
|
|
51 segments = self.path_segments(request)
|
|
52
|
|
53 # formatter
|
|
54 formatter = self.formatters[len(segments)]
|
|
55 return Response(content_type='text/plain',
|
|
56 body="WSGraph")
|
|
57
|
|
58 def POST(self, request):
|
|
59 """respond to a POST request"""
|
|
60 raise NotImplementedError
|
|
61
|
|
62 def HEAD(self, request):
|
|
63 """respond to a HEAD request"""
|
|
64 raise NotImplementedError
|
|
65
|
|
66 if __name__ == '__main__':
|
|
67
|
|
68 # imports
|
|
69 from wsgiref import simple_server
|
|
70 from .model import MemoryCache
|
|
71
|
|
72 graph = MemoryCache()
|
|
73
|
|
74 app = Dispatcher(graph=graph)
|
|
75 server = simple_server.make_server(host='0.0.0.0', port=8080, app=app)
|
|
76 server.serve_forever()
|
|
77
|