|
45 | 45 | BinaryIO, |
46 | 46 | Optional, |
47 | 47 | Union, |
| 48 | + List, |
| 49 | + Dict, |
48 | 50 | ) |
49 | 51 |
|
50 | 52 |
|
@@ -388,19 +390,30 @@ def is_ignored(self, path: str) -> Optional[bool]: |
388 | 390 | return matches[-1].is_exclude |
389 | 391 | return None |
390 | 392 |
|
391 | | - def walk(self, **kwargs) -> Generator[tuple[str, list[str], list[str]], None, None]: |
392 | | - """Wrap os.walk() without ignored files and subdirectories and kwargs are passed to walk.""" |
| 393 | + def walk_with_ignored( |
| 394 | + self, **kwargs |
| 395 | + ) -> Generator[tuple[str, list[str], list[str], list[str], list[str]], None, None]: |
| 396 | + """Wrap os.walk() and also return lists of ignored directories and files. |
| 397 | +
|
| 398 | + Yields tuples: (dirpath, included_dirnames, included_filenames, ignored_dirnames, ignored_filenames) |
| 399 | + """ |
393 | 400 | for dirpath, dirnames, filenames in os.walk(self.path, topdown=True, **kwargs): |
394 | 401 | rel_dirpath = '' if dirpath == self.path else os.path.relpath(dirpath, self.path) |
395 | 402 |
|
| 403 | + original_dirnames = list(dirnames) |
| 404 | + included_dirnames = [d for d in original_dirnames if not self.is_ignored(os.path.join(rel_dirpath, d))] |
| 405 | + |
396 | 406 | # decrease recursion depth of os.walk() by ignoring subdirectories because of topdown=True |
397 | 407 | # slicing ([:]) is mandatory to change dict in-place! |
398 | | - dirnames[:] = [d for d in dirnames if not self.is_ignored(os.path.join(rel_dirpath, d))] |
| 408 | + dirnames[:] = included_dirnames |
| 409 | + |
| 410 | + ignored_dirnames = [d for d in original_dirnames if d not in included_dirnames] |
399 | 411 |
|
400 | | - # remove ignored files |
401 | | - filenames = [f for f in filenames if not self.is_ignored(os.path.join(rel_dirpath, f))] |
| 412 | + original_filenames = list(filenames) |
| 413 | + included_filenames = [f for f in original_filenames if not self.is_ignored(os.path.join(rel_dirpath, f))] |
| 414 | + ignored_filenames = [f for f in original_filenames if f not in included_filenames] |
402 | 415 |
|
403 | | - yield dirpath, dirnames, filenames |
| 416 | + yield dirpath, dirnames, included_filenames, ignored_dirnames, ignored_filenames |
404 | 417 |
|
405 | 418 | @classmethod |
406 | 419 | def build( |
|
0 commit comments