
    Ig/                       d Z ddlmZ ddlZddlZddlZddlZddlmZ ddl	m
Z
mZ ddlmZmZmZmZmZmZmZmZ ddlmZmZmZmZmZ ddlmZmZ dd	lmZm Z  dd
l!m"Z" ddl#m$Z$ ddl%m&Z& ddl'm(Z( erddl)m*Z* ddl+m,Z,m-Z-  ej\                  e/      Z0dZ1 G d de      Z2 G d de      Z3ddZ4ddZ5 G d de e      Z6y)1Base class for all loaders that uses O365 Package    )annotationsN)abstractmethod)PathPurePath)TYPE_CHECKINGAnyDictIterableListOptionalSequenceUnion)	BaseModelFieldFilePathPrivateAttr	SecretStr)BaseSettingsSettingsConfigDict)BaseBlobParser
BaseLoader)FileSystemBlobLoader)Blob)MimeTypeBasedParser)
get_parser)Account)DriveFolderi  P c                  b    e Zd ZU  edd      Zded<    edd      Zded<    ed	d
dd      Zy)_O365Settings.O365_CLIENT_ID)aliasstr	client_idO365_CLIENT_SECRETr   client_secretFz.env ignore)case_sensitiveenv_file
env_prefixextraN)	__name__
__module____qualname__r   r%   __annotations__r'   r   model_config     k/var/www/html/answerous/venv/lib/python3.12/site-packages/langchain_community/document_loaders/base_o365.pyr!   r!   '   s9    3&67Is7$S0DEM9E%v"HLr4   r!   c                  F    e Zd ZU  ej                         dz  dz  Zded<   y)_O365TokenStorage.credentialszo365_token.txtr   
token_pathN)r.   r/   r0   r   homer9   r1   r3   r4   r5   r7   r7   0   s    $499;7:JJJJr4   r7   c                x    i }| D ]2  }t        j                  d|       \  }}|r|||<   &t        d|        |S )2Fetch the mime types for the specified file types.zfile.zUnknown mimetype of extention )	mimetypes
guess_type
ValueError)
file_typesmime_types_mappingext	mime_type_s        r5   fetch_mime_typesrE   4   s\     E ++eC5M:	1&/s#=cUCDDE r4   c                r    i }| D ]/  }t        j                  |      }|r	|||dd <   #t        d|        |S )r<      NzUnknown mimetype )r=   guess_extensionr?   )
mime_typesrA   rC   rB   s       r5   fetch_extensionsrJ   @   sV     >	''	2*3s12w'0<==> r4   c                      e Zd ZU dZ ee      Zded<   	 dZded<   	 e	Z
ded	<   	 dZded
<   	 i Zded<   	  e       Zded<    e       Zded<    e       Zded<   d fdZedd       Zeedd              ZddZ	 	 	 	 	 	 ddZddZ xZS )O365BaseLoaderr   )default_factoryr!   settingsFboolauth_with_tokenzUnion[int, str]
chunk_size	recursivezOptional[Dict[str, Any]]handlersr   _blob_parserSequence[str]_file_typesDict[str, str]_mime_typesc                ~   t        |   di | | j                  rt        | j                  j	                               }	 t        |      | _        t        t        |            | _        | j                  j                         D ci c]  \  }}| j                  |   | }}}t        |d       | _        y t        d      | _        t        | j                  t              s!t!        dt#        | j                               t        t        | j                  j                  j	                                     | _        y c c}}w # t        $ ro 	 t        |      | _        t        t        | j                  j	                                     | _        | j                  }n# t        $ r t        d| d      w xY wY w xY w)Nz=`handlers` keys must be either file extensions or mimetypes.
zj could not be interpreted as either.
File extensions and mimetypes cannot mix. Use either one or the other)rS   fallback_parserdefaultzLget_parser("default) was supposed to return MimeTypeBasedParser.It returned r3   )super__init__rS   listkeysrE   rX   setrV   itemsr?   rJ   r   rT   r   
isinstance	TypeErrortype)selfkwargshandler_keys	extensionhandlermime_handlers	__class__s         r5   r]   zO365BaseLoader.__init__y   s   "6"== 2 2 45L#3L#A #'L(9#:  /3mm.A.A.C!*	7 $$Y/8! !$ !4&!D !+9 5Dd//1DE##'(9(9#:";=   0T5F5F5O5O5T5T5V0WXD9!  '7'ED$'+C0@0@0E0E0G,H'ID$$(MMM! $X'. )66  "s>   AE D>!E >E 	F<AFF<F55F<;F<c                    | j                   S )zBReturn a dict of supported file types to corresponding mime types.)rX   re   s    r5   _fetch_mime_typesz O365BaseLoader._fetch_mime_types   s     r4   c                     y)zReturn required scopes.Nr3   rm   s    r5   _scopeszO365BaseLoader._scopes   s    r4   c              #    K   | j                   }|j                         }i }t        j                         5 }t	        j
                  t        j                  j                  |      d       |D ]  }|j                  s|j                  t        |j                               v s6|j                  || j                         |j                  |j                  t        |j                         t        |j"                        t        |j$                        t        |j&                        |j(                  t        |j*                        d||j,                  <    t/        |      }|j1                         D ]  }t3        |j                  t4              st7        d      |j                  rJ|j9                  t        |j                  j,                        i       }	|j:                  j=                  |	       |  	 ddd       | j>                  r/|jA                         D ]  }
| jC                  |
      E d{     yy# 1 sw Y   ExY w7 w)a  Lazily load all files from a specified folder of the configured MIME type.

        Args:
            folder: The Folder instance from which the files are to be loaded. This
                Folder instance should represent a directory in a file system where the
                files are stored.

        Yields:
            An iterator that yields Blob instances, which are binary representations of
                the files loaded from the folder.
        T)exist_okto_pathrQ   sourcerC   createdmodified
created_bymodified_bydescriptionidpath#Expected blob path to be a PurePathN)"rn   	get_itemstempfileTemporaryDirectoryosmakedirsr~   dirnameis_filerC   r^   valuesdownloadrQ   web_urlr$   rw   rx   ry   rz   r{   	object_idnamer   yield_blobsrb   r   NotImplementedErrorgetmetadataupdaterR   get_child_folders_load_from_folder)re   folderfile_mime_typesra   metadata_dicttemp_dirfileloaderblobfile_metadata_	subfolders              r5   r   z O365BaseLoader._load_from_folder   s     00  "35((* 	hKK1DA <<~~o.D.D.F)GGh4??S&*ll)-'*4<<'8(+DMM(:*-doo*>+.t/?/?+@+/+;+;"%dnn"5	4dii0	 *x8F**, !$))X6-.STT99%2%6%6s499>>7JB%ONMM((8
%	2 >>#557 =	11)<<<= 3	 	6 =s6   3I	AH;<%H;"EH;7;I	2I3I	;I I	c              #    K   | j                   }i }t        j                         5 }|D ]  }|j                  |      }|st	        j
                  d| d| d       4|j                  sA|j                  t        |j                               v sg|j                  || j                         |j                  |j                  |j                  |j                  t        |j                         t        |j"                        |j$                  t        |j&                        d||j(                  <   	 t+        |      }|j-                         D ]  }	t/        |	j0                  t2              st5        d      |	j0                  rJ|j7                  t        |	j0                  j(                        i       }
|	j8                  j;                  |
       |	  	 ddd       y# 1 sw Y   yxY ww)	a  Lazily load files specified by their object_ids from a drive.

        Load files into the system as binary large objects (Blobs) and return Iterable.

        Args:
            drive: The Drive instance from which the files are to be loaded. This Drive
                instance should represent a cloud storage service or similar storage
                system where the files are stored.
            object_ids: A list of object_id strings. Each object_id represents a unique
                identifier for a file in the drive.

        Yields:
            An iterator that yields Blob instances, which are binary representations of
            the files loaded from the drive using the specified object_ids.
        z!There isn't a file withobject_id z
 in drive .rs   ru   r}   r   N)rn   r   r   get_itemloggingwarningr   rC   r^   r   r   rQ   r   rw   rx   r$   ry   rz   r{   r   r   r   r   rb   r~   r   r   r   r   r   )re   drive
object_idsr   r   r   r   r   r   r   r   s              r5   _load_from_object_idsz$O365BaseLoader._load_from_object_ids   s    $ 0035((* 	h' 	~~i0OO%%.Kz%C <<~~o.D.D.F)GGh4??S&*ll)-'+||(,*-doo*>+.t/?/?+@+/+;+;"%dnn"5	4dii0, *x8F**, !$))X6-.STT99%2%6%6s499>>7JB%ONMM((8
1	 	 	s*   #G(AG)%GEG	G(G%!G(c                h   	 ddl m}m} | j                  rt               }|j                  } ||j                  |j                        } |d
| j                  j                  | j                  j                  j                         f| j                  |dddi}|S  |t        j                         dz  	      } |d
| j                  j                  | j                  j                  j                         f| j                  |dddi}|j!                          |S # t        $ r t        d      w xY w)znAuthenticates the OneDrive API client

        Returns:
            The authenticated Account object.
        r   )r   FileSystemTokenBackendzAO365 package not found, please install it with `pip install o365`)r9   token_filename)credentialsscopestoken_backendraise_http_errorsFr8   )r9   r3   )O365r   r   ImportErrorrP   r7   r9   parentr   rN   r%   r'   get_secret_valuerp   r   r:   authenticate)re   r   r   token_storager9   r   accounts          r5   _authzO365BaseLoader._auth	  s?   	<
 -/M&11J2%,,Z__M  MM++MM//@@B ||+ '.G0  399;7M  MM++MM//@@B ||+ '.G   "E  	S 	s   D D1)rf   r	   returnNone)r   rW   )r   	List[str])r   r   r   Iterable[Blob])r   r   r   r   r   r   )r   r   )r.   r/   r0   __doc__r   r!   rN   r1   rP   
CHUNK_SIZErQ   rR   rS   r   rT   rV   rX   r]   propertyrn   r   rp   r   r   r   __classcell__)rk   s   @r5   rL   rL   L   s    ;#MBHmB0!OT!I",J,VIt8)+H&+: $/=L.0!,K."--K/$YL     &  &*=X22(12	2h*r4   rL   )r@   rU   r   rW   )rI   rU   r   rW   )7r   
__future__r   r   r=   r   r   abcr   pathlibr   r   typingr   r	   r
   r   r   r   r   r   pydanticr   r   r   r   r   pydantic_settingsr   r   )langchain_community.document_loaders.baser   r   =langchain_community.document_loaders.blob_loaders.file_systemr   8langchain_community.document_loaders.blob_loaders.schemar   4langchain_community.document_loaders.parsers.genericr   5langchain_community.document_loaders.parsers.registryr   r   r   
O365.driver   r   	getLoggerr.   loggerr   r!   r7   rE   rJ   rL   r3   r4   r5   <module>r      s    7 "   	   " V V V  ? P J T L(			8	$
L K K		gZ gr4   