
    h!                         S r SSKrSSKrSSKr SSKrSSKJ	r	J
r
  SSKJr  \R                  " \5      rSrSrSrS rS	 r\4S
 jrS rS r " S S\R2                  5      r " S S\R2                  5      r " S S\5      rg! \ a    Sr Nxf = f)zyImplements reading and writing to/from WebHDFS.

The main entry point is the :func:`~smart_open.webhdfs.open` function.

    NT)utils	constantswebhdfs)zwebhdfs://host:port/path/filei   c                     [        [        U S9$ )N)schemeuri)dictSCHEME)
uri_as_strs    L/home/james-whalen/.local/lib/python3.13/site-packages/smart_open/webhdfs.py	parse_urir   '   s    v:..    c                 P    [         R                  " [        U5      n[        X40 UD6$ N)r   check_kwargsopen)r   modetransport_paramskwargss       r   open_urir   +   s%    &67F$V$$r   c                    U R                  [        5      (       a  [        U 5      n U[        R                  :X  a  [        U 5      nO,U[        R                  :X  a
  [        XS9nO[        SU-  5      eU R                  S5      S   Ul
        U$ )z
Parameters
----------
http_uri: str
    webhdfs url converted to http REST url
min_part_size: int, optional
    For writing only.

)min_part_sizez+webhdfs support for mode %r not implemented/)
startswithr
   _convert_to_http_urir   READ_BINARYBufferedInputBaseWRITE_BINARYBufferedOutputBaseNotImplementedErrorsplitname)http_urir   r   fobjs       r   r   r   0   s{     6""'1y$$$ *	''	'!(H!"ORV"VWWs#B'DIKr   c                    [         R                  R                  U 5      nUR                  nUR                  (       a  USR                  UR                  5      -  nUR                  nUR                  (       a;  UU(       a  SOSS-   [         R                  R                  UR                  5      -   -  n[         R                  R                  SUSUR                  -   US45      $ )z
Convert webhdfs uri to http url and return it as text

Parameters
----------
webhdfs_url: str
    A URL starting with webhdfs://
z:{}& z
user.name=httpz/webhdfs/v1)urllibparseurlsplithostnameportformatqueryusernamequote
urlunsplitpath)webhdfs_url	split_urinetlocr0   s       r   r   r   H   s     %%k2IF~~%,,y~~..OOESr\1FLL4F4FyGYGY4ZZ	
 <<""	7C r   c                 ,    [        U R                  5      $ r   )r   r   )
parsed_uris    r   convert_to_http_urir:   c   s    
//r   c                   f    \ rS rSrSrS rS r\S 5       rS r	S r
S rSS	 jrSS
 jrS rS rSrg)r   g   Nc                     Xl         SSS.n[        R                  " U R                   USS9U l        U R                  R                  [
        R                  :w  a  [        R                  U R                  5      eSU l	        g )NOPENr   )opoffsetT)paramsstreamr   )
_urirequestsget	_responsestatus_codehttplibOKWebHdfsExceptionfrom_response_buf)selfr   payloads      r   __init__BufferedInputBase.__init__j   s[    	1-!diiM>>%%3"00@@	r   c                 `    [         R                  S5        U R                  (       d  SU l        gg)zFlush and close this stream.zclose: calledN)loggerdebugclosedrL   rM   s    r   closeBufferedInputBase.closev   s!    _%{{DI r   c                     U R                   S L $ r   )rL   rU   s    r   rT   BufferedInputBase.closed|   s    yyD  r   c                     g)z+Return True if the stream can be read from.T rU   s    r   readableBufferedInputBase.readable       r   c                     g)zqIf False, seek(), tell() and truncate() will raise IOError.

We offer only seek support, and no truncate support.Fr[   rU   s    r   seekableBufferedInputBase.seekable   s     r   c                 "    [         R                  e)zUnsupported.ioUnsupportedOperationrU   s    r   detachBufferedInputBase.detach   s    %%%r   c                    Uc;  SU R                   U R                  R                  R                  5       -   sU l         nU$ U[	        U R                   5      :  a&  U R                   US  U R                   S U sU l         nU$  U R                   /nSnXA:  ai  U R                  R                  R                  [
        R                  5      n[	        U5      S:X  a  O&U[	        U5      -  nUR                  U5        XA:  a  Mi  SR                  W5      U l         U R                   US  U R                   S U sU l         nU$ ! [         a     NHf = f)Nr   r   )
rL   rF   rawreadlenrd   DEFAULT_BUFFER_SIZEappendStopIterationjoin)rM   sizeretvalbuffers
total_readraw_datas         r   rj   BufferedInputBase.read   s,   < #TYY1C1C1H1H1J%JDIvMC		N" $		$% 0$))ET2BDIvM	yykGJ#>>--2223I3IJ x=A%c(m+
x( # HHW%	 IIde,dii.>	6  		s   ?AD8 $D8 8
EEc                      U R                  US9$ )zThis is the same as read().)rp   )rj   )rM   rp   s     r   read1BufferedInputBase.read1   s    yydy##r   c                 v    U R                  [        U5      5      nU(       d  gX!S[        U5      & [        U5      $ )zDRead up to len(b) bytes into b, and return the number of bytes
read.r   N)rj   rk   )rM   bdatas      r   readintoBufferedInputBase.readinto   s3     yyQ *3t94yr   c                 x    SU R                   U R                  R                  R                  5       -   sU l         nU$ )Nr   )rL   rF   ri   readline)rM   rq   s     r   r   BufferedInputBase.readline   s1    T^^-?-?-H-H-J!J	6r   )rL   rF   rC   r   )r   )__name__
__module____qualname____firstlineno__rL   rO   rV   propertyrT   r\   r`   rf   rj   rw   r|   r   __static_attributes__r[   r   r   r   r   g   sH    D ! !&6$r   r   c                   N    \ rS rSr\4S jrS rS rS rS r	S r
\S 5       rS	rg
)r       c                    Xl         SU l        X l        SSS.n[        R                  " U R                   USS9nUR
                  [        R                  :X  d  [        R                  U5      eUR                  S   n[        R                  " USSS	0S
9nUR
                  [        R                  :X  d  [        R                  U5      e/ U l        SU l        SU l        SU l        SU l        g)zK
Parameters
----------
min_part_size: int, optional
    For writing only.

FCREATET)r?   	overwriterA   allow_redirectslocationr(   content-typeapplication/octet-streamr{   headersr   N)rC   _closedr   rD   putrG   rH   TEMPORARY_REDIRECTrJ   rK   r   CREATEDlinespartschunk_bytes
total_sizeri   )rM   r   r   rN   init_responseresponses         r   rO   BufferedOutputBase.__init__   s     	*!5 TYYwPUV((G,F,FF"00??##J/<<"~Ga6bc##w6"00::


 r   c                     g)z+Return True if the stream supports writing.Tr[   rU   s    r   writableBufferedOutputBase.writable   r^   r   c                 .    [         R                  " S5      e)Nzdetach() not supportedrc   rU   s    r   rf   BufferedOutputBase.detach   s    %%&>??r   c                 d   SS0n[         R                  " U R                  USS9nUR                  [        R
                  :X  d  [        R                  U5      eUR                  S   n[         R                  " XASS0S9nUR                  [        R                  :X  d  [        R                  U5      eg )	Nr?   APPENDFr   r   r   r   r   )
rD   postrC   rG   rH   r   rJ   rK   r   rI   )rM   r{   rN   r   r   r   s         r   _uploadBufferedOutputBase._upload   s    " diiQVW((G,F,FF"00??##J/==*8:T)UW##wzz1"00:: 2r   c                    U R                   (       a  [        S5      e[        U[        5      (       d  [	        S5      eU R
                  R                  U5        U =R                  [        U5      -  sl        U =R                  [        U5      -  sl	        U R                  U R                  :  a  SR                  U R
                  5      n[        R                  SU R                  [        U5      U R                  S-  5        U R                  U5        [        R!                  SU R                  5        U =R                  S-  sl        / SsU l        U l        g	g	)
zP
Write the given bytes (binary string) into the WebHDFS file from constructor.

zI/O operation on closed filezinput must be a binary stringr   z+uploading part #%i, %i bytes (total %.3fGB)      Azupload of part #%i finished   r   N)r   
ValueError
isinstancebytes	TypeErrorr   rm   r   rk   r   r   ro   rR   infor   r   rS   )rM   rz   buffs      r   writeBufferedOutputBase.write   s    
 <<;<<!U##;<<

!CF"3q6!t11188DJJ'DKK=

CIt'D LLLL6

CJJ!OJ+-q(DJ( 2r   c                 (   SR                  U R                  5      nU(       ai  [        R                  SU R                  [        U5      U R                  S-  5        U R                  U5        [        R                  SU R                  5        SU l	        g )Nr   z0uploading last part #%i, %i bytes (total %.3fGB)r   z upload of last part #%i finishedT)
ro   r   rR   r   r   rk   r   r   rS   r   )rM   r   s     r   rV   BufferedOutputBase.close  sf    xx

#KKB

CIt'D LLLL;TZZHr   c                     U R                   $ r   )r   rU   s    r   rT   BufferedOutputBase.closed  s    ||r   )r   rC   r   r   r   r   ri   r   N)r   r   r   r   MIN_PART_SIZErO   r   rf   r   r   rV   r   rT   r   r[   r   r   r    r       s9    *7 B@	;14	  r   r    c                   B   ^  \ rS rSrSU 4S jjrS r\S 5       rSrU =r	$ )rJ   i  c                 V   > Xl         X l        [        [        U ]  [        U 5      5        g r   )msgrG   superrJ   rO   repr)rM   r   rG   	__class__s      r   rO   WebHdfsException.__init__   s"    &.tDz:r   c                 x    SR                  U R                  R                  U R                  U R                  5      $ )Nz{}(status_code={}, msg={!r}))r/   r   r   rG   r   rU   s    r   __repr__WebHdfsException.__repr__%  s0    -44NN##T%5%5txx
 	
r   c                 8    U " UR                   UR                  S9$ )Nr   rG   )textrG   )clsr   s     r   rK   WebHdfsException.from_response*  s    x}}(2F2FGGr   r   )r(   N)
r   r   r   r   rO   r   classmethodrK   r   __classcell__)r   s   @r   rJ   rJ     s#    ;


 H Hr   rJ   )__doc__rd   loggingurllib.parser*   rD   ImportErrorMISSING_DEPS
smart_openr   r   http.clientclientrH   	getLoggerr   rR   r
   URI_EXAMPLESr   r   r   r   r   r:   BufferedIOBaser   r    	ExceptionrJ   r[   r   r   <module>r      s    
   ( 			8	$	 /%
 (5 060T)) Tn^** ^BHy HU  Ls   B BB