forked from childmindresearch/bids2table
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_pathlib.py
More file actions
50 lines (36 loc) · 1.28 KB
/
_pathlib.py
File metadata and controls
50 lines (36 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import os
import sys
from pathlib import Path
from typing import Iterator
try:
from cloudpathlib import AnyPath, CloudPath, S3Client
_CLOUDPATHLIB_AVAILABLE = True
# Set unsigned client as default for s3:// paths
S3Client(no_sign_request=True).set_as_default_client()
except ImportError:
AnyPath = CloudPath = Path
_CLOUDPATHLIB_AVAILABLE = False
__all__ = ["PathT", "as_path", "cloudpathlib_is_available"]
PathT = Path | CloudPath
def walk(
root: str | PathT, follow_symlinks: bool = False
) -> Iterator[tuple[str, list[str], list[str]]]:
if isinstance(root, str):
root = as_path(root)
# Py312+ or CloudPath
if sys.version_info >= (3, 12) or (
_CLOUDPATHLIB_AVAILABLE and isinstance(root, CloudPath)
):
yield from root.walk(follow_symlinks=follow_symlinks)
# Fall back to os.walk for local paths
else:
for dirpath, dirnames, filenames in os.walk(root, followlinks=follow_symlinks):
yield Path(dirpath), dirnames, filenames
def as_path(path: str | PathT) -> PathT:
"""Cast input to a `Path` type."""
if isinstance(path, str):
return AnyPath(path)
return path
def cloudpathlib_is_available() -> bool:
"""Check if cloudpathlib is available."""
return _CLOUDPATHLIB_AVAILABLE