
    Ng                         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ee          ZdS )	    N)Path)AnyDictListOptional)Document)	BaseModelmodel_validator)
BaseLoaderc                   <   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	          ed
eeef         defd                        ZdefdZdede	e         fdZdedee         fdZde	e         fdZde	e         f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                     |                     d          $|                     d          t          d          |                     d          $|                     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   s     h/var/www/html/ai-engine/env/lib/python3.11/site-packages/langchain_community/document_loaders/dropbox.pyvalidate_inputszDropboxLoader.validate_inputs$   sx    
 JJ,--9

/00<MNNN::+,,4VZZ >
 >
4 LMMM    c                     	 ddl m}m} n# t          $ r t          d          w xY w	  || j                  }|                                 n"# |j        $ r}t          d          |d}~ww xY w|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   users_get_current_account	AuthErrorr   )selfr   r    dbxexs        r   _create_dropbox_clientz$DropboxLoader._create_dropbox_client4   s    	F333333333 	F 	F 	FDEEE	F	'$344C))++++# 	 	 	W 	 
s    %$A 
A-A((A-folder_pathc                 f                                      }	 ddlm} ddlm n# t
          $ r t          d          w xY w	 |                    | j                  }n&# |j        $ r}t          d| d          |d}~ww xY wfd	|j
        D             }d
  f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                 4    g | ]}t          |          |S  )
isinstance).0entryr-   s     r   
<listcomp>z=DropboxLoader._load_documents_from_folder.<locals>.<listcomp>V   s(    WWW5z%7V7VWWWWr   c                     g | ]}||S Nr/   r1   docs     r   r3   z=DropboxLoader._load_documents_from_folder.<locals>.<listcomp>W   s%     
 
 
 r   c              3   L   K   | ]}                     |j                  V  d S r5   )_load_file_from_pathpath_display)r1   filer&   s     r   	<genexpr>z<DropboxLoader._load_documents_from_folder.<locals>.<genexpr>Y   s4      WW11$2CDDWWWWWWr   )r)   r"   r    dropbox.filesr-   r#   files_list_folderr   ApiErrorr   entries)	r&   r*   r'   r    resultsr(   files	documentsr-   s	   `       @r   _load_documents_from_folderz)DropboxLoader._load_documents_from_folderD   s9   ))++	F******2222222 	F 	F 	FDEEE	F	++K4>+RRGG" 	 	 	?{ ? ? ?  	 XWWWGOWWW
 
WWWWQVWWW
 
 
	
 s    % ?A   
B*A>>B	file_pathc                    |                                  }	 ddlm} n# t          $ r t          d          w xY w	 |                    |          }|j        r|                    |          \  }}n |j        r|                    |d          \  }}n&# |j	        $ r}t          d| d          |d}~ww xY w	 |j                            d          }nP# t          $ rB t          j                            |          d	                                         }	|	d
k    rt%          d| d           ddlm}
 t+          j                    }t/          |j                  dz  }t3          |d          5 }|                    |j                   ddd           n# 1 swxY w Y   	  |
t7          |                    }|                                }|r
|d         cY S nA# t:          $ r!}t%          d| d|            Y d}~Y dS d}~ww xY wt%          d| d           Y dS w xY wd| t          j                            |          d}t?          ||          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)page_contentmetadata) r)   r"   r    r#   files_get_metadatais_downloadablefiles_downloadexport_infofiles_exportr?   r   contentdecodeUnicodeDecodeErrorospathsplitextlowerprint$langchain_community.document_loadersrI   tempfileTemporaryDirectoryr   nameopenwritestrload	Exceptionbasenamer   )r&   rE   r'   r    file_metadata_responser(   textfile_extensionrI   temp_dirtemp_pdffloaderdocspdf_exrN   s                     r   r9   z"DropboxLoader._load_file_from_path^   s>   ))++	F******* 	F 	F 	FDEEE	F	229==M, F!00;;88 * F!..y*EE8" 	 	 	!	 ! ! !  		#**733DD! 	 	 	W--i88;AACCN''?i???@@@VVVVVV $688..:(... .!GGH,---. . . . . . . . . . . . . . . 223x==AAF!;;==D '#Aw'       RYRR&RRSSS444444  VIVVV   445	: /9..W%%i00
 
 TH====s    7AB 
B4B//B48C BH!0FH!F	H!F	H!#5GH!H!
H&H;H!HH! H!c                 j      j         st          d          d  fd j         D             D             S )z1Load documents from a list of Dropbox file paths.zfile_paths must be setc                     g | ]}||S r5   r/   r6   s     r   r3   z<DropboxLoader._load_documents_from_paths.<locals>.<listcomp>   s)     
 
 
  
 r   c              3   B   K   | ]}                     |          V  d S r5   )r9   )r1   rE   r&   s     r   r<   z;DropboxLoader._load_documents_from_paths.<locals>.<genexpr>   sE         )))44     r   )r   r   r&   s   `r   _load_documents_from_pathsz(DropboxLoader._load_documents_from_paths   sd    & 	75666
 
   !%!8  
 
 
 	
r   c                 l    | j         |                     | j                   S |                                 S )zLoad documents.)r   rD   ru   rt   s    r   rc   zDropboxLoader.load   s4    #/33D4LMMM22444r   )__name__
__module____qualname____doc__rb   __annotations__r   r   r   r   r   boolr
   classmethodr   r   r   r)   r   rD   r9   ru   rc   r/   r   r   r   r      sl          )-#---'.2c+222&ItM_(###T#s(^     [ $#     s tH~    4;>c ;>hx6H ;> ;> ;> ;>z
DN 
 
 
 
5d8n 5 5 5 5 5 5r   r   )rW   r]   pathlibr   typingr   r   r   r   langchain_core.documentsr   pydanticr	   r
   )langchain_community.document_loaders.baser   r   r/   r   r   <module>r      s    
			        , , , , , , , , , , , , - - - - - - / / / / / / / / @ @ @ @ @ @Z5 Z5 Z5 Z5 Z5J	 Z5 Z5 Z5 Z5 Z5r   