[Repoze-checkins] r1230 - in repoze.bfg/trunk: . repoze/bfg repoze/bfg/tests

Chris McDonough chrism at agendaless.com
Fri Jul 4 13:51:00 EDT 2008


Author: Chris McDonough <chrism at agendaless.com>
Date: Fri Jul  4 13:50:59 2008
New Revision: 1230

Log:
Don't depend on ZODB; shuffle policy responsibilities around a little.


Added:
   repoze.bfg/trunk/repoze/bfg/policy.py   (contents, props changed)
   repoze.bfg/trunk/repoze/bfg/tests/test_policy.py   (contents, props changed)
Removed:
   repoze.bfg/trunk/repoze/bfg/tests/test_zodb.py
   repoze.bfg/trunk/repoze/bfg/zodb.py
Modified:
   repoze.bfg/trunk/repoze/bfg/interfaces.py
   repoze.bfg/trunk/repoze/bfg/router.py
   repoze.bfg/trunk/setup.py

Modified: repoze.bfg/trunk/repoze/bfg/interfaces.py
==============================================================================
--- repoze.bfg/trunk/repoze/bfg/interfaces.py	(original)
+++ repoze.bfg/trunk/repoze/bfg/interfaces.py	Fri Jul  4 13:50:59 2008
@@ -5,6 +5,6 @@
         """ Represent a WSGI (PEP 333) application """
 
 class IPolicy(Interface):
-    def __call__(environ):
+    def __call__(root, environ):
         """ Return a tuple in the form (context, name, subpath) """
         

Added: repoze.bfg/trunk/repoze/bfg/policy.py
==============================================================================
--- (empty file)
+++ repoze.bfg/trunk/repoze/bfg/policy.py	Fri Jul  4 13:50:59 2008
@@ -0,0 +1,45 @@
+import urllib
+
+from zope.interface import implements
+
+from repoze.bfg.interfaces import IPolicy
+
+def split_path(path):
+    if path.startswith('/'):
+        path = path[1:]
+    if path.endswith('/'):
+        path = path[:-1]
+    clean=[]
+    for item in path.split('/'):
+        item = urllib.unquote(item) # deal with spaces in path segment
+        if not item or item=='.':
+            continue
+        elif item == '..':
+            del clean[-1]
+        else:
+            clean.append(item)
+    return clean
+
+class NaivePolicy:
+
+    implements(IPolicy)
+
+    def __call__(self, environ, root):
+        path = split_path(environ['PATH_INFO'])
+        
+        ob = root
+        name = ''
+        while path:
+            element = pop(path)
+            try:
+                ob = ob[element]
+            except KeyError:
+                if path:
+                    name = pop(path)
+                break
+            
+        return ob, name, path
+            
+def pop(path):
+    return path.pop(0)
+

Modified: repoze.bfg/trunk/repoze/bfg/router.py
==============================================================================
--- repoze.bfg/trunk/repoze/bfg/router.py	(original)
+++ repoze.bfg/trunk/repoze/bfg/router.py	Fri Jul  4 13:50:59 2008
@@ -2,12 +2,14 @@
 from repoze.bfg.interfaces import IWSGIApplication
 
 class Router:
-    def __init__(self, app, policy):
+    def __init__(self, app, root_finder, policy):
         self.app = app
+        self.root_finder = root_finder
         self.policy = policy
 
     def __call__(self, environ, start_response):
-        context, name, subpath = self.policy(environ)
+        root = self.root_finder(environ)
+        context, name, subpath = self.policy(root, environ)
         app = getAdapter(context, IWSGIApplication, name)
         environ['repoze.bfg.context'] = context
         environ['repoze.bfg.subpath'] = subpath

Added: repoze.bfg/trunk/repoze/bfg/tests/test_policy.py
==============================================================================
--- (empty file)
+++ repoze.bfg/trunk/repoze/bfg/tests/test_policy.py	Fri Jul  4 13:50:59 2008
@@ -0,0 +1,94 @@
+import unittest
+
+class SplitPathTests(unittest.TestCase):
+    def _getFUT(self):
+        from repoze.bfg.policy import split_path
+        return split_path
+        
+    def test_cleanPath_path_startswith_endswith(self):
+        f = self._getFUT()
+        self.assertEqual(f('/foo/'), ['foo'])
+
+    def test_cleanPath_empty_elements(self):
+        f = self._getFUT()
+        self.assertEqual(f('foo///'), ['foo'])
+
+    def test_cleanPath_onedot(self):
+        f = self._getFUT()
+        self.assertEqual(f('foo/./bar'), ['foo', 'bar'])
+
+    def test_cleanPath_twodots(self):
+        f = self._getFUT()
+        self.assertEqual(f('foo/../bar'), ['bar'])
+
+    def test_cleanPath_element_urllquoted(self):
+        f = self._getFUT()
+        self.assertEqual(f('/foo/space%20thing/bar'), ['foo', 'space thing',
+                                                       'bar'])
+   
+class NaivePolicyTests(unittest.TestCase):
+    def _getTargetClass(self):
+        from repoze.bfg.policy import NaivePolicy
+        return NaivePolicy
+
+    def _makeOne(self, *arg, **kw):
+        klass = self._getTargetClass()
+        return klass(*arg, **kw)
+
+    def test_class_conforms_to_IPolicy(self):
+        from zope.interface.verify import verifyClass
+        from repoze.bfg.interfaces import IPolicy
+        verifyClass(IPolicy, self._getTargetClass())
+
+    def test_instance_conforms_to_IPolicy(self):
+        from zope.interface.verify import verifyObject
+        from repoze.bfg.interfaces import IPolicy
+        verifyObject(IPolicy, self._makeOne())
+
+    def test_call_nonkeyerror_raises(self):
+        policy = self._makeOne()
+        environ = {'PATH_INFO':'/foo'}
+        root = None
+        self.assertRaises(TypeError, policy, environ, root)
+
+    def test_call_withconn_getitem_emptypath_nosubpath(self):
+        policy = self._makeOne()
+        context = DummyContext()
+        environ = {'PATH_INFO':''}
+        root = context
+        ctx, name, subpath = policy(environ, root)
+        self.assertEqual(context, ctx)
+        self.assertEqual(name, '')
+        self.assertEqual(subpath, [])
+
+    def test_call_withconn_getitem_withpath_nosubpath(self):
+        policy = self._makeOne()
+        context = DummyContext()
+        context2 = DummyContext(context)
+        environ = {'PATH_INFO':'/foo/bar'}
+        root = context
+        ctx, name, subpath = policy(environ, root)
+        self.assertEqual(context, ctx)
+        self.assertEqual(name, 'bar')
+        self.assertEqual(subpath, [])
+
+    def test_call_withconn_getitem_withpath_withsubpath(self):
+        policy = self._makeOne()
+        context = DummyContext()
+        context2 = DummyContext(context)
+        environ = {'PATH_INFO':'/foo/bar/baz/buz'}
+        root = context
+        ctx, name, subpath = policy(environ, root)
+        self.assertEqual(context, ctx)
+        self.assertEqual(name, 'bar')
+        self.assertEqual(subpath, ['baz', 'buz'])
+
+class DummyContext:
+    def __init__(self, next=None):
+        self.next = next
+        
+    def __getitem__(self, name):
+        if self.next is None:
+            raise KeyError, name
+        return self.next
+    

Modified: repoze.bfg/trunk/setup.py
==============================================================================
--- repoze.bfg/trunk/setup.py	(original)
+++ repoze.bfg/trunk/setup.py	Fri Jul  4 13:50:59 2008
@@ -48,8 +48,8 @@
       include_package_data=True,
       namespace_packages=['repoze', 'repoze.bfg'],
       zip_safe=False,
-      tests_require = ['zope.interface', 'repoze.zodbconn'],
-      install_requires=['zope.interface', 'repoze.zodbconn'],
+      tests_require = ['zope.interface'],
+      install_requires=['zope.interface'],
       test_suite="repoze.bfg.tests",
       entry_points = """\
       """


More information about the Repoze-checkins mailing list