
    T                        d Z ddl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	 G d dej                        Z G d	 d
ej                        Z G d dej                        Z G d dej                        Zd ZddZd ZddZ	 ddZy)zUtilities for Pub/Sub.    N)encoding)
exceptions)yaml)filesc                       e Zd ZdZdZdZdZy)
ErrorCauselistyaml_or_jsonunrecognized_fieldsmultiple_smts_validateN)__name__
__module____qualname__LISTYAML_OR_JSONUNRECOGNIZED_FIELDSMULTIPLE_SMTS_VALIDATE     *lib/googlecloudsdk/api_lib/pubsub/utils.pyr   r      s    	$,-3r   r   c                   "     e Zd ZdZ fdZ xZS )!MessageTransformsMissingFileErrorz2Error when the message transforms file is missing.c                 2    t         |   |       || _        y Nsuper__init__path)selfmessager   	__class__s      r   r   z*MessageTransformsMissingFileError.__init__$       	GWDIr   r   r   r   __doc__r   __classcell__r!   s   @r   r   r   !   s    : r   r   c                   $     e Zd ZdZd fd	Z xZS )MessageTransformsEmptyFileErrorz0Error when the message transforms file is empty.c                 2    t         |   |       || _        y r   r   )r   r   r    r!   s      r   r   z(MessageTransformsEmptyFileError.__init__,   r"   r    r#   r&   s   @r   r(   r(   )   s    8 r   r(   c                   $     e Zd ZdZd fd	Z xZS )#MessageTransformsInvalidFormatErrorz=Error when the message transforms file has an invalid format.c                 @    t         |   |       || _        || _        y r   )r   r   r   error_cause)r   r   r/   r    r!   s       r   r   z,MessageTransformsInvalidFormatError.__init__4   s    	GWDI"Dr   r*   r#   r&   s   @r   r-   r-   1   s    E# #r   r-   c                    t        | t              rdj                  | j                        S t        | t              rdj                  | j                        S t        | t
              r| j                  t        j                  k(  rdj                  | j                        S | j                  t        j                  k(  r/dj                  | j                  t        j                  |             S | j                  t        j                  k(  rdj                  | j                        S dj                  | j                  t        j                  |             S t        |       S )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(   r-   r/   r   r   r   six	text_typer   str)errs    r   GetErrorMessager7   :   s    67GNN  #6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   t        j                  |       }g }|D ]E  \  }}dj                  d |D              }|D ]#  }|j                  dj	                  ||             % G |r*t        |t        j                  dj                  |            |s(| j                  rt        |t        j                  d      yy)z0Validate all parsed message from file are valid..c              3   F   K   | ]  }t        j                  |        y wr   )r3   r4   ).0es     r   	<genexpr>z2ValidateMessageTransformMessage.<locals>.<genexpr>k   s     !M<Lq#--"2<Ls   !z{}.{}
z.aiInferenceN)	r   UnrecognizedFieldIterjoinappendr2   r-   r   r   aiInference)	r    r   enable_vertex_ai_smterrorsunrecognized_field_pathsedges_to_messagefield_namesmessage_field_path
field_names	            r   ValidateMessageTransformMessagerJ   f   s    ))'2&'-#!M<L!MM!
%%
..+Z
8 " (. 
-&&		*+ 
 
'"5"5
-&&  #6	r   c                     	 t        j                  |       }|st	        |       |S # t         j                  $ r}t        ||       d}~ww xY w)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.
  N)r   )r   ReadFileContentsMissingFileErrorr   r(   )r   contentsr<   s      r   ReadFileFromPathrO   ~   sQ    5%%d+H 

)t
44	/ 
		 5
+At
445s   ' AAAc                    t        |      }	 t        j                  |      }t        |t              st        |t        j                        g }|D ]6  }t        j                  | |      }t        |||       |j                  |       8 	 |S # t        t        t        t        j                  f$ r }t        |t        j                   |      d}~ww xY w)aM  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   r/   N)rO   r   loadr1   r	   r-   r   r   r   PyValueToMessagerJ   rA   	TypeError
ValueErrorAttributeErrorYAMLParseErrorr   )	r    r   rC   rN   message_transformsresultpy_value	transformr<   s	            r   GetMessageTransformsFromFiler[      s    & d#(P8,($//  F&++GX>i%i7KLmmI ' 
- 
	
 P .dJ4K4KQ
OOPs   A=B #C0CCc                    t        |      }	 t        j                  |      }t        |t              r.t        |      dk(  r|d   }nt        |t        j                        t        j                  | |      }t        |||       |S # t        t        t        t        j                  f$ r }t        |t        j                   |      d}~ww xY w)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)rO   r   rQ   r1   r	   lenr-   r   r   r   rR   rJ   rS   rT   rU   rV   r   )r    r   rC   rN   message_transformrX   r<   s          r   (GetMessageTransformFromFileForValidationr`      s    * d#(P		(+#T*		1	$-a01*33
 	
 &&w0ABF#FD2FG 
- 
	
 P .dJ4K4KQ
OOPs   A6B #C(CC)F)r$   enumapitools.base.pyr   googlecloudsdk.corer   r   googlecloudsdk.core.utilr   r3   Enumr   Errorr   r(   r-   r7   rJ   rO   r[   r`   r   r   r   <module>rg      s      % * $ * 
4 4
(8(8 j&6&6 #**:*: #)X00)Z ).+r   