ó
è?F[c           @   s0   d  e  f d „  ƒ  YZ d e f d „  ƒ  YZ d S(   t	   ResultSetc           B   s\   e  Z d  Z d d „ Z e d „  ƒ Z d „  Z d „  Z d „  Z	 e	 Z
 d „  Z d „  Z RS(	   s¼  
    A class used to lazily handle page-to-page navigation through a set of
    results.

    It presents a transparent iterator interface, so that all the user has
    to do is use it in a typical ``for`` loop (or list comprehension, etc.)
    to fetch results, even if they weren't present in the current page of
    results.

    This is used by the ``Table.query`` & ``Table.scan`` methods.

    Example::

        >>> users = Table('users')
        >>> results = ResultSet()
        >>> results.to_call(users.query, username__gte='johndoe')
        # Now iterate. When it runs out of results, it'll fetch the next page.
        >>> for res in results:
        ...     print res['username']

    c         C   sq   t  t |  ƒ j ƒ  d  |  _ g  |  _ i  |  _ g  |  _ d |  _ t	 |  _
 d  |  _ d |  _ | |  _ d  |  _ d  S(   Niÿÿÿÿi    (   t   superR    t   __init__t   Nonet   the_callablet	   call_argst   call_kwargst   _resultst   _offsett   Truet   _results_leftt   _last_key_seent   _fetchest   _max_page_sizet   _limit(   t   selft   max_page_size(    (    s5   lib/python2.7/site-packages/boto/dynamodb2/results.pyR      s    									c         C   s   d S(   Nt   exclusive_start_key(    (   R   (    (    s5   lib/python2.7/site-packages/boto/dynamodb2/results.pyt	   first_key$   s    c         C   s   g  |  _  d |  _ d S(   sº   
        Resets the internal state of the ``ResultSet``.

        This prevents results from being cached long-term & consuming
        excess memory.

        Largely internal.
        i    N(   R   R   (   R   (    (    s5   lib/python2.7/site-packages/boto/dynamodb2/results.pyt   _reset(   s    		c         C   s   |  S(   N(    (   R   (    (    s5   lib/python2.7/site-packages/boto/dynamodb2/results.pyt   __iter__4   s    c         C   sè   |  j  d 7_  |  j  t |  j ƒ k ry |  j t k rB t ƒ  ‚ n  |  j ƒ  x* t |  j ƒ ru |  j ru |  j ƒ  qO Wn  |  j  t |  j ƒ k  rÛ |  j d  k	 rÍ |  j d 8_ |  j d k  rÍ t ƒ  ‚ qÍ n  |  j |  j  St ƒ  ‚ d  S(   Ni   i    (	   R   t   lenR   R
   t   Falset   StopIterationt
   fetch_moreR   R   (   R   (    (    s5   lib/python2.7/site-packages/boto/dynamodb2/results.pyt   __next__7   s    
c         O   sy   t  | ƒ s t d ƒ ‚ n  | j d d ƒ |  _ |  j d k	 rZ |  j d k  rZ d |  _ n  | |  _ | |  _ | |  _ d S(   sê  
        Sets up the callable & any arguments to run it with.

        This is stored for subsequent calls so that those queries can be
        run without requiring user intervention.

        Example::

            # Just an example callable.
            >>> def squares_to(y):
            ...     for x in range(1, y):
            ...         yield x**2
            >>> rs = ResultSet()
            # Set up what to call & arguments.
            >>> rs.to_call(squares_to, y=3)

        s3   You must supply an object or function to be called.t   limiti    N(   t   callablet
   ValueErrort   popR   R   R   R   R   (   R   R   t   argst   kwargs(    (    s5   lib/python2.7/site-packages/boto/dynamodb2/results.pyt   to_callS   s    		c         C   s–  |  j  ƒ  |  j } |  j j ƒ  } |  j d k	 rE |  j | |  j <n  |  j rx |  j rx |  j |  j k rx |  j |  _ n  |  j d k	 r— |  j | d <n |  j d k	 r¶ |  j | d <n  |  j	 | | Ž  } |  j
 d 7_
 | j d g  ƒ } | j d d ƒ |  _ t | ƒ r!|  j j | d ƒ n  |  j d k	 rw|  j d k rw|  j } | t | d ƒ 8} | d k rwt |  _ qwn  |  j d k r’t |  _ n  d S(   s¯   
        When the iterator runs out of results, this method is run to re-execute
        the callable (& arguments) to fetch the next page.

        Largely internal.
        R   i   t   resultst   last_keyi    N(   R   R   R   t   copyR   R   R   R   R   R   R   t   getR   R   t   extendR   R
   (   R   R   R   R!   t   new_resultsR   (    (    s5   lib/python2.7/site-packages/boto/dynamodb2/results.pyR   v   s0    

$	N(   t   __name__t
   __module__t   __doc__R   R   t   propertyR   R   R   R   t   nextR    R   (    (    (    s5   lib/python2.7/site-packages/boto/dynamodb2/results.pyR       s   				#t   BatchGetResultSetc           B   s   e  Z d  „  Z d „  Z RS(   c         O   sG   | j  d g  ƒ |  _ | j  d d ƒ |  _ t t |  ƒ j | | Ž  d  S(   Nt   keyst   max_batch_getid   (   R   t
   _keys_leftt   _max_batch_getR   R,   R   (   R   R   R   (    (    s5   lib/python2.7/site-packages/boto/dynamodb2/results.pyR   ¨   s    c         C   s@  |  j  ƒ  |  j } |  j j ƒ  } |  j |  j  | d <|  j |  j |  _ t |  j ƒ d k rk t |  _ n  |  j	 | | Ž  } t | j
 d g  ƒ ƒ s™ d  S|  j j | d ƒ x9 t | j
 d g  ƒ ƒ D] \ } } |  j j | | ƒ qÆ Wt |  j ƒ d k r
t |  _ n  |  j j
 d ƒ r<|  j d c t | d ƒ 8<n  d  S(   NR-   i    R!   t   unprocessed_keysR   (   R   R   R   R#   R/   R0   R   R   R
   R   R$   R   R%   t	   enumeratet   insertR	   (   R   R   R   R!   t   offsett   key_data(    (    s5   lib/python2.7/site-packages/boto/dynamodb2/results.pyR   ­   s"    

%(   R'   R(   R   R   (    (    (    s5   lib/python2.7/site-packages/boto/dynamodb2/results.pyR,   §   s   	N(   t   objectR    R,   (    (    (    s5   lib/python2.7/site-packages/boto/dynamodb2/results.pyt   <module>   s   ¦