kaparoo-python 0.1.11__py3-none-any.whl → 0.2.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- kaparoo/data/sequence.py +39 -0
- kaparoo/data/utils.py +8 -11
- kaparoo/filesystem/__init__.py +37 -20
- kaparoo/filesystem/directory.py +121 -314
- kaparoo/filesystem/exceptions.py +4 -2
- kaparoo/filesystem/existence.py +64 -64
- kaparoo/filesystem/search/__init__.py +83 -0
- kaparoo/filesystem/search/classes.py +199 -0
- kaparoo/filesystem/search/deprecated.py +289 -0
- kaparoo/filesystem/search/filters.py +322 -0
- kaparoo/filesystem/search/wrappers.py +311 -0
- kaparoo/filesystem/types.py +4 -3
- kaparoo/filesystem/utils.py +136 -67
- kaparoo/utils/__init__.py +21 -0
- kaparoo/utils/optional.py +11 -33
- kaparoo/utils/timer.py +374 -0
- kaparoo_python-0.2.0.dist-info/METADATA +86 -0
- kaparoo_python-0.2.0.dist-info/RECORD +23 -0
- {kaparoo_python-0.1.11.dist-info → kaparoo_python-0.2.0.dist-info}/WHEEL +1 -1
- {kaparoo_python-0.1.11.dist-info → kaparoo_python-0.2.0.dist-info}/licenses/LICENSE +20 -20
- kaparoo/__about__.py +0 -1
- kaparoo/data/files/__init__.py +0 -0
- kaparoo/data/files/base.py +0 -69
- kaparoo/utils/types.py +0 -9
- kaparoo_python-0.1.11.dist-info/METADATA +0 -69
- kaparoo_python-0.1.11.dist-info/RECORD +0 -20
kaparoo/data/sequence.py
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
__all__ = ("DataSequence",)
|
|
4
|
+
|
|
5
|
+
from abc import abstractmethod
|
|
6
|
+
from collections.abc import Sequence
|
|
7
|
+
from typing import TYPE_CHECKING, overload
|
|
8
|
+
|
|
9
|
+
if TYPE_CHECKING:
|
|
10
|
+
from kaparoo.filesystem.types import StrPath
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class DataSequence[T](Sequence[T]):
|
|
14
|
+
@abstractmethod
|
|
15
|
+
def __init__(self, path: StrPath) -> None:
|
|
16
|
+
raise NotImplementedError
|
|
17
|
+
|
|
18
|
+
@abstractmethod
|
|
19
|
+
def __len__(self) -> int:
|
|
20
|
+
raise NotImplementedError
|
|
21
|
+
|
|
22
|
+
@overload
|
|
23
|
+
def __getitem__(self, index: int, /) -> T: ...
|
|
24
|
+
|
|
25
|
+
@overload
|
|
26
|
+
def __getitem__(self, index: slice, /) -> Sequence[T]: ...
|
|
27
|
+
|
|
28
|
+
def __getitem__(self, index: int | slice, /) -> T | Sequence[T]:
|
|
29
|
+
if isinstance(index, slice):
|
|
30
|
+
start, stop, step = index.indices(len(self))
|
|
31
|
+
return self.by_indices(range(start, stop, step))
|
|
32
|
+
return self.by_index(index)
|
|
33
|
+
|
|
34
|
+
@abstractmethod
|
|
35
|
+
def by_index(self, index: int) -> T:
|
|
36
|
+
raise NotImplementedError
|
|
37
|
+
|
|
38
|
+
def by_indices(self, indices: Sequence[int]) -> Sequence[T]:
|
|
39
|
+
return [self.by_index(index) for index in indices]
|
kaparoo/data/utils.py
CHANGED
|
@@ -7,14 +7,11 @@ from typing import TYPE_CHECKING
|
|
|
7
7
|
from kaparoo.utils.optional import replace_if_none
|
|
8
8
|
|
|
9
9
|
if TYPE_CHECKING:
|
|
10
|
-
from collections.abc import
|
|
11
|
-
from typing import Any
|
|
10
|
+
from collections.abc import Iterator, Sequence
|
|
12
11
|
|
|
13
|
-
from kaparoo.utils.types import T_co
|
|
14
12
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
sequence: Sequence[T_co],
|
|
13
|
+
def generate_batches[T](
|
|
14
|
+
sequence: Sequence[T],
|
|
18
15
|
size: int,
|
|
19
16
|
step: int = 1,
|
|
20
17
|
skip: int = 1,
|
|
@@ -22,10 +19,11 @@ def generate_batches(
|
|
|
22
19
|
stop: int | None = None,
|
|
23
20
|
*,
|
|
24
21
|
drop_last: bool = True,
|
|
25
|
-
) ->
|
|
22
|
+
) -> Iterator[Sequence[T]]:
|
|
26
23
|
def die_if_not_positive(name: str, value: int) -> None:
|
|
27
24
|
if value <= 0:
|
|
28
|
-
|
|
25
|
+
msg = f"{name} must be positive (got {value})"
|
|
26
|
+
raise ValueError(msg)
|
|
29
27
|
|
|
30
28
|
die_if_not_positive("size", size)
|
|
31
29
|
die_if_not_positive("step", step)
|
|
@@ -33,9 +31,8 @@ def generate_batches(
|
|
|
33
31
|
|
|
34
32
|
stop = replace_if_none(stop, len_ := len(sequence))
|
|
35
33
|
if not (start < stop <= len_ and start >= 0):
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
)
|
|
34
|
+
msg = f"invalid range [{start}, {stop}) for sequence of length {len_}"
|
|
35
|
+
raise ValueError(msg)
|
|
39
36
|
|
|
40
37
|
head = start
|
|
41
38
|
tail = head + (size - 1) * skip + 1
|
kaparoo/filesystem/__init__.py
CHANGED
|
@@ -1,39 +1,48 @@
|
|
|
1
1
|
__all__ = (
|
|
2
|
-
|
|
3
|
-
"
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
"
|
|
7
|
-
|
|
2
|
+
"DirectoryNotFoundError",
|
|
3
|
+
"NotAFileError",
|
|
4
|
+
"dir_empty",
|
|
5
|
+
"dir_empty_unsafe",
|
|
6
|
+
"dir_exists",
|
|
7
|
+
"dirs_empty",
|
|
8
|
+
"dirs_empty_unsafe",
|
|
9
|
+
"dirs_exist",
|
|
8
10
|
"ensure_dir_exists",
|
|
9
11
|
"ensure_dirs_exist",
|
|
10
|
-
"ensure_path_exists",
|
|
11
|
-
"ensure_paths_exist",
|
|
12
12
|
"ensure_file_exists",
|
|
13
13
|
"ensure_files_exist",
|
|
14
|
-
"
|
|
15
|
-
"
|
|
14
|
+
"ensure_path_exists",
|
|
15
|
+
"ensure_paths_exist",
|
|
16
16
|
"file_exists",
|
|
17
17
|
"files_exist",
|
|
18
|
-
"path_exists",
|
|
19
|
-
"paths_exist",
|
|
20
|
-
# directory
|
|
21
|
-
"dir_empty",
|
|
22
|
-
"dirs_empty",
|
|
23
18
|
"get_dirs",
|
|
24
19
|
"get_files",
|
|
25
20
|
"get_paths",
|
|
21
|
+
"make_dir",
|
|
26
22
|
"make_dirs",
|
|
23
|
+
"path_exists",
|
|
24
|
+
"paths_exist",
|
|
25
|
+
"search_dirs",
|
|
26
|
+
"search_files",
|
|
27
|
+
"search_paths",
|
|
28
|
+
"stringify_path",
|
|
29
|
+
"stringify_paths",
|
|
30
|
+
"wrap_path",
|
|
31
|
+
"wrap_paths",
|
|
27
32
|
)
|
|
28
33
|
|
|
29
34
|
from kaparoo.filesystem.directory import (
|
|
30
35
|
dir_empty,
|
|
36
|
+
dir_empty_unsafe,
|
|
31
37
|
dirs_empty,
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
get_paths,
|
|
38
|
+
dirs_empty_unsafe,
|
|
39
|
+
make_dir,
|
|
35
40
|
make_dirs,
|
|
36
41
|
)
|
|
42
|
+
from kaparoo.filesystem.exceptions import (
|
|
43
|
+
DirectoryNotFoundError,
|
|
44
|
+
NotAFileError,
|
|
45
|
+
)
|
|
37
46
|
from kaparoo.filesystem.existence import (
|
|
38
47
|
dir_exists,
|
|
39
48
|
dirs_exist,
|
|
@@ -48,9 +57,17 @@ from kaparoo.filesystem.existence import (
|
|
|
48
57
|
path_exists,
|
|
49
58
|
paths_exist,
|
|
50
59
|
)
|
|
60
|
+
from kaparoo.filesystem.search import (
|
|
61
|
+
get_dirs,
|
|
62
|
+
get_files,
|
|
63
|
+
get_paths,
|
|
64
|
+
search_dirs,
|
|
65
|
+
search_files,
|
|
66
|
+
search_paths,
|
|
67
|
+
)
|
|
51
68
|
from kaparoo.filesystem.utils import (
|
|
52
|
-
prepend_path,
|
|
53
|
-
prepend_paths,
|
|
54
69
|
stringify_path,
|
|
55
70
|
stringify_paths,
|
|
71
|
+
wrap_path,
|
|
72
|
+
wrap_paths,
|
|
56
73
|
)
|
kaparoo/filesystem/directory.py
CHANGED
|
@@ -1,33 +1,28 @@
|
|
|
1
1
|
from __future__ import annotations
|
|
2
2
|
|
|
3
3
|
__all__ = (
|
|
4
|
-
# make
|
|
5
|
-
"make_dirs",
|
|
6
|
-
# empty
|
|
7
4
|
"dir_empty",
|
|
5
|
+
"dir_empty_unsafe",
|
|
8
6
|
"dirs_empty",
|
|
9
|
-
|
|
10
|
-
"
|
|
11
|
-
"
|
|
12
|
-
"get_dirs",
|
|
7
|
+
"dirs_empty_unsafe",
|
|
8
|
+
"make_dir",
|
|
9
|
+
"make_dirs",
|
|
13
10
|
)
|
|
14
11
|
|
|
15
12
|
import os
|
|
16
|
-
import random
|
|
17
13
|
from pathlib import Path
|
|
18
14
|
from typing import TYPE_CHECKING, overload
|
|
19
15
|
|
|
20
16
|
from kaparoo.filesystem.existence import (
|
|
21
17
|
_join_root_if_provided,
|
|
22
|
-
|
|
18
|
+
_validate_mode,
|
|
23
19
|
ensure_dir_exists,
|
|
24
20
|
ensure_dirs_exist,
|
|
25
|
-
file_exists,
|
|
26
21
|
)
|
|
27
|
-
from kaparoo.filesystem.utils import stringify_paths
|
|
22
|
+
from kaparoo.filesystem.utils import stringify_path, stringify_paths
|
|
28
23
|
|
|
29
24
|
if TYPE_CHECKING:
|
|
30
|
-
from collections.abc import
|
|
25
|
+
from collections.abc import Sequence
|
|
31
26
|
from typing import Literal
|
|
32
27
|
|
|
33
28
|
from kaparoo.filesystem.types import StrPath, StrPaths
|
|
@@ -38,13 +33,112 @@ if TYPE_CHECKING:
|
|
|
38
33
|
# ========================== #
|
|
39
34
|
|
|
40
35
|
|
|
36
|
+
@overload
|
|
37
|
+
def make_dir(
|
|
38
|
+
path: StrPath,
|
|
39
|
+
*,
|
|
40
|
+
mode: int = 0o777,
|
|
41
|
+
exist_ok: bool = False,
|
|
42
|
+
stringify: Literal[False] = False,
|
|
43
|
+
) -> Path: ...
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
@overload
|
|
47
|
+
def make_dir(
|
|
48
|
+
path: StrPath,
|
|
49
|
+
*,
|
|
50
|
+
mode: int = 0o777,
|
|
51
|
+
exist_ok: bool = False,
|
|
52
|
+
stringify: Literal[True],
|
|
53
|
+
) -> str: ...
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
@overload
|
|
57
|
+
def make_dir(
|
|
58
|
+
path: StrPath,
|
|
59
|
+
*,
|
|
60
|
+
mode: int = 0o777,
|
|
61
|
+
exist_ok: bool = False,
|
|
62
|
+
stringify: bool,
|
|
63
|
+
) -> Path | str: ...
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
def make_dir(
|
|
67
|
+
path: StrPath,
|
|
68
|
+
*,
|
|
69
|
+
mode: int = 0o777,
|
|
70
|
+
exist_ok: bool = False,
|
|
71
|
+
stringify: bool = False,
|
|
72
|
+
) -> Path | str:
|
|
73
|
+
"""Recursively create a directory.
|
|
74
|
+
|
|
75
|
+
Args:
|
|
76
|
+
path: The directory path to create.
|
|
77
|
+
mode: The mode to use when creating the directory. Defaults to 0o777.
|
|
78
|
+
exist_ok: Whether to suppress OSError if the path already exists.
|
|
79
|
+
Defaults to False.
|
|
80
|
+
stringify: Whether to return the path as a string. Defaults to False.
|
|
81
|
+
|
|
82
|
+
Returns:
|
|
83
|
+
The created directory path as a Path object or a string,
|
|
84
|
+
depending on the value of `stringify`.
|
|
85
|
+
|
|
86
|
+
Raises:
|
|
87
|
+
ValueError: If `mode` is outside the range 0o1-0o7777
|
|
88
|
+
(not checked on Windows, where the mode is ignored).
|
|
89
|
+
NotADirectoryError: If the path exists but is not a directory.
|
|
90
|
+
OSError: If `exist_ok` is False and the path already exists.
|
|
91
|
+
"""
|
|
92
|
+
_validate_mode(mode)
|
|
93
|
+
path = Path(path)
|
|
94
|
+
if path.exists() and not path.is_dir():
|
|
95
|
+
msg = f"not a directory: {path}"
|
|
96
|
+
raise NotADirectoryError(msg)
|
|
97
|
+
path.mkdir(mode=mode, parents=True, exist_ok=exist_ok)
|
|
98
|
+
return stringify_path(path) if stringify else path
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
@overload
|
|
41
102
|
def make_dirs(
|
|
42
103
|
paths: StrPaths,
|
|
43
104
|
*,
|
|
44
105
|
root: StrPath | None = None,
|
|
45
106
|
mode: int = 0o777,
|
|
46
107
|
exist_ok: bool = False,
|
|
47
|
-
|
|
108
|
+
stringify: Literal[False] = False,
|
|
109
|
+
) -> Sequence[Path]: ...
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
@overload
|
|
113
|
+
def make_dirs(
|
|
114
|
+
paths: StrPaths,
|
|
115
|
+
*,
|
|
116
|
+
root: StrPath | None = None,
|
|
117
|
+
mode: int = 0o777,
|
|
118
|
+
exist_ok: bool = False,
|
|
119
|
+
stringify: Literal[True],
|
|
120
|
+
) -> Sequence[str]: ...
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
@overload
|
|
124
|
+
def make_dirs(
|
|
125
|
+
paths: StrPaths,
|
|
126
|
+
*,
|
|
127
|
+
root: StrPath | None = None,
|
|
128
|
+
mode: int = 0o777,
|
|
129
|
+
exist_ok: bool = False,
|
|
130
|
+
stringify: bool,
|
|
131
|
+
) -> Sequence[Path] | Sequence[str]: ...
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
def make_dirs(
|
|
135
|
+
paths: StrPaths,
|
|
136
|
+
*,
|
|
137
|
+
root: StrPath | None = None,
|
|
138
|
+
mode: int = 0o777,
|
|
139
|
+
exist_ok: bool = False,
|
|
140
|
+
stringify: bool = False,
|
|
141
|
+
) -> Sequence[Path] | Sequence[str]:
|
|
48
142
|
"""Recursively create directories.
|
|
49
143
|
|
|
50
144
|
Args:
|
|
@@ -53,21 +147,27 @@ def make_dirs(
|
|
|
53
147
|
mode: The mode to use when creating the directories. Defaults to 0o777.
|
|
54
148
|
exist_ok: Whether to suppress OSError if any of the paths already exist.
|
|
55
149
|
Defaults to False.
|
|
150
|
+
stringify: Whether to return the paths as strings. Defaults to False.
|
|
56
151
|
|
|
57
152
|
Returns:
|
|
58
|
-
The directory paths
|
|
153
|
+
The created directory paths as Path objects or strings,
|
|
154
|
+
depending on the value of `stringify`.
|
|
59
155
|
|
|
60
156
|
Raises:
|
|
157
|
+
ValueError: If `mode` is outside the range 0o1-0o7777
|
|
158
|
+
(not checked on Windows, where the mode is ignored).
|
|
61
159
|
DirectoryNotFoundError: If `root` is provided and does not exist.
|
|
62
160
|
NotADirectoryError: If `root` is provided and is not a directory.
|
|
63
161
|
ValueError: If `root` is provided and any of the paths are absolute.
|
|
64
162
|
OSError: If `exist_ok` is False and any of the paths already exist.
|
|
65
163
|
OSError: If any of the paths are not directories.
|
|
66
164
|
"""
|
|
165
|
+
_validate_mode(mode)
|
|
67
166
|
paths = _join_root_if_provided(paths, root)
|
|
68
|
-
for
|
|
69
|
-
|
|
70
|
-
|
|
167
|
+
directories = [Path(p) for p in paths]
|
|
168
|
+
for directory in directories:
|
|
169
|
+
directory.mkdir(mode=mode, parents=True, exist_ok=exist_ok)
|
|
170
|
+
return stringify_paths(directories) if stringify else directories
|
|
71
171
|
|
|
72
172
|
|
|
73
173
|
# ========================== #
|
|
@@ -81,10 +181,10 @@ def dir_empty_unsafe(path: StrPath) -> bool:
|
|
|
81
181
|
return not any(it)
|
|
82
182
|
|
|
83
183
|
|
|
84
|
-
def dirs_empty_unsafe(paths: StrPaths, root: StrPath | None = None) -> bool:
|
|
184
|
+
def dirs_empty_unsafe(paths: StrPaths, *, root: StrPath | None = None) -> bool:
|
|
85
185
|
"""Check if directories are empty without existence checks."""
|
|
86
186
|
if root is not None:
|
|
87
|
-
paths = [
|
|
187
|
+
paths = [Path(root) / p for p in paths]
|
|
88
188
|
return all(dir_empty_unsafe(p) for p in paths)
|
|
89
189
|
|
|
90
190
|
|
|
@@ -105,7 +205,7 @@ def dir_empty(path: StrPath) -> bool:
|
|
|
105
205
|
return dir_empty_unsafe(path)
|
|
106
206
|
|
|
107
207
|
|
|
108
|
-
def dirs_empty(paths: StrPaths, root: StrPath | None = None) -> bool:
|
|
208
|
+
def dirs_empty(paths: StrPaths, *, root: StrPath | None = None) -> bool:
|
|
109
209
|
"""Check if directories are empty.
|
|
110
210
|
|
|
111
211
|
Args:
|
|
@@ -117,303 +217,10 @@ def dirs_empty(paths: StrPaths, root: StrPath | None = None) -> bool:
|
|
|
117
217
|
|
|
118
218
|
Raises:
|
|
119
219
|
DirectoryNotFoundError: If `root` is provided and does not exist.
|
|
120
|
-
|
|
220
|
+
DirectoryNotFoundError: If any of the paths do not exist.
|
|
121
221
|
NotADirectoryError: If `root` is provided and is not a directory.
|
|
122
222
|
NotADirectoryError: If any of the paths are not directories.
|
|
123
223
|
ValueError: If `root` is provided and any of the paths are absolute.
|
|
124
224
|
"""
|
|
125
225
|
paths = ensure_dirs_exist(paths, root=root)
|
|
126
226
|
return all(dir_empty_unsafe(p) for p in paths)
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
# ========================== #
|
|
130
|
-
# Search #
|
|
131
|
-
# ========================== #
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
@overload
|
|
135
|
-
def get_paths(
|
|
136
|
-
root: StrPath,
|
|
137
|
-
*,
|
|
138
|
-
pattern: str = "*",
|
|
139
|
-
excludes: StrPaths | None = None,
|
|
140
|
-
condition: Callable[[StrPath], bool] | None = None,
|
|
141
|
-
num_samples: int | None = None,
|
|
142
|
-
recursive: bool = False,
|
|
143
|
-
stringify: Literal[False] = False,
|
|
144
|
-
) -> Sequence[Path]:
|
|
145
|
-
...
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
@overload
|
|
149
|
-
def get_paths(
|
|
150
|
-
root: StrPath,
|
|
151
|
-
*,
|
|
152
|
-
pattern: str = "*",
|
|
153
|
-
excludes: StrPaths | None = None,
|
|
154
|
-
condition: Callable[[StrPath], bool] | None = None,
|
|
155
|
-
num_samples: int | None = None,
|
|
156
|
-
recursive: bool = False,
|
|
157
|
-
stringify: Literal[True],
|
|
158
|
-
) -> Sequence[str]:
|
|
159
|
-
...
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
@overload
|
|
163
|
-
def get_paths(
|
|
164
|
-
root: StrPath,
|
|
165
|
-
*,
|
|
166
|
-
pattern: str = "*",
|
|
167
|
-
excludes: StrPaths | None = None,
|
|
168
|
-
condition: Callable[[StrPath], bool] | None = None,
|
|
169
|
-
num_samples: int | None = None,
|
|
170
|
-
recursive: bool = False,
|
|
171
|
-
stringify: bool,
|
|
172
|
-
) -> Sequence[Path] | Sequence[str]:
|
|
173
|
-
...
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
def get_paths(
|
|
177
|
-
root: StrPath,
|
|
178
|
-
*,
|
|
179
|
-
pattern: str = "*",
|
|
180
|
-
excludes: StrPaths | None = None,
|
|
181
|
-
condition: Callable[[StrPath], bool] | None = None,
|
|
182
|
-
num_samples: int | None = None,
|
|
183
|
-
recursive: bool = False,
|
|
184
|
-
stringify: bool = False,
|
|
185
|
-
) -> Sequence[Path] | Sequence[str]:
|
|
186
|
-
"""Get paths that match the specified criteria.
|
|
187
|
-
|
|
188
|
-
The criteria are applied in the following order:
|
|
189
|
-
`pattern` -> `excludes` -> `condition` -> `num_samples`
|
|
190
|
-
|
|
191
|
-
Args:
|
|
192
|
-
root: The root directory to search.
|
|
193
|
-
pattern: The glob pattern to search for. Defaults to "*".
|
|
194
|
-
excludes: A sequence of paths to exclude from the search. Both absolute
|
|
195
|
-
and relative paths are supported. Defaults to None.
|
|
196
|
-
condition: A predicate function to filter the paths. Only paths that satisfy
|
|
197
|
-
the predicate are returned. Defaults to None.
|
|
198
|
-
num_samples: The maximum number of paths to return. If provided, only the
|
|
199
|
-
`num_samples` paths are randomly selected and returned. Defaults to None.
|
|
200
|
-
recursive: Whether to search recursively in the root directory. Defaults to False.
|
|
201
|
-
stringify: Whether to return the paths as strings. Defaults to False.
|
|
202
|
-
|
|
203
|
-
Returns:
|
|
204
|
-
The paths that match the specified criteria as a sequence of Path objects
|
|
205
|
-
or strings, depending on the value of `stringify`.
|
|
206
|
-
|
|
207
|
-
Raises:
|
|
208
|
-
DirectoryNotFoundError: If the root directory does not exist.
|
|
209
|
-
NotADirectoryError: If the root directory is not a directory.
|
|
210
|
-
ValueError: If `num_samples` is not greater than 0.
|
|
211
|
-
"""
|
|
212
|
-
root = ensure_dir_exists(root)
|
|
213
|
-
|
|
214
|
-
paths = list(root.rglob(pattern) if recursive else root.glob(pattern))
|
|
215
|
-
|
|
216
|
-
excludes_set = {root}
|
|
217
|
-
if excludes:
|
|
218
|
-
resolve = lambda p: p if p.is_relative_to(root) else root / p # noqa: E731
|
|
219
|
-
excludes_set.update(resolve(Path(e)) for e in excludes)
|
|
220
|
-
|
|
221
|
-
paths = [p for p in paths if p not in excludes_set]
|
|
222
|
-
|
|
223
|
-
if callable(condition):
|
|
224
|
-
paths = [p for p in paths if condition(p)]
|
|
225
|
-
|
|
226
|
-
if isinstance(num_samples, int) and num_samples < len(paths):
|
|
227
|
-
if num_samples <= 0:
|
|
228
|
-
raise ValueError(f"num_samples must be greater than 0 (got {num_samples})")
|
|
229
|
-
paths = random.sample(paths, num_samples)
|
|
230
|
-
|
|
231
|
-
return stringify_paths(paths) if stringify else paths
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
@overload
|
|
235
|
-
def get_files(
|
|
236
|
-
root: StrPath,
|
|
237
|
-
*,
|
|
238
|
-
pattern: str = "*",
|
|
239
|
-
excludes: StrPaths | None = None,
|
|
240
|
-
condition: Callable[[StrPath], bool] | None = None,
|
|
241
|
-
num_samples: int | None = None,
|
|
242
|
-
recursive: bool = False,
|
|
243
|
-
stringify: Literal[False] = False,
|
|
244
|
-
) -> Sequence[Path]:
|
|
245
|
-
...
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
@overload
|
|
249
|
-
def get_files(
|
|
250
|
-
root: StrPath,
|
|
251
|
-
*,
|
|
252
|
-
pattern: str = "*",
|
|
253
|
-
excludes: StrPaths | None = None,
|
|
254
|
-
condition: Callable[[StrPath], bool] | None = None,
|
|
255
|
-
num_samples: int | None = None,
|
|
256
|
-
recursive: bool = False,
|
|
257
|
-
stringify: Literal[True],
|
|
258
|
-
) -> Sequence[str]:
|
|
259
|
-
...
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
@overload
|
|
263
|
-
def get_files(
|
|
264
|
-
root: StrPath,
|
|
265
|
-
*,
|
|
266
|
-
pattern: str = "*",
|
|
267
|
-
excludes: StrPaths | None = None,
|
|
268
|
-
condition: Callable[[StrPath], bool] | None = None,
|
|
269
|
-
num_samples: int | None = None,
|
|
270
|
-
recursive: bool = False,
|
|
271
|
-
stringify: bool,
|
|
272
|
-
) -> Sequence[Path] | Sequence[str]:
|
|
273
|
-
...
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
def get_files(
|
|
277
|
-
root: StrPath,
|
|
278
|
-
*,
|
|
279
|
-
pattern: str = "*",
|
|
280
|
-
excludes: StrPaths | None = None,
|
|
281
|
-
condition: Callable[[StrPath], bool] | None = None,
|
|
282
|
-
num_samples: int | None = None,
|
|
283
|
-
recursive: bool = False,
|
|
284
|
-
stringify: bool = False,
|
|
285
|
-
) -> Sequence[Path] | Sequence[str]:
|
|
286
|
-
"""Get file paths that match the specified criteria.
|
|
287
|
-
|
|
288
|
-
The criteria are applied in the following order:
|
|
289
|
-
`pattern` -> `excludes` -> `condition` -> `num_samples`
|
|
290
|
-
|
|
291
|
-
Args:
|
|
292
|
-
root: The root directory to search.
|
|
293
|
-
pattern: The glob pattern to search for. Defaults to "*".
|
|
294
|
-
excludes: A sequence of paths to exclude from the search. Both absolute
|
|
295
|
-
and relative paths are supported. Defaults to None.
|
|
296
|
-
condition: A predicate function to filter the paths. Only paths that satisfy
|
|
297
|
-
the predicate are returned. Defaults to None.
|
|
298
|
-
num_samples: The maximum number of paths to return. If provided, only the
|
|
299
|
-
`num_samples` paths are randomly selected and returned. Defaults to None.
|
|
300
|
-
recursive: Whether to search recursively in the root directory. Defaults to False.
|
|
301
|
-
stringify: Whether to return the paths as strings. Defaults to False.
|
|
302
|
-
|
|
303
|
-
Returns:
|
|
304
|
-
The file paths that match the specified criteria as a sequence of Path objects
|
|
305
|
-
or strings, depending on the value of `stringify`.
|
|
306
|
-
|
|
307
|
-
Raises:
|
|
308
|
-
DirectoryNotFoundError: If the root directory does not exist.
|
|
309
|
-
NotADirectoryError: If the root directory is not a directory.
|
|
310
|
-
ValueError: If `num_samples` is not greater than 0.
|
|
311
|
-
"""
|
|
312
|
-
if callable(condition):
|
|
313
|
-
file_condition = lambda path: file_exists(path) and condition(path) # noqa: E731
|
|
314
|
-
else:
|
|
315
|
-
file_condition = file_exists
|
|
316
|
-
|
|
317
|
-
return get_paths(
|
|
318
|
-
root,
|
|
319
|
-
pattern=pattern,
|
|
320
|
-
excludes=excludes,
|
|
321
|
-
condition=file_condition,
|
|
322
|
-
num_samples=num_samples,
|
|
323
|
-
recursive=recursive,
|
|
324
|
-
stringify=stringify,
|
|
325
|
-
)
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
@overload
|
|
329
|
-
def get_dirs(
|
|
330
|
-
root: StrPath,
|
|
331
|
-
*,
|
|
332
|
-
pattern: str = "*",
|
|
333
|
-
excludes: StrPaths | None = None,
|
|
334
|
-
condition: Callable[[StrPath], bool] | None = None,
|
|
335
|
-
num_samples: int | None = None,
|
|
336
|
-
recursive: bool = False,
|
|
337
|
-
stringify: Literal[False] = False,
|
|
338
|
-
) -> Sequence[Path]:
|
|
339
|
-
...
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
@overload
|
|
343
|
-
def get_dirs(
|
|
344
|
-
root: StrPath,
|
|
345
|
-
*,
|
|
346
|
-
pattern: str = "*",
|
|
347
|
-
excludes: StrPaths | None = None,
|
|
348
|
-
condition: Callable[[StrPath], bool] | None = None,
|
|
349
|
-
num_samples: int | None = None,
|
|
350
|
-
recursive: bool = False,
|
|
351
|
-
stringify: Literal[True],
|
|
352
|
-
) -> Sequence[str]:
|
|
353
|
-
...
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
@overload
|
|
357
|
-
def get_dirs(
|
|
358
|
-
root: StrPath,
|
|
359
|
-
*,
|
|
360
|
-
pattern: str = "*",
|
|
361
|
-
excludes: StrPaths | None = None,
|
|
362
|
-
condition: Callable[[StrPath], bool] | None = None,
|
|
363
|
-
num_samples: int | None = None,
|
|
364
|
-
recursive: bool = False,
|
|
365
|
-
stringify: bool,
|
|
366
|
-
) -> Sequence[Path] | Sequence[str]:
|
|
367
|
-
...
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
def get_dirs(
|
|
371
|
-
root: StrPath,
|
|
372
|
-
*,
|
|
373
|
-
pattern: str = "*",
|
|
374
|
-
excludes: StrPaths | None = None,
|
|
375
|
-
condition: Callable[[StrPath], bool] | None = None,
|
|
376
|
-
num_samples: int | None = None,
|
|
377
|
-
recursive: bool = False,
|
|
378
|
-
stringify: bool = False,
|
|
379
|
-
) -> Sequence[Path] | Sequence[str]:
|
|
380
|
-
"""Get directory paths that match the specified criteria.
|
|
381
|
-
|
|
382
|
-
The criteria are applied in the following order:
|
|
383
|
-
`pattern` -> `excludes` -> `condition` -> `num_samples`
|
|
384
|
-
|
|
385
|
-
Args:
|
|
386
|
-
root: The root directory to search.
|
|
387
|
-
pattern: The glob pattern to search for. Defaults to "*".
|
|
388
|
-
excludes: A sequence of paths to exclude from the search. Both absolute
|
|
389
|
-
and relative paths are supported. Defaults to None.
|
|
390
|
-
condition: A predicate function to filter the paths. Only paths that satisfy
|
|
391
|
-
the predicate are returned. Defaults to None.
|
|
392
|
-
num_samples: The maximum number of paths to return. If provided, only the
|
|
393
|
-
`num_samples` paths are randomly selected and returned. Defaults to None.
|
|
394
|
-
recursive: Whether to search recursively in the root directory. Defaults to False.
|
|
395
|
-
stringify: Whether to return the paths as strings. Defaults to False.
|
|
396
|
-
|
|
397
|
-
Returns:
|
|
398
|
-
The directory paths that match the specified criteria as a sequence of
|
|
399
|
-
Path objects or strings, depending on the value of `stringify`.
|
|
400
|
-
|
|
401
|
-
Raises:
|
|
402
|
-
DirectoryNotFoundError: If the root directory does not exist.
|
|
403
|
-
NotADirectoryError: If the root directory is not a directory.
|
|
404
|
-
ValueError: If `num_samples` is not greater than 0.
|
|
405
|
-
"""
|
|
406
|
-
if callable(condition):
|
|
407
|
-
dir_condition = lambda path: dir_exists(path) and condition(path) # noqa: E731
|
|
408
|
-
else:
|
|
409
|
-
dir_condition = dir_exists
|
|
410
|
-
|
|
411
|
-
return get_paths(
|
|
412
|
-
root,
|
|
413
|
-
pattern=pattern,
|
|
414
|
-
excludes=excludes,
|
|
415
|
-
condition=dir_condition,
|
|
416
|
-
num_samples=num_samples,
|
|
417
|
-
recursive=recursive,
|
|
418
|
-
stringify=stringify,
|
|
419
|
-
)
|
kaparoo/filesystem/exceptions.py
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
1
3
|
__all__ = ("DirectoryNotFoundError", "NotAFileError")
|
|
2
4
|
|
|
3
5
|
|
|
4
6
|
class DirectoryNotFoundError(FileNotFoundError):
|
|
5
|
-
"""
|
|
7
|
+
"""Raised when a directory does not exist.
|
|
6
8
|
|
|
7
9
|
Note:
|
|
8
10
|
Since this exception inherits from `FileNotFoundError`,
|
|
@@ -12,4 +14,4 @@ class DirectoryNotFoundError(FileNotFoundError):
|
|
|
12
14
|
|
|
13
15
|
|
|
14
16
|
class NotAFileError(OSError):
|
|
15
|
-
"""
|
|
17
|
+
"""Raised when a path exists but is not a file."""
|