Skip to content

Commit

Permalink
ENH: isort imports, fix typing, add type checking for path.py, improv…
Browse files Browse the repository at this point in the history
…e testing
  • Loading branch information
yarikoptic committed Dec 9, 2022
1 parent a4925fa commit bfe6637
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 8 deletions.
15 changes: 9 additions & 6 deletions datalad/support/path.py
Expand Up @@ -15,13 +15,15 @@
# TODO: RF and move all paths related functions from datalad.utils in here
import os
import os.path as op

# to not pollute API importing as _
from collections import defaultdict as _defaultdict

from functools import wraps
from itertools import dropwhile
from pathlib import Path, PurePosixPath
from pathlib import (
Path,
PurePosixPath,
)
from typing import Generator

from ..utils import (
ensure_bytes,
Expand Down Expand Up @@ -209,7 +211,8 @@ def get_parent_paths(paths, parents, only_with_parents=False, *, sep='/'):
return res


def get_limited_paths(paths: list[str|Path], limits: list[str|Path], *, include_within_path: bool = False) -> list[Path]:
def get_limited_paths(paths: list[str|Path], limits: list[str|Path], *, include_within_path: bool = False) \
-> Generator[str, None, None]:
"""Given list of relative POSIX paths (or Path objects), select the ones within limits (also relative and POSIX).
In case of include_with_path=True, if limit points to some path under a 'path' within 'paths',
Expand Down Expand Up @@ -247,8 +250,8 @@ def _harmonize_paths(l: list) -> list:
limits_parts = limits_parts[1:]
else:
break # otherwise -- consider this one!
if not limits_parts:
# none left
else:
# no limiting path left - the other paths cannot be the selected ones
break
if include_within_path:
# if one identical or subpath of another one -- their parts match in the beginning
Expand Down
24 changes: 23 additions & 1 deletion datalad/support/tests/test_path.py
Expand Up @@ -8,6 +8,7 @@
# ## ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ##

import os
from pathlib import PurePosixPath

from ...tests.utils_pytest import (
SkipTest,
Expand Down Expand Up @@ -123,5 +124,26 @@ def glp(*args, **kwargs):
return list(get_limited_paths(*args, **kwargs))

assert glp(['a', 'b'], ['a', 'c']) == ['a']
assert glp(['a', 'b'], ['b']) == ['b']
assert glp(['a', 'b'], ['c']) == []

assert glp(['a', 'b'], ['a/b', 'c']) == [] # a is not subpath of a/b
assert glp(['a', 'b'], ['a/b', 'c'], include_within_path=True) == ['a'] # a is not subpath of a/b
assert glp(['a', 'b'], ['a/b', 'c'], include_within_path=True) == ['a'] # a is not subpath of a/b

# all paths returned due to '.', and order is sorted
paths = ['a', 'b', '1/2/3', 'abc']
paths_sorted = sorted(paths)
assert glp(paths, ['.']) == paths_sorted
assert glp(paths, paths_sorted) == paths_sorted
assert glp(paths, paths_sorted, include_within_path=True) == paths_sorted
# we can take a mix of str and Path
assert glp([PurePosixPath(paths[0])] + paths[1:], ['.']) == paths_sorted


# nothing within empty "limits" matches -- so no paths yielded
assert glp(paths, []) == []

assert_raises(ValueError, glp, ['/a'], [])
assert_raises(ValueError, glp, [PurePosixPath('/a')], [])
assert_raises(ValueError, glp, ['a'], ['/a'])
assert_raises(ValueError, glp, ['../a'], ['a'])
3 changes: 2 additions & 1 deletion tox.ini
Expand Up @@ -39,7 +39,8 @@ commands =
datalad/cmd.py \
datalad/downloaders/providers.py \
datalad/runner \
datalad/support/collections.py
datalad/support/collections.py \
datalad/support/path.py

[testenv:venv]
commands = {posargs}
Expand Down

0 comments on commit bfe6637

Please sign in to comment.