comparison wsgraph/web.py @ 18:b710a2374c8d

more stubbing, goddamn content_type
author Jeff Hammel <jhammel@mozilla.com>
date Mon, 10 Dec 2012 17:42:23 -0800
parents 1053290ba067
children 8162dc8dd93a
comparison
equal deleted inserted replaced
17:1053290ba067 18:b710a2374c8d
11 # rank constants 11 # rank constants
12 GRAPH = 0 12 GRAPH = 0
13 NODE = 1 13 NODE = 1
14 EDGE = 2 14 EDGE = 2
15 15
16 # XXX stubbing hacks 16 # XXX stubbing hack
17 class JSONformatter(object): 17 class JSONformatter(object):
18 def __init__(self, sort_keys=True): 18
19 def __init__(self, content_type='application/json', sort_keys=True):
20 self.content_type = content_type
19 self.sort_keys = sort_keys 21 self.sort_keys = sort_keys
20 22
21 def format(self, _object): 23 def format(self, _object):
22 return json.dumps(graph.node(node), sort_keys=self.sort_keys) 24 return json.dumps(graph.node(node), sort_keys=self.sort_keys)
23 25
24 def node(self, node, graph, request): 26 def node(self, node, graph, request):
25 return Response(body=self.format(graph.node(node))) 27 return Response(content_type=self.content_type,
28 body=self.format(graph.node(node)))
26 29
27 def edge(self, node1, node2, graph, request): 30 def edge(self, node1, node2, graph, request):
28 return Response(body=self.format(graph.edge(node))) 31 return Response(content_type=self.content_type,
32 body=self.format(graph.edge(node)))
33
34 def graph(self, graph, request):
35 return Response(content_type=self.content_type,
36 body='hello? is it me you\'re looking for?')
29 37
30 class Dispatcher(object): 38 class Dispatcher(object):
31 39
32 def __init__(self, graph, 40 def __init__(self, graph,
33 graph_formatters=None, 41 graph_formatters=None,
113 if tuple(segments) not in self.graph: 121 if tuple(segments) not in self.graph:
114 return exc.HTTPNotFound() 122 return exc.HTTPNotFound()
115 123
116 # formatter 124 # formatter
117 formatter = self.formatters[len(segments)] 125 formatter = self.formatters[len(segments)]
126 return formatter(*segments, self.graph, request)
118 return Response(content_type='text/plain', 127 return Response(content_type='text/plain',
119 body="WSGraph") 128 body=content)
120 129
121 def POST(self, request): 130 def POST(self, request):
122 """ 131 """
123 respond to a POST request 132 respond to a POST request
124 133
152 options, args = parser.parse_args() 161 options, args = parser.parse_args()
153 162
154 # example model 163 # example model
155 graph = MemoryCache() 164 graph = MemoryCache()
156 165
166 # example formatter(s)
167 # TODO
168
157 # WSGI app 169 # WSGI app
158 app = Dispatcher(graph=graph) 170 app = Dispatcher(graph=graph)
159 server = simple_server.make_server(host='0.0.0.0', port=options.port, app=app) 171 server = simple_server.make_server(host='0.0.0.0', port=options.port, app=app)
160 print 'http://localhost:%s/' % options.port 172 print 'http://localhost:%s/' % options.port
161 server.serve_forever() 173 server.serve_forever()