o
    Zh{                     @   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d
eeef defddZdefddZdede	e fddZdedee fddZde	e fddZde	e f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 r"| ds"t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   c/var/www/html/lang_env/lib/python3.10/site-packages/langchain_community/document_loaders/dropbox.pyvalidate_inputs$   s   zDropboxLoader.validate_inputsc              
   C   sj   z
ddl m}m} W n ty   tdw z|| j}|  W |S  |jy4 } ztd|d}~ww )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_pathc              
      s     }zddlm} ddlm  W n ty   tdw z
|j|jd}W n |jy> } z	t	d| d|d}~ww  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   s    z=DropboxLoader._load_documents_from_folder.<locals>.<listcomp>c                 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   s    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_pathc                 C   s  |   }zddlm} W n ty   tdw z||}|jr)||\}}n|jr4||d\}}W n |j	yK } z	t
d| d|d}~ww 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 sw   Y  z|
t|}| }|r|d W  Y S W Y dS  ty } ztd| d|  W Y d}~Y dS d}~ww td| d Y dS w 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   ^   sl   




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   r,   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 )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   