Given a path with elements separated by posixpath.sep, generate all elements of that path. >>> list(_ancestry('b/d')) ['b/d', 'b'] >>> list(_ancestry('/b/d/')) ['/b/d', '/b'] >>> list(_ancestry('b/d/f/')) ['b/d/f', 'b/d', 'b'] >>> list(_ancestry('b')) ['b']
(path)
| 43 | |
| 44 | |
| 45 | def _ancestry(path): |
| 46 | """ |
| 47 | Given a path with elements separated by |
| 48 | posixpath.sep, generate all elements of that path. |
| 49 | |
| 50 | >>> list(_ancestry('b/d')) |
| 51 | ['b/d', 'b'] |
| 52 | >>> list(_ancestry('/b/d/')) |
| 53 | ['/b/d', '/b'] |
| 54 | >>> list(_ancestry('b/d/f/')) |
| 55 | ['b/d/f', 'b/d', 'b'] |
| 56 | >>> list(_ancestry('b')) |
| 57 | ['b'] |
| 58 | >>> list(_ancestry('')) |
| 59 | [] |
| 60 | |
| 61 | Multiple separators are treated like a single. |
| 62 | |
| 63 | >>> list(_ancestry('//b//d///f//')) |
| 64 | ['//b//d///f', '//b//d', '//b'] |
| 65 | """ |
| 66 | path = path.rstrip(posixpath.sep) |
| 67 | while path.rstrip(posixpath.sep): |
| 68 | yield path |
| 69 | path, tail = posixpath.split(path) |
| 70 | |
| 71 | |
| 72 | _dedupe = dict.fromkeys |