3
 [ð]°*  ã               @   s–   d Z ddlZddlmZ y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j|ƒ S |dj|ƒ S )z2concatenate any string type in an intelligent way.ó    Ú )ÚiterÚnextÚ
isinstanceÚbytesÚjoin)ÚiterableÚsentinelÚiteratorZ
first_item© r   ú;/tmp/pip-build-w1as9q70/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   s2   yt |ƒ}W n tk
r&   t||ƒ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 )Nr   )Úselfr   r   r   Ú__iter__m   s    zIterIO.__iter__c             C   s   | j rtdƒ‚| jS )NzI/O operation on closed file)ÚclosedÚ
ValueErrorÚpos)r   r   r   r   Útellp   s    zIterIO.tellc             C   s   | j rtdƒ‚dS )NzI/O operation on closed fileF)r   r   )r   r   r   r   Úisattyu   s    zIterIO.isattyr   c             C   s   | j rtdƒ‚tddƒ‚d S )NzI/O operation on closed fileé	   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 )NzI/O operation on closed filer#   zBad file descriptor)r   r   r$   )r   Úsizer   r   r   Útruncate   s    zIterIO.truncatec             C   s   | j rtdƒ‚tddƒ‚d S )NzI/O operation on closed filer#   zBad file descriptor)r   r   r$   )r   Úsr   r   r   Úwrite„   s    zIterIO.writec             C   s   | j rtdƒ‚tddƒ‚d S )NzI/O operation on closed filer#   zBad file descriptor)r   r   r$   )r   Úlistr   r   r   Ú
writelines‰   s    zIterIO.writelinesé   c             C   s   | j rtdƒ‚tddƒ‚d S )NzI/O operation on closed filer#   zBad file descriptor)r   r   r$   )r   Únr   r   r   ÚreadŽ   s    zIterIO.readc             C   s   | j rtdƒ‚tddƒ‚d S )NzI/O operation on closed filer#   zBad file descriptor)r   r   r$   )r   Úsizehintr   r   r   Ú	readlines“   s    zIterIO.readlinesc             C   s   | j rtdƒ‚tddƒ‚d S )NzI/O operation on closed filer#   zBad file descriptor)r   r   r$   )r   Úlengthr   r   r   Úreadline˜   s    zIterIO.readlinec             C   s   | j rtdƒ‚tddƒ‚d S )NzI/O operation on closed filer#   zBad file descriptor)r   r   r$   )r   r   r   r   Úflush�   s    zIterIO.flushc             C   s"   | j rtƒ ‚| jƒ }|stƒ ‚|S )N)r   ÚStopIterationr3   )r   Úliner   r   r   Ú__next__¢   s    zIterIO.__next__)r   )r   )Néÿÿÿÿ)r8   )r   )N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r!   r"   r&   r(   r*   r,   r/   r1   r3   r4   r7   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             #   s|   t d krtdƒ‚tj| ƒ‰t jƒ ˆ_g ˆ_dˆ_|ˆ_dˆ_	‡ ‡fdd„}t j |ˆjƒ}x|j
ƒ }|sjd S |d V  qZW d S )NzIterI requires greenlet supportFr   c                  s   ˆ ˆƒ ˆj ƒ  d S )N)Úcloser   )ÚfuncÚstreamr   r   Úrun¸   s    zIterI.__new__.<locals>.run)ÚgreenletÚRuntimeErrorÚobjectr   Z
getcurrentÚ_parentÚ_bufferr   r   r    Úswitch)r   r>   r   r@   ÚgÚrvr   )r>   r?   r   r   ®   s    

zIterI.__new__c             C   s   | j sd| _ | jƒ  d S )NT)r   Ú_flush_impl)r   r   r   r   r=   Ã   s    zIterI.closec             C   s4   | j rtdƒ‚|r0|  jt|ƒ7  _| jj|ƒ d S )NzI/O operation on closed file)r   r   r    ÚlenrE   Úappend)r   r)   r   r   r   r*   È   s
    zIterI.writec             C   s   x|D ]}| j |ƒ qW d S )N)r*   )r   r+   Úitemr   r   r   r,   Ï   s    
zIterI.writelinesc             C   s   | j rtdƒ‚| jƒ  d S )NzI/O operation on closed file)r   r   rI   )r   r   r   r   r4   Ó   s    zIterI.flushc             C   s>   t | j| jƒ}g | _| r,| jr,| jjƒ  n| jj|fƒ d S )N)r   rE   r   r   rD   rF   )r   Údatar   r   r   rI   Ø   s
    zIterI._flush_implN)r   )
r9   r:   r;   r<   r   r=   r*   r,   r4   rI   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 j| ƒ}||_d |_||_d|_d|_|S )NFr   )rC   r   Ú_genÚ_bufr   r   r    )r   Úgenr   r   r   r   r   r   ä   s    
zIterO.__new__c             C   s   | S )Nr   )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)rO   )r   Ústringr   r   r   Ú_buf_appendð   s    zIterO._buf_appendc             C   s&   | j s"d| _ t| jdƒr"| jjƒ  d S )NTr=   )r   ÚhasattrrN   r=   )r   r   r   r   r=   ø   s    zIterO.closer   c             C   sÚ   | j rtdƒ‚|dkr"|| j7 }n8|dkrJ| jƒ  t| j| j| ƒ| _d S |dkrZtdƒ‚g }y@t| jpjdƒ}x,||kršt| j	ƒ}|t|ƒ7 }|j
|ƒ qpW W n tk
r²   Y nX |rÊ| jt|| jƒƒ td|ƒ| _d S )NzI/O operation on closed filer-   r   r   zInvalid argumentr   )r   r   r    r/   Úminr$   rJ   rO   r   rN   rK   r5   rR   r   r   Úmax)r   r    r%   ÚbufÚtmp_end_posrL   r   r   r   r&   þ   s,    

z
IterO.seekr-   c             C   s(  | j rtdƒ‚|dk rP| jt| j| jƒƒ | j| jd … }|  jt|ƒ7  _|S | j| }g }yZ| jd krndnt| jƒ}x<||ks’| jd kr´| r´t	| jƒ}|t|ƒ7 }|j
|ƒ qzW W n tk
rÌ   Y nX |rä| jt|| jƒƒ | jd krô| jS td|ƒ}z| j| j|… S t|t| jƒƒ| _X d S )NzI/O operation on closed filer   )r   r   rR   r   rN   r   rO   r    rJ   r   rK   r5   rU   rT   )r   r.   ÚresultÚnew_posrV   rW   rL   r   r   r   r/     s2    



z
IterO.readNc             C   sF  | j rtdƒ‚d}| jr.| jjt| jƒ| jƒ}g }yn| jd krF| j}n
t| jƒ}xL|dk rœt| jƒ}|jt|ƒƒ}|j	|ƒ |dkrŽ|| }P |t|ƒ7 }qRW W n t
k
r´   Y nX |rÌ| j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|… S t|t| jƒƒ| _X d S )NzI/O operation on closed filer-   r   r8   )r   r   rO   Úfindr   r    rJ   r   rN   rK   r5   rR   r   r   rT   )r   r2   Únl_posrV   r    rL   Z	local_posrY   r   r   r   r3   4  s@    






zIterO.readlinec             C   sV   d}g }| j ƒ }x@|rP|j|ƒ |t|ƒ7 }d|  k o>|kn  rFP | j ƒ }qW |S )Nr   )r3   rK   rJ   )r   r0   ÚtotalÚlinesr6   r   r   r   r1   \  s    
zIterO.readlines)r   )r   r8   )r8   )N)r   )r9   r:   r;   r<   r   r   rR   r=   r&   r/   r3   r1   r   r   r   r   r   á   s   
	


(r   )r<   ÚwarningsÚ_compatr   rA   ÚImportErrorÚwarnÚDeprecationWarningr   r   rC   r   r   r   r   r   r   r   Ú<module>)   s    
	`6