
                             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 Zd Zd Zd Zd Zy)z0Declarative hooks for `gcloud dialogflow agent`.    )absolute_import)division)unicode_literalsN)storage_util)log)
properties)filesc                     t         j                  j                  j                  j	                         }dj                  ||       S )Nzprojects/{}/agent/sessions/{})r   VALUEScoreproject	GetOrFailformat)sessionr   s     8lib/googlecloudsdk/command_lib/dialogflow/agent/hooks.pyAddSessionPrefixr      s5    ""**446'	(	/	/	AA    c                     |j                   j                  }|j                  d      r|j                  |j                  _        |S |j                  d      r|j                  |j                  _        |S )N
query_textquery_audio_file)*googleCloudDialogflowV2DetectIntentRequest
queryInputIsSpecifiedlanguagetextlanguageCodeaudioConfig)unused_instance_refargsrequestquery_inputs       r   SetQueryLanguager"   #   sb    BBMM+	l#$(MMK! 
. *++/==K(	.r   c                 B    t         j                  j                  d       y )NzSuccessfully trained agent.)r   statusPrint)unused_responseunused_argss     r   LogTrainSuccessr(   ,   s    **01r   c                 @    | j                  t        j                        S N)
startswithr   GSUTIL_BUCKET_PREFIX)paths    r   IsBucketUrir.   0   s    	::	;;r   c                 f    |j                   }t        |      rt        j                  r	d|i|_        |S )NagentUri)destinationr.   r   ValidateBucketUrl)googleCloudDialogflowV2ExportAgentRequest)r   r   r    dests       r   SetAgentUrir5   4   s/    			$<999CT8JG5	.r   c                 X   |j                   }t        |      s| j                  }t        d |D              }t	        j
                  |j                  j                        }t        j                  ||d       |dk7  r.t        j                  j                  dj                  |             | S )Nc              3   @   K   | ]  }|j                   d k(  s|  yw)agentContentN)key).0props     r   	<genexpr>z"SaveAgentToFile.<locals>.<genexpr>?   s     N%$488~3M%s   T)binary-zWrote agent to [{}].)r1   r.   additionalPropertiesnextbase64	b64decodevaluestring_valuer   WriteToFileOrStdoutr$   r%   r   )responser   r4   propsagent_contentagent_content_bins         r   SaveAgentToFilerJ   ;   s    			$	T	))EN%NNM(()<)<)I)IJD"3DAs{	jj-44T:;	/r   c                     |j                   ryy)Nrestoreimport)replace_all)r   r   s     r   ChooseImportOrRestoreMethodrO   G   s    		r   c                 Z    | j                  d      rd| iS dt        j                  |       iS )Nzgs://r0   r8   )r+   r	   ReadBinaryFileContents)sources    r   _GetAgentRequestBodyrS   M   s1    wE88@AAr   c                 h    t        |j                        }|j                  r	||_        |S ||_        |S r*   )rS   rR   rN   *googleCloudDialogflowV2RestoreAgentRequest)googleCloudDialogflowV2ImportAgentRequest)r   r   r    bodys       r   ModifyImportOrRestoreRequestrX   T   s7    	dkk	*$	9=G6 
. 9=G5	.r   c                 2   |j                   }|j                  s~|dk7  r/t        j                  j	                  dj                  |             nt        j                  j	                  d       |j                  rt        j                  j	                  d       | S )Nr>   z&Successfully imported agent from [{}].zSuccessfully imported agent.z Replaced all existing resources.)rR   async_r   r$   r%   r   rN   )rF   r   r-   s      r   LogImportSuccessr[   _   sk    	$	s{	jj?FFtLM	jj56	jj9:	/r   )__doc__
__future__r   r   r   rA   googlecloudsdk.api_lib.storager   googlecloudsdk.corer   r   googlecloudsdk.core.utilr	   r   r"   r(   r.   r5   rJ   rO   rS   rX   r[    r   r   <module>rb      sV     7 &  '  7 # * *B
2<	B	r   