keep_root_paths

pathwalker.keep_root_paths(paths: List[Union[str, pathlib.Path]]) List[pathlib.Path]

Keeps root paths within a list of paths. Sub paths are dropped.

Notes

The purpose of this method is to get the minimum list of paths for a path recursion afterwards. This should avoid listing the items of sub paths or double entries within the list.

Parameters

paths – Any paths; which will be resolved within this process.

Returns

Resolved paths.

Return type

List[Path]

Examples

>>> from doctestprinter import doctest_iter_print
>>> from pathlib import Path

The root should remain for later recursion.

>>> sample_paths = (
...         "./tests/resources/foo",
...         "./tests/resources/bar",
...         "./tests/resources/",
...         "./tests/resources/foo/bar",
...         "./tests/resources/another_bar",
...     )
>>> cleared_sample_paths = keep_root_paths(paths=sample_paths)
>>> current_work_path = Path(".").resolve()
>>> doctest_iter_print(
...     cleared_sample_paths,
...     edits_item=lambda x: x.relative_to(current_work_path)
... )
tests/resources
>>> sample_paths = (
...         "./tests/resources/foo",
...         "./tests/resources/bar",
...         "./tests/resources/foo/bar",
...         "./tests/resources/another_bar",
...     )
>>> cleared_sample_paths = keep_root_paths(
...     paths=sample_paths
... )
>>> current_work_path = Path(".").resolve()
>>> doctest_iter_print(
...     cleared_sample_paths,
...     edits_item=lambda x: x.relative_to(current_work_path)
... )
tests/resources/another_bar
tests/resources/bar
tests/resources/foo

Double entries are removed from the list leaving single tree roots only.

>>> samples = (
...         "./tests/resources/foo",
...         "./tests/resources/foo",
...         "./tests/resources/bar",
...         "./tests/resources/foo/bar",
...         "./tests/resources/foo/bar",
...         "./tests/resources/another_bar",
...         "./tests/resources/another_bar",
...     )
>>> cleared_sample_paths = keep_root_paths(
...     paths=samples
... )
>>> current_work_path = Path(".").resolve()
>>> doctest_iter_print(
...     cleared_sample_paths,
...     edits_item=lambda x: x.relative_to(current_work_path)
... )
tests/resources/another_bar
tests/resources/bar
tests/resources/foo
Warnings:

This method does resolve the paths. Non existing paths will not be dropped. Also this function will not raise a FileNotExist-Error for non existing paths.

>>> samples = (
...         "./tests/resources/foo",
...         "./not/existing",
...         "./not/existing/either",
...     )
>>> cleared_sample_paths = keep_root_paths(
...     paths=samples
... )
>>> current_work_path = Path(".").resolve()
>>> doctest_iter_print(
...     cleared_sample_paths,
...     edits_item=lambda x: x.relative_to(current_work_path)
... )
not/existing
tests/resources/foo