
                         r    S r SSKJr  SSKJr  SSKJr  SSKJr  SSKrSSKr " S S\R                  5      r	g)	zHIterator wrapper for checking wrapped iterator's emptiness or plurality.    )absolute_import)print_function)division)unicode_literalsNc                   F    \ rS rSrSrS rSS jrS rS rS r	S r
S	 rS
rg)PluralityCheckableIterator#   a  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                 :    Xl         S U l        / U l        SU l        g )NF)orig_iteratorbase_iteratorheadunderlying_iter_empty)selfits     5platform/gsutil/gslib/plurality_checkable_iterator.py__init__#PluralityCheckableIterator.__init__0   s!     DDI!&D    c                    U R                   (       d  [        U R                  5      U:  a   U R                  (       d  [	        U R
                  5      U l        [        U R                  5      nSU l         [        U[        5      (       a=  [        US   [        5      (       a%  U R                  R                  SUS   US   45        OU R                  R                  SU45         U R                   (       d  [        U R                  5      U:  a  M  [        U R                  5      $ ! [         a
    SU l          NT[         a>  nU R                  R                  SU[        R                  " 5       S   45         SnANSnAff = f)	zPopulates 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   _PopulateHead(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$ D$ $E>7	E> 4E99E>c                     U $ N r   s    r   __iter__#PluralityCheckableIterator.__iter__W   s    Kr   c                     U R                  5       (       aT  U R                  R                  S5      nUS   S:X  a  US   $ [        R                  " US   R
                  US   US   5      e[        5       e)Nr   r   r   r   )r&   r   popsixreraise	__class__r!   )r   
item_tuples     r   __next__#PluralityCheckableIterator.__next__Z   sf    99==#j	A)	#!}kk*Q-11:a=*Q-PP
/r   c                 ,    U R                  5       (       + $ r)   r&   r+   s    r   IsEmpty"PluralityCheckableIterator.IsEmptyc   s    !!###r   c                 &    U R                  SS9S:  $ )Nr   )r$   r   r7   r+   s    r   HasPlurality'PluralityCheckableIterator.HasPluralityf   s     1-11r   c                     U R                  5       (       aQ  U R                  S   S   S:X  a:  U R                  S   n[        R                  " US   R                  US   US   5      egg)z9Raises an exception if the first iterated element raised.r   r   r   r   N)r&   r   r0   r1   r2   )r   exception_tuples     r   PeekException(PluralityCheckableIterator.PeekExceptionk   sf    		!Q; >		!oKK*44oa6H'*, , !?r   )r   r   r   r   N)r   )__name__
__module____qualname____firstlineno____doc__r   r&   r,   r4   r8   r;   r?   __static_attributes__r*   r   r   r   r   #   s*    
'>$2
,r   r   )
rE   
__future__r   r   r   r   r"   r0   Iteratorr   r*   r   r   <module>rI      s1    O ' %  ' 
 
M, M,r   