diff options
| author | Chris McDonough <chrism@agendaless.com> | 2008-07-20 03:04:58 +0000 |
|---|---|---|
| committer | Chris McDonough <chrism@agendaless.com> | 2008-07-20 03:04:58 +0000 |
| commit | bb5d643b83cde30d2fa37aaae21a8d4b225f584b (patch) | |
| tree | bbfc266fb1161dc6189a1d25f93903ca378373e1 /repoze | |
| parent | 6c840ed454d91741273984bcbf9a32404a78a2c1 (diff) | |
| download | pyramid-bb5d643b83cde30d2fa37aaae21a8d4b225f584b.tar.gz pyramid-bb5d643b83cde30d2fa37aaae21a8d4b225f584b.tar.bz2 pyramid-bb5d643b83cde30d2fa37aaae21a8d4b225f584b.zip | |
- Add find_interface API.
Diffstat (limited to 'repoze')
| -rw-r--r-- | repoze/bfg/tests/test_traversal.py | 28 | ||||
| -rw-r--r-- | repoze/bfg/traversal.py | 8 |
2 files changed, 36 insertions, 0 deletions
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 |
