165 'virtual_root': vroot, |
165 'virtual_root': vroot, |
166 'virtual_root_path': vroot_tuple, |
166 'virtual_root_path': vroot_tuple, |
167 'root': root} |
167 'root': root} |
168 |
168 |
169 |
169 |
170 def get_parent(context, interface=Interface, allow_context=True): |
170 def get_parent(context, interface=Interface, allow_context=True, condition=None): |
171 """Get first parent of the context that implements given interface""" |
171 """Get first parent of the context that implements given interface |
|
172 |
|
173 @context: base element |
|
174 @interface: the interface that parend should implement |
|
175 @allow_context: if 'True' (the default), traversing is done starting with context; otherwise, |
|
176 traversing is done starting from context's parent |
|
177 @condition: an optional function that should return a 'True' result when called with parent |
|
178 as first argument |
|
179 """ |
172 if allow_context: |
180 if allow_context: |
173 parent = context |
181 parent = context |
174 else: |
182 else: |
175 parent = getattr(context, '__parent__', None) |
183 parent = getattr(context, '__parent__', None) |
176 while parent is not None: |
184 while parent is not None: |
177 if interface.providedBy(parent): |
185 if interface.providedBy(parent): |
178 return interface(parent) |
186 target = interface(parent) |
|
187 if (not condition) or condition(target): |
|
188 return target |
179 parent = getattr(parent, '__parent__', None) |
189 parent = getattr(parent, '__parent__', None) |
180 return None |
190 return None |
181 |
191 |
182 |
192 |
183 @adapter_config(context=IContained, provides=IPathElements) |
193 @adapter_config(context=IContained, provides=IPathElements) |