
    x                         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d
lmZ dZd Z G d dej$                        Z G d de      Zd Zd Zd Zd Zy)z Declarative hooks for ml speech.    )absolute_import)division)unicode_literalsN)apis)base)
exceptions)
properties)files	translatec                 p    | j                   j                         t        j                  j                  k(  ryy)Nv3v3beta1)calliope_commandReleaseTrackcalliope_baseBETA)argss    4lib/googlecloudsdk/command_lib/ml/translate/hooks.py_GetApiVersionr   #   s+    	'')]-G-G-L-LL    c                       e Zd ZdZy)ErrorzExceptions for this module.N__name__
__module____qualname____doc__ r   r   r   r   *   s    #r   r   c                       e Zd ZdZy)ContentFileErrorz8Error if content file can't be read and isn't a GCS URL.Nr   r   r   r   r    r    .   s    @r   r    c                 0   |j                   }|j                  }t        j                  t        t        |            }|j                         }t        j                  j                  j                  j                         }dj                  ||j                        |_        |j                  d      r`t        j                  j                  j                  j                         }dj                  ||j                  |j                         }||_        |rTt"        j$                  j'                  |      rt)        j*                  |      |_         n!t-        dj                  |            ||_         |j                  d      r|j.                  |_        ||_        |S )z?The hook to inject content into the language detection request.projects/{}/locations/{}modelz5projects/{}/locations/{}/models/language-detection/{}PCould not find --content-file [{}]. Content file must be a path to a local file)	mime_type)contentcontent_filer   GetMessagesModule
SPEECH_APIr   DetectLanguageRequestr	   VALUEScoreproject	GetOrFailformatzoneparentIsSpecifiedr#   ospathisfiler
   ReadFileContentsr    r%   mimeTypedetectLanguageRequest)	unused_instance_refr   requestr&   r'   messagesdetect_language_requestr-   r#   s	            r   UpdateRequestLangDetectionr=   2   s@   LL'"",##Jt0DE($::<""**446'-44WdiiH'.	g$$,,668GCJJDJJ(E$)!	ww~~l#(-(>(>|(L%#VL13 3 '.#	k"'+~~$"9'	.r   c                    |j                   }|j                  }t        j                  t        t        |            }|j                         }t        j                  j                  j                  j                         }dj                  ||j                        |_        |j                  d      r`t        j                  j                  j                  j                         }dj                  ||j                  |j                         }||_        |rUt"        j$                  j'                  |      rt)        j*                  |      g|_        n"t/        dj                  |            |g|_        |j                  d      r|j0                  |_        |j                  d      r!|j5                  |j6                        |_        |j                  d      r|j8                  |_        |j<                  |_        ||_         |S )	z6The hook to inject content into the translate request.r"   r#   "projects/{}/locations/{}/models/{}r$   r%   glossary_configglossarysource_language)!r&   r'   r   r(   r)   r   TranslateTextRequestr	   r+   r,   r-   r.   r/   r0   r1   r2   r#   r3   r4   r5   r
   r6   contentsr    r%   r7   TranslateTextGlossaryConfigglossaryConfigrC   sourceLanguageCodetarget_languagetargetLanguageCodetranslateTextRequest)	r9   r   r:   r&   r'   r;   translate_text_requestr-   r#   s	            r   UpdateRequestTranslateTextrM   P   s   LL'"",##Jt0DE(#88:""**446'-44WdiiH'.	g$$,,668G077DJJ(E#( 	ww~~l#).)?)?)M(N%#VL13 3 (/i#	k"&*nn#	'(**D4G4G*H )	'(040D0D-.2.B.B+!7'	.r   c                 *   t         j                  j                  j                  j	                         }dj                  ||j                        |_        |j                  d      r.dj                  ||j                  |j                        }||_	        |S )zBThe hook to inject content into the getSupportedLanguages request.r"   r#   r?   )
r	   r+   r,   r-   r.   r/   r0   r1   r2   r#   )r9   r   r:   r-   r#   s        r   "UpdateRequestGetSupportedLanguagesrO   u   ss    ""**446'-44WdiiH'.	g077DJJ(EGM	.r   c                 f   t        j                  t        t        |            }|j	                         }t
        j                  j                  j                  j                         }dj                  ||j                        |_        |j                  |_        |j                  |_        |j#                  |j%                  |j&                              |_        t+        |j,                  j/                               D cg c],  \  }}|j1                  |j3                  |      |r|nd      . c}}|_        |j7                  d      r|j                  j9                  t+        |j:                  j/                               D cg c]G  \  }}|j                  j8                  j=                  |dj                  ||j                  |      	      I c}}
      |_        |j7                  d      rt+        |j>                  j/                               D cg c]W  \  }}|j                  j@                  j=                  ||jC                  dj                  ||j                  |            	      Y c}}}|j                  jA                  |
      |_        ||_"        |S c c}}w c c}}w c c}}w )z<The hook to inject content into the batch translate request.r"   )outputUriPrefix)gcsDestination)inputUriN)	gcsSourcer7   modelsr?   )keyvalue)additionalProperties
glossariesz&projects/{}/locations/{}/glossaries/{}rA   )#r   r(   r)   r   BatchTranslateTextRequestr	   r+   r,   r-   r.   r/   r0   r1   rC   rH   target_language_codestargetLanguageCodesOutputConfigGcsDestinationdestinationoutputConfigsortedsourceitemsInputConfig	GcsSourceinputConfigsr2   ModelsValuerU   AdditionalPropertyrY   GlossariesValuerF   batchTranslateTextRequest)	r9   r   r:   r;   batch_translate_text_requestr-   kvadditional_propertiess	            r   UpdateRequestBatchTranslateTextro      s   ##Jt0DE(!)!C!C!E""**446'-44WdiiH'.484H4H1595O5O2.6.C.C,,T=M=M,N /D /P+
 ))+,.,A H$6$6$6$B()1t  5,. + 
h&&22 9?t{{?P?P?R8S
 9T1 ..::MMAHHTYY+ N ,8S
 3  !' 
l# mssw  tC  tC  tI  tI  tK  mLM mLdhdegh ))99LLx;;?FFwPTPYPY[\] < _ M ` mLM 
 ((884 9 6 !+ 'C'#	.-.
Ms    1J!AJ'
AJ-)r   
__future__r   r   r   r3   googlecloudsdk.api_lib.utilr   googlecloudsdk.callioper   r   googlecloudsdk.corer   r	   googlecloudsdk.core.utilr
   r)   r   r   r    r=   rM   rO   ro   r   r   r   <module>ru      sh     ' '  ' 	 , 9 * * * 
$J $Au A<"J!r   