mirror of
https://github.com/django-components/django-components.git
synced 2025-08-11 09:48:00 +00:00
refactor: Prepare autodiscover and template loader for v1 (#533)
This commit is contained in:
parent
b1bd430a07
commit
8cb88558f0
32 changed files with 643 additions and 552 deletions
|
@ -1,41 +1,4 @@
|
|||
import glob
|
||||
from pathlib import Path
|
||||
from typing import Any, Callable, List, NamedTuple, Optional
|
||||
|
||||
from django.template.engine import Engine
|
||||
|
||||
from django_components.template_loader import Loader
|
||||
|
||||
|
||||
class SearchResult(NamedTuple):
|
||||
searched_dirs: List[Path]
|
||||
matched_files: List[Path]
|
||||
|
||||
|
||||
def search(search_glob: Optional[str] = None, engine: Optional[Engine] = None) -> SearchResult:
|
||||
"""
|
||||
Search for directories that may contain components.
|
||||
|
||||
If `search_glob` is given, the directories are searched for said glob pattern,
|
||||
and glob search results are returned as a flattened list.
|
||||
"""
|
||||
current_engine = engine
|
||||
if current_engine is None:
|
||||
current_engine = Engine.get_default()
|
||||
|
||||
loader = Loader(current_engine)
|
||||
dirs = loader.get_dirs()
|
||||
|
||||
if search_glob is None:
|
||||
return SearchResult(searched_dirs=dirs, matched_files=[])
|
||||
|
||||
component_filenames: List[Path] = []
|
||||
for directory in dirs:
|
||||
for path in glob.iglob(str(Path(directory) / search_glob), recursive=True):
|
||||
component_filenames.append(Path(path))
|
||||
|
||||
return SearchResult(searched_dirs=dirs, matched_files=component_filenames)
|
||||
|
||||
from typing import Any, Callable, List
|
||||
|
||||
# Global counter to ensure that all IDs generated by `gen_id` WILL be unique
|
||||
_id = 0
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue