
    q                        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Jr  S SKJrJrJr  S	rS
\S\4S jrS\S\4S jrS\ R                   S\4S jrS\S\ R                   4S jrS\S\4S jrS\S\S\4S jrS\S\4S jrS\R6                  R8                  S\R6                  R8                  4S jrS\S\4S jrS\S\4S jrg)    N)InvalidArgument)	Timestamp)PubsubMessage)MessageTransformer)fast_serialize)	PartitionMessageMetadata)AttributeValuesSequencedMessagePubSubMessagezx-goog-pubsublite-event-timetsreturnc                 Z    [         R                  " U R                  U R                  /5      $ N)r   dumpsecondsnanos)r   s    Ilib/third_party/google/cloud/pubsublite/cloudpubsub/message_transforms.py"_encode_attribute_event_time_protor      s     

BHH566    attrc                      [        5       n[        R                  " U 5      nUS   Ul        US   Ul        U$ ! [
         a    [        S5      ef = f)Nr      z'Invalid value for event time attribute.)r   r   loadr   r   	Exceptionr   )r   r   loadeds      r   "_decode_attribute_event_time_protor   !   sW    I[$$T*AY
!9	 IGHHIs	   58 Adtc                     [        5       nUR                  U R                  [        R                  R
                  5      5        [        U5      $ r   )r   FromDatetime
astimezonedatetimetimezoneutcr   )r   r   s     r   encode_attribute_event_timer%   ,   s5    	BOOBMM("3"3"7"789-b11r   c                 ~    [        U 5      R                  5       R                  [        R                  R
                  S9$ )N)tzinfo)r   
ToDatetimereplacer"   r#   r$   )r   s    r   decode_attribute_event_timer*   2   s.    *40		))--	.r   valuesc                     [        U R                  5      S:X  d  [        S5      eU R                  S   n UR                  S5      $ ! [         a    [        S5      ef = f)Nr   zFReceived an unparseable message with multiple values for an attribute.r   utf-8z:Received an unparseable message with a non-utf8 attribute.)lenr+   r   decodeUnicodeError)r+   values     r   _parse_attributesr2   :   sf    v}}"T
 	
 ==#E
||G$$ 
H
 	

s   A A	partitiontransformerc                 P   ^ ^ S[         4U U4S jjn[        R                  " U5      $ )Nsourcec                   > U R                   nTR                  U 5      nUR                   nUR                  (       a  [        S5      e[        R
                  " TR                  UR                  R                  5      Ul        U$ )Nz8Message after transforming has the message_id field set.)	_pb	transform
message_idr   r	   _encode_partsr1   cursoroffset)r6   	source_pbmessage
message_pbr3   r4   s       r   add_id_to_message>add_id_to_cps_subscribe_transformer.<locals>.add_id_to_messageK   so    JJ	!,!6!6v!>[[
  !J  !0 = =OOY--44!

 r   )r   r   of_callable)r3   r4   rA   s   `` r   #add_id_to_cps_subscribe_transformerrD   H   s)    "2   ))*;<<r   r6   c                     U R                   n[        UR                  5      nUR                  R	                  UR                  5        [        5       nX#l         U$ r   )r8   _to_cps_publish_message_protor?   publish_timeCopyFromr   )r6   r>   out_pbouts       r   to_cps_subscribe_messagerK   [   sF    

I*9+<+<=F
  !7!78
/CGJr   c                    [         R                  R                  5       n U R                  R	                  S5      Ul        [        U R                  ;   a  [        S5      eU R                  Ul
        U R                  R                  5        H  u  p#[        U5      UR                  U'   M     U R                  S5      (       a&  [        U R                  5      UR                  [        '   U$ ! [         a    [        S5      ef = f)Nr-   z4Received an unparseable message with a non-utf8 key.zLSpecial timestamp attribute exists in wire message. Unable to parse message.
event_time)r   metapbkeyr/   ordering_keyr0   r   PUBSUB_LITE_EVENT_TIME
attributesdataitemsr2   HasFieldr   rM   )r6   rJ   rP   r+   s       r   rF   rF   d   s     




!CV!::,,W5 !2!22Z
 	
 {{CH((..0/7s 1|$$1S2
-. J  VTUUVs    C) )C?c                 N    [        5       n[        U R                  5      Ul        U$ r   )r   rF   r8   )r6   rJ   s     r   to_cps_publish_messagerX   z   s    
/C+FJJ7CGJr   c                 
   U R                   n[        5       nUR                   n[        UR                  ;   a5  UR                  R                  [        UR                  [           5      5        UR                  Ul        UR                  R                  S5      Ul
        UR                  R                  5        HH  u  pEU[        :w  d  M  UR                  U   R                  R                  UR                  S5      5        MJ     U$ )Nr-   )r8   r   rR   rS   rM   rH   r   rT   rQ   encoderP   rU   r+   append)r6   r>   rJ   rI   rP   r1   s         r   from_cps_publish_messager\      s    

I
/CWWF!5!55"".$$%;<	

 ..FK''..w7FJ**002
((c"))00g1FG 3 Jr   ) r"   google.api_core.exceptionsr   &cloudsdk.google.protobuf.timestamp_pb2r   google.pubsub_v1r   #google.cloud.pubsublite.cloudpubsubr    google.cloud.pubsublite.internalr   google.cloud.pubsublite.typesr   r	   google.cloud.pubsublite_v1r
   r   r   rR   strr   r   r%   r*   r2   rD   rK   rN   rO   rF   rX   r\    r   r   <module>rf      s    6 < * B ; D W W7 79 7 7IS IY I2H$5$5 2# 2c h.?.? 
o 
# 
=='9==&%5 - !!,= ] ] } r   