ó
ú2ec           @   sa   d  Z  d g Z d d l Z d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d S(	   s  
Helper for looping over sequences, particular in templates.

Often in a loop in a template it's handy to know what's next up,
previously up, if this is the first or last item in the sequence, etc.
These can be awkward to manage in a normal Python loop, but using the
looper you can get a better sense of the context.  Use like::

    >>> for loop, item in looper(['a', 'b', 'c']):
    ...     print("%s %s" % (loop.number, item))
    ...     if not loop.last:
    ...         print('---')
    1 a
    ---
    2 b
    ---
    3 c

t   looperiÿÿÿÿNc           B   s)   e  Z d  Z d „  Z d „  Z d „  Z RS(   s¤   
    Helper for looping (particularly in templates)

    Use this like::

        for loop, item in looper(seq):
            if loop.first:
                ...
    c         C   s   | |  _  d  S(   N(   t   seq(   t   selfR   (    (    s;   /usr/local/lib/python2.7/dist-packages/paste/util/looper.pyt   __init__%   s    c         C   s   t  |  j ƒ S(   N(   t   looper_iterR   (   R   (    (    s;   /usr/local/lib/python2.7/dist-packages/paste/util/looper.pyt   __iter__(   s    c         C   s   d |  j  j |  j f S(   Ns   <%s for %r>(   t	   __class__t   __name__R   (   R   (    (    s;   /usr/local/lib/python2.7/dist-packages/paste/util/looper.pyt   __repr__+   s    (   R   t
   __module__t   __doc__R   R   R   (    (    (    s;   /usr/local/lib/python2.7/dist-packages/paste/util/looper.pyR       s   			R   c           B   s)   e  Z d  „  Z d „  Z d „  Z e Z RS(   c         C   s   t  | ƒ |  _ d |  _ d  S(   Ni    (   t   listR   t   pos(   R   R   (    (    s;   /usr/local/lib/python2.7/dist-packages/paste/util/looper.pyR   1   s    c         C   s   |  S(   N(    (   R   (    (    s;   /usr/local/lib/python2.7/dist-packages/paste/util/looper.pyR   5   s    c         C   sY   |  j  t |  j ƒ k r! t ‚ n  t |  j |  j  ƒ |  j |  j  f } |  j  d 7_  | S(   Ni   (   R   t   lenR   t   StopIterationt   loop_pos(   R   t   result(    (    s;   /usr/local/lib/python2.7/dist-packages/paste/util/looper.pyt   next8   s
    	%(   R   R	   R   R   R   t   __next__(    (    (    s;   /usr/local/lib/python2.7/dist-packages/paste/util/looper.pyR   /   s   			R   c           B   s  e  Z d  „  Z d „  Z d „  Z e e ƒ Z d „  Z e e ƒ Z d „  Z e e ƒ Z d „  Z e e ƒ Z d „  Z	 e e	 ƒ Z	 d „  Z
 e e
 ƒ Z
 d „  Z e e ƒ Z d	 „  Z e e ƒ Z d
 „  Z e e ƒ Z d „  Z e e ƒ Z d d „ Z d d „ Z d „  Z RS(   c         C   s   | |  _  | |  _ d  S(   N(   R   R   (   R   R   R   (    (    s;   /usr/local/lib/python2.7/dist-packages/paste/util/looper.pyR   B   s    	c         C   s   d |  j  |  j |  j f S(   Ns   <loop pos=%r at %r>(   R   R   (   R   (    (    s;   /usr/local/lib/python2.7/dist-packages/paste/util/looper.pyR   F   s    c         C   s   |  j  S(   N(   R   (   R   (    (    s;   /usr/local/lib/python2.7/dist-packages/paste/util/looper.pyt   indexJ   s    c         C   s   |  j  d S(   Ni   (   R   (   R   (    (    s;   /usr/local/lib/python2.7/dist-packages/paste/util/looper.pyt   numberN   s    c         C   s   |  j  |  j S(   N(   R   R   (   R   (    (    s;   /usr/local/lib/python2.7/dist-packages/paste/util/looper.pyt   itemR   s    c         C   s/   y |  j  |  j d SWn t k
 r* d  SXd  S(   Ni   (   R   R   t
   IndexErrort   None(   R   (    (    s;   /usr/local/lib/python2.7/dist-packages/paste/util/looper.pyR   V   s    c         C   s%   |  j  d k r d  S|  j |  j  d S(   Ni    i   (   R   R   R   (   R   (    (    s;   /usr/local/lib/python2.7/dist-packages/paste/util/looper.pyt   previous]   s    c         C   s   |  j  d S(   Ni   (   R   (   R   (    (    s;   /usr/local/lib/python2.7/dist-packages/paste/util/looper.pyt   oddc   s    c         C   s   |  j  d S(   Ni   (   R   (   R   (    (    s;   /usr/local/lib/python2.7/dist-packages/paste/util/looper.pyt   eveng   s    c         C   s   |  j  d k S(   Ni    (   R   (   R   (    (    s;   /usr/local/lib/python2.7/dist-packages/paste/util/looper.pyt   firstk   s    c         C   s   |  j  t |  j ƒ d k S(   Ni   (   R   R   R   (   R   (    (    s;   /usr/local/lib/python2.7/dist-packages/paste/util/looper.pyt   lasto   s    c         C   s   t  |  j ƒ S(   N(   R   R   (   R   (    (    s;   /usr/local/lib/python2.7/dist-packages/paste/util/looper.pyt   lengths   s    c         C   s&   |  j  r t S|  j |  j |  j | ƒ S(   s  
        Returns true if this item is the start of a new group,
        where groups mean that some attribute has changed.  The getter
        can be None (the item itself changes), an attribute name like
        ``'.attr'``, a function, or a dict key or list index.
        (   R   t   Truet   _compare_groupR   R   (   R   t   getter(    (    s;   /usr/local/lib/python2.7/dist-packages/paste/util/looper.pyt   first_groupw   s    	c         C   s&   |  j  r t S|  j |  j |  j | ƒ S(   s  
        Returns true if this item is the end of a new group,
        where groups mean that some attribute has changed.  The getter
        can be None (the item itself changes), an attribute name like
        ``'.attr'``, a function, or a dict key or list index.
        (   R   R   R   R   R   (   R   R    (    (    s;   /usr/local/lib/python2.7/dist-packages/paste/util/looper.pyt
   last_group‚   s    	c         C   sÜ   | d  k r | | k St | t j t j f ƒ r¤ | j d ƒ r¤ | d } | j d ƒ r… | d  } t | | ƒ ƒ  t | | ƒ ƒ  k St | | ƒ t | | ƒ k Sn4 t | ƒ rÆ | | ƒ | | ƒ k S| | | | k Sd  S(   Nt   .i   s   ()iþÿÿÿ(	   R   t
   isinstancet   sixt   binary_typet	   text_typet
   startswitht   endswitht   getattrt   callable(   R   R   t   otherR    (    (    s;   /usr/local/lib/python2.7/dist-packages/paste/util/looper.pyR      s    


"N(   R   R	   R   R   R   t   propertyR   R   R   R   R   R   R   R   R   R   R!   R"   R   (    (    (    s;   /usr/local/lib/python2.7/dist-packages/paste/util/looper.pyR   @   s2   												(   R
   t   __all__R%   t   objectR    R   R   (    (    (    s;   /usr/local/lib/python2.7/dist-packages/paste/util/looper.pyt   <module>   s
   	