ó
‰]c           @   sÀ   d  Z  d d l Z d d l m Z y d d l Z Wn e k
 rK d Z n Xe j d e d d ƒd „  Z	 d „  Z
 e d	 e f d
 „  ƒ  Yƒ Z d e f d „  ƒ  YZ d e f d „  ƒ  YZ d S(   s×  
    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
iÿÿÿÿNi   (   t   implements_iterators^   'werkzeug.contrib.iterio' is deprecated as of version 0.15 and will be removed in version 1.0.t
   stacklevelc         C   sL   t  |  ƒ } t | | ƒ } t | t ƒ r; | d j | ƒ S| d j | ƒ S(   s2   concatenate any string type in an intelligent way.t    u    (   t   itert   nextt
   isinstancet   bytest   join(   t   iterablet   sentinelt   iteratort
   first_item(    (    s6   lib/python2.7/site-packages/werkzeug/contrib/iterio.pyt   _mixed_join;   s
    c         C   s   t  |  t ƒ r d Sd S(   Ns   
u   
(   R   R   (   t   reference_string(    (    s6   lib/python2.7/site-packages/werkzeug/contrib/iterio.pyt   _newlineD   s    t   IterIOc           B   s•   e  Z d  Z d d „ Z d „  Z d „  Z d „  Z d d „ Z d d „ Z	 d	 „  Z
 d
 „  Z d d „ Z d d „ Z d d „ Z d „  Z d „  Z RS(   sÁ  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   s;   y t  | ƒ } Wn t k
 r- t | | ƒ SXt | | ƒ S(   N(   R   t	   TypeErrort   IterIt   IterO(   t   clst   objR	   R
   (    (    s6   lib/python2.7/site-packages/werkzeug/contrib/iterio.pyt   __new__f   s
    c         C   s   |  S(   N(    (   t   self(    (    s6   lib/python2.7/site-packages/werkzeug/contrib/iterio.pyt   __iter__m   s    c         C   s   |  j  r t d ƒ ‚ n  |  j S(   Ns   I/O operation on closed file(   t   closedt
   ValueErrort   pos(   R   (    (    s6   lib/python2.7/site-packages/werkzeug/contrib/iterio.pyt   tellp   s    	c         C   s   |  j  r t d ƒ ‚ n  t S(   Ns   I/O operation on closed file(   R   R   t   False(   R   (    (    s6   lib/python2.7/site-packages/werkzeug/contrib/iterio.pyt   isattyu   s    	i    c         C   s+   |  j  r t d ƒ ‚ n  t d d ƒ ‚ d  S(   Ns   I/O operation on closed filei	   s   Bad file descriptor(   R   R   t   IOError(   R   R   t   mode(    (    s6   lib/python2.7/site-packages/werkzeug/contrib/iterio.pyt   seekz   s    	c         C   s+   |  j  r t d ƒ ‚ n  t d d ƒ ‚ d  S(   Ns   I/O operation on closed filei	   s   Bad file descriptor(   R   R   R   (   R   t   size(    (    s6   lib/python2.7/site-packages/werkzeug/contrib/iterio.pyt   truncate   s    	c         C   s+   |  j  r t d ƒ ‚ n  t d d ƒ ‚ d  S(   Ns   I/O operation on closed filei	   s   Bad file descriptor(   R   R   R   (   R   t   s(    (    s6   lib/python2.7/site-packages/werkzeug/contrib/iterio.pyt   write„   s    	c         C   s+   |  j  r t d ƒ ‚ n  t d d ƒ ‚ d  S(   Ns   I/O operation on closed filei	   s   Bad file descriptor(   R   R   R   (   R   t   list(    (    s6   lib/python2.7/site-packages/werkzeug/contrib/iterio.pyt
   writelines‰   s    	iÿÿÿÿc         C   s+   |  j  r t d ƒ ‚ n  t d d ƒ ‚ d  S(   Ns   I/O operation on closed filei	   s   Bad file descriptor(   R   R   R   (   R   t   n(    (    s6   lib/python2.7/site-packages/werkzeug/contrib/iterio.pyt   readŽ   s    	c         C   s+   |  j  r t d ƒ ‚ n  t d d ƒ ‚ d  S(   Ns   I/O operation on closed filei	   s   Bad file descriptor(   R   R   R   (   R   t   sizehint(    (    s6   lib/python2.7/site-packages/werkzeug/contrib/iterio.pyt	   readlines“   s    	c         C   s+   |  j  r t d ƒ ‚ n  t d d ƒ ‚ d  S(   Ns   I/O operation on closed filei	   s   Bad file descriptor(   R   R   R   (   R   t   length(    (    s6   lib/python2.7/site-packages/werkzeug/contrib/iterio.pyt   readline˜   s    	c         C   s+   |  j  r t d ƒ ‚ n  t d d ƒ ‚ d  S(   Ns   I/O operation on closed filei	   s   Bad file descriptor(   R   R   R   (   R   (    (    s6   lib/python2.7/site-packages/werkzeug/contrib/iterio.pyt   flush   s    	c         C   s7   |  j  r t ƒ  ‚ n  |  j ƒ  } | s3 t ƒ  ‚ n  | S(   N(   R   t   StopIterationR,   (   R   t   line(    (    s6   lib/python2.7/site-packages/werkzeug/contrib/iterio.pyt   __next__¢   s    	N(   t   __name__t
   __module__t   __doc__R   R   R   R   R    t   NoneR"   R$   R&   R(   R*   R,   R-   R0   (    (    (    s6   lib/python2.7/site-packages/werkzeug/contrib/iterio.pyR   J   s   						R   c           B   sG   e  Z d  Z d d „ Z d „  Z d „  Z d „  Z d „  Z d „  Z RS(   s#   Convert an stream into an iterator.R   c         #   s®   t  d  k r t d ƒ ‚ n  t j |  ƒ ‰ t  j ƒ  ˆ _ g  ˆ _ t ˆ _	 | ˆ _
 d ˆ _ ‡  ‡ f d †  } t  j  | ˆ j ƒ } x# | j ƒ  } | s d  S| d Vq‡ Wd  S(   Ns   IterI requires greenlet supporti    c              s   ˆ  ˆ ƒ ˆ j  ƒ  d  S(   N(   t   close(    (   t   funct   stream(    s6   lib/python2.7/site-packages/werkzeug/contrib/iterio.pyt   run¸   s    
(   t   greenletR4   t   RuntimeErrort   objectR   t
   getcurrentt   _parentt   _bufferR   R   R	   R   t   switch(   R   R6   R	   R8   t   gt   rv(    (   R6   R7   s6   lib/python2.7/site-packages/werkzeug/contrib/iterio.pyR   ®   s    				c         C   s#   |  j  s t |  _  |  j ƒ  n  d  S(   N(   R   t   Truet   _flush_impl(   R   (    (    s6   lib/python2.7/site-packages/werkzeug/contrib/iterio.pyR5   Ã   s    		c         C   sJ   |  j  r t d ƒ ‚ n  | rF |  j t | ƒ 7_ |  j j | ƒ n  d  S(   Ns   I/O operation on closed file(   R   R   R   t   lenR>   t   append(   R   R#   (    (    s6   lib/python2.7/site-packages/werkzeug/contrib/iterio.pyR$   È   s
    	c         C   s"   x | D] } |  j  | ƒ q Wd  S(   N(   R$   (   R   R%   t   item(    (    s6   lib/python2.7/site-packages/werkzeug/contrib/iterio.pyR&   Ï   s    c         C   s&   |  j  r t d ƒ ‚ n  |  j ƒ  d  S(   Ns   I/O operation on closed file(   R   R   RC   (   R   (    (    s6   lib/python2.7/site-packages/werkzeug/contrib/iterio.pyR-   Ó   s    	c         C   sU   t  |  j |  j ƒ } g  |  _ | r> |  j r> |  j j ƒ  n |  j j | f ƒ d  S(   N(   R   R>   R	   R   R=   R?   (   R   t   data(    (    s6   lib/python2.7/site-packages/werkzeug/contrib/iterio.pyRC   Ø   s
    	(	   R1   R2   R3   R   R5   R$   R&   R-   RC   (    (    (    s6   lib/python2.7/site-packages/werkzeug/contrib/iterio.pyR   «   s   				R   c           B   se   e  Z d  Z d d „ Z d „  Z d „  Z d „  Z d d „ Z d d	 „ Z d d
 „ Z
 d d „ Z RS(   sC   Iter output.  Wrap an iterator and give it a stream like interface.R   c         C   s@   t  j |  ƒ } | | _ d  | _ | | _ t | _ d | _ | S(   Ni    (	   R;   R   t   _genR4   t   _bufR	   R   R   R   (   R   t   genR	   R   (    (    s6   lib/python2.7/site-packages/werkzeug/contrib/iterio.pyR   ä   s    					c         C   s   |  S(   N(    (   R   (    (    s6   lib/python2.7/site-packages/werkzeug/contrib/iterio.pyR   í   s    c         C   s(   |  j  s | |  _  n |  j  | 7_  d S(   s[   Replace string directly without appending to an empty string,
        avoiding type issues.N(   RI   (   R   t   string(    (    s6   lib/python2.7/site-packages/werkzeug/contrib/iterio.pyt   _buf_appendð   s    	c         C   s;   |  j  s7 t |  _  t |  j d ƒ r7 |  j j ƒ  q7 n  d  S(   NR5   (   R   RB   t   hasattrRH   R5   (   R   (    (    s6   lib/python2.7/site-packages/werkzeug/contrib/iterio.pyR5   ø   s    		i    c         C   s/  |  j  r t d ƒ ‚ n  | d k r4 | |  j 7} nQ | d k rj |  j ƒ  t |  j |  j | ƒ |  _ d  S| d k r… t d ƒ ‚ n  g  } yX t |  j p d ƒ } x< | | k rá t |  j	 ƒ } | t | ƒ 7} | j
 | ƒ q¦ WWn t k
 rö n X| r|  j t | |  j ƒ ƒ n  t d | ƒ |  _ d  S(   Ns   I/O operation on closed filei   i   i    s   Invalid argumentR   (   R   R   R   R(   t   minR   RD   RI   R   RH   RE   R.   RL   R   R	   t   max(   R   R   R   t   buft   tmp_end_posRF   (    (    s6   lib/python2.7/site-packages/werkzeug/contrib/iterio.pyR    þ   s,    	
iÿÿÿÿc         C   sŒ  |  j  r t d ƒ ‚ n  | d k  ri |  j t |  j |  j ƒ ƒ |  j |  j } |  j t | ƒ 7_ | S|  j | } g  } y} |  j d  k r” d n t |  j ƒ } xR | | k sÈ |  j d  k r÷ | r÷ t
 |  j ƒ } | t | ƒ 7} | j | ƒ q¦ WWn t k
 rn X| r/|  j t | |  j ƒ ƒ n  |  j d  k rE|  j St d | ƒ } z |  j |  j | !SWd  t | t |  j ƒ ƒ |  _ Xd  S(   Ns   I/O operation on closed filei    (   R   R   RL   R   RH   R	   RI   R   RD   R4   R   RE   R.   RO   RN   (   R   R'   t   resultt   new_posRP   RQ   RF   (    (    s6   lib/python2.7/site-packages/werkzeug/contrib/iterio.pyR(     s2    	$%c         C   sÈ  |  j  r t d ƒ ‚ n  d } |  j rK |  j j t |  j ƒ |  j ƒ } n  g  } yœ |  j d  k ro |  j } n t |  j ƒ } xk | d k  rë t |  j	 ƒ } | j t | ƒ ƒ } | j
 | ƒ | d k rØ | | } Pn  | t | ƒ 7} q WWn t k
 r n X| r#|  j t | |  j ƒ ƒ n  |  j d  k r9|  j S| d k  rWt |  j ƒ } n
 | d } | d  k	 r|  j | | k  r|  j | } n  z |  j |  j | !SWd  t | t |  j ƒ ƒ |  _ Xd  S(   Ns   I/O operation on closed fileiÿÿÿÿi    i   (   R   R   RI   t   findR   R   R4   RD   R   RH   RE   R.   RL   R   R	   RN   (   R   R+   t   nl_posRP   R   RF   t	   local_posRS   (    (    s6   lib/python2.7/site-packages/werkzeug/contrib/iterio.pyR,   4  s@    		$

c         C   sr   d } g  } |  j  ƒ  } xS | rm | j | ƒ | t | ƒ 7} d | k  oU | k n r^ Pn  |  j  ƒ  } q W| S(   Ni    (   R,   RE   RD   (   R   R)   t   totalt   linesR/   (    (    s6   lib/python2.7/site-packages/werkzeug/contrib/iterio.pyR*   \  s    	N(   R1   R2   R3   R   R   RL   R5   R    R(   R4   R,   R*   (    (    (    s6   lib/python2.7/site-packages/werkzeug/contrib/iterio.pyR   á   s   				((   R3   t   warningst   _compatR    R9   t   ImportErrorR4   t   warnt   DeprecationWarningR   R   R;   R   R   R   (    (    (    s6   lib/python2.7/site-packages/werkzeug/contrib/iterio.pyt   <module>)   s    
			`6