
    qi                     l    S r SSKrSSKJr  Sr\R
                  " \5      r " S S5      r " S S5      r	g)	a  
Implement the FileLikeQueue helper class.

This helper class is intended to handle use cases where an incoming PUT
request should be directly streamed to a remote target.

Usage: return an instance of this class to`begin_write` and pass it to the
consumer at the same time::

    def begin_write(self, contentType=None):
        queue = FileLikeQueue(max_size=1)
        requests.post(..., data=queue)
        return queue

    N)utilreStructuredTextc                   8    \ rS rSrSrS	S jrS	S jrS rS rSr	g)
FileLikeQueue    a  A queue for chunks that behaves like a file-like.

read() and write() are typically called from different threads.

This helper class is intended to handle use cases where an incoming PUT
request should be directly streamed to a remote target:

def begin_write(self, contentType=None):
    # Create a proxy buffer
    queue = FileLikeQueue(max_size=1)
    # ... and use it as source for the consumer:
    requests.post(..., data=queue)
    # pass it to the PUT handler as target
    return queue
c                 V    SU l         [        R                  " U5      U l        SU l        g )NF    )	is_closedqueueQueueunread)selfmax_sizes     I/home/kali/flask_env/lib/python3.13/site-packages/wsgidav/stream_tools.py__init__FileLikeQueue.__init__1   s     [[*
r	   c                    U R                   nSU l         US:X  d  US:  d  US:  aU  [        U5      U:  aF   X R                  R                  SS5      -  nUS:X  a  M'  US:  a  M/  US:  a  [        U5      U:  a  MF  US:  a  [        U5      U:  a  X!S U l         USU n[        U5      [        L d   eU$ ! [        R                   a    U R
                  (       a   Ma   Nf = f)aa  Read a chunk of bytes from queue.

size = 0: Read next chunk (arbitrary length)
     > 0: Read one chunk of `size` bytes (or less if stream was closed)
     < 0: Read all bytes as single chunk (i.e. blocks until stream is closed)

This method blocks until the requested size become available.
However, if close() was called, '' is returned immediately.
r	   r   Tg?N)r   lenr   getEmptyr
   typebytes)r   sizeress      r   readFileLikeQueue.read6   s     kkSjD1Hc#ho zz~~dC00	 SjD1Hc#ho !8C4e*DKet*CCyE!!!
 ;; >> "s   B4 4%CCc                    [        U5      [        L d   eU R                  (       a  [        S5      e[        R
                  " U5      (       a  U R                  R                  U5        gU H  nU R                  R                  U5        M      g)zhPut a chunk of bytes (or an iterable) to the queue.

May block if max_size number of chunks is reached.
zCannot write to closed objectN)r   r   r
   
ValueErrorr   is_basestringr   put)r   chunkos      r   writeFileLikeQueue.writeT   sf    
 E{e###>><== e$$JJNN5!

q! r	   c                     SU l         g )NT)r
   )r   s    r   closeFileLikeQueue.closed   s	    r	   )r
   r   r   N)r   )
__name__
__module____qualname____firstlineno____doc__r   r   r#   r&   __static_attributes__ r	   r   r   r       s     
<" r	   r   c                   (    \ rS rSrSrS rSS jrSrg)StreamingFile~   z7A file object wrapped around an iterator / data stream.c                     Xl         SU l        g)z+Initialise the object with the data stream. N)data_streambuffer)r   r4   s     r   r   StreamingFile.__init__   s    &r	   Nc                 P   Ub  [        U R                  5      U:  aI   U =R                  [        U R                  5      -  sl        Uc  M.  [        U R                  5      U:  a  MI  U R                  SU nUc	  SU l        U$ U R                  US U l        U$ ! [         a     M>  f = f)zRead bytes from an iterator.Nr3   )r   r5   nextr4   StopIteration)r   r   sized_chunks      r   r   StreamingFile.read   s    lc$++.5tD$4$455 lc$++.5 kk%4(<DK  ++de,DK ! s   (B 
B%$B%)r5   r4   )N)r(   r)   r*   r+   r,   r   r   r-   r.   r	   r   r0   r0   ~   s    A
r	   r0   )
r,   r   wsgidavr   __docformat__get_module_loggerr(   _loggerr   r0   r.   r	   r   <module>r@      s>     "

 
 
*F F| r	   