
    KiD                         S SK r S SKrS SKrS SKrS SKrS SKrS SKJr  S SKJr  S SK	J
r
  S SKrSSKJrJr  SSKJrJr  \ R$                  " S5      r " S	 S
\5      r " S S\5      rg)    N)suppress)datetime)quote   )AbstractBufferedFileAbstractFileSystem)infer_storage_optionstokenizewebhdfsc                   h  ^  \ rS rSrSr\" \R                  " 5       5      rSr	           S!U 4S jjr
\S 5       rS rS"S jr     S#S jr\S	 5       r\S
 5       r\S 5       rS rS rS rS$S jrS rS rS rS%S jrS rS rS rS&S jr S r!S r"S$S jr#S r$S$S jr%S r&S r'S r(S r)U =r*$ )'WebHDFS   a  
Interface to HDFS over HTTP using the WebHDFS API. Supports also HttpFS gateways.

Four auth mechanisms are supported:

insecure: no auth is done, and the user is assumed to be whoever they
    say they are (parameter ``user``), or a predefined value such as
    "dr.who" if not given
spnego: when kerberos authentication is enabled, auth is negotiated by
    requests_kerberos https://github.com/requests/requests-kerberos .
    This establishes a session based on existing kinit login and/or
    specified principal/password; parameters are passed with ``kerb_kwargs``
token: uses an existing Hadoop delegation token from another secured
    service. Indeed, this client can also generate such tokens when
    not insecure. Note that tokens expire, but can be renewed (by a
    previously specified user) and may allow for proxying.
basic-auth: used when both parameter ``user`` and parameter ``password``
    are provided.

)r   webHDFSc                 "  > U R                   (       a  g[        TU ]  " S0 UD6  U
(       a  SOS SU SU S3U l        X0l        U=(       d    0 U l        0 U l        U	=(       d    0 U l        Ub  Uc  Ub  [        S5      eX@R                  S'   XPl	        X`l
        Ub  Uc  [        S	5      eOUb  XPR                  S
'   Ub  XpR                  S'   U(       a  Ub  [        S5      eXl        Xl        U R                  5         S[        X5       3U l        g)ab  
Parameters
----------
host: str
    Name-node address
port: int
    Port for webHDFS
kerberos: bool
    Whether to authenticate with kerberos for this connection
token: str or None
    If given, use this token on every call to authenticate. A user
    and user-proxy may be encoded in the token and should not be also
    given
user: str or None
    If given, assert the user name to connect with
password: str or None
    If given, assert the password to use for basic auth. If password
    is provided, user must be provided also
proxy_to: str or None
    If given, the user has the authority to proxy, and this value is
    the user in who's name actions are taken
kerb_kwargs: dict
    Any extra arguments for HTTPKerberosAuth, see
    `<https://github.com/requests/requests-kerberos/blob/master/requests_kerberos/kerberos_.py>`_
data_proxy: dict, callable or None
    If given, map data-node addresses. This can be necessary if the
    HDFS cluster is behind a proxy, running on Docker or otherwise has
    a mismatch between the host-names given by the name-node and the
    address by which to refer to them from the client. If a dict,
    maps host names ``host->data_proxy[host]``; if a callable, full
    URLs are passed, and function must conform to
    ``url->data_proxy(url)``.
use_https: bool
    Whether to connect to the Name-node using HTTPS instead of HTTP
session_cert: str or Tuple[str, str] or None
    Path to a certificate file, or tuple of (cert, key) files to use
    for the requests.Session
session_verify: str, bool or None
    Path to a certificate file to use for verifying the requests.Session.
kwargs
Nhttpshttpz://:z/webhdfs/v1z_If passing a delegation token, must not set user or proxy_to, as these are encoded in the token
delegationzQIf passing a password, the user must also beset in order to set up the basic-authz	user.namedoaszJIf using Kerberos auth, do not specify the user, this is handled by kinit.webhdfs_ )_cachedsuper__init__urlkerbkerb_kwargsparsproxy
ValueErroruserpasswordsession_certsession_verify_connectr
   _fsid)selfhostportkerberostokenr!   r"   proxy_tor   
data_proxy	use_httpsr#   r$   kwargs	__class__s                 `/var/www/html/dynamic-report/venv/lib/python3.13/site-packages/fsspec/implementations/webhdfs.pyr   WebHDFS.__init__.   s,   r <<"6"!*g7s4&${S	&,"	%2
8#7  
 ',IIl#	 | <   )-		+& (IIf(2 
 ), 456
    c                     U R                   $ N)r&   r'   s    r1   fsidWebHDFS.fsid   s    zzr3   c                    [         R                  " 5       U l        U R                  (       a  U R                  U R                  l        U R
                  U R                  l        U R                  (       a'  SSKJ	n  U" S0 U R                  D6U R                  l        U R                  bA  U R                  b3  SSKJn  U" U R                  U R                  5      U R                  l        g g g )Nr   )HTTPKerberosAuth)HTTPBasicAuthr   )requestsSessionsessionr#   certr$   verifyr   requests_kerberosr:   r   authr!   r"   requests.authr;   )r'   r:   r;   s      r1   r%   WebHDFS._connect   s    '') $ 1 1DLL"1199: 0 D43C3C DDLL99 T]]%>3 -dii GDLL &? r3   c                    Ub  U R                  U5      OSnU R                  U R                  [        USS9-   5      nUR	                  5       nUR                  U R                  5        UR                  5       US'   [        R                  SXr5        U R                  R                  UR                  5       UUUUS9n	U	R                  S;   a_   U	R                  5       n
U
S   S	   nU
S   S
   nUS;   a  [        U5      eUS;   a  [        U5      eUS;   a  [!        U5      e[#        U5      eU	R'                  5         U	$ ! [        [$        4 a     N$f = f)N z/=)safeopzsending %s with %s)methodr   paramsdataallow_redirects)i  i  i  i  i  RemoteExceptionmessage	exception)IllegalArgumentExceptionUnsupportedOperationException)SecurityExceptionAccessControlException)FileNotFoundException)_strip_protocol_apply_proxyr   r   copyupdater   upperloggerdebugr>   requeststatus_codejsonr    PermissionErrorFileNotFoundErrorRuntimeErrorKeyErrorraise_for_status)r'   rH   rI   pathrK   redirectr/   r   argsouterrmsgexps                r1   _callWebHDFS._call   sQ   -1-=t##D)25D+A AB{{}DIIXXZT
)37ll""<<>$ # 
 ??77,hhj+,Y7+,[9 WW$S/)KK)#..55+C00&s++
 ) s    D= =EEc                 `    U=(       d    U R                   n[        U UUUU R                  UUUS9$ )a  

Parameters
----------
path: str
    File location
mode: str
    'rb', 'wb', etc.
block_size: int
    Client buffer size for read-ahead or write buffer
autocommit: bool
    If False, writes to temporary file that only gets put in final
    location upon commit
replication: int
    Number of copies of file on the cluster, write mode only
permissions: str or int
    posix permissions, write mode only
kwargs

Returns
-------
WebHDFile instance
)mode
block_sizetempdir
autocommitreplicationpermissions)	blocksize	WebHDFilerp   )r'   rd   rn   ro   rq   rr   rs   r/   s           r1   _openWebHDFS._open   s<    B  14>>
!LL!##	
 		
r3   c                 B    U S   R                  5       U S'   U S   U S'   U $ )Ntypelengthsize)lower)infos    r1   _process_infoWebHDFS._process_info   s*    F|))+VH~Vr3   c                     [        U5      S   $ )Nrd   )r	   )clsrd   s     r1   rU   WebHDFS._strip_protocol   s    $T*622r3   c                     [        U 5      nUR                  SS 5        UR                  SS 5        SU;   a  UR                  S5      US'   U$ )Nrd   protocolusernamer!   )r	   pop)urlpathrg   s     r1   _get_kwargs_from_urlsWebHDFS._get_kwargs_from_urls  sG    #G,
D!''*-CK
r3   c                 r    U R                  SUS9nUR                  5       S   nXS'   U R                  U5      $ )NGETFILESTATUSrd   
FileStatusname)rk   r^   r~   )r'   rd   rg   r}   s       r1   r}   WebHDFS.info
  s<    jjtj4xxz,'V!!$''r3   c                     U R                  U5      nUR                  SS5      nUb  [        R                  " US-  5      $ [	        S5      e)z=Return the created timestamp of a file as a datetime.datetimemodificationTimeN  z5Could not retrieve creation time (modification time).r}   getr   fromtimestampra   r'   rd   r}   mtimes       r1   createdWebHDFS.created  sI     yy+T2))%$,77RSSr3   c                     U R                  U5      nUR                  SS5      nUb  [        R                  " US-  5      $ [	        S5      e)z>Return the modified timestamp of a file as a datetime.datetimer   Nr   z%Could not retrieve modification time.r   r   s       r1   modifiedWebHDFS.modified  sG    yy+T2))%$,77BCCr3   c                    U R                  SUS9nUR                  5       S   S   nU H1  nU R                  U5        UR                  S5      S-   US   -   US'   M3     U(       a  [	        US S	9$ [	        S
 U 5       5      $ )N
LISTSTATUSr   FileStatusesr   /
pathSuffixr   c                     U S   $ )Nr   r   )is    r1   <lambda>WebHDFS.ls.<locals>.<lambda>(  s    qyr3   )keyc              3   *   #    U  H	  oS    v   M     g7f)r   Nr   ).0r}   s     r1   	<genexpr>WebHDFS.ls.<locals>.<genexpr>*  s     954v,5s   )rk   r^   r~   rstripsorted)r'   rd   detailr/   rg   infosr}   s          r1   ls
WebHDFS.ls!  s    jjDj1
>*<8Dt$;;s+c1D4FFDL  %%89995999r3   c                 H    U R                  SUS9nUR                  5       S   $ )z8Total numbers of files, directories and bytes under pathGETCONTENTSUMMARYr   ContentSummaryrk   r^   )r'   rd   rg   s      r1   content_summaryWebHDFS.content_summary,  s'    jj,4j8xxz*++r3   c                 B   U R                  SUSS9nSUR                  ;   a\  U R                  UR                  S   5      nU R                  R	                  U5      nUR                  5         UR                  5       S   $ UR                  5         UR                  5       S   $ )z/Checksum info of file, giving method and resultGETFILECHECKSUMF)rd   re   LocationFileChecksum)rk   headersrV   r>   r   rc   r^   )r'   rd   rg   locationout2s        r1   ukeyWebHDFS.ukey1  s    jj*jF$((Z)@AH<<##H-D!!#99;~..  "88:n--r3   c                 J    U R                  S5      nUR                  5       S   $ )zGet user's home directoryGETHOMEDIRECTORYPathr   )r'   rg   s     r1   home_directoryWebHDFS.home_directory=  s"    jj+,xxz&!!r3   c                     U(       a  U R                  SUS9nOU R                  S5      nUR                  5       S   nUc  [        S5      eUS   $ )zRetrieve token which can give the same authority to other uses

Parameters
----------
renewer: str or None
    User who may use this token; if None, will be current user
GETDELEGATIONTOKEN)renewerTokenz1No token available for this user/security context	urlString)rk   r^   r    )r'   r   rg   ts       r1   get_delegation_tokenWebHDFS.get_delegation_tokenB  sT     **17*CC**12CHHJw9PQQ~r3   c                 J    U R                  SSUS9nUR                  5       S   $ )z/Make token live longer. Returns new expiry timeRENEWDELEGATIONTOKENputrI   r+   longr   )r'   r+   rg   s      r1   renew_delegation_tokenWebHDFS.renew_delegation_tokenS  s(    jj/UjKxxz&!!r3   c                 &    U R                  SSUS9  g)z Stop the token from being usefulCANCELDELEGATIONTOKENr   r   Nrk   )r'   r+   s     r1   cancel_delegation_tokenWebHDFS.cancel_delegation_tokenX  s    

*5
Fr3   c                 &    U R                  SSXS9  g)zSet the permission at path

Parameters
----------
path: str
    location to set (file or directory)
mod: str or int
    posix epresentation or permission, give as oct string, e.g, '777'
    or 0o777
SETPERMISSIONr   )rI   rd   
permissionNr   )r'   rd   mods      r1   chmodWebHDFS.chmod\  s     	

?5t
Lr3   c                 N    0 nUb  X$S'   Ub  X4S'   U R                   " SSUS.UD6  g)zChange owning user and/or groupNownergroupr   rI   rd   )SETOWNERr   )r'   rd   r   r   r/   s        r1   chownWebHDFS.chowni  s7    #7O#7O

Ae$A&Ar3   c                 (    U R                  SUSUS9  g)z
Set file replication factor

Parameters
----------
path: str
    File location (not for directories)
replication: int
    Number of copies of file on the cluster. Should be smaller than
    number of data nodes; normally 3 on most systems.
SETREPLICATIONr   )rd   rI   rr   Nr   )r'   rd   rr   s      r1   set_replicationWebHDFS.set_replicationr  s     	

#$u+
Vr3   c                 &    U R                  SSUS9  g )NMKDIRSr   r   r   r'   rd   r/   s      r1   mkdirWebHDFS.mkdir  s    

8E
5r3   c                 r    USL a!  U R                  U5      (       a  [        U5      eU R                  U5        g )NF)existsFileExistsErrorr   )r'   rd   exist_oks      r1   makedirsWebHDFS.makedirs  s/    uT!2!2!$''

4r3   c                 &    U R                  SSXS9  g )NRENAMEr   )rI   rd   destinationr   )r'   path1path2r/   s       r1   mv
WebHDFS.mv  s    

8E
Ir3   c                 :    U R                  SSUU(       a  SOSS9  g )NDELETEdeletetruefalse)rI   rd   	recursiver   )r'   rd   r  r/   s       r1   rm
WebHDFS.rm  s#    

 )fw	 	 	
r3   c                 &    U R                  U5        g r5   )r  r   s      r1   rm_fileWebHDFS.rm_file  s    r3   c                    U R                  U5       nSR                  U R                  U5      S[        R                  " S5       3/5      n U R                  US5       n[
        R                  " XF5        S S S 5        U R                  XR5         S S S 5        g ! , (       d  f       N)= f! [         a<    [        [        5         U R                  U5        S S S 5        e ! , (       d  f       e = ff = f! , (       d  f       g = f)Nr   z.tmp.   wb)openjoin_parentsecrets	token_hexshutilcopyfileobjr   BaseExceptionr   r`   r  )r'   lpathrpathr/   lstream	tmp_fnamerstreams          r1   cp_fileWebHDFS.cp_file  s    YYu$,,u"5w?P?PQS?T>U7V!WXIYYy$/7&&w8 0	) 
 0/ ! /0GGI& 1 10 sM   :C3B*B6B*
B'	#B**C0C	C0
C,(C00C33
Dc                    U R                   (       a-  [        U R                   5      (       a  U R                  U5      nU$ U R                   (       a5  U R                   R                  5        H  u  p#UR                  X#S5      nM     U$ )N   )r   callableitemsreplace)r'   r   kvs       r1   rV   WebHDFS._apply_proxy  se    ::(4::..zz(+H
 	 ZZ

((*#++A!4 +r3   )r&   r   r   r   r"   r   r>   r#   r$   r   r!   )i  FNNNNNNFNT)r   NNT)rbNTNNFr5   )NN)+__name__
__module____qualname____firstlineno____doc__strtempfile
gettempdirrp   r   r   propertyr7   r%   rk   rv   staticmethodr~   classmethodrU   r   r}   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r  rV   __static_attributes____classcell__r0   s   @r1   r   r      s/   * (%%'(G#H
 c7J  H$H +
Z  
 3 3  (TD	:,

."
""
GMBW6
J
 r3   r   c                   N   ^  \ rS rSrSrU 4S jrS
S jrS rS rS r	S r
S	rU =r$ )ru   i  z"A file living in HDFS over webHDFSc                   > [         TU ]  " X40 UD6  UR                  5       nUR                  SS 5      c  UR	                  SS 5        UR                  SS 5      c  UR	                  SS 5        UR	                  SS5      U l        UR	                  S5      nUR	                  SS5      SL aS  U R                  U l        [        R                  R                  U[        [        R                  " 5       5      5      U l        g g )Nrs   rr   i  rp   rq   F)r   r   rW   r   r   rs   rd   targetosr  r(  uuiduuid4)r'   fsrd   r/   rp   r0   s        r1   r   WebHDFile.__init__  s    ,V,::mT*2JJ}d+::mT*2JJ}d+!::mS9**Y'::lE*e3))DKWc$**,.?@DI 4r3   c                     U R                   R                  R                  U R                  U R                  R                  5       SS0S9nUR                  5         g)zWrite one part of a multi-block file upload

Parameters
==========
final: bool
    This is the last block, so should complete file, if
    self.autocommit is True.
content-typeapplication/octet-stream)rK   r   T)r7  r>   postr   buffergetvaluerc   )r'   finalrg   s      r1   _upload_chunkWebHDFile._upload_chunk  sR     ggoo""MM%%'#%?@ # 

 	r3   c                 V   U R                   R                  5       nSU R                  ;   a  Su  p#O	Su  p#SUS'   U R                  R                  " X#U R
                  4SS0UD6nU R                  R                  UR                  S   5      nS	U R                  ;   a  U R                  R                  R                  US
S0S9nUR                  5         U R                  R                  " SSU R
                  4SS0UD6nU R                  R                  UR                  S   5      U l        gg)zCreate remote file/uploada)APPENDPOST)CREATEPUTr   	overwritere   Fr   wr:  r;  )r   rD  rE  N)r/   rW   rn   r7  rk   rd   rV   r   r>   r   rc   r   )r'   r/   rH   rI   rg   r   r   s          r1   _initiate_uploadWebHDFile._initiate_upload  s   !!#$)))JB(JB"(F;ggmmB		LELVL77''J(?@$))77??&&>3M"N ' D !!#77==6499WuWPVWD GG00j1IJDM r3   c                    [        US5      n[        U R                  U5      nX:  d  XR                  :  a  gU R                  R	                  SU R
                  XU-
  SS9nUR                  5         SUR                  ;   aY  UR                  S   nU R                  R                  R                  U R                  R                  U5      5      nUR                  $ UR                  $ )Nr   r3   OPENF)rd   offsetrz   re   r   )maxminr{   r7  rk   rd   rc   r   r>   r   rV   content)r'   startendrg   r   r   s         r1   _fetch_rangeWebHDFile._fetch_range  s    E1$))S!<5II-ggmm5uu  
 	${{:.H77??&&tww';';H'EFD<<;;r3   c                 d    U R                   R                  U R                  U R                  5        g r5   )r7  r   rd   r3  r6   s    r1   commitWebHDFile.commit  s    

499dkk*r3   c                 N    U R                   R                  U R                  5        g r5   )r7  r  rd   r6   s    r1   discardWebHDFile.discard  s    

499r3   )r   rd   rs   r3  r"  )r#  r$  r%  r&  r'  r   r@  rJ  rT  rW  rZ  r.  r/  r0  s   @r1   ru   ru     s+    ,A"K( + r3   ru   )loggingr4  r  r  r)  r5  
contextlibr   r   urllib.parser   r<   specr   r   utilsr	   r
   	getLoggerrZ   r   ru   r   r3   r1   <module>rb     sZ     	         ; 3			9	%V  VrI$ Ir3   