
                             d Z ddlmZ ddlmZ ddlm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
 Zd Zd Zd Zd ZddZy)z-Utilities for AI Platform endpoints commands.    )absolute_import)division)unicode_literalsN)errors)	resources)yaml)
console_ioc                     d| v r"	 t         j                  j                  | d      S t         j                  j                  | d      S # t         j                  $ r Y 6w xY w)zParse operation resource to the operation reference object.

  Args:
    operation_name: The operation resource to wait on

  Returns:
    The operation reference object
  z/endpoints/z2aiplatform.projects.locations.endpoints.operations)
collectionz(aiplatform.projects.locations.operations)r   REGISTRYParseRelativeName WrongResourceCollectionException)operation_names    3lib/googlecloudsdk/command_lib/ai/endpoints_util.pyParseOperationr      su     n$11
I 2   
			-	-!K 
. 
  55 
s    A AAc                     t        j                  | d      }t        j                  |      5 }	 t	        j
                  |      cddd       S # t        $ r t        j                  d| d      w xY w# 1 sw Y   yxY w)zLoads a YAML file.T)binaryNz6Input is not in JSON format. See `gcloud ai endpoints z --help` for details.)	r	   ReadFromFileOrStdinioBytesIOr   load
ValueErrorr   InvalidInstancesFileError)	file_path
sdk_methoddatafs       r   	_LoadYamlr   3   sv    		'	'	$	?$	zz$1YYq\   ,,B:,
!"  s   A3A#A00A33A<c                     t        | d      }t        |t              st        j                  d      d|vrt        j                  d      t        |d   t
              st        j                  d      |S )a  Reads the instances from the given file path ('-' for stdin).

  Args:
    json_request: str or None, a path to a file ('-' for stdin) containing the
      JSON body of a prediction request.

  Returns:
    A list of instances.

  Raises:
    InvalidInstancesFileError: If the input file is invalid (invalid format or
        contains too many/zero instances), or an improper combination of input
        files was given.
  predictr   z]Input instances are not in JSON format. See `gcloud ai endpoints predict --help` for details.	instancesz3Invalid JSON request: missing "instances" attributez0Invalid JSON request: "instances" must be a list)r   
isinstancedictr   r   listjson_requestrequests     r   ReadInstancesFromArgsr)   @   s     ly9'	GT	"

*
*	@ 
 

*
*=  
GK($	/

*
*:  
.    c                 R    t        | d      }d|vrt        j                  d      |S )z7Validates and reads json request for Direct Prediction.zdirect-predictr!   inputsz Input json must contain "inputs"r   r   r   r&   s     r   ReadInputsFromArgsr.   d   s.    l/?@'W

*
*+M
NN	.r*   c                     t        | d      }d|vrt        j                  d      d|vrd|vrt        j                  d      |S )z;Validates and reads json request for Direct Raw Prediction.zdirect-raw-predictr!   inputzInput json must contain "input"method_name
methodNamez5Input json must contain "method_name" or "methodName"r-   r&   s     r   ReadInputFromArgsr3   l   sU    l/CD'G

*
*+L
MM'!l'&A

*
*?  
.r*   c                     t        | t              sy| syt        | d   t              r=dj                  t	        | d   j                                     }dj                  ||      S dj                  |      S )z1Get default output format for prediction results.jsonNr   z, zc
          table(
              {}:format="table(
                  {}
              )"
          )ztable[no-heading]({}))r#   r%   r$   joinsortedkeysformat)predictionskey_namer8   s      r   GetDefaultFormatr<   x   sr    	K	&  +a.$'99VKN//123D
 vh%& #))(33r*   )r:   )__doc__
__future__r   r   r   r   googlecloudsdk.command_lib.air   googlecloudsdk.corer   r   googlecloudsdk.core.consoler	   r   r   r)   r.   r3   r<    r*   r   <module>rC      sA    4 &  ' 	 0 ) $ 2,
!H	4r*   