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/search.py
ADDED
|
@@ -0,0 +1,472 @@
|
|
|
1
|
+
"""Content search utilities for finding text/regex in files."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import mmap
|
|
6
|
+
from collections.abc import Iterator
|
|
7
|
+
from pathlib import Path
|
|
8
|
+
from typing import Any
|
|
9
|
+
|
|
10
|
+
from path_file._internal._log import get_logger, log_debug
|
|
11
|
+
from path_file._internal._parsers import _compile_regex
|
|
12
|
+
from path_file.entries import Entry
|
|
13
|
+
|
|
14
|
+
logger = get_logger(__name__)
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
# ──────────────────────────────────────────────────────────────────────────────
|
|
18
|
+
# TEXT FILE DETECTION
|
|
19
|
+
# ──────────────────────────────────────────────────────────────────────────────
|
|
20
|
+
|
|
21
|
+
_TEXT_EXTENSIONS = {
|
|
22
|
+
# Code
|
|
23
|
+
".py",
|
|
24
|
+
".js",
|
|
25
|
+
".ts",
|
|
26
|
+
".jsx",
|
|
27
|
+
".tsx",
|
|
28
|
+
".java",
|
|
29
|
+
".c",
|
|
30
|
+
".cpp",
|
|
31
|
+
".h",
|
|
32
|
+
".hpp",
|
|
33
|
+
".cs",
|
|
34
|
+
".go",
|
|
35
|
+
".rs",
|
|
36
|
+
".rb",
|
|
37
|
+
".php",
|
|
38
|
+
".swift",
|
|
39
|
+
".kt",
|
|
40
|
+
".scala",
|
|
41
|
+
".clj",
|
|
42
|
+
".sh",
|
|
43
|
+
".bash",
|
|
44
|
+
".zsh",
|
|
45
|
+
".fish",
|
|
46
|
+
".ps1",
|
|
47
|
+
".bat",
|
|
48
|
+
".cmd",
|
|
49
|
+
".html",
|
|
50
|
+
".htm",
|
|
51
|
+
".css",
|
|
52
|
+
".scss",
|
|
53
|
+
".sass",
|
|
54
|
+
".less",
|
|
55
|
+
".xml",
|
|
56
|
+
".json",
|
|
57
|
+
".yaml",
|
|
58
|
+
".yml",
|
|
59
|
+
".toml",
|
|
60
|
+
".ini",
|
|
61
|
+
".cfg",
|
|
62
|
+
".conf",
|
|
63
|
+
".config",
|
|
64
|
+
".sql",
|
|
65
|
+
".r",
|
|
66
|
+
".m",
|
|
67
|
+
".pl",
|
|
68
|
+
".lua",
|
|
69
|
+
".vim",
|
|
70
|
+
".elm",
|
|
71
|
+
".dart",
|
|
72
|
+
".ex",
|
|
73
|
+
".exs",
|
|
74
|
+
# Text
|
|
75
|
+
".txt",
|
|
76
|
+
".md",
|
|
77
|
+
".rst",
|
|
78
|
+
".adoc",
|
|
79
|
+
".tex",
|
|
80
|
+
".bib",
|
|
81
|
+
".csv",
|
|
82
|
+
".tsv",
|
|
83
|
+
".log",
|
|
84
|
+
".out",
|
|
85
|
+
".err",
|
|
86
|
+
".trace",
|
|
87
|
+
".dump",
|
|
88
|
+
# Config
|
|
89
|
+
".gitignore",
|
|
90
|
+
".gitattributes",
|
|
91
|
+
".dockerignore",
|
|
92
|
+
".editorconfig",
|
|
93
|
+
# Docs
|
|
94
|
+
".doc",
|
|
95
|
+
".docx",
|
|
96
|
+
".odt",
|
|
97
|
+
".rtf", # These are binary but we'll try
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
_BINARY_EXTENSIONS = {
|
|
101
|
+
".exe",
|
|
102
|
+
".dll",
|
|
103
|
+
".so",
|
|
104
|
+
".dylib",
|
|
105
|
+
".bin",
|
|
106
|
+
".dat",
|
|
107
|
+
".db",
|
|
108
|
+
".sqlite",
|
|
109
|
+
".jpg",
|
|
110
|
+
".jpeg",
|
|
111
|
+
".png",
|
|
112
|
+
".gif",
|
|
113
|
+
".bmp",
|
|
114
|
+
".tiff",
|
|
115
|
+
".webp",
|
|
116
|
+
".ico",
|
|
117
|
+
".mp3",
|
|
118
|
+
".mp4",
|
|
119
|
+
".avi",
|
|
120
|
+
".mkv",
|
|
121
|
+
".mov",
|
|
122
|
+
".wav",
|
|
123
|
+
".flac",
|
|
124
|
+
".ogg",
|
|
125
|
+
".zip",
|
|
126
|
+
".tar",
|
|
127
|
+
".gz",
|
|
128
|
+
".bz2",
|
|
129
|
+
".xz",
|
|
130
|
+
".7z",
|
|
131
|
+
".rar",
|
|
132
|
+
".jar",
|
|
133
|
+
".war",
|
|
134
|
+
".pdf",
|
|
135
|
+
".doc",
|
|
136
|
+
".docx",
|
|
137
|
+
".xls",
|
|
138
|
+
".xlsx",
|
|
139
|
+
".ppt",
|
|
140
|
+
".pptx",
|
|
141
|
+
".ttf",
|
|
142
|
+
".otf",
|
|
143
|
+
".woff",
|
|
144
|
+
".woff2",
|
|
145
|
+
".eot",
|
|
146
|
+
".class",
|
|
147
|
+
".pyc",
|
|
148
|
+
".pyo",
|
|
149
|
+
".pyd",
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
_MAX_TEXT_FILE_SIZE = 100 * 1024 * 1024 # 100 MB
|
|
153
|
+
|
|
154
|
+
|
|
155
|
+
def is_text_file(path: Path, check_content: bool = True) -> bool:
|
|
156
|
+
"""
|
|
157
|
+
Determine if a file is likely a text file.
|
|
158
|
+
|
|
159
|
+
Args:
|
|
160
|
+
path: File path
|
|
161
|
+
check_content: If True, check first bytes for null characters
|
|
162
|
+
|
|
163
|
+
Returns:
|
|
164
|
+
True if likely text file
|
|
165
|
+
"""
|
|
166
|
+
# Check extension first
|
|
167
|
+
suffix = path.suffix.lower()
|
|
168
|
+
if suffix in _TEXT_EXTENSIONS:
|
|
169
|
+
return True
|
|
170
|
+
if suffix in _BINARY_EXTENSIONS:
|
|
171
|
+
# For empty files, still check content - empty files are text
|
|
172
|
+
try:
|
|
173
|
+
if path.stat().st_size == 0:
|
|
174
|
+
return True
|
|
175
|
+
except (OSError, PermissionError):
|
|
176
|
+
pass
|
|
177
|
+
return False
|
|
178
|
+
|
|
179
|
+
# Check filename (no extension)
|
|
180
|
+
if not suffix and path.name in {
|
|
181
|
+
"README",
|
|
182
|
+
"LICENSE",
|
|
183
|
+
"CHANGELOG",
|
|
184
|
+
"CONTRIBUTING",
|
|
185
|
+
"AUTHORS",
|
|
186
|
+
"Makefile",
|
|
187
|
+
"CMakeLists.txt",
|
|
188
|
+
"Dockerfile",
|
|
189
|
+
"Vagrantfile",
|
|
190
|
+
".gitignore",
|
|
191
|
+
".gitattributes",
|
|
192
|
+
".dockerignore",
|
|
193
|
+
}:
|
|
194
|
+
return True
|
|
195
|
+
|
|
196
|
+
if not check_content:
|
|
197
|
+
return False
|
|
198
|
+
|
|
199
|
+
# Check content for null bytes (binary indicator)
|
|
200
|
+
try:
|
|
201
|
+
with path.open("rb") as f:
|
|
202
|
+
chunk = f.read(8192)
|
|
203
|
+
if b"\x00" in chunk:
|
|
204
|
+
return False
|
|
205
|
+
# Check if mostly printable ASCII/UTF-8
|
|
206
|
+
text_chars = sum(1 for b in chunk if 32 <= b <= 126 or b in (9, 10, 13))
|
|
207
|
+
if len(chunk) > 0 and text_chars / len(chunk) < 0.7:
|
|
208
|
+
return False
|
|
209
|
+
except (OSError, PermissionError):
|
|
210
|
+
return False
|
|
211
|
+
|
|
212
|
+
return True
|
|
213
|
+
|
|
214
|
+
|
|
215
|
+
def get_encoding(path: Path) -> str:
|
|
216
|
+
"""
|
|
217
|
+
Detect file encoding.
|
|
218
|
+
|
|
219
|
+
Tries: utf-8, utf-16, latin-1
|
|
220
|
+
"""
|
|
221
|
+
# Try UTF-8 first
|
|
222
|
+
try:
|
|
223
|
+
path.read_text(encoding="utf-8")
|
|
224
|
+
return "utf-8"
|
|
225
|
+
except UnicodeDecodeError:
|
|
226
|
+
pass
|
|
227
|
+
|
|
228
|
+
# Try UTF-16
|
|
229
|
+
try:
|
|
230
|
+
path.read_text(encoding="utf-16")
|
|
231
|
+
return "utf-16"
|
|
232
|
+
except UnicodeDecodeError:
|
|
233
|
+
pass
|
|
234
|
+
|
|
235
|
+
# Fallback to latin-1 (never fails)
|
|
236
|
+
return "latin-1"
|
|
237
|
+
|
|
238
|
+
|
|
239
|
+
# ──────────────────────────────────────────────────────────────────────────────
|
|
240
|
+
# CONTENT SEARCH
|
|
241
|
+
# ──────────────────────────────────────────────────────────────────────────────
|
|
242
|
+
|
|
243
|
+
|
|
244
|
+
def search_content(
|
|
245
|
+
entries: list[Entry],
|
|
246
|
+
text: str,
|
|
247
|
+
encoding: str = "utf-8",
|
|
248
|
+
case_sensitive: bool = False,
|
|
249
|
+
max_file_size: int = _MAX_TEXT_FILE_SIZE,
|
|
250
|
+
) -> list[tuple[Entry, list[int]]]:
|
|
251
|
+
"""
|
|
252
|
+
Search for text in files.
|
|
253
|
+
|
|
254
|
+
Args:
|
|
255
|
+
entries: List of entries to search
|
|
256
|
+
text: Text to search for
|
|
257
|
+
encoding: File encoding
|
|
258
|
+
case_sensitive: Case sensitive search
|
|
259
|
+
max_file_size: Skip files larger than this
|
|
260
|
+
|
|
261
|
+
Returns:
|
|
262
|
+
List of (Entry, [line_numbers]) tuples
|
|
263
|
+
"""
|
|
264
|
+
results = []
|
|
265
|
+
search_text = text if case_sensitive else text.lower()
|
|
266
|
+
|
|
267
|
+
for entry in entries:
|
|
268
|
+
if not entry.is_file:
|
|
269
|
+
continue
|
|
270
|
+
if entry.size > max_file_size:
|
|
271
|
+
log_debug(f"Skipping large file: {entry.path} ({entry.size_human})")
|
|
272
|
+
continue
|
|
273
|
+
if not is_text_file(entry.path):
|
|
274
|
+
continue
|
|
275
|
+
|
|
276
|
+
try:
|
|
277
|
+
content = entry.read_text(encoding)
|
|
278
|
+
search_content = content if case_sensitive else content.lower()
|
|
279
|
+
|
|
280
|
+
line_numbers = []
|
|
281
|
+
for i, line in enumerate(search_content.splitlines(), 1):
|
|
282
|
+
if search_text in line:
|
|
283
|
+
line_numbers.append(i)
|
|
284
|
+
|
|
285
|
+
if line_numbers:
|
|
286
|
+
results.append((entry, line_numbers))
|
|
287
|
+
|
|
288
|
+
except (UnicodeDecodeError, OSError, PermissionError) as e:
|
|
289
|
+
log_debug(f"Error reading {entry.path}: {e}")
|
|
290
|
+
continue
|
|
291
|
+
|
|
292
|
+
return results
|
|
293
|
+
|
|
294
|
+
|
|
295
|
+
def search_regex(
|
|
296
|
+
entries: list[Entry],
|
|
297
|
+
pattern: str,
|
|
298
|
+
encoding: str = "utf-8",
|
|
299
|
+
flags: int = 0,
|
|
300
|
+
max_file_size: int = _MAX_TEXT_FILE_SIZE,
|
|
301
|
+
) -> list[tuple[Entry, list[tuple[int, str | tuple[str, ...]]]]]:
|
|
302
|
+
"""
|
|
303
|
+
Search for regex pattern in files.
|
|
304
|
+
|
|
305
|
+
Args:
|
|
306
|
+
entries: List of entries to search
|
|
307
|
+
pattern: Regex pattern
|
|
308
|
+
encoding: File encoding
|
|
309
|
+
flags: Regex flags (re.IGNORECASE, etc.)
|
|
310
|
+
max_file_size: Skip files larger than this
|
|
311
|
+
|
|
312
|
+
Returns:
|
|
313
|
+
List of (Entry, [(line_num, matched_text)]) tuples
|
|
314
|
+
"""
|
|
315
|
+
compiled = _compile_regex(pattern, flags)
|
|
316
|
+
results = []
|
|
317
|
+
|
|
318
|
+
for entry in entries:
|
|
319
|
+
if not entry.is_file:
|
|
320
|
+
continue
|
|
321
|
+
if entry.size > max_file_size:
|
|
322
|
+
continue
|
|
323
|
+
if not is_text_file(entry.path):
|
|
324
|
+
continue
|
|
325
|
+
|
|
326
|
+
try:
|
|
327
|
+
content = entry.read_text(encoding)
|
|
328
|
+
matches: list[tuple[int, str | tuple[str, ...]]] = []
|
|
329
|
+
for i, line in enumerate(content.splitlines(), 1):
|
|
330
|
+
for match in compiled.finditer(line):
|
|
331
|
+
# Return captured groups if any, otherwise full match
|
|
332
|
+
if match.groups():
|
|
333
|
+
matches.append((i, match.groups()))
|
|
334
|
+
else:
|
|
335
|
+
matches.append((i, match.group()))
|
|
336
|
+
if matches:
|
|
337
|
+
results.append((entry, matches))
|
|
338
|
+
except (UnicodeDecodeError, OSError, PermissionError) as e:
|
|
339
|
+
log_debug(f"Error reading {entry.path}: {e}")
|
|
340
|
+
continue
|
|
341
|
+
|
|
342
|
+
return results
|
|
343
|
+
|
|
344
|
+
|
|
345
|
+
def search_bytes(
|
|
346
|
+
entries: list[Entry],
|
|
347
|
+
pattern: bytes,
|
|
348
|
+
max_file_size: int = _MAX_TEXT_FILE_SIZE,
|
|
349
|
+
) -> list[tuple[Entry, list[int]]]:
|
|
350
|
+
"""
|
|
351
|
+
Search for byte pattern in files (works on binary files too).
|
|
352
|
+
|
|
353
|
+
Args:
|
|
354
|
+
entries: List of entries to search
|
|
355
|
+
pattern: Byte pattern to search for
|
|
356
|
+
max_file_size: Skip files larger than this
|
|
357
|
+
|
|
358
|
+
Returns:
|
|
359
|
+
List of (Entry, [byte_offsets]) tuples
|
|
360
|
+
"""
|
|
361
|
+
results = []
|
|
362
|
+
|
|
363
|
+
for entry in entries:
|
|
364
|
+
if not entry.is_file:
|
|
365
|
+
continue
|
|
366
|
+
if entry.size > max_file_size:
|
|
367
|
+
continue
|
|
368
|
+
|
|
369
|
+
try:
|
|
370
|
+
# Use mmap for efficient searching
|
|
371
|
+
with (
|
|
372
|
+
entry.path.open("rb") as f,
|
|
373
|
+
mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_READ) as mm,
|
|
374
|
+
):
|
|
375
|
+
offsets = []
|
|
376
|
+
start = 0
|
|
377
|
+
while True:
|
|
378
|
+
pos = mm.find(pattern, start)
|
|
379
|
+
if pos == -1:
|
|
380
|
+
break
|
|
381
|
+
offsets.append(pos)
|
|
382
|
+
start = pos + 1
|
|
383
|
+
if offsets:
|
|
384
|
+
results.append((entry, offsets))
|
|
385
|
+
except (OSError, PermissionError, ValueError) as e:
|
|
386
|
+
log_debug(f"Error searching {entry.path}: {e}")
|
|
387
|
+
continue
|
|
388
|
+
|
|
389
|
+
return results
|
|
390
|
+
|
|
391
|
+
|
|
392
|
+
# ──────────────────────────────────────────────────────────────────────────────
|
|
393
|
+
# STREAMING SEARCH (for very large files)
|
|
394
|
+
# ──────────────────────────────────────────────────────────────────────────────
|
|
395
|
+
|
|
396
|
+
|
|
397
|
+
def search_streaming(
|
|
398
|
+
entries: list[Entry],
|
|
399
|
+
text: str,
|
|
400
|
+
encoding: str = "utf-8",
|
|
401
|
+
chunk_size: int = 65536,
|
|
402
|
+
) -> Iterator[tuple[Entry, int, str]]:
|
|
403
|
+
"""
|
|
404
|
+
Stream search through large files without loading entirely into memory.
|
|
405
|
+
|
|
406
|
+
Yields:
|
|
407
|
+
(Entry, line_number, matching_line)
|
|
408
|
+
"""
|
|
409
|
+
search_bytes = text.encode(encoding)
|
|
410
|
+
|
|
411
|
+
for entry in entries:
|
|
412
|
+
if not entry.is_file:
|
|
413
|
+
continue
|
|
414
|
+
if not is_text_file(entry.path, check_content=False):
|
|
415
|
+
continue
|
|
416
|
+
|
|
417
|
+
try:
|
|
418
|
+
with entry.path.open("rb") as f:
|
|
419
|
+
line_num = 0
|
|
420
|
+
buffer = b""
|
|
421
|
+
while True:
|
|
422
|
+
chunk = f.read(chunk_size)
|
|
423
|
+
if not chunk:
|
|
424
|
+
# Process remaining buffer
|
|
425
|
+
if buffer:
|
|
426
|
+
line_num += 1
|
|
427
|
+
try:
|
|
428
|
+
line = buffer.decode(encoding, errors="replace")
|
|
429
|
+
if search_bytes in buffer:
|
|
430
|
+
yield entry, line_num, line
|
|
431
|
+
except UnicodeDecodeError:
|
|
432
|
+
pass
|
|
433
|
+
break
|
|
434
|
+
|
|
435
|
+
buffer += chunk
|
|
436
|
+
lines = buffer.split(b"\n")
|
|
437
|
+
buffer = lines[-1] # Keep incomplete line
|
|
438
|
+
|
|
439
|
+
for line_bytes in lines[:-1]:
|
|
440
|
+
line_num += 1
|
|
441
|
+
if search_bytes in line_bytes:
|
|
442
|
+
try:
|
|
443
|
+
line = line_bytes.decode(encoding, errors="replace")
|
|
444
|
+
yield entry, line_num, line
|
|
445
|
+
except UnicodeDecodeError:
|
|
446
|
+
pass
|
|
447
|
+
except (OSError, PermissionError) as e:
|
|
448
|
+
log_debug(f"Error streaming {entry.path}: {e}")
|
|
449
|
+
continue
|
|
450
|
+
|
|
451
|
+
|
|
452
|
+
# ──────────────────────────────────────────────────────────────────────────────
|
|
453
|
+
# CONVENIENCE FUNCTIONS
|
|
454
|
+
# ──────────────────────────────────────────────────────────────────────────────
|
|
455
|
+
|
|
456
|
+
|
|
457
|
+
def grep(
|
|
458
|
+
entries: list[Entry],
|
|
459
|
+
pattern: str,
|
|
460
|
+
**kwargs: Any,
|
|
461
|
+
) -> list[tuple[Entry, list[tuple[int, str | tuple[str, ...]]]]]:
|
|
462
|
+
"""Alias for search_regex (grep-like)."""
|
|
463
|
+
return search_regex(entries, pattern, **kwargs)
|
|
464
|
+
|
|
465
|
+
|
|
466
|
+
def grep_text(
|
|
467
|
+
entries: list[Entry],
|
|
468
|
+
text: str,
|
|
469
|
+
**kwargs: Any,
|
|
470
|
+
) -> list[tuple[Entry, list[int]]]:
|
|
471
|
+
"""Alias for search_content."""
|
|
472
|
+
return search_content(entries, text, **kwargs)
|