U
    be                     @   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 )zImplements reading and writing to/from HDFS.

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

Uses the command-line hdfs utility under the covers.

    N)utils)hdfsZviewfs)zhdfs:///path/filezhdfs://path/filezviewfs:///path/filezviewfs://path/filec                 C   sR   t j| }|jtkst|j|j }d|d }|sDt	d|  t
|j|dS )N/zinvalid HDFS URI: %r)schemeuri_path)urllibparseurlsplitr   SCHEMESAssertionErrornetlocpathlstripRuntimeErrordict)Zuri_as_stringZ	split_urir    r   Q/var/www/html/services/stratfitenv/lib/python3.8/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   Zcheck_kwargsopenr   splitname)urimodeZtransport_paramsZ
parsed_uriZfobjr   r   r   open_uri0   s
    r   c                 C   s0   |dkrt | S |dkr t| S td| d S )Nrbwbz(hdfs support for mode %r not implemented)CliRawInputBaseCliRawOutputBaseNotImplementedError)r   r   r   r   r   r   9   s
    r   c                   @   sT   e Zd ZdZdd Z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.
    c                 C   s,   || _ tjddd| j gtjd| _d | _d S )Nr   dfsz-cat)stdoutZ_uri
subprocessPopenPIPE_subrawselfr   r   r   r   __init__H   s    zCliRawInputBase.__init__c                 C   s   t d | j  d| _dS )zFlush and close this stream.zclose: calledN)loggerdebugr&   	terminater)   r   r   r   closeT   s    

zCliRawInputBase.closec                 C   s
   | j dk	S )z+Return True if the stream can be read from.Nr&   r.   r   r   r   readableZ   s    zCliRawInputBase.readablec                 C   s   dS z;If False, seek(), tell() and truncate() will raise IOError.Fr   r.   r   r   r   seekable^   s    zCliRawInputBase.seekablec                 C   s
   t jdS )zUnsupported.NioUnsupportedOperationr.   r   r   r   detache   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   r8   i   s    zCliRawInputBase.readc                 C   s   | j |dS )zThis is the same as read().)r:   )r8   r9   r   r   r   read1m   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)r8   len)r)   bdatar   r   r   readintoq   s
    zCliRawInputBase.readintoN)r   )r   )__name__
__module____qualname____doc__r*   r/   r1   r3   r7   r8   r;   r?   r   r   r   r   r   B   s   

r   c                   @   sH   e Zd ZdZdd Z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.
    c                 C   s0   || _ tjddddd| j gtjd| _d | _d S )Nr   r    z-putz-f-)stdinr"   r(   r   r   r   r*      s
    zCliRawOutputBase.__init__c                 C   s"   |    | jj  | j  d S N)flushr&   rE   r/   waitr.   r   r   r   r/      s    zCliRawOutputBase.closec                 C   s   | j j  d S rF   )r&   rE   rG   r.   r   r   r   rG      s    zCliRawOutputBase.flushc                 C   s
   | j dk	S )z(Return True if this object is writeable.Nr0   r.   r   r   r   	writeable   s    zCliRawOutputBase.writeablec                 C   s   dS r2   r   r.   r   r   r   r3      s    zCliRawOutputBase.seekablec                 C   s   | j j| d S rF   )r&   rE   write)r)   r=   r   r   r   rJ      s    zCliRawOutputBase.writec                 C   s   t dd S )Nzdetach() not supportedr4   r.   r   r   r   r7      s    zCliRawOutputBase.detachN)r@   rA   rB   rC   r*   r/   rG   rI   r3   rJ   r7   r   r   r   r   r   {   s   
r   )rC   r5   loggingr#   urllib.parser   Z
smart_openr   	getLoggerr@   r+   r
   ZURI_EXAMPLESr   r   r   	RawIOBaser   r   r   r   r   r   <module>	   s   
		9