
    dhI$                        S r SSKJr  SSKrSSKJr  SSKJrJrJ	r	J
r
Jr  SSKrSSKJr  SSKJr  SSKJr  SS	KJr   " S
 S\\5      rg)z7Loader that loads data from Sharepoint Document Library    )annotationsN)Path)AnyDictIteratorListOptional)
BaseLoader)Document)Field)O365BaseLoaderc                      \ rS rSr% Sr\" S5      rS\S'    SrS\S'    Sr	S	\S
'    Sr
S\S'    SrS\S'    \R                  " 5       S-  S-  rS\S'    SrS\S'    \SS j5       rSS jrSS jrSS jrSS jrSrg)SharePointLoader   zLoad  from `SharePoint`..strdocument_library_idNzOptional[str]folder_pathzOptional[List[str]]
object_ids	folder_idFzOptional[bool]	load_authz.credentialszo365_token.txtr   
token_pathload_extended_metadatac                
    SS/$ )zKReturn required scopes.
Returns:
    List[str]: A list of required scopes.

sharepointbasic )selfs    g/var/www/html/shao/venv/lib/python3.13/site-packages/langchain_community/document_loaders/sharepoint.py_scopesSharePointLoader._scopes%   s     g&&    c              #    #     SSK JnJn  U R	                  5       R                  5       R                  U R                  5      n[        X15      (       d  [        SU R                   S35      eU R                  (       GaK  UR                  U R                  5      n[        XB5      (       d  [        SU R                   S35      eU R                  U5       H  n[        UR                  R                  S5      5      nU R                   SL a  U R#                  U5      nU R$                  SL a.  U R'                  U5      nUR)                  S	UR*                  05        U R,                  R/                  U5       HO  n	U R                   SL a  WU	R                  S
'   U R$                  SL a  U	R                  R)                  W5        U	v   MQ     M     U R0                  (       GaK  UR3                  U R0                  5      n[        XB5      (       d  [        SU R                   S35      eU R                  U5       H  n[        UR                  R                  S5      5      nU R                   SL a  U R#                  U5      nU R$                  SL a.  U R'                  U5      nUR)                  S	UR*                  05        U R,                  R/                  U5       HO  n	U R                   SL a  WU	R                  S
'   U R$                  SL a  U	R                  R)                  W5        U	v   MQ     M     U R4                  (       a  U R7                  X0R4                  5       H  n[        UR                  R                  S5      5      nU R                   SL a  U R#                  U5      nU R$                  SL a  U R'                  U5      nU R,                  R/                  U5       HO  n	U R                   SL a  WU	R                  S
'   U R$                  SL a  U	R                  R)                  W5        U	v   MQ     M     U R                  (       Gd  U R0                  (       Gdw  U R4                  (       Gdd  UR9                  5       n[        XB5      (       d  [        S5      eU R                  U5       GH"  n[        UR                  R                  S5      5      nU R                   SL a  U R#                  U5      nU R$                  SL a  U R'                  U5      nU R,                  R/                  U5       H  n
U
R                  R)                  UR                  5        U R                   SL a  WU
R                  S
'   U R$                  SL aB  U
R                  R)                  W5        U
R                  R)                  S	UR*                  05        U
v   M     GM%     gggg! [         a    [        S5      ef = f7f)z
Load documents lazily. Use this when working at a large scale.
Yields:
    Document: A document object representing the parsed blob.
r   )DriveFolderzAO365 package not found, please install it with `pip install o365`zThere isn't a Drive with id .zThere isn't a folder with path idTsource_full_urlauthorized_identitieszUnable to fetch root folderN)
O365.driver#   r$   ImportError_authstorage	get_driver   
isinstance
ValueErrorr   get_item_by_path_load_from_folderr   metadatagetr   r(   r   get_extended_metadataupdateweb_url_blob_parser
lazy_parser   get_itemr   _load_from_object_idsget_root_folder)r   r#   r$   drivetarget_folderblobfile_idauth_identitiesextended_metadataparsed_blob	blob_parts              r   	lazy_loadSharePointLoader.lazy_load-   s    	0
 

$$&001I1IJ%'';D<T<T;UUVWXX!2243C3CDMm44 #B4CSCSBTTU!VWW..}=dmm//56>>T)&*&@&@&IO..$6(,(B(B7(K%%,,.?AVAV-WX#'#4#4#?#?#EK~~-HW,,-DE22d:#,,334EF%% $F > >>>!NN4>>:Mm44 #B4CSCSBTTU!VWW..}=dmm//56>>T)&*&@&@&IO..$6(,(B(B7(K%%,,.?AVAV-WX#'#4#4#?#?#EK~~-HW,,-DE22d:#,,334EF%% $F > ??225//Jdmm//56>>T)&*&@&@&IO..$6(,(B(B7(K%#'#4#4#?#?#EK~~-HW,,-DE22d:#,,334EF%% $F K    DNNNdooo!113Mm44 !>??..}=dmm//56>>T)&*&@&@&IO..$6(,(B(B7(K%!%!2!2!=!=d!CI&&--dmm<~~-FU	**+BC22d:!**112CD!**11.0E0EF $O "D >	 7FN o  	S 	s   W!W V<W!WW!c                n   U R                  5       nUR                  S5      nSU R                   SU S3nSSU 30n[        R                  " SXES9nUR                  5       n/ nUR                  S	5       H  n	U	R                  S
5      (       d  M  U	R                  S
5      R                  S5      =(       dG    U	R                  S
5      R                  S5      =(       d     U	R                  S
5      R                  S5      n
U
(       d  M  U
R                  S5      nU(       d  M  UR                  U5        M     U$ )z
Retrieve the access identities (user/group emails) for a given file.
Args:
    file_id (str): The ID of the file.
Returns:
    List: A list of group names (email addresses) that have
          access to the file.
access_token(https://graph.microsoft.com/v1.0/drives//items/z/permissionsAuthorizationBearer GETheadersvaluegrantedToV2siteUserusergroupemail)_fetch_access_tokenr3   r   requestsrequestjsonappend)r   r?   datarG   urlrN   responseaccess_listgroup_namesaccess_data	site_datarT   s               r   r(   &SharePointLoader.authorized_identities   s!    '')xx/(()	G 	 #gl^$<=##E3@mmo&??73K}-- __]377
C E#6::6BE#6::7C 
 9%MM'2Eu#**51 4 r!   c                    [        U R                  SS9 nUR                  5       nSSS5        [        R                  " W5      nU$ ! , (       d  f       N&= f)z\
Fetch the access token from the token file.
Returns:
    The access token as a dictionary.
zutf-8)encodingN)openr   readrX   loads)r   fsrZ   s       r   rU   $SharePointLoader._fetch_access_token   s?     $//G4A 5zz!} 54s   A
Ac                   U R                  5       nUR                  S5      nSU R                   SU S3nSSU 30n[        R                  " SXES9nUR                  5       nUR                  S	S
5      UR                  S0 5      R                  S0 5      R                  SS5      UR                  S0 5      R                  SS5      R                  S5      S   S-   UR                  SS5      -   S.nU$ )a  
Retrieve extended metadata for a file in SharePoint.
As of today, following fields are supported in the extended metadata:
- size: size of the source file.
- owner: display name of the owner of the source file.
- full_path: pretty human readable path of the source file.
Args:
    file_id (str): The ID of the file.
Returns:
    dict: A dictionary containing the extended metadata of the file,
          including size, owner, and full path.
rG   rH   rI   z,?$select=size,createdBy,parentReference,namerJ   rK   rL   rM   sizer   	createdByrR   displayName parentReferencepath:/name)rk   owner	full_path)rU   r3   r   rV   rW   rX   split)	r   r?   rZ   rG   r[   rN   r\   r2   staged_metadatas	            r   r4   &SharePointLoader.get_extended_metadata   s     '')xx/6''(y:; 	
 #gl^$<=##E3@==?LL+\\+r2S_S#!&7<S_U3Z  ll62&	'

 r!   r   )returnz	List[str])rz   zIterator[Document])r?   r   rz   r   )rz   r   )r?   r   rz   r   )__name__
__module____qualname____firstlineno____doc__r   r   __annotations__r   r   r   r   r   homer   r   propertyr   rD   r(   rU   r4   __static_attributes__r   r!   r   r   r      s    "$Sz)G!%K%4&*J#*4#I}#2 %I~%4yy{^36FFJF2-2N2H' 'R$h D	"r!   r   )r   
__future__r   rX   pathlibr   typingr   r   r   r   r	   rV   langchain_core.document_loadersr
   langchain_core.documentsr   pydanticr   .langchain_community.document_loaders.base_o365r   r   r   r!   r   <module>r      s8    = "   6 6  6 - 
}~z }r!   