Mercurial > hg > WSGraph
diff 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 |
line wrap: on
line diff
--- a/wsgraph/web.py Sun Dec 09 20:10:07 2012 -0800 +++ b/wsgraph/web.py Mon Dec 10 07:54:10 2012 -0800 @@ -7,13 +7,14 @@ import json from webob import Request, Response, exc +# rank constants GRAPH = 0 NODE = 1 EDGE = 2 +# XXX stubbing hacks def JSONFormatter(**kwargs): return json.dumps(kwargs, sort_keys=True) - def JSONGraphFormatter(graph): return json.dumps({'nodes': graph.nodes(), 'edges': graph.edges()}, @@ -22,15 +23,27 @@ class Dispatcher(object): def __init__(self, graph, - graph_formatters=None, # TODO - node_formatters=None, # TODO - edge_formatters=None, # TODO + graph_formatters=None, + node_formatters=None, + edge_formatters=None, require_auth=False): self.graph = graph self.require_auth = require_auth - self.formatters = {0: JSONGraphFormatter, - 1: JSONFormatter, - 2: JSONFormatter} + + # use JSON formatter by default + if graph_formatters is None: + graph_formatters = {None: JSONGraphFormatter} + if node_formatters is None: + node_formatters = {None: JSONFormatter} + if edge_formatters is None: + edge_formatters = {None: JSONFormatter} + + self.formatters = {GRAPH: graph_formatters, + NODE: node_formatters, + EDGE: edge_formatters} + for key, value in formatters.items(): + # ensure default formatter + assert None in value self.methods = dict([(i, getattr(self, i)) for i in dir(self) @@ -45,8 +58,12 @@ return response(environ, start_response) @staticmethod - def path_segments(request): - import pdb; pdb.set_trace() + def path_segments(path): + """split a path into segments""" + segments = path.strip('/').split('/') + if segments == ['']: + segments = [] + return segments # HTTP methods @@ -69,7 +86,7 @@ """ - segments = self.path_segments(request) + segments = self.path_segments(request.path_info) if len(segments) not in (0,1,2): return exc.HTTPNotFound()