a
    bgy4                     @  s  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ZddlZddl	m
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/m0Z0 e1e2Z3dZ4G dd deZ5G dd deZ6dddddZ7dddddZ8G dd de#eZ9dS ) 1Base class for all loaders that uses O365 Package    )annotationsN)abstractmethod)datetime)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                   @  sF   e Zd ZU edddZded< edddZded< ed	d
dddZdS )_O365Settings.ZO365_CLIENT_ID)aliasstr	client_idZO365_CLIENT_SECRETr   client_secretFz.env ignore)case_sensitiveZenv_fileZ
env_prefixextraN)	__name__
__module____qualname__r   r#   __annotations__r$   r   Zmodel_config r-   r-   |/var/www/html/cobodadashboardai.evdpl.com/venv/lib/python3.9/site-packages/langchain_community/document_loaders/base_o365.pyr    *   s
   
r    c                   @  s&   e Zd ZU e d d Zded< dS )_O365TokenStorage.credentialszo365_token.txtr   
token_pathN)r)   r*   r+   r   homer1   r,   r-   r-   r-   r.   r/   3   s   
r/   Sequence[str]Dict[str, str])
file_typesreturnc                 C  sB   i }| D ]4}t d| \}}|r.|||< qtd| q|S )2Fetch the mime types for the specified file types.zfile.zUnknown mimetype of extension )	mimetypes
guess_type
ValueError)r5   mime_types_mappingext	mime_type_r-   r-   r.   fetch_mime_types7   s    
r?   )
mime_typesr6   c                 C  s@   i }| D ]2}t |}|r,|||dd < qtd| q|S )r7      NzUnknown mimetype )r8   guess_extensionr:   )r@   r;   r=   r<   r-   r-   r.   fetch_extensionsC   s    
rC   c                      s   e Zd ZU dZeedZded< dZded< e	Z
ded	< d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dd fddZeddddZeedddd Zd!d"d#d$d%Zd&dd"d'd(d)Zd*dd+d,Z  ZS )-O365BaseLoaderr   )default_factoryr    settingsFboolauth_with_tokenzUnion[int, str]
chunk_size	recursiveNzOptional[datetime]modified_sincezOptional[Dict[str, Any]]handlersr   _blob_parserr3   _file_typesr4   _mime_typesr	   None)kwargsr6   c                   s  t  jf i |  jrt j }z4t| _tt| _ fdd j	 D }W n^ t
y   z(t| _tt j  _ j}W n" t
y   t
d| dY n0 Y n0 t|d d _n@td _t jtstdt j tt jj  _d S )Nc                   s   i | ]\}} j | |qS r-   rO   ).0	extensionhandlerselfr-   r.   
<dictcomp>   s   z+O365BaseLoader.__init__.<locals>.<dictcomp>z=`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)rL   Zfallback_parserdefaultzLget_parser("default) was supposed to return MimeTypeBasedParser.It returned )super__init__rL   listkeysr?   rO   setrN   itemsr:   rC   r   rM   r   
isinstance	TypeErrortype)rW   rQ   Zhandler_keysZmime_handlers	__class__rV   r.   r[      s>    






zO365BaseLoader.__init__)r6   c                 C  s   | j S )zBReturn a dict of supported file types to corresponding mime types.rR   rV   r-   r-   r.   _fetch_mime_types   s    z O365BaseLoader._fetch_mime_typesz	List[str]c                 C  s   dS )zReturn required scopes.Nr-   rV   r-   r-   r.   _scopes   s    zO365BaseLoader._scopesr   zIterable[Blob])folderr6   c                 c  s  | j }| }i }t 2}tjtj|dd |D ]}|jr8|j	t
| v r8| jrf|j| jkr8|j}td|jr|jjd tj|j }|j|| jd ||j	t|jt|jt|jt|j|jt|jd||j< q8t|d}| D ]H}	t |	jt!st"d|	jr8|#t|	jji }
|	j$%|
 |	V  qW d	   n1 sV0    Y  | j&r|' D ]}| (|E d	H  qpd	S )
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"Doc.aspx\?sourcedoc=.*file=([^&]+)/Zto_pathrI   sourcer=   createdmodified
created_bymodified_bydescriptionidpath#Expected blob path to be a PurePathN))re   Z	get_itemstempfileTemporaryDirectoryosmakedirsru   dirnameis_filer=   r\   valuesrK   ro   web_urlresearch_parenturllibparsequotenamedownloadrI   r"   rn   rp   rq   rr   	object_idr   yield_blobsr`   r   NotImplementedErrorgetmetadataupdaterJ   Zget_child_folders_load_from_folder)rW   rg   file_mime_typesr_   metadata_dicttemp_dirfilerm   loaderblobfile_metadata_Z	subfolderr-   r-   r.   r      sT    

(z O365BaseLoader._load_from_folderr   )drive
object_idsr6   c                 c  sR  | j }i }t (}|D ]}||}|sFtd| d| d q|jr|jt|	 v r|j
}td|j
r|jj
d tj|j }|j|| jd ||j|j|jt|jt|j|jt|jd||j< qt|d}	|	 D ]F}
t|
jtst d	|
jr&|!t|
jji }|
j"#| |
V  qW d
   n1 sD0    Y  d
S )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 .ri   rj   rk   rl   rt   rv   N)$re   rw   rx   Zget_itemloggingwarningr|   r=   r\   r}   r~   r   r   r   r   r   r   r   r   rI   rn   ro   r"   rp   rq   rr   r   r   r   r`   ru   r   r   r   r   r   )rW   r   r   r   r   r   r   r   rm   r   r   r   r-   r-   r.   _load_from_object_ids   sX    

z$O365BaseLoader._load_from_object_idsr   c                 C  s   zddl m}m} W n ty.   tdY n0 | jrt }|j}||j|jd}|f | j	j
| j	j f| j|dddi}nF|t d d	}|f | j	j
| j	j f| j|dddi}|  |S )
znAuthenticates the OneDrive API client

        Returns:
            The authenticated Account object.
        r   )r   FileSystemTokenBackendzAO365 package not found, please install it with `pip install o365`)r1   Ztoken_filename)credentialsZscopestoken_backendZraise_http_errorsFr0   )r1   )O365r   r   ImportErrorrH   r/   r1   parentr   rF   r#   r$   Zget_secret_valuerf   r   r2   Zauthenticate)rW   r   r   Ztoken_storager1   r   accountr-   r-   r.   _auth$  sF    





zO365BaseLoader._auth)r)   r*   r+   __doc__r   r    rF   r,   rH   
CHUNK_SIZErI   rJ   rK   rL   r   rM   rN   rO   r[   propertyre   r   rf   r   r   r   __classcell__r-   r-   rc   r.   rD   O   s&   
&8=rD   ):r   
__future__r   r   r8   ry   r   rw   r   abcr   r   pathlibr   r   typingr   r	   r
   r   r   r   r   r   Zpydanticr   r   r   r   r   Zpydantic_settingsr   r   Z)langchain_community.document_loaders.baser   r   Z=langchain_community.document_loaders.blob_loaders.file_systemr   Z8langchain_community.document_loaders.blob_loaders.schemar   Z4langchain_community.document_loaders.parsers.genericr   Z5langchain_community.document_loaders.parsers.registryr   r   r   Z
O365.driver   r   	getLoggerr)   loggerr   r    r/   r?   rC   rD   r-   r-   r-   r.   <module>   s8   (
	