view wsgraph/web.py @ 0:cfcfa093e4b4

initial commit
author Jeff Hammel <jhammel@mozilla.com>
date Sun, 09 Dec 2012 10:21:43 -0800
parents
children 7a3b21cafc00
line wrap: on
line source

#!/usr/bin/env python

"""
web handler for WSGraph

Formatters are keyed off of:
- (TODO)

formatters = {0: {
}
"""

import json
from webob import Request, Response, exc

def JSONFormatter(**kwargs):
    return json.dumps(kwargs, sort_keys=True)

def JSONGraphFormatter(graph):
    return json.dumps({'nodes': graph.nodes(),
                       'edges': graph.edges()},
                      sort_keys=True)

class Dispatcher(object):

    def __init__(self, graph,
                 graph_formatters=None, # TODO
                 node_formatters=None, # TODO
                 edge_formatters=None, # TODO
                 require_auth=False):
        self.graph = graph
        self.require_auth = require_auth
        self.formatters = {0: JSONGraphFormatter,
                           1: JSONFormatter,
                           2: JSONFormatter}

    def __call__(self, environ, start_response):
        request = Request(environ)
        import pdb; pdb.set_trace()
        return response(environ, start_response)

    @staticmethod
    def path_segments(request):
        import pdb; pdb.set_trace()

    # HTTP methods

    def GET(self, request):
        """respond to a GET request"""

        segments = self.path_segments(request)

        # formatter
        formatter = self.formatters[len(segments)]
        return Response(content_type='text/plain',
                        body="WSGraph")

    def POST(self, request):
        """respond to a POST request"""
        raise NotImplementedError

    def HEAD(self, request):
        """respond to a HEAD request"""
        raise NotImplementedError

if __name__ == '__main__':

    # imports
    from wsgiref import simple_server
    from .model import MemoryCache

    graph = MemoryCache()

    app = Dispatcher(graph=graph)
    server = simple_server.make_server(host='0.0.0.0', port=8080, app=app)
    server.serve_forever()