
    #HJj'                       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m	Z	m
Z
 ddlZe	rddlmZmZ ddlmZ  ej$                  e      ZdZdZ G d	 d
e
      ZddZddZddZ G d dej6                        Z G d dej6                        Zy)z,Implements reading and writing to/from HDFS.    )annotationsN)TYPE_CHECKING	TypedDict)ReadableBufferWriteableBuffer)TransportParams)hdfsviewfs)zhdfs:///path/filezhdfs://host/path/filezhdfs://host:port/path/filezviewfs:///path/filezviewfs://host/path/filec                  "    e Zd ZU ded<   ded<   y)_HDFSUristrschemeuri_pathN)__name__
__module____qualname____annotations__     Y/Users/ahmed/devFolder/claude-voice/.venv/lib/python3.12/site-packages/smart_open/hdfs.pyr   r   &   s    KMr   r   c                    t         j                  j                  |       }|j                  t        v sJ |j
                  r| n|j                  }|r|dk(  rd| }t        |      |j                  |dS )zBParse an ``hdfs://`` or ``viewfs://`` URI into its path component./zinvalid HDFS URI: )r   r   )urllibparseurlsplitr   SCHEMESnetlocpathRuntimeError)uri_as_string	split_urir   msgs       r   	parse_urir#   +   sq    %%m4Iw&&&
 !* 0 0}innHx3"="343&&H==r   c                    t         j                  j                  t        |       t	        |       }t        |d   |      }|d   j                  d      d   |_        |S )z;Open an HDFS URI using the given mode and transport params.r   r   )
smart_openutilscheck_kwargsopenr#   splitname)urimodetransport_params
parsed_urifobjs        r   open_urir1   ;   sT    !!$(893J
:&-D:&,,S1"5DIKr   c                d    |dk(  rt        |       S |dk(  rt        |       S d|d}t        |      )z@Open an HDFS `uri` for reading (``"rb"``) or writing (``"wb"``).rbwbzhdfs support for mode z not implemented)CliRawInputBaseCliRawOutputBaseNotImplementedError)r,   r-   r"   s      r   r)   r)   E   s@    t|s##t|$$"4(*:
;C
c
""r   c                      e Zd ZU dZded<   dZded<   ddZddZedd	       Z	dd
Z
ddZddZdddZdddZddZy)r5   zReads bytes from HDFS via the "hdfs dfs" command-line interface.

    Implements the io.RawIOBase interface of the standard library.
    r   r+   Nsubprocess.Popen[bytes] | None_subc                    || _         t        j                  ddd| j                   gt        j                        | _        y )Nr	   dfsz-cat)stdout_uri
subprocessPopenPIPEr:   selfr,   s     r   __init__zCliRawInputBase.__init__X   s1    	$$feVTYY%GPZP_P_`	r   c                z    t         j                  d       | j                  }||j                          d| _        yyzFlush and close this stream.zclose: calledN)loggerdebugr:   	terminaterD   subs     r   closezCliRawInputBase.close_   s1    _%ii?MMODI r   c                    | j                   du S z$Return True if the stream is closed.Nr:   rD   s    r   closedzCliRawInputBase.closedg        yyD  r   c                    | j                   duS )z+Return True if the stream can be read from.NrP   rQ   s    r   readablezCliRawInputBase.readablel       yy$$r   c                     yz2Return False; HDFS streams do not support seeking.Fr   rQ   s    r   seekablezCliRawInputBase.seekablep       r   c                "    t         j                  )Unsupported.ioUnsupportedOperationrQ   s    r   detachzCliRawInputBase.detachw   s    %%%r   c                |    | j                   }|J |j                  J |d}|j                  j                  |      S )z6Read up to size bytes from the object and return them.r%   )r:   r=   read)rD   sizerL   s      r   rb   zCliRawInputBase.read{   sB    iizz%%%<Dzzt$$r   c                &    | j                  |      S )zThis is the same as read().)rc   )rb   )rD   rc   s     r   read1zCliRawInputBase.read1   s    yydy##r   c                    t        |      j                  d      }| j                  t        |            }|sy||dt        |       t        |      S )zIRead up to ``len(b)`` bytes into `b` and return the number of bytes read.Br   N)
memoryviewcastrb   len)rD   bmvdatas       r   readintozCliRawInputBase.readinto   sG    ]$yyR!;SY4yr   r,   r   returnNonerp   rq   rp   boolrp   zio.RawIOBase)r%   )rc   z
int | Nonerp   bytes)rk   r   rp   int)r   r   r   __doc__r   r:   rE   rM   propertyrR   rU   rY   r`   rb   re   rn   r   r   r   r5   r5   O   sV    
 I+/D
(/a ! !%&%$r   r5   c                  t    e Zd ZU dZded<   dZded<   ddZddZedd	       Z	dd
Z
ddZddZddZddZy)r6   zWrites bytes to HDFS via the "hdfs dfs" command-line interface.

    Implements the io.RawIOBase interface of the standard library.
    r   r+   Nr9   r:   c                    || _         t        j                  ddddd| j                   gt        j                        | _        y )Nr	   r<   z-putz-f-)stdinr>   rC   s     r   rE   zCliRawOutputBase.__init__   s5    	$$feVT3		%RZdZiZij	r   c                    t         j                  d       | j                  }|P|j                  J | j	                          |j                  j                          |j                          d| _        yyrG   )rH   rI   r:   r}   flushrM   waitrK   s     r   rM   zCliRawOutputBase.close   sX    _%ii?99(((JJLIIOOHHJDI r   c                    | j                   du S rO   rP   rQ   s    r   rR   zCliRawOutputBase.closed   rS   r   c                t    | j                   }|J |j                  J |j                  j                          y)z8Flush the underlying ``hdfs dfs -put`` subprocess stdin.N)r:   r}   r   rK   s     r   r   zCliRawOutputBase.flush   s3    iiyy$$$		r   c                    | j                   duS )z(Return True if this object is writeable.NrP   rQ   s    r   	writeablezCliRawOutputBase.writeable   rV   r   c                     yrX   r   rQ   s    r   rY   zCliRawOutputBase.seekable   rZ   r   c                t    | j                   }|J |j                  J |j                  j                  |      S )a  Write the given buffer to the underlying raw stream.

        Returns the number of bytes written, as required by
        :class:`io.RawIOBase`. Without this return value, callers that wrap
        this stream and rely on the documented ``write`` contract (for
        example, ``ray._private.external_storage._write_multiple_objects``,
        which asserts ``written_bytes == payload_len``) fail with an
        ``AssertionError`` because ``write`` would otherwise implicitly
        return ``None``.
        )r:   r}   write)rD   rk   rL   s      r   r   zCliRawOutputBase.write   s:     iiyy$$$yyq!!r   c                0    d}t        j                  |      )r\   zdetach() not supportedr]   )rD   r"   s     r   r`   zCliRawOutputBase.detach   s    &%%c**r   ro   rr   rs   )rk   r   rp   rw   ru   )r   r   r   rx   r   r:   rE   rM   ry   rR   r   r   rY   r   r`   r   r   r   r6   r6      sQ    
 I+/D
(/k	 ! !%"&+r   r6   )r    r   rp   r   )r,   r   r-   r   r.   r   rp   "CliRawInputBase | CliRawOutputBase)r,   r   r-   r   rp   r   )rx   
__future__r   r^   loggingr@   urllib.parser   typingr   r   smart_open.utilsr&   	_typeshedr   r   smart_open._typingr   	getLoggerr   rH   r   URI_EXAMPLESr   r#   r1   r)   	RawIOBaser5   r6   r   r   r   <module>r      s    3 " 	    + 92			8	$
y 
> #@bll @FB+r|| B+r   