
    K                        S r SSKJr  SSKJr  SSKJr  SSKrSSKJr  SSKJ	r	  SSK
Jr  SS	K
Jr  SS
K
Jr  SSKJr  SSKJr  SSKJr  SrSrSr " S S\R.                  5      r " S S\5      r " S S\5      rS r\4S jrS rS rg)z(Wrapper for interacting with speech API.    )absolute_import)division)unicode_literalsN)storage_util)apis)
exceptions)log)
properties)
console_io)files)urllibspeechv1zl[{}] is not a valid format for result output. Must be a Google Cloud Storage URI (format: gs://bucket/file).c                       \ rS rSrSrSrg)Error,   zExceptions for this module. N__name__
__module____qualname____firstlineno____doc____static_attributes__r       0lib/googlecloudsdk/command_lib/ml/speech/util.pyr   r   ,   s    #r   r   c                       \ rS rSrSrSrg)AudioException0   zRaised if audio is not found.r   Nr   r   r   r   r   r   0   s    %r   r   c                       \ rS rSrSrSrg)UriFormatError4   z&Error if the specified URI is invalid.r   Nr   r   r   r   r!   r!   4   s    .r   r!   c                 f   [         R                  " [        U5      nUR                  5       n[        R
                  R                  U 5      (       a  [        R                  " U 5      Ul	        U$ [        R                  R                  U 5      (       a  Xl        U$ [        SR                  U 5      5      e)zGDetermine whether path to audio is local, set RecognitionAudio message.z}Invalid audio source [{}]. The source must either be a local path or a Google Cloud Storage URL (such as gs://bucket/object).)r   GetMessagesModule
SPEECH_APIRecognitionAudioospathisfiler   ReadBinaryFileContentscontentr   ObjectReferenceIsStorageUrlurir   format)r(   versionmessagesaudios       r   GetRecognitionAudioFromPathr3   8   s    ##J8(

#
#
%%WW^^D006EM 
, ##0066I 
,	 	FFLfG r   c                    ^  U 4S jnU$ )zFReturns a hook to get the RecognitionAudio message for an API version.c                    > [        U T5      $ )a*  Determine whether path to audio is local, build RecognitionAudio message.

Args:
  path: str, the path to the audio.

Raises:
  AudioException: If audio is not found locally and does not appear to be
    Google Cloud Storage URL.

Returns:
  speech_v1_messages.RecognitionAudio, the audio message.
)r3   )r(   r0   s    r   GetAudioFromPath&GetAudioHook.<locals>.GetAudioFromPathK   s     'tW55r   r   )r0   r6   s   ` r   GetAudioHookr8   I   s    6 
r   c                     U (       aB  [         R                  R                  U 5      (       d  [        [        R                  U 5      5      eU $ )zValidates given output URI against validator function.

Args:
  output_uri: str, the output URI for the analysis.

Raises:
  UriFormatError: if the URI is not valid.

Returns:
  str, The same output_uri.
)r   r,   r-   r!   OUTPUT_ERROR_MESSAGEr/   )
output_uris    r   ValidateOutputUrir<   \   s:     44AA*MM
-44Z@
AA	r   c                 `   [         R                  " 5       (       d7  [        R                  R                  R
                  R                  5       (       a  gU R                  R                  nU(       d  g[        R                  R                  UR                  S5      (       a  USS OU5      U R                  R                  U R                  R                  U R                  R                   U R                  R"                  U R                  R$                  U R                  R&                  S.nSR)                  S [+        UR-                  5       5       5       5      nSSS	US
S
4n[        R                  R/                  U5      n[0        R2                  R5                  SR7                  U5      5        g)zGPrint Url to the Speech-to-text UI console for given recognize request.Nzgs://   )r2   encodingmodellocalesamplingchannelsenhanced;c              3      #    U  H@  u  pU(       d  M  S [        U5      R                  5       ;  d  M-  SR                  X5      v   MB     g7f)unspecifiedz{}={}N)strlowerr/   ).0keyvalues      r   	<genexpr>&MaybePrintSttUiLink.<locals>.<genexpr>   sE      L(? /"/s5z7G7G7I"I /GNN3..(?s   A
A
A
httpszconsole.cloud.google.comz/speech/transcriptions/create z*Try this using the Speech-to-Text UI at {})r   IsRunFromShellScriptr
   VALUEScoredisable_promptsGetBoolr2   r.   r   parse
quote_plus
startswithconfigr?   r@   languageCodesampleRateHertzaudioChannelCountuseEnhancedjoinsorteditems
urlunparser	   statusPrintr/   )request	audio_uripayloadparams	url_tuple
target_urls         r   MaybePrintSttUiLinkrj   m   sJ   %%'',,4466
mm)	
 ,,
!
!(33G<<im)M ..
!
!
..


..
%
%
..
(
(
..
*
*
..
$
$'$ 88 L(.w}}(?L L& 2.B@)||&&y1***299*EGr   )r   
__future__r   r   r   r'   googlecloudsdk.api_lib.storager   googlecloudsdk.api_lib.utilr   googlecloudsdk.corer   r	   r
   googlecloudsdk.core.consoler   googlecloudsdk.core.utilr   	six.movesr   r%   SPEECH_API_VERSIONr:   r   r   r!   r3   r8   r<   rj   r   r   r   <module>rs      s     / &  ' 	 7 , * # * 2 *  
 6 
$J $&U &/U /" , &"!Gr   