
    g                        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m	Z	 ddl
mZ ddlm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ZdZddZ ej,                  ej.                  j0                         G d dej2                               Z ej,                  ej.                  j6                  ej.                  j8                         G d de             Zy)z(Cloud Pub/Sub subscription pull command.    )absolute_import)division)unicode_literals)
exceptions)subscriptions)base)flags)resource_args)utilNztable[box](
  message.data.decode(base64).decode(utf-8),
  message.messageId,
  message.orderingKey,
  message.attributes.list(separator='
'),
  deliveryAttempt,
  ackId.if(NOT auto_ack)
)
ztable[box](
  message.data.decode(base64).decode(utf-8),
  message.messageId,
  message.orderingKey,
  message.attributes.list(separator='
'),
  deliveryAttempt,
  ackId.if(NOT auto_ack),
  ackStatus.if(auto_ack)
)
c                    t        j                         }| j                  j                  j	                         }|j                  |||      }i }g }| j                  rV|j                  rJ|j                  D cg c]  }|j                   }	}	 |j                  |	|       |s|D ]  }|d   ||d   <    | j                  s|j                  S g }|j                  D ]x  }i }|j+                         D ]*  }t-        ||j.                        }|s|||j.                  <   , |j                  |v r||j                     |d<   nd|d<   |j1                  |       z |S c c}w # t        j                  $ rw}
t        j                  |
      }t        j                  |j                   j"                        }|s,|	D ]'  }dt%        j&                  |
j(                        z   ||<   ) Y d}
~
Ad}
~
ww xY w)z#Pulls messages from a subscription.FAILURE_NFailureReasonAckId	ackStatusSUCCESS)r   SubscriptionsClientCONCEPTSsubscriptionParsePullauto_ackreceivedMessagesackIdAckapi_ex	HttpErrorutil_exHttpExceptionr   ParseExactlyOnceErrorInfopayloaddetailssix	text_typestatus_code
all_fieldsgetattrnameappend)argsmax_messagesreturn_immediatelyclientsubscription_refpull_responsefailed_ack_idsack_ids_and_failure_reasonsmessageack_idserrorexcack_idack_ids_and_failure_reason
return_valmessage_copyfieldvalues                     (lib/surface/pubsub/subscriptions/pull.py_Runr<   8   s   ,,.&]]//557++.02- . "	]]}55,9,J,JK,Jw}},JGKQjj*+ (C
$2?C 	1 	 )D 
))) *//gL##%guzz*e	#(UZZ  & }}&"0"?l;"+l;l# 0 
I L  	Q!!%(c$($B$B
++

%!
 )F#-e>O>O0P#P.
  	Qs   6EE G$-A,GG$c                   .    e Zd ZdZddiZed        Zd Zy)r   =Pulls one or more Cloud Pub/Sub messages from a subscription.DESCRIPTIONa             Returns one or more messages from the specified Cloud Pub/Sub
          subscription, if there are any messages enqueued.

          By default, this command returns only one message from the
          subscription. Use the `--limit` flag to specify the max messages to
          return.

          Please note that this command is not guaranteed to return all the
          messages in your backlog or the maximum specified in the --limit
          argument.  Receiving fewer messages than available occasionally
          is normal.c                     | j                   j                  t               t        j                  | d       t        j                  |        t        j                  j                  | d       y )Nto pull messages from.   )
display_info	AddFormatMESSAGE_FORMAT_WITH_ACK_STATUSr
   AddSubscriptionResourceArgr	   AddPullFlagsr   
LIMIT_FLAG
SetDefaultparsers    r;   Argsz	Pull.Args~   sJ    
!!"@A,,V5MN	vOOvq)    c                 .    t        ||j                        S )N)r<   limit)selfr)   s     r;   RunzPull.Run   s    djj!!rM   N)__name__
__module____qualname____doc__detailed_helpstaticmethodrL   rQ    rM   r;   r   r   j   s/    E -  * *"rM   r   c                   &    e Zd ZdZed        Zd Zy)PullBetar>   c                     | j                   j                  t               t        j                  | d       t        j                  | ddd       y )NrA   T)add_deprecatedadd_waitadd_return_immediately)rC   rD   rE   r
   rF   r	   rG   rJ   s    r;   rL   zPullBeta.Args   s?    
!!"@A,,V5MN	td4rM   c                 :   |j                  d      r4|j                  d      rt        j                  dd      |j                  }n|j                  }d}|j                  d      r|j
                  }n|j                  d      r|j                   }t        |||      S )NrO   r*   z--max-messagesz--limitFr+   wait)IsSpecifiedr   ConflictingArgumentsExceptionrO   r*   r+   r`   r<   )rP   r)   r*   r+   s       r;   rQ   zPullBeta.Run   s     			.	)667G7@B 	BZZl&&l,-22			&	!#yy=l$677rM   N)rR   rS   rT   rU   rW   rL   rQ   rX   rM   r;   rZ   rZ      s    E 8rM   rZ   )F)rU   
__future__r   r   r   apitools.base.pyr   r   googlecloudsdk.api_lib.pubsubr   googlecloudsdk.api_lib.utilr   googlecloudsdk.callioper   !googlecloudsdk.command_lib.pubsubr	   r
   r   r"   MESSAGE_FORMATrE   r<   ReleaseTracksReleaseTrackGAListCommandr   BETAALPHArZ   rX   rM   r;   <module>rp      s    / &  ' 1 7 = ( . 3 ; 2 
	
" /d D%%(()"4 " *"> D%%**D,=,=,C,CD8t 8 E8rM   