
    H                        S 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	r	 " S S\R                  5      r " S	 S
\R                  5      r " S S\R                  5      r " S S\R                  5      rS rSS jrS rSS jr SS jrg)zUtilities for Pub/Sub.    N)encoding)
exceptions)yaml)filesc                   $    \ rS rSrSrSrSrSrSrg)
ErrorCause   listyaml_or_jsonunrecognized_fieldsmultiple_smts_validate N)	__name__
__module____qualname____firstlineno__LISTYAML_OR_JSONUNRECOGNIZED_FIELDSMULTIPLE_SMTS_VALIDATE__static_attributes__r       *lib/googlecloudsdk/api_lib/pubsub/utils.pyr   r      s    	$,-3r   r   c                   ,   ^  \ rS rSrSrU 4S jrSrU =r$ )!MessageTransformsMissingFileError!   z2Error when the message transforms file is missing.c                 0   > [         TU ]  U5        X l        g Nsuper__init__path)selfmessager"   	__class__s      r   r!   *MessageTransformsMissingFileError.__init__$       	GWIr   r"   r   r   r   r   __doc__r!   r   __classcell__r%   s   @r   r   r   !   s    : r   r   c                   0   ^  \ rS rSrSrSU 4S jjrSrU =r$ )MessageTransformsEmptyFileError)   z0Error when the message transforms file is empty.c                 0   > [         TU ]  U5        Xl        g r   r   )r#   r"   r$   r%   s      r   r!   (MessageTransformsEmptyFileError.__init__,   r'   r   r(    r)   r,   s   @r   r.   r.   )   s    8 r   r.   c                   0   ^  \ rS rSrSrSU 4S jjrSrU =r$ )#MessageTransformsInvalidFormatError1   z=Error when the message transforms file has an invalid format.c                 <   > [         TU ]  U5        Xl        X l        g r   )r    r!   r"   error_cause)r#   r"   r8   r$   r%   s       r   r!   ,MessageTransformsInvalidFormatError.__init__4   s    	GWI"r   )r8   r"   r2   r)   r,   s   @r   r5   r5   1   s    E# #r   r5   c                    [        U [        5      (       a  SR                  U R                  5      $ [        U [        5      (       a  SR                  U R                  5      $ [        U [
        5      (       a  U R                  [        R                  :X  a  SR                  U R                  5      $ U R                  [        R                  :X  a0  SR                  U R                  [        R                  " U 5      5      $ U R                  [        R                  :X  a  SR                  U R                  5      $ SR                  U R                  [        R                  " U 5      5      $ [        U 5      $ )zReturns the formatted error string for an error type.

Args:
  err: Error raised during the GetMessageTransformsFromFile execution.

Returns:
  Formatted error message as a string.
z:Message transforms file [{0}] is missing or does not existz#Empty message transforms file [{0}]z>Message transforms file [{0}] not properly formatted as a listzTMessage transforms file [{0}] is not properly formatted in YAML or JSON due to [{1}]zMessage transform file [{0}] contains a list of message transforms instead of a single (1) message transform. Please edit your message-transform-file to contain a single element.zAMessage transforms file [{0}] contains unrecognized fields: [{1}])
isinstancer   formatr"   r.   r5   r8   r   r   r   six	text_typer   str)errs    r   GetErrorMessagerA   :   s    677GNN  #677077AA#:;;
*//)
J6#(( 
J33	3""(&3==3E"F 
J==	=AAGhhB N6#((CMM#./
 s8Or   c                    [         R                  " U 5      n/ nU HF  u  pVSR                  S U 5       5      nU H#  nUR                  SR	                  Xx5      5        M%     MH     U(       a*  [        U[        R                  SR                  U5      5      eU(       d-  U R                  (       a  [        U[        R                  S5      egg)z0Validate all parsed message from file are valid..c              3   N   #    U  H  n[         R                  " U5      v   M     g 7fr   )r=   r>   ).0es     r   	<genexpr>2ValidateMessageTransformMessage.<locals>.<genexpr>k   s     !M<Lq#--"2"2<Ls   #%z{}.{}
z.aiInferenceN)	r   UnrecognizedFieldIterjoinappendr<   r5   r   r   aiInference)	r$   r"   enable_vertex_ai_smterrorsunrecognized_field_pathsedges_to_messagefield_namesmessage_field_path
field_names	            r   ValidateMessageTransformMessagerU   f   s    ))'2&'-#!M<L!MM!
%%
..+
8 " (. 
-&&		*+ 
 
'"5"5
-&&  #6	r   c                      [         R                  " U 5      nU(       d	  [	        U S9eU$ ! [         R                   a  n[        X 5      eSnAff = f)a  Reads a file from a local path.

Args:
  path: A local path to an object specification in YAML or JSON format.

Returns:
  The contents of the file as a string.

Raises:
  MessageTransformsMissingFileError: If file is missing.
  MessageTransformsEmptyFileError: If file is empty.
Nr(   )r   ReadFileContentsMissingFileErrorr   r.   )r"   contentsrF   s      r   ReadFileFromPathrZ   ~   sO    5%%d+H 

)t
44	/ 
		 5
+A
445s   * AA		Ac                    [        U5      n [        R                  " U5      n[        U[        5      (       d  [        U[        R                  S9e/ nU H6  n[        R                  " X5      n[        XqU5        UR                  U5        M8     U$ ! [        [        [        [        R                  4 a   n[        U[        R                   U5      eSnAff = f)a/  Reads a YAML or JSON object of type message from local path.

Parses a list of message transforms.

Args:
  message: The message type to be parsed from the file.
  path: A local path to an object specification in YAML or JSON format.
  enable_vertex_ai_smt: Whether or not to enable Vertex AI message transforms.

Returns:
  Sequence of objects of type message, if successful.

Raises:
  MessageTransformsMissingFileError: If file is missing.
  MessageTransformsEmptyFileError: If file is empty.
  MessageTransformsInvalidFormat: If file's format is invalid.
)r"   r8   N)rZ   r   loadr;   r
   r5   r   r   r   PyValueToMessagerU   rL   	TypeError
ValueErrorAttributeErrorYAMLParseErrorr   )	r$   r"   rN   rY   message_transformsresultpy_value	transformrF   s	            r   GetMessageTransformsFromFilerf      s    $ d#(P8,($///  F&++G>i%i7KLmmI ' 
- 
	
 P .dJ4K4KQ
OOPs   BB $C4CCc                    [        U5      n [        R                  " U5      n[        U[        5      (       a/  [        U5      S:X  a  US   nO[        U[        R                  5      e[        R                  " X5      n[        XQU5        U$ ! [        [        [        [        R                  4 a   n[        U[        R                   U5      eSnAff = f)a   Reads a YAML or JSON object of type message from local path.

Parses a single message transform.

Args:
  message: The message type to be parsed from the file.
  path: A local path to an object specification in YAML or JSON format.
  enable_vertex_ai_smt: Whether or not to enable Vertex AI message transforms.

Returns:
  Object of type message, if successful.

Raises:
  MessageTransformsMissingFileError: If file is missing.
  MessageTransformsEmptyFileError: If file is empty.
  MessageTransformsInvalidFormat: If file's format is invalid.
   r   N)rZ   r   r\   r;   r
   lenr5   r   r   r   r]   rU   r^   r_   r`   ra   r   )r$   r"   rN   rY   message_transformrc   rF   s          r   (GetMessageTransformFromFileForValidationrk      s    ( d#(P		(+#T**		1	$-a01*33
 	
 &&wBF#F2FG 
- 
	
 P .dJ4K4KQ
OOPs   A<B $C/C

C)F)r*   enumapitools.base.pyr   googlecloudsdk.corer   r   googlecloudsdk.core.utilr   r=   Enumr   Errorr   r.   r5   rA   rU   rZ   rf   rk   r   r   r   <module>rr      s      % * $ * 
4 4
(8(8 j&6&6 #**:*: #)X00(X ).*r   