[docs]classFileEncoding(NamedTuple):"""File encoding as the NamedTuple."""encoding:Optional[str]"""The encoding of the file."""confidence:float"""The confidence of the encoding."""language:Optional[str]"""The language of the file."""
[docs]defdetect_file_encodings(file_path:Union[str,Path],timeout:int=5)->List[FileEncoding]:"""Try to detect the file encoding. Returns a list of `FileEncoding` tuples with the detected encodings ordered by confidence. Args: file_path: The path to the file to detect the encoding for. timeout: The timeout in seconds for the encoding detection. """importchardetfile_path=str(file_path)defread_and_detect(file_path:str)->List[dict]:withopen(file_path,"rb")asf:rawdata=f.read()returncast(List[dict],chardet.detect_all(rawdata))withconcurrent.futures.ThreadPoolExecutor()asexecutor:future=executor.submit(read_and_detect,file_path)try:encodings=future.result(timeout=timeout)exceptconcurrent.futures.TimeoutError:raiseTimeoutError(f"Timeout reached while detecting encoding for {file_path}")ifall(encoding["encoding"]isNoneforencodinginencodings):raiseRuntimeError(f"Could not detect encoding for {file_path}")return[FileEncoding(**enc)forencinencodingsifenc["encoding"]isnotNone]