
                         r    d Z ddlmZ ddlmZ ddlmZ ddlmZ ddlZddlZ G d dej                        Z	y)	zHIterator wrapper for checking wrapped iterator's emptiness or plurality.    )absolute_import)print_function)division)unicode_literalsNc                   <    e Zd ZdZd Zd
dZd Zd Zd Zd Z	d Z
y	)PluralityCheckableIteratora  Iterator wrapper class.

    Allows you to check whether the wrapped iterator is empty and
    whether it has more than 1 element. This iterator accepts three types of
    values from the iterator it wraps:
      1. A yielded element (this is the normal case).
      2. A raised exception, which will be buffered and re-raised when it
         is reached in this iterator.
      3. A yielded tuple of (exception, stack trace), which will be buffered
         and raised with it is reached in this iterator.
  c                 <    || _         d | _        g | _        d| _        y )NF)orig_iteratorbase_iteratorheadunderlying_iter_empty)selfits     5platform/gsutil/gslib/plurality_checkable_iterator.py__init__z#PluralityCheckableIterator.__init__0   s#     DDDI!&D    c                    | j                   st        | j                        |k  r	 | j                  st	        | j
                        | _        t        | j                        }d| _         t        |t              r8t        |d   t              r%| j                  j                  d|d   |d   f       n| j                  j                  d|f       | j                   st        | j                        |k  rt        | j                        S # t        $ r
 d| _         Y Lt        $ r=}| j                  j                  d|t        j                         d   f       Y d}~d}~ww xY w)	a  Populates self.head from the underlying iterator.

    Args:
      num_elements: Populate until self.head contains this many
          elements (or until the underlying iterator runs out).

    Returns:
      Number of elements at self.head after execution complete.
    Fr   	exception   elementT   N)r   lenr   r   iterr
   next
isinstancetuple	ExceptionappendStopIterationsysexc_info)r   num_elementses      r   _PopulateHeadz(PluralityCheckableIterator._PopulateHead8   s    ((S^l-J>!!#D$6$67$
##$%*"aJqtY$?
))

K1qt4
5
))

Iq>
* ((S^l-J& tyy>  * &*" > 			+q#,,.*;<==	>s   B'D EE!3EEc                     | S N r   s    r   __iter__z#PluralityCheckableIterator.__iter__W   s    Kr   c                     | j                         rR| j                  j                  d      }|d   dk(  r|d   S t        j                  |d   j
                  |d   |d         t               )Nr   r   r   r   )r$   r   popsixreraise	__class__r   )r   
item_tuples     r   __next__z#PluralityCheckableIterator.__next__Z   sc    99==#j	A)	#!}kk*Q-11:a=*Q-PP
/r   c                 $    | j                          S r&   r$   r(   s    r   IsEmptyz"PluralityCheckableIterator.IsEmptyc   s    !!###r   c                 ,    | j                  d      dkD  S )Nr   )r"   r   r2   r(   s    r   HasPluralityz'PluralityCheckableIterator.HasPluralityf   s     1-11r   c                     | j                         rO| j                  d   d   dk(  r9| j                  d   }t        j                  |d   j                  |d   |d         yy)z9Raises an exception if the first iterated element raised.r   r   r   r   N)r$   r   r,   r-   r.   )r   exception_tuples     r   PeekExceptionz(PluralityCheckableIterator.PeekExceptionk   sc    		!Q; >		!oKK*44oa6H'*, , !?r   N)r   )__name__
__module____qualname____doc__r   r$   r)   r0   r3   r5   r8   r'   r   r   r   r   #   s*    
'>$2
,r   r   )
r<   
__future__r   r   r   r   r    r,   Iteratorr   r'   r   r   <module>r?      s1    O ' %  ' 
 
M, M,r   