kaparoo-python 0.1.11__tar.gz → 0.2.0__tar.gz
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_python-0.1.11 → kaparoo_python-0.2.0}/LICENSE +20 -20
- kaparoo_python-0.2.0/PKG-INFO +86 -0
- kaparoo_python-0.2.0/README.md +65 -0
- kaparoo_python-0.2.0/kaparoo/data/sequence.py +39 -0
- {kaparoo_python-0.1.11 → kaparoo_python-0.2.0}/kaparoo/data/utils.py +8 -11
- {kaparoo_python-0.1.11 → kaparoo_python-0.2.0}/kaparoo/filesystem/__init__.py +37 -20
- kaparoo_python-0.2.0/kaparoo/filesystem/directory.py +226 -0
- {kaparoo_python-0.1.11 → kaparoo_python-0.2.0}/kaparoo/filesystem/exceptions.py +4 -2
- {kaparoo_python-0.1.11 → kaparoo_python-0.2.0}/kaparoo/filesystem/existence.py +64 -64
- kaparoo_python-0.2.0/kaparoo/filesystem/search/__init__.py +83 -0
- kaparoo_python-0.2.0/kaparoo/filesystem/search/classes.py +199 -0
- kaparoo_python-0.2.0/kaparoo/filesystem/search/deprecated.py +289 -0
- kaparoo_python-0.2.0/kaparoo/filesystem/search/filters.py +322 -0
- kaparoo_python-0.2.0/kaparoo/filesystem/search/wrappers.py +311 -0
- kaparoo_python-0.2.0/kaparoo/filesystem/types.py +9 -0
- kaparoo_python-0.2.0/kaparoo/filesystem/utils.py +208 -0
- kaparoo_python-0.2.0/kaparoo/utils/__init__.py +21 -0
- {kaparoo_python-0.1.11 → kaparoo_python-0.2.0}/kaparoo/utils/optional.py +11 -33
- kaparoo_python-0.2.0/kaparoo/utils/timer.py +374 -0
- kaparoo_python-0.2.0/pyproject.toml +254 -0
- kaparoo_python-0.1.11/.gitignore +0 -207
- kaparoo_python-0.1.11/PKG-INFO +0 -69
- kaparoo_python-0.1.11/README.md +0 -23
- kaparoo_python-0.1.11/kaparoo/__about__.py +0 -1
- kaparoo_python-0.1.11/kaparoo/data/files/__init__.py +0 -0
- kaparoo_python-0.1.11/kaparoo/data/files/base.py +0 -69
- kaparoo_python-0.1.11/kaparoo/filesystem/directory.py +0 -419
- kaparoo_python-0.1.11/kaparoo/filesystem/types.py +0 -8
- kaparoo_python-0.1.11/kaparoo/filesystem/utils.py +0 -139
- kaparoo_python-0.1.11/kaparoo/utils/__init__.py +0 -0
- kaparoo_python-0.1.11/kaparoo/utils/types.py +0 -9
- kaparoo_python-0.1.11/pyproject.toml +0 -129
- {kaparoo_python-0.1.11 → kaparoo_python-0.2.0}/kaparoo/__init__.py +0 -0
- {kaparoo_python-0.1.11 → kaparoo_python-0.2.0}/kaparoo/data/__init__.py +0 -0
- {kaparoo_python-0.1.11 → kaparoo_python-0.2.0}/kaparoo/py.typed +0 -0
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2023 Jaewoo Park
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
-
in the Software without restriction, including without limitation the rights
|
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
-
furnished to do so, subject to the following conditions:
|
|
11
|
-
|
|
12
|
-
The above copyright notice and this permission notice shall be included in all
|
|
13
|
-
copies or substantial portions of the Software.
|
|
14
|
-
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023 Jaewoo Park
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
21
|
SOFTWARE.
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: kaparoo-python
|
|
3
|
+
Version: 0.2.0
|
|
4
|
+
Summary: Personally common and useful Python features
|
|
5
|
+
Keywords: filesystem,pathlib,paths,utilities
|
|
6
|
+
Author: Jaewoo Park
|
|
7
|
+
Author-email: Jaewoo Park <kaparoo2001@gmail.com>
|
|
8
|
+
License-Expression: MIT
|
|
9
|
+
License-File: LICENSE
|
|
10
|
+
Classifier: Development Status :: 4 - Beta
|
|
11
|
+
Classifier: Intended Audience :: Developers
|
|
12
|
+
Classifier: Operating System :: OS Independent
|
|
13
|
+
Classifier: Programming Language :: Python :: 3
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
15
|
+
Classifier: Programming Language :: Python :: Implementation :: CPython
|
|
16
|
+
Classifier: Typing :: Typed
|
|
17
|
+
Requires-Python: >=3.14
|
|
18
|
+
Project-URL: GitHub, https://www.github.com/kaparoo/kaparoo-python
|
|
19
|
+
Project-URL: Issues, https://www.github.com/kaparoo/kaparoo-python/issues
|
|
20
|
+
Description-Content-Type: text/markdown
|
|
21
|
+
|
|
22
|
+
# kaparoo-python
|
|
23
|
+
|
|
24
|
+
[](https://pypi.org/project/kaparoo-python/)
|
|
25
|
+
[](https://pypi.org/project/kaparoo-python/)
|
|
26
|
+
[](https://www.python.org/)
|
|
27
|
+
[](./LICENSE)
|
|
28
|
+
[](https://github.com/astral-sh/uv)
|
|
29
|
+
[](https://github.com/astral-sh/ruff)
|
|
30
|
+
[](https://github.com/astral-sh/ty)
|
|
31
|
+
[](https://github.com/copier-org/copier)
|
|
32
|
+
|
|
33
|
+
*Personally common and useful Python features.*
|
|
34
|
+
|
|
35
|
+
## 📦 Installation
|
|
36
|
+
|
|
37
|
+
Requires Python 3.14+.
|
|
38
|
+
|
|
39
|
+
```bash
|
|
40
|
+
# With uv (recommended)
|
|
41
|
+
uv add kaparoo-python
|
|
42
|
+
|
|
43
|
+
# With pip
|
|
44
|
+
pip install kaparoo-python
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
## 🧩 Modules
|
|
48
|
+
|
|
49
|
+
### `kaparoo.filesystem`
|
|
50
|
+
|
|
51
|
+
`pathlib`-based filesystem helpers.
|
|
52
|
+
|
|
53
|
+
- **`existence`** — existence checks (`*_exists`) and `ensure_*` validators.
|
|
54
|
+
- **`directory`** — `make_dir(s)`, `dir_empty(s)` (with `_unsafe` variants).
|
|
55
|
+
- **`utils`** — `stringify_path(s)`, `wrap_path(s)`.
|
|
56
|
+
- **`exceptions`** — `DirectoryNotFoundError`, `NotAFileError`.
|
|
57
|
+
- **`types`** — `StrPath`, `StrPaths`.
|
|
58
|
+
|
|
59
|
+
### `kaparoo.filesystem.search`
|
|
60
|
+
|
|
61
|
+
Filesystem traversal with composable filters.
|
|
62
|
+
|
|
63
|
+
- **Entry points** — `search_paths`, `search_files`, `search_dirs`.
|
|
64
|
+
- **Pattern filters** — `Equals`, `StartsWith`, `EndsWith`, `Contains`,
|
|
65
|
+
`Regex`, `Glob`.
|
|
66
|
+
- **Multi-pattern filters** — `EqualsAny`, `StartsWithAny`, `EndsWithAny`,
|
|
67
|
+
`ContainsAny`.
|
|
68
|
+
- **Logical filters** — `And`, `Or`, `Not`.
|
|
69
|
+
- **Deprecated** — `get_paths`, `get_files`, `get_dirs` (use `search_*`).
|
|
70
|
+
|
|
71
|
+
### `kaparoo.utils`
|
|
72
|
+
|
|
73
|
+
- **`timer`** — `Timer` and `LapTimer` context-manager / decorator timers.
|
|
74
|
+
- **`optional`** — `replace_if_none`, `factory_if_none`, `unwrap_or_*`.
|
|
75
|
+
|
|
76
|
+
## 📋 TODO
|
|
77
|
+
|
|
78
|
+
See [TODO.md](./TODO.md) for tracked open items.
|
|
79
|
+
|
|
80
|
+
## 📜 Changelog
|
|
81
|
+
|
|
82
|
+
See [CHANGELOG.md](./CHANGELOG.md) for the version history.
|
|
83
|
+
|
|
84
|
+
## ⚖️ License
|
|
85
|
+
|
|
86
|
+
This project is distributed under the terms of the [MIT](./LICENSE) license.
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
# kaparoo-python
|
|
2
|
+
|
|
3
|
+
[](https://pypi.org/project/kaparoo-python/)
|
|
4
|
+
[](https://pypi.org/project/kaparoo-python/)
|
|
5
|
+
[](https://www.python.org/)
|
|
6
|
+
[](./LICENSE)
|
|
7
|
+
[](https://github.com/astral-sh/uv)
|
|
8
|
+
[](https://github.com/astral-sh/ruff)
|
|
9
|
+
[](https://github.com/astral-sh/ty)
|
|
10
|
+
[](https://github.com/copier-org/copier)
|
|
11
|
+
|
|
12
|
+
*Personally common and useful Python features.*
|
|
13
|
+
|
|
14
|
+
## 📦 Installation
|
|
15
|
+
|
|
16
|
+
Requires Python 3.14+.
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
# With uv (recommended)
|
|
20
|
+
uv add kaparoo-python
|
|
21
|
+
|
|
22
|
+
# With pip
|
|
23
|
+
pip install kaparoo-python
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## 🧩 Modules
|
|
27
|
+
|
|
28
|
+
### `kaparoo.filesystem`
|
|
29
|
+
|
|
30
|
+
`pathlib`-based filesystem helpers.
|
|
31
|
+
|
|
32
|
+
- **`existence`** — existence checks (`*_exists`) and `ensure_*` validators.
|
|
33
|
+
- **`directory`** — `make_dir(s)`, `dir_empty(s)` (with `_unsafe` variants).
|
|
34
|
+
- **`utils`** — `stringify_path(s)`, `wrap_path(s)`.
|
|
35
|
+
- **`exceptions`** — `DirectoryNotFoundError`, `NotAFileError`.
|
|
36
|
+
- **`types`** — `StrPath`, `StrPaths`.
|
|
37
|
+
|
|
38
|
+
### `kaparoo.filesystem.search`
|
|
39
|
+
|
|
40
|
+
Filesystem traversal with composable filters.
|
|
41
|
+
|
|
42
|
+
- **Entry points** — `search_paths`, `search_files`, `search_dirs`.
|
|
43
|
+
- **Pattern filters** — `Equals`, `StartsWith`, `EndsWith`, `Contains`,
|
|
44
|
+
`Regex`, `Glob`.
|
|
45
|
+
- **Multi-pattern filters** — `EqualsAny`, `StartsWithAny`, `EndsWithAny`,
|
|
46
|
+
`ContainsAny`.
|
|
47
|
+
- **Logical filters** — `And`, `Or`, `Not`.
|
|
48
|
+
- **Deprecated** — `get_paths`, `get_files`, `get_dirs` (use `search_*`).
|
|
49
|
+
|
|
50
|
+
### `kaparoo.utils`
|
|
51
|
+
|
|
52
|
+
- **`timer`** — `Timer` and `LapTimer` context-manager / decorator timers.
|
|
53
|
+
- **`optional`** — `replace_if_none`, `factory_if_none`, `unwrap_or_*`.
|
|
54
|
+
|
|
55
|
+
## 📋 TODO
|
|
56
|
+
|
|
57
|
+
See [TODO.md](./TODO.md) for tracked open items.
|
|
58
|
+
|
|
59
|
+
## 📜 Changelog
|
|
60
|
+
|
|
61
|
+
See [CHANGELOG.md](./CHANGELOG.md) for the version history.
|
|
62
|
+
|
|
63
|
+
## ⚖️ License
|
|
64
|
+
|
|
65
|
+
This project is distributed under the terms of the [MIT](./LICENSE) license.
|
|
@@ -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]
|
|
@@ -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
|
|
@@ -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
|
)
|
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
__all__ = (
|
|
4
|
+
"dir_empty",
|
|
5
|
+
"dir_empty_unsafe",
|
|
6
|
+
"dirs_empty",
|
|
7
|
+
"dirs_empty_unsafe",
|
|
8
|
+
"make_dir",
|
|
9
|
+
"make_dirs",
|
|
10
|
+
)
|
|
11
|
+
|
|
12
|
+
import os
|
|
13
|
+
from pathlib import Path
|
|
14
|
+
from typing import TYPE_CHECKING, overload
|
|
15
|
+
|
|
16
|
+
from kaparoo.filesystem.existence import (
|
|
17
|
+
_join_root_if_provided,
|
|
18
|
+
_validate_mode,
|
|
19
|
+
ensure_dir_exists,
|
|
20
|
+
ensure_dirs_exist,
|
|
21
|
+
)
|
|
22
|
+
from kaparoo.filesystem.utils import stringify_path, stringify_paths
|
|
23
|
+
|
|
24
|
+
if TYPE_CHECKING:
|
|
25
|
+
from collections.abc import Sequence
|
|
26
|
+
from typing import Literal
|
|
27
|
+
|
|
28
|
+
from kaparoo.filesystem.types import StrPath, StrPaths
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
# ========================== #
|
|
32
|
+
# Make #
|
|
33
|
+
# ========================== #
|
|
34
|
+
|
|
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
|
|
102
|
+
def make_dirs(
|
|
103
|
+
paths: StrPaths,
|
|
104
|
+
*,
|
|
105
|
+
root: StrPath | None = None,
|
|
106
|
+
mode: int = 0o777,
|
|
107
|
+
exist_ok: bool = False,
|
|
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]:
|
|
142
|
+
"""Recursively create directories.
|
|
143
|
+
|
|
144
|
+
Args:
|
|
145
|
+
paths: The directory paths to create.
|
|
146
|
+
root: The root directory to prepend to each path. Defaults to None.
|
|
147
|
+
mode: The mode to use when creating the directories. Defaults to 0o777.
|
|
148
|
+
exist_ok: Whether to suppress OSError if any of the paths already exist.
|
|
149
|
+
Defaults to False.
|
|
150
|
+
stringify: Whether to return the paths as strings. Defaults to False.
|
|
151
|
+
|
|
152
|
+
Returns:
|
|
153
|
+
The created directory paths as Path objects or strings,
|
|
154
|
+
depending on the value of `stringify`.
|
|
155
|
+
|
|
156
|
+
Raises:
|
|
157
|
+
ValueError: If `mode` is outside the range 0o1-0o7777
|
|
158
|
+
(not checked on Windows, where the mode is ignored).
|
|
159
|
+
DirectoryNotFoundError: If `root` is provided and does not exist.
|
|
160
|
+
NotADirectoryError: If `root` is provided and is not a directory.
|
|
161
|
+
ValueError: If `root` is provided and any of the paths are absolute.
|
|
162
|
+
OSError: If `exist_ok` is False and any of the paths already exist.
|
|
163
|
+
OSError: If any of the paths are not directories.
|
|
164
|
+
"""
|
|
165
|
+
_validate_mode(mode)
|
|
166
|
+
paths = _join_root_if_provided(paths, root)
|
|
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
|
|
171
|
+
|
|
172
|
+
|
|
173
|
+
# ========================== #
|
|
174
|
+
# Empty #
|
|
175
|
+
# ========================== #
|
|
176
|
+
|
|
177
|
+
|
|
178
|
+
def dir_empty_unsafe(path: StrPath) -> bool:
|
|
179
|
+
"""Check if a directory is empty without existence checks."""
|
|
180
|
+
with os.scandir(path) as it:
|
|
181
|
+
return not any(it)
|
|
182
|
+
|
|
183
|
+
|
|
184
|
+
def dirs_empty_unsafe(paths: StrPaths, *, root: StrPath | None = None) -> bool:
|
|
185
|
+
"""Check if directories are empty without existence checks."""
|
|
186
|
+
if root is not None:
|
|
187
|
+
paths = [Path(root) / p for p in paths]
|
|
188
|
+
return all(dir_empty_unsafe(p) for p in paths)
|
|
189
|
+
|
|
190
|
+
|
|
191
|
+
def dir_empty(path: StrPath) -> bool:
|
|
192
|
+
"""Check if a directory is empty.
|
|
193
|
+
|
|
194
|
+
Args:
|
|
195
|
+
path: The directory path to check.
|
|
196
|
+
|
|
197
|
+
Returns:
|
|
198
|
+
True if the directory is empty, False otherwise.
|
|
199
|
+
|
|
200
|
+
Raises:
|
|
201
|
+
DirectoryNotFoundError: If the path does not exist.
|
|
202
|
+
NotADirectoryError: If the path is not a directory.
|
|
203
|
+
"""
|
|
204
|
+
path = ensure_dir_exists(path)
|
|
205
|
+
return dir_empty_unsafe(path)
|
|
206
|
+
|
|
207
|
+
|
|
208
|
+
def dirs_empty(paths: StrPaths, *, root: StrPath | None = None) -> bool:
|
|
209
|
+
"""Check if directories are empty.
|
|
210
|
+
|
|
211
|
+
Args:
|
|
212
|
+
paths: A sequence of directory paths to check.
|
|
213
|
+
root: The root directory to prepend to each path. Defaults to None.
|
|
214
|
+
|
|
215
|
+
Returns:
|
|
216
|
+
True if all directories are empty, False otherwise.
|
|
217
|
+
|
|
218
|
+
Raises:
|
|
219
|
+
DirectoryNotFoundError: If `root` is provided and does not exist.
|
|
220
|
+
DirectoryNotFoundError: If any of the paths do not exist.
|
|
221
|
+
NotADirectoryError: If `root` is provided and is not a directory.
|
|
222
|
+
NotADirectoryError: If any of the paths are not directories.
|
|
223
|
+
ValueError: If `root` is provided and any of the paths are absolute.
|
|
224
|
+
"""
|
|
225
|
+
paths = ensure_dirs_exist(paths, root=root)
|
|
226
|
+
return all(dir_empty_unsafe(p) for p in paths)
|
|
@@ -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."""
|