|
1 | 1 | """
|
2 | 2 | Basic, pure-Python search strategy.
|
3 | 3 | """
|
| 4 | +import fnmatch |
4 | 5 | import os
|
5 | 6 | import re
|
6 |
| -import fnmatch |
| 7 | +from pathlib import Path |
7 | 8 | from typing import Dict, List, Optional, Tuple
|
8 | 9 |
|
9 | 10 | from .base import SearchStrategy, create_word_boundary_pattern, is_safe_regex_pattern
|
@@ -83,33 +84,38 @@ def search(
|
83 | 84 | except re.error as e:
|
84 | 85 | raise ValueError(f"Invalid regex pattern: {pattern}, error: {e}")
|
85 | 86 |
|
86 |
| - for root, _, files in os.walk(base_path): |
| 87 | + file_filter = getattr(self, 'file_filter', None) |
| 88 | + base = Path(base_path) |
| 89 | + |
| 90 | + for root, dirs, files in os.walk(base_path): |
| 91 | + if file_filter: |
| 92 | + dirs[:] = [d for d in dirs if not file_filter.should_exclude_directory(d)] |
| 93 | + |
87 | 94 | for file in files:
|
88 |
| - # Improved file pattern matching with glob support |
89 | 95 | if file_pattern and not self._matches_pattern(file, file_pattern):
|
90 | 96 | continue
|
91 | 97 |
|
92 |
| - file_path = os.path.join(root, file) |
| 98 | + file_path = Path(root) / file |
| 99 | + |
| 100 | + if file_filter and not file_filter.should_process_path(file_path, base): |
| 101 | + continue |
| 102 | + |
93 | 103 | rel_path = os.path.relpath(file_path, base_path)
|
94 |
| - |
| 104 | + |
95 | 105 | try:
|
96 | 106 | with open(file_path, 'r', encoding='utf-8', errors='ignore') as f:
|
97 | 107 | for line_num, line in enumerate(f, 1):
|
98 | 108 | if search_regex.search(line):
|
99 | 109 | content = line.rstrip('\n')
|
100 |
| - # Truncate content if it exceeds max_line_length |
101 | 110 | if max_line_length and len(content) > max_line_length:
|
102 | 111 | content = content[:max_line_length] + '... (truncated)'
|
103 |
| - |
| 112 | + |
104 | 113 | if rel_path not in results:
|
105 | 114 | results[rel_path] = []
|
106 |
| - # Strip newline for consistent output |
107 | 115 | results[rel_path].append((line_num, content))
|
108 | 116 | except (UnicodeDecodeError, PermissionError, OSError):
|
109 |
| - # Ignore files that can't be opened or read due to encoding/permission issues |
110 | 117 | continue
|
111 | 118 | except Exception:
|
112 |
| - # Ignore any other unexpected exceptions to maintain robustness |
113 | 119 | continue
|
114 | 120 |
|
115 | 121 | return results
|
0 commit comments