a
    bg                     @   sr   d dl Z d dlZd dlmZ d dlmZmZmZmZ d dl	m
Z
 d dlmZmZ d dlmZ G dd deeZdS )	    N)Path)AnyDictListOptional)Document)	BaseModelmodel_validator)
BaseLoaderc                   @   s   e Zd ZU dZeed< dZee ed< dZee	e  ed< dZ
eed< edd	eeeef ed
ddZedddZee	e dddZeee dddZe	e dddZe	e dddZdS )DropboxLoaderzLoad files from `Dropbox`.

    In addition to common files such as text and PDF files, it also supports
    *Dropbox Paper* files.
    dropbox_access_tokenNdropbox_folder_pathdropbox_file_pathsF	recursivebeforemode)valuesreturnc                 C   sH   | ddur$| ddur$td| ddu rD| dsDtd|S )zDValidate that either folder_path or file_paths is set, but not both.r   Nr   z.Cannot specify both folder_path and file_pathsz-Must specify either folder_path or file_paths)get
ValueError)clsr    r   z/var/www/html/cobodadashboardai.evdpl.com/venv/lib/python3.9/site-packages/langchain_community/document_loaders/dropbox.pyvalidate_inputs$   s    zDropboxLoader.validate_inputs)r   c              
   C   s|   zddl m}m} W n ty.   tdY n0 z|| j}|  W n0 |jyv } ztd|W Y d}~n
d}~0 0 |S )zCreate a Dropbox client.r   )Dropbox
exceptions!You must run `pip install dropboxzEInvalid Dropbox access token. Please verify your token and try again.N)dropboxr   r   ImportErrorr   Zusers_get_current_accountZ	AuthErrorr   )selfr   r   dbxexr   r   r   _create_dropbox_client4   s    
z$DropboxLoader._create_dropbox_client)folder_pathr   c              
      s     }zddlm} ddlm  W n ty>   tdY n0 z|j|jd}W n8 |jy } zt	d| d|W Y d}~n
d}~0 0  fd	d
|j
D }dd
 fdd|D D }|S )z%Load documents from a Dropbox folder.r   r   FileMetadatar   )r   z$Could not list files in the folder: z.. Please verify the folder path and try again.Nc                    s   g | ]}t | r|qS r   )
isinstance).0entryr&   r   r   
<listcomp>V       z=DropboxLoader._load_documents_from_folder.<locals>.<listcomp>c                 S   s   g | ]}|d ur|qS Nr   r)   docr   r   r   r+   W   s   c                 3   s   | ]}  |jV  qd S r-   )_load_file_from_pathZpath_display)r)   filer    r   r   	<genexpr>Y   r,   z<DropboxLoader._load_documents_from_folder.<locals>.<genexpr>)r#   r   r   Zdropbox.filesr'   r   Zfiles_list_folderr   ApiErrorr   entries)r    r$   r!   r   resultsr"   filesZ	documentsr   )r'   r    r   _load_documents_from_folderD   s&    
z)DropboxLoader._load_documents_from_folder)	file_pathr   c                 C   s  |   }zddlm} W n ty2   tdY n0 z:||}|jrV||\}}n|jrl||d\}}W n8 |j	y } zt
d| d|W Y d}~n
d}~0 0 z|jd}W n ty   tj|d	  }	|	d
krtd| d ddlm}
 t }t|jd }t|dd}||j W d   n1 sJ0    Y  z,|
t|}| }|r~|d W  Y S W n> ty } z$td| d|  W Y d}~Y dS d}~0 0 ntd| d Y dS 0 d| tj|d}t||dS )z Load a file from a Dropbox path.r   r%   r   markdownzCould not load file: z+. Please verify the file pathand try again.Nzutf-8   z.pdfzFile z type detected as .pdf)UnstructuredPDFLoaderztmp.pdfwbr   z Error while trying to parse PDF z: z/ could not be decoded as pdf or text. Skipping.z
dropbox://)sourcetitle)Zpage_contentmetadata) r#   r   r   r   Zfiles_get_metadataZis_downloadableZfiles_downloadZexport_infoZfiles_exportr4   r   contentdecodeUnicodeDecodeErrorospathsplitextlowerprintZ$langchain_community.document_loadersr<   tempfileTemporaryDirectoryr   nameopenwritestrload	Exceptionbasenamer   )r    r9   r!   r   Zfile_metadata_responser"   textZfile_extensionr<   temp_dirZtemp_pdffloaderdocsZpdf_exr@   r   r   r   r0   ^   sX    


,

z"DropboxLoader._load_file_from_pathc                    s,    j stddd  fdd j D D S )z1Load documents from a list of Dropbox file paths.zfile_paths must be setc                 S   s   g | ]}|d ur|qS r-   r   r.   r   r   r   r+      s   z<DropboxLoader._load_documents_from_paths.<locals>.<listcomp>c                 3   s   | ]}  |V  qd S r-   )r0   )r)   r9   r2   r   r   r3      s   z;DropboxLoader._load_documents_from_paths.<locals>.<genexpr>)r   r   r2   r   r2   r   _load_documents_from_paths   s    
z(DropboxLoader._load_documents_from_pathsc                 C   s"   | j dur| | j S |  S dS )zLoad documents.N)r   r8   rY   r2   r   r   r   rO      s    
zDropboxLoader.load)__name__
__module____qualname____doc__rN   __annotations__r   r   r   r   r   boolr	   classmethodr   r   r   r#   r   r8   r0   rY   rO   r   r   r   r   r      s   
=r   )rD   rI   pathlibr   typingr   r   r   r   Zlangchain_core.documentsr   Zpydanticr   r	   Z)langchain_community.document_loaders.baser
   r   r   r   r   r   <module>	   s   