def ancestor_finder(resource, predicate, include_self=False):
    """Generate ancestors that satisfy ``predicate``.

    Generator that climbs the tree yielding resources for which
    ``predicate(current_resource)`` returns True.
    """
    resource = resource if include_self else getattr(resource, '__parent__',
        None)
    while resource is not None:
        if predicate(resource):
            yield resource
        resource = getattr(resource, '__parent__', None)
