[docs]classLocalFile(File):""" Class for a file in a package that is stored locally. :param str path: The path to the file. :param str filename: The name of the file. """
[docs]@classmethoddefget_file_matching_extension(cls,dir:str,filename:str,extensions:list[str])->"LocalFile":""" Get the file with the given filename and one of the given extensions. :param str dir: The directory to search in. :param str filename: The filename. :param list[str] extensions: The extensions. :return LocalFile: The file object. :raises FileNotFoundError: If no file is found. """forextinextensions:path=os.path.join(dir,filename+"."+ext)ifos.path.exists(path):returncls(path)raiseFileNotFoundError("No file found with the given filename and extensions in the directory.")
def__init__(self,path:str,exists=True):""" Initialize the file. :param str path: The path to the file. :param bool exists: If True, check if the file exists. :raises FileNotFoundError: If the file doesn't exist. """ifnotos.path.exists(path)andexists:raiseFileNotFoundError(f"File {path} does not exist.")super().__init__(path)self.filename=os.path.basename(path)def__repr__(self):returnf"<LocalFile {self.path}>"