
                             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J	r	  SSK
Jr  SS	K
Jr  SS
KJr  SSKJr  SSKJr  SSKJr  SSKJr  SSKJr  Sr " S S\R0                  5      rS r " S S\5      rg)z)Utilities for Pub/Sub Lite subscriptions.    )absolute_import)division)unicode_literals)futuresN)Optional)cloudpubsub)types)PubsubMessage)	lite_util)
exceptions)
gapic_util)log)queuel    c                       \ rS rSrSrSrg)SubscribeOperationException&   z2Error when something went wrong while subscribing. N)__name__
__module____qualname____firstlineno____doc____static_attributes__r       7lib/googlecloudsdk/api_lib/pubsub/lite_subscriptions.pyr   r   &   s    :r   r   c                  P    [         R                  " [        R                  " 5       S9$ )N)credentials)r   SubscriberClientr   GetGapicCredentialsr   r   r   GetDefaultSubscriberClientr    *   s!    		%	%002
4 4r   c                   P    \ rS rSrSr SS jrS rS rS rS r	S	\
\   4S
 jrSrg)r   /   z4GCloud wrapper client for a Pub/Sub Lite subscriber.Nc                 L   U=(       d
    [        5       U l        [        R                  " 5       U l        U R                  U5      U l        U Vs1 s H  n[        R                  " U5      iM     snU l	        [        R                  " U[        S9U l        X@l        S U l        g s  snf )N)messages_outstandingbytes_outstanding)r    _clientr   Queue	_messages_SubscriptionResourceToPath_subscriptionr	   	Partition_partitionsFlowControlSettings
_MAX_INT64_flow_control_settings	_auto_ack_pull_future)selfsubscription_resource
partitionsmax_messagesauto_ackclient	partitions          r   __init__SubscriberClient.__init__2   s     979DL[[]DN99:OPDDNOJy	2JOD"'";";)$#D ND Ps    B!c                     U R                   R                  5         U R                   R                  U R                  U R                  R
                  U R                  U R                  S9U l        U $ )N)callback#per_partition_flow_control_settingsfixed_partitions)	r&   	__enter__	subscriber*   r(   putr/   r,   r1   )r2   s    r   r?   SubscriberClient.__enter__C   s]    LL..##,0,G,G))	 / +D
 Kr   c                 D   [         R                  " S5        U R                  R                  5       (       d5   U R                  R	                  5         U R                  R                  5         U R                  R                  XU5        g ! [        R                   a     N3f = f)N   )
timesleepr1   donecancelresultr   CancelledErrorr&   __exit__)r2   exc_type	exc_value	tracebacks       r   rK   SubscriberClient.__exit__L   s}    JJqM!!## 	  "  " 	LL(y9 ## s   4B BBc                     [         R                  " [        R                  " UR                  5      [        R
                  " UR                  5      UR                  S9$ )N)projectlocationname)r	   SubscriptionPathr   ProjectIdToProjectNumber
projectsIdLocationToZoneOrRegionlocationsIdsubscriptionsId)r2   resources     r   r)   ,SubscriberClient._SubscriptionResourceToPathX   sF    !!2283F3FG11(2F2FG%%' 'r   c                    U R                   R                  5       (       ae  U R                   R                  5       nU(       a  [        SR	                  US95      e[
        R                  " S5        [        R                  " S5      eg )Nz.Subscribe operation failed with error: {error})errorzNThe streaming pull future completed unexpectedly without raising an exception.z-The subscribe stream terminated unexpectedly.)	r1   rG   	exceptionr   formatr   debugr   InternalError)r2   es     r   _RaiseIfFailedSubscriberClient._RaiseIfFailed^   sz    



%
%
'a	
)<CC!CLN 	N	ii ( )$$
9; ;  r   returnc                     U R                  5          U R                  R                  SS9nU R                  (       a  UR	                  5         U$ ! [
        R                   a     gf = f)z~Pulls and optionally acks a message from the provided subscription.

Returns:
  A PubsubMessage pulled from the subscription.
rD   )timeoutN)rc   r(   getr0   ackr   Empty)r2   messages     r   PullSubscriberClient.Pulli   sW     	""1"-g	n;; s   ;A A%$A%)r0   r&   r/   r(   r,   r1   r*   )N)r   r   r   r   r   r9   r?   rK   r)   rc   r   r
   rl   r   r   r   r   r   r   /   s4    < "
:'	;H]+ r   r   )r   
__future__r   r   r   
concurrentr   rE   typingr   google.cloud.pubsubliter   r	   google.pubsub_v1r
   !googlecloudsdk.command_lib.pubsubr   googlecloudsdk.corer   r   r   	six.movesr   r.   Errorr   r    objectr   r   r   r   <module>rx      s^     0 &  '    / ) * 7 * * # 
;*"2"2 ;4
Gv Gr   