Yield all subpaths from a POSIX path. For example:: >>> path = 'foo/bar/baz' >>> results = list(get_ancestor_paths(path)) >>> assert results == ['foo', 'foo/bar'], results >>> results = list(get_ancestor_paths(path, include_self=True)) >>> assert results == ['foo', 'foo
(path, include_self=False)
| 2187 | |
| 2188 | |
| 2189 | def get_ancestor_paths(path, include_self=False): |
| 2190 | """ |
| 2191 | Yield all subpaths from a POSIX path. |
| 2192 | |
| 2193 | For example:: |
| 2194 | >>> path = 'foo/bar/baz' |
| 2195 | >>> results = list(get_ancestor_paths(path)) |
| 2196 | >>> assert results == ['foo', 'foo/bar'], results |
| 2197 | >>> results = list(get_ancestor_paths(path, include_self=True)) |
| 2198 | >>> assert results == ['foo', 'foo/bar', 'foo/bar/baz'], results |
| 2199 | >>> results = list(get_ancestor_paths('foo', include_self=False)) |
| 2200 | >>> assert results == [], results |
| 2201 | """ |
| 2202 | assert path |
| 2203 | segments = path.split("/") |
| 2204 | if not include_self: |
| 2205 | segments = segments[:-1] |
| 2206 | subpath = [] |
| 2207 | for segment in segments: |
| 2208 | subpath.append(segment) |
| 2209 | yield "/".join(subpath) |
no test coverage detected