MCPcopy Create free account
hub / github.com/aboutcode-org/scancode-toolkit / get_ancestor_paths

Function get_ancestor_paths

src/commoncode/resource.py:2189–2209  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

2187
2188
2189def 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)

Callers 2

_populateMethod · 0.85

Calls 3

splitMethod · 0.45
appendMethod · 0.45
joinMethod · 0.45

Tested by

no test coverage detected