Skip to content

Commit 35b3272

Browse files
committed
Use fallback for walk in place of path.walk
1 parent e9e506e commit 35b3272

2 files changed

Lines changed: 23 additions & 4 deletions

File tree

bids2table/_indexing.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import enum
88
import fnmatch
99
import importlib.metadata
10-
import os
1110
import re
1211
from concurrent.futures import Executor, ProcessPoolExecutor
1312
from functools import partial
@@ -22,7 +21,7 @@
2221
validate_bids_entities,
2322
)
2423
from ._logging import setup_logger
25-
from ._pathlib import PathT, as_path
24+
from ._pathlib import PathT, as_path, walk
2625

2726
_BIDS_SUBJECT_DIR_PATTERN = re.compile(r"sub-[a-zA-Z0-9]+")
2827

@@ -147,9 +146,9 @@ def find_bids_datasets(
147146
dir_count = 0
148147
ds_count = 0
149148

150-
for dirpath, dirnames, _ in os.walk(root, followlinks=follow_symlinks):
149+
root = as_path(root)
150+
for dirpath, dirnames, _ in walk(root, follow_symlinks=follow_symlinks):
151151
dir_count += 1
152-
dirpath = as_path(dirpath)
153152

154153
if _is_bids_dataset(dirpath):
155154
ds_count += 1

bids2table/_pathlib.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
import os
2+
import sys
13
from pathlib import Path
4+
from typing import Iterator
25

36
try:
47
from cloudpathlib import AnyPath, CloudPath, S3Client
@@ -18,6 +21,23 @@
1821
PathT = Path | CloudPath
1922

2023

24+
def walk(
25+
root: str | PathT, follow_symlinks: bool = False
26+
) -> Iterator[tuple[str, list[str], list[str]]]:
27+
if isinstance(root, str):
28+
root = as_path(root)
29+
30+
# Py312+ or CloudPath
31+
if sys.version_info >= (3, 12) or (
32+
_CLOUDPATHLIB_AVAILABLE and isinstance(root, CloudPath)
33+
):
34+
yield from root.walk(follow_symlinks=follow_symlinks)
35+
# Fall back to os.walk for local paths
36+
else:
37+
for dirpath, dirnames, filenames in os.walk(root, followlinks=follow_symlinks):
38+
yield Path(dirpath), dirnames, filenames
39+
40+
2141
def as_path(path: str | PathT) -> PathT:
2242
"""Cast input to a `Path` type."""
2343
if isinstance(path, str):

0 commit comments

Comments
 (0)