U
    (Q^°*  ã                   @   s–   d Z ddlZddlmZ zddlZW n ek
r<   dZY nX ejdedd dd„ Zd	d
„ Z	eG dd„ de
ƒƒZG dd„ deƒZG dd„ deƒZdS )a×  
    werkzeug.contrib.iterio
    ~~~~~~~~~~~~~~~~~~~~~~~

    This module implements a :class:`IterIO` that converts an iterator into
    a stream object and the other way round.  Converting streams into
    iterators requires the `greenlet`_ module.

    To convert an iterator into a stream all you have to do is to pass it
    directly to the :class:`IterIO` constructor.  In this example we pass it
    a newly created generator::

        def foo():
            yield "something\n"
            yield "otherthings"
        stream = IterIO(foo())
        print stream.read()         # read the whole iterator

    The other way round works a bit different because we have to ensure that
    the code execution doesn't take place yet.  An :class:`IterIO` call with a
    callable as first argument does two things.  The function itself is passed
    an :class:`IterIO` stream it can feed.  The object returned by the
    :class:`IterIO` constructor on the other hand is not an stream object but
    an iterator::

        def foo(stream):
            stream.write("some")
            stream.write("thing")
            stream.flush()
            stream.write("otherthing")
        iterator = IterIO(foo)
        print iterator.next()       # prints something
        print iterator.next()       # prints otherthing
        iterator.next()             # raises StopIteration

    .. _greenlet: https://github.com/python-greenlet/greenlet

    :copyright: 2007 Pallets
    :license: BSD-3-Clause
é    Né   )Úimplements_iteratorz^'werkzeug.contrib.iterio' is deprecated as of version 0.15 and will be removed in version 1.0.)Ú
stacklevelc                 C   s8   t | ƒ}t||ƒ}t|tƒr*|d |¡ S |d |¡ S )z2concatenate any string type in an intelligent way.ó    Ú )ÚiterÚnextÚ
isinstanceÚbytesÚjoin)ÚiterableÚsentinelÚiteratorZ
first_item© r   ú=/tmp/pip-install-bd4o36v9/Werkzeug/werkzeug/contrib/iterio.pyÚ_mixed_join;   s
    

r   c                 C   s   t | tƒrdS dS )Nó   
Ú
)r	   r
   )Zreference_stringr   r   r   Ú_newlineD   s    
r   c                   @   s„   e Zd ZdZd dd„Zdd„ Zdd„ Zd	d
„ Zd!dd„Zd"dd„Z	dd„ Z
dd„ Zd#dd„Zd$dd„Zd%dd„Zdd„ Zdd„ ZdS )&ÚIterIOaÁ  Instances of this object implement an interface compatible with the
    standard Python :class:`file` object.  Streams are either read-only or
    write-only depending on how the object is created.

    If the first argument is an iterable a file like object is returned that
    returns the contents of the iterable.  In case the iterable is empty
    read operations will return the sentinel value.

    If the first argument is a callable then the stream object will be
    created and passed to that function.  The caller itself however will
    not receive a stream but an iterable.  The function will be executed
    step by step as something iterates over the returned iterable.  Each
    call to :meth:`flush` will create an item for the iterable.  If
    :meth:`flush` is called without any writes in-between the sentinel
    value will be yielded.

    Note for Python 3: due to the incompatible interface of bytes and
    streams you should set the sentinel value explicitly to an empty
    bytestring (``b''``) if you are expecting to deal with bytes as
    otherwise the end of the stream is marked with the wrong sentinel
    value.

    .. versionadded:: 0.9
       `sentinel` parameter was added.
    r   c                 C   s6   zt |ƒ}W n tk
r*   t||ƒ Y S X t||ƒS ©N)r   Ú	TypeErrorÚIterIÚIterO)ÚclsÚobjr   r   r   r   r   Ú__new__f   s
    zIterIO.__new__c                 C   s   | S r   r   ©Úselfr   r   r   Ú__iter__m   s    zIterIO.__iter__c                 C   s   | j rtdƒ‚| jS ©NúI/O operation on closed file)ÚclosedÚ
ValueErrorÚposr   r   r   r   Útellp   s    zIterIO.tellc                 C   s   | j rtdƒ‚dS )Nr!   F)r"   r#   r   r   r   r   Úisattyu   s    zIterIO.isattyr   c                 C   s   | j rtdƒ‚tddƒ‚d S ©Nr!   é	   zBad file descriptor©r"   r#   ÚIOError)r   r$   Úmoder   r   r   Úseekz   s    zIterIO.seekNc                 C   s   | j rtdƒ‚tddƒ‚d S r'   r)   )r   Úsizer   r   r   Útruncate   s    zIterIO.truncatec                 C   s   | j rtdƒ‚tddƒ‚d S r'   r)   ©r   Úsr   r   r   Úwrite„   s    zIterIO.writec                 C   s   | j rtdƒ‚tddƒ‚d S r'   r)   )r   Úlistr   r   r   Ú
writelines‰   s    zIterIO.writelineséÿÿÿÿc                 C   s   | j rtdƒ‚tddƒ‚d S r'   r)   )r   Únr   r   r   ÚreadŽ   s    zIterIO.readc                 C   s   | j rtdƒ‚tddƒ‚d S r'   r)   )r   Úsizehintr   r   r   Ú	readlines“   s    zIterIO.readlinesc                 C   s   | j rtdƒ‚tddƒ‚d S r'   r)   )r   Úlengthr   r   r   Úreadline˜   s    zIterIO.readlinec                 C   s   | j rtdƒ‚tddƒ‚d S r'   r)   r   r   r   r   Úflush   s    zIterIO.flushc                 C   s"   | j rtƒ ‚|  ¡ }|stƒ ‚|S r   )r"   ÚStopIterationr:   )r   Úliner   r   r   Ú__next__¢   s    zIterIO.__next__)r   )r   )N)r4   )r   )N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r%   r&   r,   r.   r1   r3   r6   r8   r:   r;   r>   r   r   r   r   r   J   s   





r   c                   @   sB   e Zd ZdZddd„Zdd„ Zdd„ Zd	d
„ Zdd„ Zdd„ Z	dS )r   z#Convert an stream into an iterator.r   c                 #   sx   t d krtdƒ‚t | ¡‰t  ¡ ˆ_g ˆ_dˆ_|ˆ_dˆ_	‡ ‡fdd„}t   |ˆj¡}| 
¡ }|shd S |d V  qXd S )NzIterI requires greenlet supportFr   c                      s   ˆ ˆƒ ˆ  ¡  d S r   )Úcloser   ©ÚfuncÚstreamr   r   Úrun¸   s    zIterI.__new__.<locals>.run)ÚgreenletÚRuntimeErrorÚobjectr   Z
getcurrentÚ_parentÚ_bufferr"   r   r$   Úswitch)r   rE   r   rG   ÚgÚrvr   rD   r   r   ®   s    

zIterI.__new__c                 C   s   | j sd| _ |  ¡  d S )NT)r"   Ú_flush_implr   r   r   r   rC   Ã   s    zIterI.closec                 C   s4   | j rtdƒ‚|r0|  jt|ƒ7  _| j |¡ d S r    )r"   r#   r$   ÚlenrL   Úappendr/   r   r   r   r1   È   s
    zIterI.writec                 C   s   |D ]}|   |¡ qd S r   )r1   )r   r2   Úitemr   r   r   r3   Ï   s    zIterI.writelinesc                 C   s   | j rtdƒ‚|  ¡  d S r    )r"   r#   rP   r   r   r   r   r;   Ó   s    zIterI.flushc                 C   s<   t | j| jƒ}g | _|s*| jr*| j ¡  n| j |f¡ d S r   )r   rL   r   r"   rK   rM   )r   Údatar   r   r   rP   Ø   s
    
zIterI._flush_implN)r   )
r?   r@   rA   rB   r   rC   r1   r3   r;   rP   r   r   r   r   r   «   s   
r   c                   @   sZ   e Zd ZdZddd„Zdd„ Zdd„ Zd	d
„ Zddd„Zddd„Z	ddd„Z
ddd„ZdS )r   zCIter output.  Wrap an iterator and give it a stream like interface.r   c                 C   s,   t  | ¡}||_d |_||_d|_d|_|S )NFr   )rJ   r   Ú_genÚ_bufr   r"   r$   )r   Úgenr   r   r   r   r   r   ä   s    
zIterO.__new__c                 C   s   | S r   r   r   r   r   r   r   í   s    zIterO.__iter__c                 C   s    | j s|| _ n|  j |7  _ dS )z[Replace string directly without appending to an empty string,
        avoiding type issues.N)rV   )r   Ústringr   r   r   Ú_buf_appendð   s    zIterO._buf_appendc                 C   s&   | j s"d| _ t| jdƒr"| j ¡  d S )NTrC   )r"   ÚhasattrrU   rC   r   r   r   r   rC   ø   s    zIterO.closer   c                 C   sÖ   | j rtdƒ‚|dkr"|| j7 }n8|dkrJ|  ¡  t| j| j| ƒ| _d S |dkrZtdƒ‚g }z<t| jpjdƒ}||kr˜t| j	ƒ}|t|ƒ7 }| 
|¡ qnW n tk
r®   Y nX |rÆ|  t|| jƒ¡ td|ƒ| _d S )Nr!   é   r   r   zInvalid argumentr   )r"   r#   r$   r6   Úminr*   rQ   rV   r   rU   rR   r<   rY   r   r   Úmax)r   r$   r+   ÚbufÚtmp_end_posrS   r   r   r   r,   þ   s,    
z
IterO.seekr4   c              
   C   s&  | j rtdƒ‚|dk rP|  t| j| jƒ¡ | j| jd … }|  jt|ƒ7  _|S | j| }g }zT| jd krndnt| jƒ}||ksŽ| jd kr°|s°t	| jƒ}|t|ƒ7 }| 
|¡ qxW n tk
rÆ   Y nX |rÞ|  t|| jƒ¡ | jd krî| jS td|ƒ}z| j| j|… W ¢S t|t| jƒƒ| _X d S )Nr!   r   )r"   r#   rY   r   rU   r   rV   r$   rQ   r   rR   r<   r]   r\   )r   r5   ÚresultÚnew_posr^   r_   rS   r   r   r   r6     s2    



z
IterO.readNc              
   C   sF  | j rtdƒ‚d}| jr.| j t| jƒ| j¡}g }zj| jd krF| j}n
t| jƒ}|dk ršt| jƒ}| t|ƒ¡}| 	|¡ |dkrŒ|| }qš|t|ƒ7 }qPW n t
k
r°   Y nX |rÈ|  t|| jƒ¡ | jd krØ| jS |dk rìt| jƒ}n|d }|d k	r| j| |k r| j| }z| j| j|… W ¢S t|t| jƒƒ| _X d S )Nr!   r4   r   r[   )r"   r#   rV   Úfindr   r$   rQ   r   rU   rR   r<   rY   r   r   r\   )r   r9   Znl_posr^   r$   rS   Z	local_posra   r   r   r   r:   4  s@    





zIterO.readlinec                 C   sR   d}g }|   ¡ }|rN| |¡ |t|ƒ7 }d|  k r>|krDqN nqN|   ¡ }q|S )Nr   )r:   rR   rQ   )r   r7   ÚtotalÚlinesr=   r   r   r   r8   \  s    

zIterO.readlines)r   )r   )r4   )N)r   )r?   r@   rA   rB   r   r   rY   rC   r,   r6   r:   r8   r   r   r   r   r   á   s   
	


(r   )rB   ÚwarningsÚ_compatr   rH   ÚImportErrorÚwarnÚDeprecationWarningr   r   rJ   r   r   r   r   r   r   r   Ú<module>   s"   (
ü	`6