Mercurial > hg > WSGraph
comparison wsgraph/web.py @ 5:9d5a8c90c482
more stubbing
author | Jeff Hammel <jhammel@mozilla.com> |
---|---|
date | Mon, 10 Dec 2012 07:54:10 -0800 |
parents | c7170cab1184 |
children | 259210f2e029 |
comparison
equal
deleted
inserted
replaced
4:c7170cab1184 | 5:9d5a8c90c482 |
---|---|
5 """ | 5 """ |
6 | 6 |
7 import json | 7 import json |
8 from webob import Request, Response, exc | 8 from webob import Request, Response, exc |
9 | 9 |
10 # rank constants | |
10 GRAPH = 0 | 11 GRAPH = 0 |
11 NODE = 1 | 12 NODE = 1 |
12 EDGE = 2 | 13 EDGE = 2 |
13 | 14 |
15 # XXX stubbing hacks | |
14 def JSONFormatter(**kwargs): | 16 def JSONFormatter(**kwargs): |
15 return json.dumps(kwargs, sort_keys=True) | 17 return json.dumps(kwargs, sort_keys=True) |
16 | |
17 def JSONGraphFormatter(graph): | 18 def JSONGraphFormatter(graph): |
18 return json.dumps({'nodes': graph.nodes(), | 19 return json.dumps({'nodes': graph.nodes(), |
19 'edges': graph.edges()}, | 20 'edges': graph.edges()}, |
20 sort_keys=True) | 21 sort_keys=True) |
21 | 22 |
22 class Dispatcher(object): | 23 class Dispatcher(object): |
23 | 24 |
24 def __init__(self, graph, | 25 def __init__(self, graph, |
25 graph_formatters=None, # TODO | 26 graph_formatters=None, |
26 node_formatters=None, # TODO | 27 node_formatters=None, |
27 edge_formatters=None, # TODO | 28 edge_formatters=None, |
28 require_auth=False): | 29 require_auth=False): |
29 self.graph = graph | 30 self.graph = graph |
30 self.require_auth = require_auth | 31 self.require_auth = require_auth |
31 self.formatters = {0: JSONGraphFormatter, | 32 |
32 1: JSONFormatter, | 33 # use JSON formatter by default |
33 2: JSONFormatter} | 34 if graph_formatters is None: |
35 graph_formatters = {None: JSONGraphFormatter} | |
36 if node_formatters is None: | |
37 node_formatters = {None: JSONFormatter} | |
38 if edge_formatters is None: | |
39 edge_formatters = {None: JSONFormatter} | |
40 | |
41 self.formatters = {GRAPH: graph_formatters, | |
42 NODE: node_formatters, | |
43 EDGE: edge_formatters} | |
44 for key, value in formatters.items(): | |
45 # ensure default formatter | |
46 assert None in value | |
34 | 47 |
35 self.methods = dict([(i, getattr(self, i)) | 48 self.methods = dict([(i, getattr(self, i)) |
36 for i in dir(self) | 49 for i in dir(self) |
37 if i.isupper()]) | 50 if i.isupper()]) |
38 | 51 |
43 return exc.HTTPMethodNotAllowed()(environ, start_response) | 56 return exc.HTTPMethodNotAllowed()(environ, start_response) |
44 response = method(request) | 57 response = method(request) |
45 return response(environ, start_response) | 58 return response(environ, start_response) |
46 | 59 |
47 @staticmethod | 60 @staticmethod |
48 def path_segments(request): | 61 def path_segments(path): |
49 import pdb; pdb.set_trace() | 62 """split a path into segments""" |
63 segments = path.strip('/').split('/') | |
64 if segments == ['']: | |
65 segments = [] | |
66 return segments | |
50 | 67 |
51 # HTTP methods | 68 # HTTP methods |
52 | 69 |
53 def GET(self, request): | 70 def GET(self, request): |
54 """ | 71 """ |
67 | 84 |
68 ?format=<format> | 85 ?format=<format> |
69 | 86 |
70 """ | 87 """ |
71 | 88 |
72 segments = self.path_segments(request) | 89 segments = self.path_segments(request.path_info) |
73 if len(segments) not in (0,1,2): | 90 if len(segments) not in (0,1,2): |
74 return exc.HTTPNotFound() | 91 return exc.HTTPNotFound() |
75 | 92 |
76 # formatter | 93 # formatter |
77 formatter = self.formatters[len(segments)] | 94 formatter = self.formatters[len(segments)] |