summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChris McDonough <chrism@agendaless.com>2008-07-20 03:04:58 +0000
committerChris McDonough <chrism@agendaless.com>2008-07-20 03:04:58 +0000
commitbb5d643b83cde30d2fa37aaae21a8d4b225f584b (patch)
treebbfc266fb1161dc6189a1d25f93903ca378373e1
parent6c840ed454d91741273984bcbf9a32404a78a2c1 (diff)
downloadpyramid-bb5d643b83cde30d2fa37aaae21a8d4b225f584b.tar.gz
pyramid-bb5d643b83cde30d2fa37aaae21a8d4b225f584b.tar.bz2
pyramid-bb5d643b83cde30d2fa37aaae21a8d4b225f584b.zip
- Add find_interface API.
-rw-r--r--CHANGES.txt4
-rw-r--r--docs/api/traversal.rst10
-rw-r--r--docs/index.rst1
-rw-r--r--repoze/bfg/tests/test_traversal.py28
-rw-r--r--repoze/bfg/traversal.py8
5 files changed, 51 insertions, 0 deletions
diff --git a/CHANGES.txt b/CHANGES.txt
index d94b25e99..f1945e814 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -1,3 +1,7 @@
+After 0.2
+
+ - Add find_interface API.
+
0.2
- Add wsgiapp decorator.
diff --git a/docs/api/traversal.rst b/docs/api/traversal.rst
new file mode 100644
index 000000000..56dad2290
--- /dev/null
+++ b/docs/api/traversal.rst
@@ -0,0 +1,10 @@
+.. _traversal_module:
+
+:mod:`repoze.bfg.traversal`
+---------------------------
+
+.. currentmodule:: repoze.bfg.traversal
+
+.. autofunction:: find_interface
+
+
diff --git a/docs/index.rst b/docs/index.rst
index ceb2e5fd9..28b2d91d4 100644
--- a/docs/index.rst
+++ b/docs/index.rst
@@ -35,6 +35,7 @@ and run a web application.
api/router
api/security
api/template
+ api/traversal
api/wsgi
Indices and tables
diff --git a/repoze/bfg/tests/test_traversal.py b/repoze/bfg/tests/test_traversal.py
index 61dff6d88..2cd02da5e 100644
--- a/repoze/bfg/tests/test_traversal.py
+++ b/repoze/bfg/tests/test_traversal.py
@@ -130,6 +130,34 @@ class NaivePublishTraverserTests(unittest.TestCase, PlacelessSetup):
self.assertEqual(ctx.__parent__.__parent__.__parent__.__name__, None)
self.assertEqual(ctx.__parent__.__parent__.__parent__.__parent__, None)
+class FindInterfaceTests(unittest.TestCase):
+ def _getFUT(self):
+ from repoze.bfg.traversal import find_interface
+ return find_interface
+
+ def test_it(self):
+ baz = DummyContext()
+ bar = DummyContext(baz)
+ foo = DummyContext(bar)
+ root = DummyContext(foo)
+ root.__parent__ = None
+ root.__name__ = 'root'
+ foo.__parent__ = root
+ foo.__name__ = 'foo'
+ bar.__parent__ = foo
+ bar.__name__ = 'bar'
+ baz.__parent__ = bar
+ baz.__name__ = 'baz'
+ request = DummyRequest()
+ from zope.interface import directlyProvides
+ from zope.interface import Interface
+ class IFoo(Interface):
+ pass
+ directlyProvides(root, IFoo)
+ finder = self._getFUT()
+ result = finder(baz, IFoo)
+ self.assertEqual(result.__name__, 'root')
+
class DummyContext(object):
def __init__(self, next=None):
self.next = next
diff --git a/repoze/bfg/traversal.py b/repoze/bfg/traversal.py
index a9f127841..ee66ae887 100644
--- a/repoze/bfg/traversal.py
+++ b/repoze/bfg/traversal.py
@@ -3,6 +3,7 @@ import urllib
from zope.interface import classProvides
from zope.interface import implements
from zope.location.location import located
+from zope.location.location import LocationIterator
from zope.location.interfaces import ILocation
from repoze.bfg.interfaces import ITraverser
@@ -63,3 +64,10 @@ class NaiveTraverser(object):
return ob, name, path
+def find_interface(context, interface):
+ """ Return an object providing 'interface' anywhere in the parent
+ chain of 'context' or None if no object providing that interface
+ can be found in the parent chain """
+ for location in LocationIterator(context):
+ if interface.providedBy(location):
+ return location