ztfy/utils/traversing.py
changeset 0 712d20d2751e
child 2 20f3c0eb8fdf
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ztfy/utils/traversing.py	Sat Jan 10 00:35:35 2009 +0100
@@ -0,0 +1,47 @@
+### -*- coding: utf-8 -*- ####################################################
+##############################################################################
+#
+# Copyright (c) 2008 Thierry Florac <tflorac AT ulthar.net>
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+"""
+
+$Id: $
+"""
+
+__version__   = "$Revision: $"
+__release__   = "$Id: $"
+__docformat__ = "restructuredtext"
+ 
+# import standard packages
+
+# import Zope3 interfaces
+
+# import local interfaces
+
+# import Zope3 packages
+from zope.interface import Interface
+from zope.app import zapi
+
+# import local packages
+
+
+def getParent(context, interface=Interface, allow_context=True):
+    """Get first parent of the given context that implements given interface"""
+    if allow_context:
+        parent = context
+    else:
+        parent = zapi.getParent(context)
+    while parent is not None:
+        if interface.providedBy(parent):
+            return interface(parent)
+        parent = zapi.getParent(parent)
+    return None