refactor: exclude underscored files from autodiscovery (#888)

This commit is contained in:
Juro Oravec 2025-01-07 19:48:41 +01:00 committed by GitHub
parent 1e4b556b4d
commit 203d29f511
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 21 additions and 4 deletions

View file

@ -17,6 +17,8 @@ def autodiscover(
See [Autodiscovery](../../concepts/fundamentals/autodiscovery).
NOTE: Subdirectories and files starting with an underscore `_` (except for `__init__.py` are ignored.
Args:
map_module (Callable[[str], str], optional): Map the module paths with `map_module` function.\
This serves as an escape hatch for when you need to use this function in tests.

View file

@ -126,6 +126,8 @@ def get_component_files(suffix: Optional[str] = None) -> List[ComponentFileEntry
Requires `BASE_DIR` setting to be set.
Subdirectories and files starting with an underscore `_` (except `__init__.py`) are ignored.
Args:
suffix (Optional[str], optional): The suffix to search for. E.g. `.py`, `.js`, `.css`.\
Defaults to `None`, which will search for all files.
@ -232,8 +234,17 @@ def _search_dirs(dirs: List[Path], search_glob: str) -> List[Path]:
"""
matched_files: List[Path] = []
for directory in dirs:
for path in glob.iglob(str(Path(directory) / search_glob), recursive=True):
matched_files.append(Path(path))
for path_str in glob.iglob(str(Path(directory) / search_glob), recursive=True):
path = Path(path_str)
# Skip any subdirectory or file (under the top-level directory) that starts with an underscore
rel_dir_parts = list(path.relative_to(directory).parts)
name_part = rel_dir_parts.pop()
if any(part.startswith("_") for part in rel_dir_parts):
continue
if name_part.startswith("_") and name_part != "__init__.py":
continue
matched_files.append(path)
return matched_files