o
    
ic                     @   s   d Z ddlZddlZddlZddlZddlmZ ee	Z
dZdZdd Zdd	 Zd
d ZG dd dejZG dd dejZdS )z,Implements reading and writing to/from HDFS.    N)utils)hdfsviewfs)zhdfs:///path/filezhdfs://path/filezviewfs:///path/filezviewfs://path/filec                 C   sR   t j| }|jtv sJ |j|j }d|d }|s"td|  t	|j|dS )N/zinvalid HDFS URI: %r)schemeuri_path)
urllibparseurlsplitr   SCHEMESnetlocpathlstripRuntimeErrordict)uri_as_string	split_urir    r   C/home/ubuntu/.local/lib/python3.10/site-packages/smart_open/hdfs.py	parse_uri   s   r   c                 C   s:   t t| t| }t|d |}|d dd |_|S )Nr   r   )r   check_kwargsopenr   splitname)urimodetransport_params
parsed_urifobjr   r   r   open_uri*   s
   r    c                 C   s,   |dkrt | S |dkrt| S td| )Nrbwbz(hdfs support for mode %r not implemented)CliRawInputBaseCliRawOutputBaseNotImplementedError)r   r   r   r   r   r   3   s
   r   c                   @   sd   e Zd ZdZdZ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dS )r#   zReads bytes from HDFS via the "hdfs dfs" command-line interface.

    Implements the io.RawIOBase interface of the standard library.
    Nc                 C   s&   || _ tjddd| j gtjd| _d S )Nr   dfsz-cat)stdout_uri
subprocessPopenPIPE_subselfr   r   r   r   __init__C   s    zCliRawInputBase.__init__c                 C   s(   t d | js| j  d| _dS dS )zFlush and close this stream.close: calledN)loggerdebugclosedr-   	terminater/   r   r   r   closeJ   s
   


zCliRawInputBase.closec                 C   
   | j d u S Nr-   r6   r   r   r   r4   Q      
zCliRawInputBase.closedc                 C   
   | j duS )z+Return True if the stream can be read from.Nr:   r6   r   r   r   readableU   r;   zCliRawInputBase.readablec                 C      dS z;If False, seek(), tell() and truncate() will raise IOError.Fr   r6   r   r   r   seekableY      zCliRawInputBase.seekablec                 C   s   t j)zUnsupported.ioUnsupportedOperationr6   r   r   r   detach`   s   zCliRawInputBase.detachr   c                 C   s   | j j|S )z6Read up to size bytes from the object and return them.)r-   r'   readr/   sizer   r   r   rF   d   s   zCliRawInputBase.readc                 C   s   | j |dS )zThis is the same as read().)rH   )rF   rG   r   r   r   read1h   s   zCliRawInputBase.read1c                 C   s.   |  t|}|sdS ||dt|< t|S )zLRead up to len(b) bytes into b, and return the number of bytes
        read.r   N)rF   len)r/   bdatar   r   r   readintol   s
   zCliRawInputBase.readinto)r   )__name__
__module____qualname____doc__r-   r0   r7   propertyr4   r=   r@   rE   rF   rI   rM   r   r   r   r   r#   <   s    


r#   c                   @   sX   e Zd ZdZdZ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dS )r$   zWrites bytes to HDFS via the "hdfs dfs" command-line interface.

    Implements the io.RawIOBase interface of the standard library.
    Nc                 C   s*   || _ tjddddd| j gtjd| _d S )Nr   r&   z-putz-f-)stdinr(   r.   r   r   r   r0   }   s   zCliRawOutputBase.__init__c                 C   s<   t d | js|   | jj  | j  d | _d S d S )Nr1   )r2   r3   r4   flushr-   rT   r7   waitr6   r   r   r   r7      s   


zCliRawOutputBase.closec                 C   r8   r9   r:   r6   r   r   r   r4      r;   zCliRawOutputBase.closedc                 C   s   | j j  d S r9   )r-   rT   rU   r6   r   r   r   rU      s   zCliRawOutputBase.flushc                 C   r<   )z(Return True if this object is writeable.Nr:   r6   r   r   r   	writeable   r;   zCliRawOutputBase.writeablec                 C   r>   r?   r   r6   r   r   r   r@      rA   zCliRawOutputBase.seekablec                 C   s   | j j| d S r9   )r-   rT   write)r/   rK   r   r   r   rX      s   zCliRawOutputBase.writec                 C   s
   t d)Nzdetach() not supportedrB   r6   r   r   r   rE      s   
zCliRawOutputBase.detach)rN   rO   rP   rQ   r-   r0   r7   rR   r4   rU   rW   r@   rX   rE   r   r   r   r   r$   v   s    
r$   )rQ   rC   loggingr*   urllib.parser   
smart_openr   	getLoggerrN   r2   r   URI_EXAMPLESr   r    r   	RawIOBaser#   r$   r   r   r   r   <module>   s   
		: