
    7ThA                         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
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)quote   )AbstractBufferedFileAbstractFileSystem)infer_storage_optionstokenizewebhdfsc                   \  ^  \ 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"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                 V/var/www/auris/envauris/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    r0   fsidWebHDFS.fsid   s    zzr2   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_kerberosr9   r   authr    r!   requests.authr:   )r&   r9   r:   s      r0   r$   WebHDFS._connect   s    '') $ 1 1DLL"1199: 0 D43C3C DDLL99 T]]%>3 -dii GDLL &? r2   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&   rG   rH   pathrJ   redirectr.   r   argsouterrmsgexps                r0   _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	WebHDFilero   )r&   rc   rm   rn   rp   rq   rr   r.   s           r0   _openWebHDFS._open   s<    B  14>>
!LL!##	
 		
r2   c                 B    U S   R                  5       U S'   U S   U S'   U $ )Ntypelengthsize)lower)infos    r0   _process_infoWebHDFS._process_info   s*    F|))+VH~Vr2   c                     [        U5      S   $ )Nrc   )r   )clsrc   s     r0   rT   WebHDFS._strip_protocol   s    $T*622r2   c                     [        U 5      nUR                  SS 5        UR                  SS 5        SU;   a  UR                  S5      US'   U$ )Nrc   protocolusernamer    )r   pop)urlpathrf   s     r0   _get_kwargs_from_urlsWebHDFS._get_kwargs_from_urls   sG    #G,
D!''*-CK
r2   c                 r    U R                  SUS9nUR                  5       S   nXS'   U R                  U5      $ )NGETFILESTATUSrc   
FileStatusname)rj   r]   r}   )r&   rc   rf   r|   s       r0   r|   WebHDFS.info	  s<    jjtj4xxz,'V!!$''r2   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    r0   <lambda>WebHDFS.ls.<locals>.<lambda>  s    qyr2   )keyc              3   *   #    U  H	  oS    v   M     g7f)r   Nr   ).0r|   s     r0   	<genexpr>WebHDFS.ls.<locals>.<genexpr>  s     954v,5s   )rj   r]   r}   rstripsorted)r&   rc   detailrf   infosr|   s         r0   ls
WebHDFS.ls  s    jjDj1
>*<8Dt$;;s+c1D4FFDL  %%89995999r2   c                 H    U R                  SUS9nUR                  5       S   $ )z8Total numbers of files, directories and bytes under pathGETCONTENTSUMMARYr   ContentSummaryrj   r]   )r&   rc   rf   s      r0   content_summaryWebHDFS.content_summary  s'    jj,4j8xxz*++r2   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)rc   rd   LocationFileChecksum)rj   headersrU   r=   getrb   r]   )r&   rc   rf   locationout2s        r0   ukeyWebHDFS.ukey  s    jj*jF$((Z)@AH<<##H-D!!#99;~..  "88:n--r2   c                 J    U R                  S5      nUR                  5       S   $ )zGet user's home directoryGETHOMEDIRECTORYPathr   )r&   rf   s     r0   home_directoryWebHDFS.home_directory+  s"    jj+,xxz&!!r2   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)rj   r]   r   )r&   r   rf   ts       r0   get_delegation_tokenWebHDFS.get_delegation_token0  sT     **17*CC**12CHHJw9PQQ~r2   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rH   r*   longr   )r&   r*   rf   s      r0   renew_delegation_tokenWebHDFS.renew_delegation_tokenA  s(    jj/UjKxxz&!!r2   c                 &    U R                  SSUS9  g)z Stop the token from being usefulCANCELDELEGATIONTOKENr   r   Nrj   )r&   r*   s     r0   cancel_delegation_tokenWebHDFS.cancel_delegation_tokenF  s    

*5
Fr2   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   )rH   rc   
permissionNr   )r&   rc   mods      r0   chmodWebHDFS.chmodJ  s     	

?5t
Lr2   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   rH   rc   )SETOWNERr   )r&   rc   r   r   r.   s        r0   chownWebHDFS.chownW  s7    #7O#7O

Ae$A&Ar2   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   )rc   rH   rq   Nr   )r&   rc   rq   s      r0   set_replicationWebHDFS.set_replication`  s     	

#$u+
Vr2   c                 &    U R                  SSUS9  g )NMKDIRSr   r   r   r&   rc   r.   s      r0   mkdirWebHDFS.mkdirn  s    

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

4r2   c                 &    U R                  SSXS9  g )NRENAMEr   )rH   rc   destinationr   )r&   path1path2r.   s       r0   mv
WebHDFS.mvv  s    

8E
Ir2   c                 :    U R                  SSUU(       a  SOSS9  g )NDELETEdeletetruefalse)rH   rc   	recursiver   )r&   rc   r   r.   s       r0   rm
WebHDFS.rmy  s#    

 )fw	 	 	
r2   c                 &    U R                  U5        g r4   )r   r   s      r0   rm_fileWebHDFS.rm_file  s    r2   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          r0   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       r0   rU   WebHDFS._apply_proxy  se    ::(4::..zz(+H
 	 ZZ

((*#++A!4 +r2   )r%   r   r   r   r!   r   r=   r"   r#   r   r    )i  FNNNNNNFNT)r   NNT)rbNTNNFr4   )NN))__name__
__module____qualname____firstlineno____doc__strtempfile
gettempdirro   r   r   propertyr6   r$   rj   ru   staticmethodr}   classmethodrT   r   r|   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r
  rU   __static_attributes____classcell__r/   s   @r0   r   r      s#   * (%%'(G#H
 c7J  H$H +
Z  
 3 3  (	:,

."
""
GMBW6
J
 r2   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$ )rt   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 )Nrr   rq   i  ro   rp   F)r   r   rV   r   r   rr   rc   targetosr   r  uuiduuid4)r&   fsrc   r.   ro   r/   s        r0   r   WebHDFile.__init__  s    ,V,::mT*2JJ}d+::mT*2JJ}d+!::mS9**Y'::lE*e3))DKWc$**,.?@DI 4r2   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)rJ   r   T)r*  r=   postr   buffergetvaluerb   )r&   finalrf   s      r0   _upload_chunkWebHDFile._upload_chunk  sR     ggoo""MM%%'#%?@ # 

 	r2   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   	overwriterd   Fr   wr-  r.  )r   r7  r8  N)r.   rV   rm   r*  rj   rc   rU   r   r=   r   rb   r   )r&   r.   rG   rH   rf   r   r   s          r0   _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 r2   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   r2   OPENF)rc   offsetry   rd   r   )maxminrz   r*  rj   rc   rb   r   r=   r   rU   content)r&   startendrf   r   r   s         r0   _fetch_rangeWebHDFile._fetch_range  s    E1$))S!<5II-ggmm5uu  
 	${{:.H77??&&tww';';H'EFD<<;;r2   c                 d    U R                   R                  U R                  U R                  5        g r4   )r*  r   rc   r&  r5   s    r0   commitWebHDFile.commit  s    

499dkk*r2   c                 N    U R                   R                  U R                  5        g r4   )r*  r   rc   r5   s    r0   discardWebHDFile.discard  s    

499r2   )r   rc   rr   r&  r  )r  r  r  r  r  r   r3  r=  rG  rJ  rM  r!  r"  r#  s   @r0   rt   rt     s+    ,A"K( + r2   rt   )loggingr'  r   r  r  r(  
contextlibr   urllib.parser   r;   specr   r   utilsr   r	   	getLoggerrY   r   rt   r   r2   r0   <module>rU     sW     	        ; 3			9	%E  EPI$ Ir2   