finders - Django-Components" > finders - Django-Components" >
Skip to content

finders ¤

ComponentsFileSystemFinder ¤

ComponentsFileSystemFinder(app_names: Any = None, *args: Any, **kwargs: Any)

Bases: BaseFinder

A static files finder based on FileSystemFinder.

Differences: - This finder uses COMPONENTS.dirs setting to locate files instead of STATICFILES_DIRS. - Whether a file within COMPONENTS.dirs is considered a STATIC file is configured by COMPONENTS.static_files_allowed and COMPONENTS.forbidden_static_files. - If COMPONENTS.dirs is not set, defaults to settings.BASE_DIR / "components"

Source code in src/django_components/finders.py
def __init__(self, app_names: Any = None, *args: Any, **kwargs: Any) -> None:
    component_dirs = [str(p) for p in get_dirs()]

    # NOTE: The rest of the __init__ is the same as `django.contrib.staticfiles.finders.FileSystemFinder`,
    # but using our locations instead of STATICFILES_DIRS.

    # List of locations with static files
    self.locations: List[Tuple[str, str]] = []

    # Maps dir paths to an appropriate storage instance
    self.storages: Dict[str, FileSystemStorage] = {}
    for root in component_dirs:
        if isinstance(root, (list, tuple)):
            prefix, root = root
        else:
            prefix = ""
        if (prefix, root) not in self.locations:
            self.locations.append((prefix, root))
    for prefix, root in self.locations:
        filesystem_storage = FileSystemStorage(location=root)
        filesystem_storage.prefix = prefix
        self.storages[root] = filesystem_storage

    super().__init__(*args, **kwargs)

find ¤

find(path: str, all: bool = False) -> Union[List[str], str]

Look for files in the extra locations as defined in COMPONENTS.dirs.

Source code in src/django_components/finders.py
def find(self, path: str, all: bool = False) -> Union[List[str], str]:
    """
    Look for files in the extra locations as defined in COMPONENTS.dirs.
    """
    matches: List[str] = []
    for prefix, root in self.locations:
        if root not in searched_locations:
            searched_locations.append(root)
        matched_path = self.find_location(root, path, prefix)
        if matched_path:
            if not all:
                return matched_path
            matches.append(matched_path)
    return matches

find_location ¤

find_location(root: str, path: str, prefix: Optional[str] = None) -> Optional[str]

Find a requested static file in a location and return the found absolute path (or None if no match).

Source code in src/django_components/finders.py
def find_location(self, root: str, path: str, prefix: Optional[str] = None) -> Optional[str]:
    """
    Find a requested static file in a location and return the found
    absolute path (or ``None`` if no match).
    """
    if prefix:
        prefix = "%s%s" % (prefix, os.sep)
        if not path.startswith(prefix):
            return None
        path = path.removeprefix(prefix)
    path = safe_join(root, path)

    if os.path.exists(path) and self._is_path_valid(path):
        return path
    return None

list ¤

list(ignore_patterns: List[str]) -> Iterable[Tuple[str, FileSystemStorage]]

List all files in all locations.

Source code in src/django_components/finders.py
def list(self, ignore_patterns: List[str]) -> Iterable[Tuple[str, FileSystemStorage]]:
    """
    List all files in all locations.
    """
    for prefix, root in self.locations:
        # Skip nonexistent directories.
        if os.path.isdir(root):
            storage = self.storages[root]
            for path in get_files(storage, ignore_patterns):
                if self._is_path_valid(path):
                    yield path, storage