# HG changeset patch # User Thierry Florac # Date 1476195685 -7200 # Node ID 8b172808ca6cf5a35c50083289b0b2d49adf349b # Parent 1e9c6d17203eb8b886f8157da7ba16340f8e395c Added optional "condition" argument to "get_parent" traversing helper function diff -r 1e9c6d17203e -r 8b172808ca6c src/pyams_utils/traversing.py --- a/src/pyams_utils/traversing.py Thu Jun 02 16:41:14 2016 +0200 +++ b/src/pyams_utils/traversing.py Tue Oct 11 16:21:25 2016 +0200 @@ -167,15 +167,25 @@ 'root': root} -def get_parent(context, interface=Interface, allow_context=True): - """Get first parent of the context that implements given interface""" +def get_parent(context, interface=Interface, allow_context=True, condition=None): + """Get first parent of the context that implements given interface + + @context: base element + @interface: the interface that parend should implement + @allow_context: if 'True' (the default), traversing is done starting with context; otherwise, + traversing is done starting from context's parent + @condition: an optional function that should return a 'True' result when called with parent + as first argument + """ if allow_context: parent = context else: parent = getattr(context, '__parent__', None) while parent is not None: if interface.providedBy(parent): - return interface(parent) + target = interface(parent) + if (not condition) or condition(target): + return target parent = getattr(parent, '__parent__', None) return None