ó
`¾Tc           @   sž   d  Z  y d d l Z Wn e k
 r/ d Z n Xd d l m Z 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: http://codespeak.net/py/dist/greenlet.html

    :copyright: (c) 2014 by the Werkzeug Team, see AUTHORS for more details.
    :license: BSD, see LICENSE for more details.
iÿÿÿÿN(   t   implements_iteratorc         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(    (    s]   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/contrib/iterio.pyt   _mixed_join2   s
    c         C   s   t  |  t ƒ r d Sd S(   Ns   
u   
(   R   R   (   t   reference_string(    (    s]   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/contrib/iterio.pyt   _newline;   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 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	   (    (    s]   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/contrib/iterio.pyt   __new__]   s
    c         C   s   |  S(   N(    (   t   self(    (    s]   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/contrib/iterio.pyt   __iter__d   s    c         C   s   |  j  r t d ƒ ‚ n  |  j S(   Ns   I/O operation on closed file(   t   closedt
   ValueErrort   pos(   R   (    (    s]   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/contrib/iterio.pyt   tellg   s    	c         C   s   |  j  r t d ƒ ‚ n  t S(   Ns   I/O operation on closed file(   R   R   t   False(   R   (    (    s]   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/contrib/iterio.pyt   isattyl   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(    (    s]   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/contrib/iterio.pyt   seekq   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(    (    s]   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/contrib/iterio.pyt   truncatev   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(    (    s]   /var/www/send.findwatt.com/datamanager/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(    (    s]   /var/www/send.findwatt.com/datamanager/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(    (    s]   /var/www/send.findwatt.com/datamanager/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(    (    s]   /var/www/send.findwatt.com/datamanager/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(    (    s]   /var/www/send.findwatt.com/datamanager/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   (    (    s]   /var/www/send.findwatt.com/datamanager/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(    (    s]   /var/www/send.findwatt.com/datamanager/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,   R/   (    (    (    s]   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/contrib/iterio.pyR   A   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‡ d  S(   Ns   IterI requires greenlet supporti    c              s   ˆ ˆ  ƒ ˆ  j  ƒ  d  S(   N(   t   close(    (   t   streamt   func(    s]   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/contrib/iterio.pyt   run¯   s    
(   t   greenletR3   t   RuntimeErrort   objectR   t
   getcurrentt   _parentt   _bufferR   R   R   R   t   switch(   R   R6   R   R7   t   gt   rv(    (   R5   R6   s]   /var/www/send.findwatt.com/datamanager/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   (    (    s]   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/contrib/iterio.pyR4   º   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"   (    (    s]   /var/www/send.findwatt.com/datamanager/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(    (    s]   /var/www/send.findwatt.com/datamanager/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   RB   (   R   (    (    s]   /var/www/send.findwatt.com/datamanager/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(    (    s]   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/contrib/iterio.pyRB   Ï   s
    	(	   R0   R1   R2   R   R4   R#   R%   R,   RB   (    (    (    s]   /var/www/send.findwatt.com/datamanager/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   _genR3   t   _bufR   R   R   R   (   R   t   genR   R   (    (    s]   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/contrib/iterio.pyR   Û   s    					c         C   s   |  S(   N(    (   R   (    (    s]   /var/www/send.findwatt.com/datamanager/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(   RH   (   R   t   string(    (    s]   /var/www/send.findwatt.com/datamanager/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(   NR4   (   R   RA   t   hasattrRG   R4   (   R   (    (    s]   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/contrib/iterio.pyR4   ï   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  } yR t |  j ƒ } x< | | k rÛ |  j 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 argument(   R   R   R   R'   t   minR   RC   RH   RG   R   RD   R-   RK   R   R   t   max(   R   R   R   t   buft   tmp_end_posRE   (    (    s]   /var/www/send.findwatt.com/datamanager/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   RK   R   RG   R   RH   R   RC   R3   R   RD   R-   RN   RM   (   R   R&   t   resultt   new_posRO   RP   RE   (    (    s]   /var/www/send.findwatt.com/datamanager/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 } 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 r|  j S| d k  r6t	 |  j ƒ } n
 | d } | d  k	 ro|  j | | k  ro|  j | } n  z |  j |  j | !SWd  t | t	 |  j ƒ ƒ |  _ Xd  S(   Ns   I/O operation on closed fileiÿÿÿÿi    i   (   R   R   RH   t   findR   R   R   RG   RD   RC   R-   RK   R   R   R3   RM   (   R   R*   t   nl_posRO   R   RE   t	   local_posRR   (    (    s]   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/contrib/iterio.pyR+   +  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+   RD   RC   (   R   R(   t   totalt   linesR.   (    (    s]   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/contrib/iterio.pyR)   P  s    	N(   R0   R1   R2   R   R   RK   R4   R   R'   R3   R+   R)   (    (    (    s]   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/contrib/iterio.pyR   Ø   s   				%(   R2   R8   t   ImportErrorR3   t   werkzeug._compatR    R   R   R:   R   R   R   (    (    (    s]   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/werkzeug/contrib/iterio.pyt   <module>)   s   
			`6