�
=OXc@spdZddlZddlZddlZddlmZmZdefd��YZde fd��YZ
dS(s�
Attempt to generalize the "feeder" part of a `.Channel`: an object which can be
read from and closed, but is reading from a buffer fed by another thread. The
read operations are blocking and can have a timeout set.
i����N(tPY2tbtPipeTimeoutcBseZdZRS(sP
Indicates that a timeout was reached on a read from a `.BufferedPipe`.
(t__name__t
__module__t__doc__(((s:/usr/lib/python2.7/site-packages/paramiko/buffered_pipe.pyRstBufferedPipecBs�eZdZd�Zer3d�Zd
d�Znd�Zd
d�Zd�Zd�Z d�Z
d
d �Zd
�Zd�Z
d�ZRS(s�
A buffer that obeys normal read (with timeout) & close semantics for a
file or socket, but is fed data from another thread. This is used by
`.Channel`.
cCsLtj�|_tj|j�|_d|_tjd�|_t |_
dS(NtB(t threadingtLockt_lockt Conditiont_cvtNonet_eventtarrayt_buffertFalset_closed(tself((s:/usr/lib/python2.7/site-packages/paramiko/buffered_pipe.pyt__init__-s
cCs|jj|�dS(N(Rt
fromstring(Rtdata((s:/usr/lib/python2.7/site-packages/paramiko/buffered_pipe.pyt_buffer_frombytes5scCs|j| j�S(N(Rttostring(Rtlimit((s:/usr/lib/python2.7/site-packages/paramiko/buffered_pipe.pyt_buffer_tobytes8scCs|jj|�dS(N(Rt frombytes(RR((s:/usr/lib/python2.7/site-packages/paramiko/buffered_pipe.pyR;scCs|j| j�S(N(Rttobytes(RR((s:/usr/lib/python2.7/site-packages/paramiko/buffered_pipe.pyR>scCsd|jj�zB||_|js7t|j�dkrD|j�n
|j�Wd|jj�XdS(s
Set an event on this buffer. When data is ready to be read (or the
buffer has been closed), the event will be set. When no data is
ready, the event will be cleared.
:param threading.Event event: the event to set/clear
iN( R
tacquireRRtlenRtsettcleartrelease(Rtevent((s:/usr/lib/python2.7/site-packages/paramiko/buffered_pipe.pyt set_eventAs
cCse|jj�zC|jdk r/|jj�n|jt|��|jj�Wd|jj �XdS(s�
Feed new data into this pipe. This method is assumed to be called
from a separate thread, so synchronization is done.
:param data: the data to add, as a `str` or `bytes`
N(
R
RRR
RRRRt notifyAllR!(RR((s:/usr/lib/python2.7/site-packages/paramiko/buffered_pipe.pytfeedXs
cCsC|jj�z!t|j�dkr)tStSWd|jj�XdS(sl
Returns true if data is buffered and ready to be read from this
feeder. A ``False`` result does not mean that the feeder has closed;
it means you may need to wait before more data arrives.
:return:
``True`` if a `read` call would immediately return at least one
byte; ``False`` otherwise.
iN(R
RRRRtTrueR!(R((s:/usr/lib/python2.7/site-packages/paramiko/buffered_pipe.pyt
read_readyhs
cCsVt�}|jj�z+t|j�dkr�|jr;|S|dkrSt��nx}t|j�dkr�|jr�tj�}|jj |�|dk rV|tj�|8}|dkr�t��q�qVqVWnt|j�|kr'|j�}|j2|jdk r@|jr@|jj
�q@n|j|�}|j|4Wd|jj�X|S(sq
Read data from the pipe. The return value is a string representing
the data received. The maximum amount of data to be received at once
is specified by ``nbytes``. If a string of length zero is returned,
the pipe has been closed.
The optional ``timeout`` argument can be a nonnegative float expressing
seconds, or ``None`` for no timeout. If a float is given, a
`.PipeTimeout` will be raised if the timeout period value has elapsed
before any data arrives.
:param int nbytes: maximum number of bytes to read
:param float timeout:
maximum seconds to wait (or ``None``, the default, to wait forever)
:return: the read data, as a `bytes`
:raises PipeTimeout:
if a timeout was specified and no data was ready before that
timeout
igN(tbytesR
RRRRRttimeRtwaitR
RRR R!(Rtnbytesttimeouttouttthen((s:/usr/lib/python2.7/site-packages/paramiko/buffered_pipe.pytreadzs0
"cCsf|jj�zD|j�}|j2|jdk rL|jrL|jj�n|SWd|jj�XdS(s�
Clear out the buffer and return all data that was in it.
:return:
any data that was in the buffer prior to clearing it out, as a
`str`
N( R
RRRRR
RR R!(RR-((s:/usr/lib/python2.7/site-packages/paramiko/buffered_pipe.pytempty�s
cCs[|jj�z9t|_|jj�|jdk rE|jj�nWd|jj �XdS(s�
Close this pipe object. Future calls to `read` after the buffer
has been emptied will return immediately with an empty string.
N(
R
RR&RRR$RR
RR!(R((s:/usr/lib/python2.7/site-packages/paramiko/buffered_pipe.pytclose�s
cCs3|jj�zt|j�SWd|jj�XdS(sq
Return the number of bytes buffered.
:return: number (`int`) of bytes buffered
N(R
RRRR!(R((s:/usr/lib/python2.7/site-packages/paramiko/buffered_pipe.pyt__len__�s
N(RRRRRRR
RR#R%R'R/R0R1R2(((s:/usr/lib/python2.7/site-packages/paramiko/buffered_pipe.pyR&s 6 (RRRR)tparamiko.py3compatRRtIOErrorRtobjectR(((s:/usr/lib/python2.7/site-packages/paramiko/buffered_pipe.pyt<module>s |