Work on pseudo-REST dispatcher
authorAaron Bentley <aaron.bentley@utoronto.ca>
Wed, 21 Dec 2005 07:11:30 +0000 (02:11 -0500)
committerAaron Bentley <aaron.bentley@utoronto.ca>
Wed, 21 Dec 2005 07:11:30 +0000 (02:11 -0500)
beweb/beweb/prest.py [new file with mode: 0644]

diff --git a/beweb/beweb/prest.py b/beweb/beweb/prest.py
new file mode 100644 (file)
index 0000000..3a94ab5
--- /dev/null
@@ -0,0 +1,58 @@
+from unittest import TestCase
+import unittest
+"""A pseudo-REST dispatching method in which only the noun comes from the path.
+The action performed will depend on kwargs.
+"""
+class PrestHandler(object):
+    def __init__(self):
+        object.__init__(self)
+
+    def decode(self, path, data=None, has_id=False):
+        """Convert the """
+        if data is None:
+            data = {}
+        if len(path) < 2 or not hasattr(self, path[1]):
+            if len(path) == 1:
+                resource = self.instantiate(*data)
+            else:
+                resource = None
+            return self, resource, data, path[1:] 
+        if len(path) > 2:
+            data[path[1]] = path[2]
+        return getattr(self, path[1]).decode(path[2:], data)
+
+    def default(self, *args, **kwargs):
+        child, resource, data, extra = self.decode([None,] + list(args))
+        child.dispatch(*([data, resource]+extra), **kwargs)
+
+
+class PrestTester(TestCase):
+    def test_decode(self):
+        class ProjectHandler(PrestHandler):
+            def dispatch(self, project_data, project, *args, **kwargs):
+                self.project_id = project_data['project']
+                self.resource = project
+                self.args = args
+                self.kwargs = kwargs
+
+            def instantiate(self, project):
+                return [project]
+
+        foo = PrestHandler()
+        foo.project = ProjectHandler()
+        handler, resource, data, extra = foo.decode([None, 'project', '83', 
+                                                     'bloop', 'yeah'])
+        assert handler is foo.project
+        self.assertEqual({'project': '83'}, data)
+        self.assertEqual(['bloop', 'yeah'], extra)
+        foo.default(*['project', '27', 'extra'], **{'a':'b', 'b':'97'})
+        self.assertEqual(foo.project.args, ('extra',))
+                
+def test():
+    patchesTestSuite = unittest.makeSuite(PrestTester,'test')
+    runner = unittest.TextTestRunner(verbosity=0)
+    return runner.run(patchesTestSuite)
+    
+
+if __name__ == "__main__":
+    test()