changeset 23:24d57daaca21

well, now the request dispatches
author Jeff Hammel <jhammel@mozilla.com>
date Mon, 10 Dec 2012 21:15:24 -0800
parents a401bf63c938
children e46b4466abac
files wsgraph/model.py wsgraph/web.py
diffstat 2 files changed, 30 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/wsgraph/model.py	Mon Dec 10 17:51:50 2012 -0800
+++ b/wsgraph/model.py	Mon Dec 10 21:15:24 2012 -0800
@@ -30,6 +30,23 @@
     def edges(self):
         """returns a list of all edges"""
 
+    def __call__(self):
+        """
+        returns JSGN format of graph:
+        {'nodes': {'node1': {},
+                   'node2': {}, ...},
+         'edges': {'node1': {'node2': {},
+                             'node3': {}, ...},
+                   'node2': {'node1': {}, ...}}
+        }
+        """
+        retval = {'nodes': {}, 'edges': {}}
+        for node in self.nodes():
+            retval['nodes'][node] = self.node(node)
+        for node1, node2 in self.edges():
+            retval['edges'].setdefault(node1, {})[node2] = self.edge(node1, node2)
+        return retval
+
     def __getitem__(self, key):
         """
         if key is a basestring, return the node of that name;
--- a/wsgraph/web.py	Mon Dec 10 17:51:50 2012 -0800
+++ b/wsgraph/web.py	Mon Dec 10 21:15:24 2012 -0800
@@ -34,7 +34,7 @@
 
     def graph(self, request, graph):
         return Response(content_type=self.content_type,
-                        body='hello? is it me you\'re looking for?')
+                        body = self.format(graph()))
 
 
 class Dispatcher(object):
@@ -76,6 +76,13 @@
             segments = []
         return segments
 
+
+    def formatter(self, rank, request):
+        """returns the formatter for a request"""
+        formatters = self.formatters[rank]
+        format = request.GET.get('format')
+        return formatters.get(format, formatters[None])
+
     # HTTP methods
 
     def GET(self, request):
@@ -117,10 +124,8 @@
                 return exc.HTTPNotFound()
 
         # formatter
-        formatter = self.formatters[len(segments)]
+        formatter = self.formatter(rank, request)
         return formatter(request, self.graph, *segments)
-        return Response(content_type='text/plain',
-                        body=content)
 
     def POST(self, request):
         """
@@ -131,6 +136,10 @@
         ?update :
         """
 
+        # check authorization
+        if self.require_auth:
+            raise NotImplementedError
+
         return exc.HTTPSeeOther(location='/') # TODO: /path/to/self
 
     def HEAD(self, request):