pathflow-toolkit 0.1.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.
- path_file/__init__.py +223 -0
- path_file/__main__.py +6 -0
- path_file/_internal/__init__.py +87 -0
- path_file/_internal/_iterators.py +349 -0
- path_file/_internal/_log.py +153 -0
- path_file/_internal/_parsers.py +234 -0
- path_file/_internal/_safety.py +268 -0
- path_file/_version.py +1 -0
- path_file/actions.py +855 -0
- path_file/archive.py +447 -0
- path_file/chain.py +1028 -0
- path_file/cli.py +582 -0
- path_file/entries.py +459 -0
- path_file/exceptions.py +174 -0
- path_file/filters.py +654 -0
- path_file/hash.py +338 -0
- path_file/progress.py +321 -0
- path_file/py.typed +0 -0
- path_file/report.py +348 -0
- path_file/rules.py +481 -0
- path_file/search.py +472 -0
- path_file/sync.py +384 -0
- path_file/units.py +420 -0
- path_file/utils.py +509 -0
- path_file/watch.py +310 -0
- pathflow_toolkit-0.1.0.dist-info/METADATA +219 -0
- pathflow_toolkit-0.1.0.dist-info/RECORD +30 -0
- pathflow_toolkit-0.1.0.dist-info/WHEEL +4 -0
- pathflow_toolkit-0.1.0.dist-info/entry_points.txt +2 -0
- pathflow_toolkit-0.1.0.dist-info/licenses/LICENSE +21 -0
path_file/__init__.py
ADDED
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
"""
|
|
2
|
+
path_file - The elegant file automation library for Python.
|
|
3
|
+
|
|
4
|
+
A zero-dependency, chainable API for file operations with dry-run safety,
|
|
5
|
+
progress reporting, and declarative rules.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
from pathlib import Path
|
|
11
|
+
|
|
12
|
+
from path_file._internal._log import debug as debug
|
|
13
|
+
from path_file._internal._log import get_logger as get_logger
|
|
14
|
+
from path_file._internal._log import quiet as quiet
|
|
15
|
+
from path_file._internal._log import set_verbosity as set_verbosity
|
|
16
|
+
from path_file._internal._log import verbose as verbose
|
|
17
|
+
from path_file._version import __version__
|
|
18
|
+
from path_file.archive import create_archive as create_archive
|
|
19
|
+
from path_file.archive import detect_archive_type as detect_archive_type
|
|
20
|
+
from path_file.archive import list_archive as list_archive
|
|
21
|
+
from path_file.chain import FileChain
|
|
22
|
+
from path_file.entries import ActionResult, Entry, Report, SyncDiff, SyncResult
|
|
23
|
+
from path_file.exceptions import (
|
|
24
|
+
ArchiveError,
|
|
25
|
+
DestExistsError,
|
|
26
|
+
DryRunRequiredError,
|
|
27
|
+
FileNotFoundError,
|
|
28
|
+
HashError,
|
|
29
|
+
InvalidDurationError,
|
|
30
|
+
InvalidPatternError,
|
|
31
|
+
InvalidSizeError,
|
|
32
|
+
PathflowError,
|
|
33
|
+
PermissionError,
|
|
34
|
+
RulesParseError,
|
|
35
|
+
SyncConflictError,
|
|
36
|
+
WatchError,
|
|
37
|
+
)
|
|
38
|
+
from path_file.hash import find_duplicates as find_duplicates
|
|
39
|
+
from path_file.hash import hash_files as hash_files
|
|
40
|
+
from path_file.hash import verify_hashes as verify_hashes
|
|
41
|
+
from path_file.progress import ProgressBar as ProgressBar
|
|
42
|
+
from path_file.progress import Spinner as Spinner
|
|
43
|
+
from path_file.progress import create_progress as create_progress
|
|
44
|
+
from path_file.progress import terminal_width as terminal_width
|
|
45
|
+
from path_file.report import PathflowJSONEncoder as PathflowJSONEncoder
|
|
46
|
+
from path_file.report import build_report as build_report
|
|
47
|
+
from path_file.report import format_entry_table as format_entry_table
|
|
48
|
+
from path_file.report import format_extension_table as format_extension_table
|
|
49
|
+
from path_file.report import format_report as format_report
|
|
50
|
+
from path_file.report import format_size_bucket_table as format_size_bucket_table
|
|
51
|
+
from path_file.report import format_table as format_table
|
|
52
|
+
from path_file.report import to_csv as to_csv
|
|
53
|
+
from path_file.report import to_json as to_json
|
|
54
|
+
from path_file.rules import Rule, Rules
|
|
55
|
+
from path_file.search import is_text_file as is_text_file
|
|
56
|
+
from path_file.search import search_content as search_content
|
|
57
|
+
from path_file.search import search_regex as search_regex
|
|
58
|
+
from path_file.sync import Syncer, sync
|
|
59
|
+
from path_file.units import (
|
|
60
|
+
human_ago,
|
|
61
|
+
human_duration,
|
|
62
|
+
human_size,
|
|
63
|
+
parse_datetime,
|
|
64
|
+
parse_duration,
|
|
65
|
+
parse_size,
|
|
66
|
+
)
|
|
67
|
+
from path_file.utils import (
|
|
68
|
+
change_extension,
|
|
69
|
+
copy_file,
|
|
70
|
+
count_files,
|
|
71
|
+
delete_dir,
|
|
72
|
+
delete_file,
|
|
73
|
+
disk_usage,
|
|
74
|
+
duration,
|
|
75
|
+
expand,
|
|
76
|
+
extract_archive,
|
|
77
|
+
find_largest,
|
|
78
|
+
find_newest,
|
|
79
|
+
find_oldest,
|
|
80
|
+
free_space,
|
|
81
|
+
get_extension,
|
|
82
|
+
get_mime_type,
|
|
83
|
+
get_size,
|
|
84
|
+
get_size_human,
|
|
85
|
+
hash_file,
|
|
86
|
+
hsize,
|
|
87
|
+
is_binary,
|
|
88
|
+
is_symlink,
|
|
89
|
+
make_archive,
|
|
90
|
+
mkdir,
|
|
91
|
+
move_file,
|
|
92
|
+
read_bytes,
|
|
93
|
+
read_text,
|
|
94
|
+
readlink,
|
|
95
|
+
resolve_link,
|
|
96
|
+
run_command,
|
|
97
|
+
run_shell,
|
|
98
|
+
safe_join,
|
|
99
|
+
size,
|
|
100
|
+
strip_extension,
|
|
101
|
+
symlink,
|
|
102
|
+
tempdir,
|
|
103
|
+
touch,
|
|
104
|
+
verify_hash,
|
|
105
|
+
which,
|
|
106
|
+
write_bytes,
|
|
107
|
+
write_text,
|
|
108
|
+
)
|
|
109
|
+
from path_file.watch import Watcher, WatchEvent, watch
|
|
110
|
+
|
|
111
|
+
# ──────────────────────────────────────────────────────────────────────────────
|
|
112
|
+
# ENTRY POINTS
|
|
113
|
+
# ──────────────────────────────────────────────────────────────────────────────
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
def find(path: str | Path) -> FileChain:
|
|
117
|
+
"""
|
|
118
|
+
Create a FileChain from a directory path.
|
|
119
|
+
|
|
120
|
+
Args:
|
|
121
|
+
path: Directory to search (recursive by default)
|
|
122
|
+
|
|
123
|
+
Returns:
|
|
124
|
+
FileChain for chaining filters and actions
|
|
125
|
+
|
|
126
|
+
Example:
|
|
127
|
+
pf.find("~/Downloads").files().with_extension(".jpg").copy_to("~/Pictures")
|
|
128
|
+
"""
|
|
129
|
+
return FileChain(path)
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
def scan(path: str | Path) -> FileChain:
|
|
133
|
+
"""Alias for find()."""
|
|
134
|
+
return find(path)
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
def current() -> FileChain:
|
|
138
|
+
"""Create a FileChain from the current working directory."""
|
|
139
|
+
return FileChain(Path.cwd())
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
# ──────────────────────────────────────────────────────────────────────────────
|
|
143
|
+
# PUBLIC API
|
|
144
|
+
# ──────────────────────────────────────────────────────────────────────────────
|
|
145
|
+
|
|
146
|
+
__all__ = [
|
|
147
|
+
"ActionResult",
|
|
148
|
+
"ArchiveError",
|
|
149
|
+
"DestExistsError",
|
|
150
|
+
"DryRunRequiredError",
|
|
151
|
+
"Entry",
|
|
152
|
+
"FileChain",
|
|
153
|
+
"FileNotFoundError",
|
|
154
|
+
"HashError",
|
|
155
|
+
"InvalidDurationError",
|
|
156
|
+
"InvalidPatternError",
|
|
157
|
+
"InvalidSizeError",
|
|
158
|
+
"PathflowError",
|
|
159
|
+
"PermissionError",
|
|
160
|
+
"Report",
|
|
161
|
+
"Rule",
|
|
162
|
+
"Rules",
|
|
163
|
+
"RulesParseError",
|
|
164
|
+
"SyncConflictError",
|
|
165
|
+
"SyncDiff",
|
|
166
|
+
"SyncResult",
|
|
167
|
+
"Syncer",
|
|
168
|
+
"WatchError",
|
|
169
|
+
"WatchEvent",
|
|
170
|
+
"Watcher",
|
|
171
|
+
"__version__",
|
|
172
|
+
"change_extension",
|
|
173
|
+
"copy_file",
|
|
174
|
+
"count_files",
|
|
175
|
+
"current",
|
|
176
|
+
"delete_dir",
|
|
177
|
+
"delete_file",
|
|
178
|
+
"disk_usage",
|
|
179
|
+
"duration",
|
|
180
|
+
"expand",
|
|
181
|
+
"extract_archive",
|
|
182
|
+
"find",
|
|
183
|
+
"find_largest",
|
|
184
|
+
"find_newest",
|
|
185
|
+
"find_oldest",
|
|
186
|
+
"free_space",
|
|
187
|
+
"get_extension",
|
|
188
|
+
"get_mime_type",
|
|
189
|
+
"get_size",
|
|
190
|
+
"get_size_human",
|
|
191
|
+
"hash_file",
|
|
192
|
+
"hsize",
|
|
193
|
+
"human_ago",
|
|
194
|
+
"human_duration",
|
|
195
|
+
"human_size",
|
|
196
|
+
"is_binary",
|
|
197
|
+
"is_symlink",
|
|
198
|
+
"make_archive",
|
|
199
|
+
"mkdir",
|
|
200
|
+
"move_file",
|
|
201
|
+
"parse_datetime",
|
|
202
|
+
"parse_duration",
|
|
203
|
+
"parse_size",
|
|
204
|
+
"read_bytes",
|
|
205
|
+
"read_text",
|
|
206
|
+
"readlink",
|
|
207
|
+
"resolve_link",
|
|
208
|
+
"run_command",
|
|
209
|
+
"run_shell",
|
|
210
|
+
"safe_join",
|
|
211
|
+
"scan",
|
|
212
|
+
"size",
|
|
213
|
+
"strip_extension",
|
|
214
|
+
"symlink",
|
|
215
|
+
"sync",
|
|
216
|
+
"tempdir",
|
|
217
|
+
"touch",
|
|
218
|
+
"verify_hash",
|
|
219
|
+
"watch",
|
|
220
|
+
"which",
|
|
221
|
+
"write_bytes",
|
|
222
|
+
"write_text",
|
|
223
|
+
]
|
path_file/__main__.py
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
"""Internal modules for path_file - not part of public API."""
|
|
2
|
+
|
|
3
|
+
from path_file._internal._iterators import (
|
|
4
|
+
SymlinkTracker,
|
|
5
|
+
WalkConfig,
|
|
6
|
+
scan_dir,
|
|
7
|
+
walk,
|
|
8
|
+
walk_bfs,
|
|
9
|
+
walk_iterative,
|
|
10
|
+
walk_recursive,
|
|
11
|
+
)
|
|
12
|
+
from path_file._internal._log import (
|
|
13
|
+
debug,
|
|
14
|
+
get_logger,
|
|
15
|
+
log_debug,
|
|
16
|
+
log_error,
|
|
17
|
+
log_exception,
|
|
18
|
+
log_info,
|
|
19
|
+
log_warning,
|
|
20
|
+
quiet,
|
|
21
|
+
set_verbosity,
|
|
22
|
+
verbose,
|
|
23
|
+
with_debug,
|
|
24
|
+
with_quiet,
|
|
25
|
+
with_verbose,
|
|
26
|
+
)
|
|
27
|
+
from path_file._internal._parsers import (
|
|
28
|
+
DateTimeParser,
|
|
29
|
+
DurationParser,
|
|
30
|
+
PatternMatcher,
|
|
31
|
+
SizeParser,
|
|
32
|
+
_compile_regex,
|
|
33
|
+
make_glob_filter,
|
|
34
|
+
make_regex_filter,
|
|
35
|
+
make_size_filter,
|
|
36
|
+
make_time_filter,
|
|
37
|
+
)
|
|
38
|
+
from path_file._internal._safety import (
|
|
39
|
+
BackupManager,
|
|
40
|
+
confirm_destructive,
|
|
41
|
+
is_same_file,
|
|
42
|
+
is_subpath,
|
|
43
|
+
require_confirm_or_dry_run,
|
|
44
|
+
safe_path,
|
|
45
|
+
validate_dest,
|
|
46
|
+
validate_source,
|
|
47
|
+
)
|
|
48
|
+
|
|
49
|
+
__all__ = [
|
|
50
|
+
"BackupManager",
|
|
51
|
+
"DateTimeParser",
|
|
52
|
+
"DurationParser",
|
|
53
|
+
"PatternMatcher",
|
|
54
|
+
"SizeParser",
|
|
55
|
+
"SymlinkTracker",
|
|
56
|
+
"WalkConfig",
|
|
57
|
+
"_compile_regex",
|
|
58
|
+
"confirm_destructive",
|
|
59
|
+
"debug",
|
|
60
|
+
"get_logger",
|
|
61
|
+
"is_same_file",
|
|
62
|
+
"is_subpath",
|
|
63
|
+
"log_debug",
|
|
64
|
+
"log_error",
|
|
65
|
+
"log_exception",
|
|
66
|
+
"log_info",
|
|
67
|
+
"log_warning",
|
|
68
|
+
"make_glob_filter",
|
|
69
|
+
"make_regex_filter",
|
|
70
|
+
"make_size_filter",
|
|
71
|
+
"make_time_filter",
|
|
72
|
+
"quiet",
|
|
73
|
+
"require_confirm_or_dry_run",
|
|
74
|
+
"safe_path",
|
|
75
|
+
"scan_dir",
|
|
76
|
+
"set_verbosity",
|
|
77
|
+
"validate_dest",
|
|
78
|
+
"validate_source",
|
|
79
|
+
"verbose",
|
|
80
|
+
"walk",
|
|
81
|
+
"walk_bfs",
|
|
82
|
+
"walk_iterative",
|
|
83
|
+
"walk_recursive",
|
|
84
|
+
"with_debug",
|
|
85
|
+
"with_quiet",
|
|
86
|
+
"with_verbose",
|
|
87
|
+
]
|
|
@@ -0,0 +1,349 @@
|
|
|
1
|
+
"""High-performance filesystem iterators using os.scandir."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import contextlib
|
|
6
|
+
import os
|
|
7
|
+
import threading
|
|
8
|
+
from collections.abc import Callable, Iterator
|
|
9
|
+
from pathlib import Path
|
|
10
|
+
|
|
11
|
+
from path_file.entries import Entry
|
|
12
|
+
|
|
13
|
+
# ──────────────────────────────────────────────────────────────────────────────
|
|
14
|
+
# WALK CONFIGURATION
|
|
15
|
+
# ──────────────────────────────────────────────────────────────────────────────
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
class WalkConfig:
|
|
19
|
+
"""Configuration for directory walking."""
|
|
20
|
+
|
|
21
|
+
def __init__(
|
|
22
|
+
self,
|
|
23
|
+
follow_symlinks: bool = False,
|
|
24
|
+
max_depth: int | None = None,
|
|
25
|
+
min_depth: int = 0,
|
|
26
|
+
ignore_permission_errors: bool = True,
|
|
27
|
+
on_error: Callable[[str, OSError], None] | None = None,
|
|
28
|
+
) -> None:
|
|
29
|
+
self.follow_symlinks = follow_symlinks
|
|
30
|
+
self.max_depth = max_depth
|
|
31
|
+
self.min_depth = min_depth
|
|
32
|
+
self.ignore_permission_errors = ignore_permission_errors
|
|
33
|
+
self.on_error = on_error
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
# ──────────────────────────────────────────────────────────────────────────────
|
|
37
|
+
# SYMLINK CYCLE DETECTION
|
|
38
|
+
# ──────────────────────────────────────────────────────────────────────────────
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
class SymlinkTracker:
|
|
42
|
+
"""Track visited symlinks to detect cycles."""
|
|
43
|
+
|
|
44
|
+
def __init__(self) -> None:
|
|
45
|
+
self._visited: set[tuple[int, int]] = set() # (st_dev, st_ino)
|
|
46
|
+
self._lock = threading.Lock()
|
|
47
|
+
|
|
48
|
+
def visit(self, path: Path) -> bool:
|
|
49
|
+
"""
|
|
50
|
+
Mark path as visited.
|
|
51
|
+
Returns True if this is a new visit, False if cycle detected.
|
|
52
|
+
"""
|
|
53
|
+
try:
|
|
54
|
+
st = path.stat()
|
|
55
|
+
key = (st.st_dev, st.st_ino)
|
|
56
|
+
with self._lock:
|
|
57
|
+
if key in self._visited:
|
|
58
|
+
return False
|
|
59
|
+
self._visited.add(key)
|
|
60
|
+
return True
|
|
61
|
+
except (OSError, PermissionError):
|
|
62
|
+
return True # Can't stat, assume safe
|
|
63
|
+
|
|
64
|
+
def clear(self) -> None:
|
|
65
|
+
"""Clear visited set."""
|
|
66
|
+
with self._lock:
|
|
67
|
+
self._visited.clear()
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
# ──────────────────────────────────────────────────────────────────────────────
|
|
71
|
+
# RECURSIVE WALK (DFS) - MAIN IMPLEMENTATION
|
|
72
|
+
# ──────────────────────────────────────────────────────────────────────────────
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
def walk_recursive(
|
|
76
|
+
root: Path,
|
|
77
|
+
config: WalkConfig | None = None,
|
|
78
|
+
_depth: int = 0,
|
|
79
|
+
_tracker: SymlinkTracker | None = None,
|
|
80
|
+
) -> Iterator[Entry]:
|
|
81
|
+
"""
|
|
82
|
+
Depth-first recursive walk using os.scandir for performance.
|
|
83
|
+
|
|
84
|
+
Yields Entry objects for each filesystem entry.
|
|
85
|
+
"""
|
|
86
|
+
config = config or WalkConfig()
|
|
87
|
+
root = Path(root).resolve()
|
|
88
|
+
|
|
89
|
+
# Single file as root: yield it directly
|
|
90
|
+
if root.is_file():
|
|
91
|
+
if _depth >= config.min_depth:
|
|
92
|
+
with contextlib.suppress(OSError, PermissionError):
|
|
93
|
+
yield Entry.from_path(root)
|
|
94
|
+
return
|
|
95
|
+
|
|
96
|
+
if config.max_depth is not None and _depth > config.max_depth:
|
|
97
|
+
return
|
|
98
|
+
|
|
99
|
+
if _tracker is None and config.follow_symlinks:
|
|
100
|
+
_tracker = SymlinkTracker()
|
|
101
|
+
|
|
102
|
+
try:
|
|
103
|
+
with os.scandir(root) as it:
|
|
104
|
+
for entry in it:
|
|
105
|
+
try:
|
|
106
|
+
yield from _process_entry(entry, _depth, config, _tracker)
|
|
107
|
+
except (PermissionError, OSError) as e:
|
|
108
|
+
if not config.ignore_permission_errors:
|
|
109
|
+
raise
|
|
110
|
+
if config.on_error:
|
|
111
|
+
config.on_error(entry.path, e)
|
|
112
|
+
except (PermissionError, OSError) as e:
|
|
113
|
+
if not config.ignore_permission_errors:
|
|
114
|
+
raise
|
|
115
|
+
if config.on_error:
|
|
116
|
+
config.on_error(str(root), e)
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
def _process_entry(
|
|
120
|
+
entry: os.DirEntry[str],
|
|
121
|
+
depth: int,
|
|
122
|
+
config: WalkConfig,
|
|
123
|
+
tracker: SymlinkTracker | None,
|
|
124
|
+
) -> Iterator[Entry]:
|
|
125
|
+
"""Process a single directory entry."""
|
|
126
|
+
path = Path(entry.path)
|
|
127
|
+
|
|
128
|
+
# Check if we should follow symlinks
|
|
129
|
+
is_symlink = entry.is_symlink()
|
|
130
|
+
|
|
131
|
+
if is_symlink and config.follow_symlinks:
|
|
132
|
+
if tracker and not tracker.visit(path):
|
|
133
|
+
# Cycle detected
|
|
134
|
+
return
|
|
135
|
+
# Yield the symlink entry itself before following it
|
|
136
|
+
if depth >= config.min_depth:
|
|
137
|
+
try:
|
|
138
|
+
yield Entry.from_stat(path, entry.stat(follow_symlinks=False))
|
|
139
|
+
except (OSError, PermissionError):
|
|
140
|
+
with contextlib.suppress(OSError, PermissionError):
|
|
141
|
+
yield Entry.from_path(path)
|
|
142
|
+
# Follow the symlink - recurse if it's a directory
|
|
143
|
+
if entry.is_dir(follow_symlinks=True):
|
|
144
|
+
yield from walk_recursive(path, config, depth + 1, tracker)
|
|
145
|
+
return
|
|
146
|
+
|
|
147
|
+
# Yield current entry if at minimum depth
|
|
148
|
+
if depth >= config.min_depth:
|
|
149
|
+
try:
|
|
150
|
+
yield Entry.from_stat(path, entry.stat(follow_symlinks=False))
|
|
151
|
+
except (OSError, PermissionError):
|
|
152
|
+
# Fallback to basic Entry
|
|
153
|
+
with contextlib.suppress(OSError, PermissionError):
|
|
154
|
+
yield Entry.from_path(path)
|
|
155
|
+
|
|
156
|
+
# Recurse into subdirectories
|
|
157
|
+
if entry.is_dir(follow_symlinks=False):
|
|
158
|
+
if is_symlink and not config.follow_symlinks:
|
|
159
|
+
return # Don't follow symlinks to directories unless configured
|
|
160
|
+
yield from walk_recursive(path, config, depth + 1, tracker)
|
|
161
|
+
|
|
162
|
+
|
|
163
|
+
# ──────────────────────────────────────────────────────────────────────────────
|
|
164
|
+
# ITERATIVE WALK (NON-RECURSIVE) - FOR VERY DEEP TREES
|
|
165
|
+
# ──────────────────────────────────────────────────────────────────────────────
|
|
166
|
+
|
|
167
|
+
|
|
168
|
+
def walk_iterative(
|
|
169
|
+
root: Path,
|
|
170
|
+
config: WalkConfig | None = None,
|
|
171
|
+
) -> Iterator[Entry]:
|
|
172
|
+
"""
|
|
173
|
+
Non-recursive iterative walk using explicit stack.
|
|
174
|
+
Better for very deep directory trees to avoid recursion limits.
|
|
175
|
+
"""
|
|
176
|
+
config = config or WalkConfig()
|
|
177
|
+
root = Path(root).resolve()
|
|
178
|
+
|
|
179
|
+
stack = [(root, 0)]
|
|
180
|
+
tracker = SymlinkTracker() if config.follow_symlinks else None
|
|
181
|
+
|
|
182
|
+
while stack:
|
|
183
|
+
current_path, depth = stack.pop()
|
|
184
|
+
|
|
185
|
+
if config.max_depth is not None and depth > config.max_depth:
|
|
186
|
+
continue
|
|
187
|
+
|
|
188
|
+
try:
|
|
189
|
+
with os.scandir(current_path) as it:
|
|
190
|
+
entries = list(it)
|
|
191
|
+
except (PermissionError, OSError) as e:
|
|
192
|
+
if not config.ignore_permission_errors:
|
|
193
|
+
raise
|
|
194
|
+
if config.on_error:
|
|
195
|
+
config.on_error(str(current_path), e)
|
|
196
|
+
continue
|
|
197
|
+
|
|
198
|
+
# Process in reverse for DFS-like order (stack is LIFO)
|
|
199
|
+
for entry in reversed(entries):
|
|
200
|
+
path = Path(entry.path)
|
|
201
|
+
is_symlink = entry.is_symlink()
|
|
202
|
+
|
|
203
|
+
if is_symlink and config.follow_symlinks:
|
|
204
|
+
if tracker and not tracker.visit(path):
|
|
205
|
+
continue
|
|
206
|
+
if entry.is_dir(follow_symlinks=True):
|
|
207
|
+
stack.append((path, depth + 1))
|
|
208
|
+
continue
|
|
209
|
+
|
|
210
|
+
if depth >= config.min_depth:
|
|
211
|
+
try:
|
|
212
|
+
yield Entry.from_stat(path, entry.stat(follow_symlinks=False))
|
|
213
|
+
except (OSError, PermissionError):
|
|
214
|
+
with contextlib.suppress(OSError, PermissionError):
|
|
215
|
+
yield Entry.from_path(path)
|
|
216
|
+
|
|
217
|
+
if entry.is_dir(follow_symlinks=False):
|
|
218
|
+
if is_symlink and not config.follow_symlinks:
|
|
219
|
+
continue
|
|
220
|
+
stack.append((path, depth + 1))
|
|
221
|
+
|
|
222
|
+
|
|
223
|
+
# ──────────────────────────────────────────────────────────────────────────────
|
|
224
|
+
# BREADTH-FIRST WALK
|
|
225
|
+
# ──────────────────────────────────────────────────────────────────────────────
|
|
226
|
+
|
|
227
|
+
|
|
228
|
+
def walk_bfs(
|
|
229
|
+
root: Path,
|
|
230
|
+
config: WalkConfig | None = None,
|
|
231
|
+
) -> Iterator[Entry]:
|
|
232
|
+
"""
|
|
233
|
+
Breadth-first walk using queue.
|
|
234
|
+
Processes directories level by level.
|
|
235
|
+
"""
|
|
236
|
+
from collections import deque
|
|
237
|
+
|
|
238
|
+
config = config or WalkConfig()
|
|
239
|
+
root = Path(root).resolve()
|
|
240
|
+
|
|
241
|
+
queue = deque([(root, 0)])
|
|
242
|
+
tracker = SymlinkTracker() if config.follow_symlinks else None
|
|
243
|
+
|
|
244
|
+
while queue:
|
|
245
|
+
current_path, depth = queue.popleft()
|
|
246
|
+
|
|
247
|
+
if config.max_depth is not None and depth > config.max_depth:
|
|
248
|
+
continue
|
|
249
|
+
|
|
250
|
+
try:
|
|
251
|
+
with os.scandir(current_path) as it:
|
|
252
|
+
entries = list(it)
|
|
253
|
+
except (PermissionError, OSError) as e:
|
|
254
|
+
if not config.ignore_permission_errors:
|
|
255
|
+
raise
|
|
256
|
+
if config.on_error:
|
|
257
|
+
config.on_error(str(current_path), e)
|
|
258
|
+
continue
|
|
259
|
+
|
|
260
|
+
# First yield all entries at this level
|
|
261
|
+
for entry in entries:
|
|
262
|
+
path = Path(entry.path)
|
|
263
|
+
is_symlink = entry.is_symlink()
|
|
264
|
+
|
|
265
|
+
if is_symlink and config.follow_symlinks and tracker and not tracker.visit(path):
|
|
266
|
+
continue
|
|
267
|
+
|
|
268
|
+
if depth >= config.min_depth:
|
|
269
|
+
try:
|
|
270
|
+
yield Entry.from_stat(path, entry.stat(follow_symlinks=False))
|
|
271
|
+
except (OSError, PermissionError):
|
|
272
|
+
with contextlib.suppress(OSError, PermissionError):
|
|
273
|
+
yield Entry.from_path(path)
|
|
274
|
+
|
|
275
|
+
# Then queue subdirectories for next level
|
|
276
|
+
for entry in entries:
|
|
277
|
+
if entry.is_dir(follow_symlinks=False):
|
|
278
|
+
is_symlink = entry.is_symlink()
|
|
279
|
+
if is_symlink and not config.follow_symlinks:
|
|
280
|
+
continue
|
|
281
|
+
path = Path(entry.path)
|
|
282
|
+
queue.append((path, depth + 1))
|
|
283
|
+
|
|
284
|
+
|
|
285
|
+
# ──────────────────────────────────────────────────────────────────────────────
|
|
286
|
+
# CONVENIENCE FUNCTIONS
|
|
287
|
+
# ──────────────────────────────────────────────────────────────────────────────
|
|
288
|
+
|
|
289
|
+
|
|
290
|
+
def walk(
|
|
291
|
+
root: str | Path,
|
|
292
|
+
follow_symlinks: bool = False,
|
|
293
|
+
max_depth: int | None = None,
|
|
294
|
+
min_depth: int = 0,
|
|
295
|
+
breadth_first: bool = False,
|
|
296
|
+
iterative: bool = False,
|
|
297
|
+
config: WalkConfig | None = None,
|
|
298
|
+
) -> Iterator[Entry]:
|
|
299
|
+
"""
|
|
300
|
+
Main entry point for walking a directory tree.
|
|
301
|
+
|
|
302
|
+
Args:
|
|
303
|
+
root: Root directory to walk
|
|
304
|
+
follow_symlinks: Follow symbolic links to directories
|
|
305
|
+
max_depth: Maximum depth to traverse (None = unlimited)
|
|
306
|
+
min_depth: Minimum depth before yielding entries
|
|
307
|
+
breadth_first: Use BFS instead of DFS
|
|
308
|
+
iterative: Use iterative (non-recursive) implementation
|
|
309
|
+
config: Optional WalkConfig object (overrides other params)
|
|
310
|
+
|
|
311
|
+
Yields:
|
|
312
|
+
Entry objects for each file/directory
|
|
313
|
+
"""
|
|
314
|
+
if config is None:
|
|
315
|
+
config = WalkConfig(
|
|
316
|
+
follow_symlinks=follow_symlinks,
|
|
317
|
+
max_depth=max_depth,
|
|
318
|
+
min_depth=min_depth,
|
|
319
|
+
)
|
|
320
|
+
|
|
321
|
+
if breadth_first:
|
|
322
|
+
return walk_bfs(Path(root), config)
|
|
323
|
+
elif iterative:
|
|
324
|
+
return walk_iterative(Path(root), config)
|
|
325
|
+
else:
|
|
326
|
+
return walk_recursive(Path(root), config)
|
|
327
|
+
|
|
328
|
+
|
|
329
|
+
# ──────────────────────────────────────────────────────────────────────────────
|
|
330
|
+
# FAST SCOPED WALK (for single directory, no recursion)
|
|
331
|
+
# ──────────────────────────────────────────────────────────────────────────────
|
|
332
|
+
|
|
333
|
+
|
|
334
|
+
def scan_dir(path: str | Path) -> Iterator[Entry]:
|
|
335
|
+
"""
|
|
336
|
+
Fast non-recursive scan of a single directory.
|
|
337
|
+
Uses os.scandir directly, no recursion.
|
|
338
|
+
"""
|
|
339
|
+
path = Path(path).resolve()
|
|
340
|
+
try:
|
|
341
|
+
with os.scandir(path) as it:
|
|
342
|
+
for entry in it:
|
|
343
|
+
try:
|
|
344
|
+
yield Entry.from_stat(Path(entry.path), entry.stat(follow_symlinks=False))
|
|
345
|
+
except (OSError, PermissionError):
|
|
346
|
+
with contextlib.suppress(OSError, PermissionError):
|
|
347
|
+
yield Entry.from_path(entry.path)
|
|
348
|
+
except (PermissionError, OSError):
|
|
349
|
+
return # Silently return empty iterator
|