
    &                     l   d Z ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlm	Z	 ddl
mZ dd	l
mZ dd
lmZ ddlmZ ddlmZ ddlmZ ddlmZ  G d dej(                        Zd Zd Zd ZddZ	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 ddZd dZd dZd dZ eg d      Zd Z d dZ!d Z"y)!z#Utilities for ml versions commands.    )absolute_import)division)unicode_literals)versions_api)models_util)uploads)labels_util)repeated)
exceptions)log)
properties)	resources)
console_ioc                       e Zd ZdZy)InvalidArgumentCombinationErrorz<Indicates that a given combination of arguments was invalid.N)__name__
__module____qualname____doc__     9lib/googlecloudsdk/command_lib/ml_engine/versions_util.pyr   r   !   s    Dr   r   c                 V    t        j                  || j                  j                        S N)r	   ParseCreateArgsversion_classLabelsValue)clientargss     r   ParseCreateLabelsr    &   s!    		$	$T6+?+?+K+K	LLr   c                 v    t        j                  || j                  j                  |j	                  d            S )Nlabels)r	   ProcessUpdateArgsLazyr   r   GetAttrThunk)r   
get_resultr   s      r   ParseUpdateLabelsr&   *   s5    		*	*
F  ,,j.E.Eh.O
Q Qr   c                     t         j                  j                  |t        j                  j
                  j                  j                  | dd      S )z9Parses a model/version ID into a version resource object.)
projectsIdmodelsIdzml.projects.models.versions)params
collection)r   REGISTRYParser   VALUEScoreproject	GetOrFail)modelversions     r   ParseVersionr4   /   sK    				!	!"))..66@@ / 
" 
0 0r   Nc                 D    |r|S | j                  ||      j                  S )a  Waits for an operation if asyncronous flag is on.

  Args:
    operations_client: api_lib.ml_engine.operations.OperationsClient, the client
      via which to poll
    op: Cloud ML Engine operation, the operation to poll
    asyncronous: bool, whether to wait for the operation or return immediately
    message: str, the message to display while waiting for the operation

  Returns:
    The result of the operation if asyncronous is true, or the Operation message
        otherwise
  message)WaitForOperationresponse)operations_clientopasyncronousr7   s       r   WaitForOpMayber=   :   s(     I		+	+B	+	@	I	IIr   c!                 `   |r	 t        j                  ||      }|t        j
                  j                  d       t        j                  |      }!| j                  |||||	||
|||||||||||||||||||||       }"|"j                  s|rt        d      t        |"d      xr. t        |"j                  d      xr |"j                  j                  }#|"j                  s|#s|st        d      | j                  |!|"      }$t        ||$|d	      S # t         j                  $ r t        d      w xY w)
z<Create a version, optionally waiting for creation to finish.zPIf --origin is provided as a local path, --staging-bucket must be given as well.zExplanations reflect patterns in your model, but don't necessarily reveal fundamental relationships about your data population. See https://cloud.google.com/vertex-ai/docs/explainable-ai/limitations for more information.)pathdeployment_uriruntime_versionr"   descriptionmachine_type	frameworkpython_versionpackage_urisprediction_classaccelerator_configservice_accountexplanation_methodnum_integral_steps	num_pathsimagecommandcontainer_argsenv_varsportspredict_routehealth_route	min_nodes	max_nodesmetricscontainers_hiddenautoscaling_hiddenzgEither `--origin` must be provided or `deploymentUri` must be provided in the file given by `--config`.	containerrM   zlEither `--origin`, `--image`, or equivalent parameters in a config file (from `--config`) must be specified.z3Creating version (this might take a few minutes)...r<   r7   )r   UploadDirectoryIfNecessaryMissingStagingBucketExceptionr   r   statusPrintr   
ParseModelBuildVersiondeploymentUrihasattrrY   rM   Creater=   )%versions_clientr:   
version_idr2   originstaging_bucketrA   config_filer<   r"   rC   rB   rD   rE   rG   rF   rH   rI   rJ   rK   rL   rM   rN   rO   rP   rQ   rR   rS   rT   rU   rV   rW   rX   	model_refr3   	has_imager;   s%                                        r   rc   rc   M   s   D 11&.If
 #JJ	 ! $$U+)((%#'+%++#!)+9 ) -': 
		#4
)	45 5 g{# 0A0A7(K   
		y9J
)	45 5 i1"	C
E Ek 00 + s   D D-c                     t        ||      }t        j                  dj                  |j                        d       | j                  |      }t        ||ddj                  |j                              S )Nz This will delete version [{}]...T)cancel_on_noFzDeleting version [{}]...rZ   )r4   r   PromptContinueformat
versionsIdDeleter=   )rd   r:   r3   r2   version_refr;   s         r   rp   rp      so    UG,+(//0F0FG k*"	(//0F0FG
I Ir   c                 <    t        ||      }| j                  |      S r   )r4   Getrd   r3   r2   rq   s       r   Describeru      s    UG,+			[	))r   c                 N    t        j                  |      }| j                  |      S r   )r   r_   List)rd   r2   ri   s      r   rw   rw      s#    $$U+)			i	((r   )autoScalingrB   manualScalingrequestLoggingConfigc           
         t         j                  j                  | j                  |      }d}t	        d      r,j
                  r | j                  j
                  t              }j                  xs |r|j                  nd}t        | |      }d}|r,t	        |j                  d      r|j                  j                  }d}	|r,t	        |j                  d      r|j                  j                  }	d}
|r,t	        |j                  d      r|j                  j                  }
t        dd      }|.|r,t	        |j                   d      r|j                   j"                  }t        dd      }|.|r,t	        |j                   d	      r|j                   j$                  }g d
}	 | j'                  |||||	|
||      }|j)                  |dj+                  |j-                                     j.                  S # t0        j2                  $ r7 t5        fd|D              s t6        j8                  j;                  d       Y yw xY w)zUpdate the given version.NconfignodesminNodesmaxNodesbigquery_table_namebigqueryTableNamesampling_percentagesamplingPercentage)update_labelsclear_labelsremove_labelsrB   )manual_scaling_nodesauto_scaling_min_nodesauto_scaling_max_nodesr   r   zUpdating version [{}]r6   c              3   @   K   | ]  }j                  |        y wr   )IsSpecified).0argr   s     r   	<genexpr>zUpdate.<locals>.<genexpr>   s     9t$s   zNo update to perform.)r
   CachedResultFromFuncrs   rb   r|   
ReadConfig_ALLOWED_UPDATE_YAML_FIELDSrB   r&   ry   r}   rx   r~   r   getattrrz   r   r   Patchr8   rn   Namer9   r   NoFieldsSpecifiedErroranyr   r]   r^   )rd   r:   rq   r   r%   r3   rB   labels_updater   r   r   r   r   all_argsr;   s      `           r   Updater      s*   $$--;(* 'T8((02G   NGW%8%8+ $OZF-..8"0066,,j9$0099,,j9$0099&;TB W""$729!66HH&;TB W""$82:!66IIN(Q			155// 
 
1B --
+22;3C3C3EF . HHPQ 
	,	, 999JJ,-	s    G3 3AH=<H=c                 <    t        ||      }| j                  |      S r   )r4   
SetDefaultrt   s       r   r   r     s    UG,+		#	#K	00r   c                     t        j                         j                  j                  }| |j                  k7  r,|j                  d      st        dj                  |            y y )Nmlz=Machine type {0} is currently only supported with tensorflow.)r   GetMessagesModuleGoogleCloudMlV1VersionFrameworkValueValuesEnum
TENSORFLOW
startswithr   rn   )rD   rC   frameworks_enums      r   !ValidateFrameworkAndMachineTypeGar   
  se    $$&==  ?---

!
!$
'
)GNN	  ( .r   )FN)NNNNNNNNNNNNNNNNNNNNNNNNNNNNTTr   )#r   
__future__r   r   r    googlecloudsdk.api_lib.ml_enginer   $googlecloudsdk.command_lib.ml_enginer   r   $googlecloudsdk.command_lib.util.argsr	   r
   googlecloudsdk.corer   r   r   r   googlecloudsdk.core.consoler   Errorr   r    r&   r4   r=   rc   rp   ru   rw   	frozensetr   r   r   r   r   r   r   <module>r      s    * &  ' 9 < 8 < 9 * # * ) 2j&6&6 
MQ
0J,  """!"A\E~I*
)
 ( )  9Qx1
r   