U
    hh                     @   sh   d Z ddlZddlmZ ddlmZmZmZm	Z	m
Z
 G dd deZde	eef eee dd	d
ZdS )zDocument loader helpers.    N)Path)List
NamedTupleOptionalUnioncastc                   @   s2   e Zd ZU dZee ed< eed< ee ed< dS )FileEncodingz File encoding as the NamedTuple.encoding
confidencelanguageN)__name__
__module____qualname____doc__r   str__annotations__float r   r   P/tmp/pip-unpacked-wheel-9gdii04g/langchain_community/document_loaders/helpers.pyr      s
   
r      )	file_pathtimeoutreturnc              	      s   ddl  t| } ttt d fdd}tj J}||| }z|j|d}W n& tjj	k
rv   t	d|  Y nX W 5 Q R X t
dd	 |D rtd
|  dd |D S )a  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.
    r   N)r   r   c              	      s2   t | d}| }W 5 Q R X ttt  |S )Nrb)openreadr   r   dictZ
detect_all)r   frawdatachardetr   r   read_and_detect#   s    z.detect_file_encodings.<locals>.read_and_detect)r   z-Timeout reached while detecting encoding for c                 s   s   | ]}|d  dkV  qdS r	   Nr   ).0r	   r   r   r   	<genexpr>1   s     z(detect_file_encodings.<locals>.<genexpr>zCould not detect encoding for c                 S   s"   g | ]}|d  dk	rt f |qS r"   )r   )r#   encr   r   r   
<listcomp>3   s      z)detect_file_encodings.<locals>.<listcomp>)r    r   r   r   
concurrentZfuturesZThreadPoolExecutorZsubmitresultTimeoutErrorallRuntimeError)r   r   r!   executorfuture	encodingsr   r   r   detect_file_encodings   s    r/   )r   )r   concurrent.futuresr'   pathlibr   typingr   r   r   r   r   r   r   intr/   r   r   r   r   <module>   s    
 