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"
def__init__(self,app_names:Any=None,*args:Any,**kwargs:Any)->None:component_dirs=[str(p)forpinget_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 filesself.locations:List[Tuple[str,str]]=[]# Maps dir paths to an appropriate storage instanceself.storages:Dict[str,FileSystemStorage]={}forrootincomponent_dirs:ifisinstance(root,(list,tuple)):prefix,root=rootelse:prefix=""if(prefix,root)notinself.locations:self.locations.append((prefix,root))forprefix,rootinself.locations:filesystem_storage=FileSystemStorage(location=root)filesystem_storage.prefix=prefixself.storages[root]=filesystem_storagesuper().__init__(*args,**kwargs)
deffind(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]=[]forprefix,rootinself.locations:ifrootnotinsearched_locations:searched_locations.append(root)matched_path=self.find_location(root,path,prefix)ifmatched_path:ifnotall:returnmatched_pathmatches.append(matched_path)returnmatches
deffind_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). """ifprefix:prefix="%s%s"%(prefix,os.sep)ifnotpath.startswith(prefix):returnNonepath=path.removeprefix(prefix)path=safe_join(root,path)ifos.path.exists(path)andself._is_path_valid(path):returnpathreturnNone
deflist(self,ignore_patterns:List[str])->Iterable[Tuple[str,FileSystemStorage]]:""" List all files in all locations. """forprefix,rootinself.locations:# Skip nonexistent directories.ifos.path.isdir(root):storage=self.storages[root]forpathinget_files(storage,ignore_patterns):ifself._is_path_valid(path):yieldpath,storage