
    K                        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dlmZ ddlmZ ddlmZ dZdZdZ G d dej.                        Z G d de      Z G d de      Zd ZefdZd Zd Zy)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                       e Zd ZdZy)ErrorzExceptions for this module.N__name__
__module____qualname____doc__     0lib/googlecloudsdk/command_lib/ml/speech/util.pyr   r   ,   s    #r   r   c                       e Zd ZdZy)AudioExceptionzRaised if audio is not found.Nr   r   r   r   r   r   0   s    %r   r   c                       e Zd ZdZy)UriFormatErrorz&Error if the specified URI is invalid.Nr   r   r   r   r   r   4   s    .r   r   c                 P   t        j                  t        |      }|j                         }t        j
                  j                  |       rt        j                  |       |_	        |S t        j                  j                  |       r	| |_        |S t        dj                  |             )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   GetRecognitionAudioFromPathr.   8   s    ##J8(

#
#
%%WW^^D006EM 
, ##006EI 
,	 	FFLfG r   c                       fd}|S )zFReturns a hook to get the RecognitionAudio message for an API version.c                     t        |       S )aJ  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.
    )r.   )r#   r+   s    r   GetAudioFromPathz&GetAudioHook.<locals>.GetAudioFromPathK   s     'tW55r   r   )r+   r1   s   ` r   GetAudioHookr2   I   s    6 
r   c                     | r=t         j                  j                  |       st        t        j                  |             | S )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   ValidateOutputUrir6   \   s7     44AA*M
-44Z@
AA	r   c                 6   t        j                         s2t        j                  j                  j
                  j                         ry| j                  j                  }|syt        j                  j                  |j                  d      r|dd n|      | j                  j                  | j                  j                  | j                  j                   | j                  j"                  | j                  j$                  | j                  j&                  d}dj)                  d t+        |j-                               D              }ddd	|d
d
f}t        j                  j/                  |      }t0        j2                  j5                  dj7                  |             y)zGPrint Url to the Speech-to-text UI console for given recognize request.Nzgs://   )r-   encodingmodellocalesamplingchannelsenhanced;c              3      K   | ]6  \  }}|r/d t        |      j                         vrdj                  ||       8 yw)unspecifiedz{}={}N)strlowerr*   ).0keyvalues      r   	<genexpr>z&MaybePrintSttUiLink.<locals>.<genexpr>   s?      L(?e-s5z7G7G7I"I NN3.(?s   <>httpszconsole.cloud.google.comz/speech/transcriptions/create z*Try this using the Speech-to-Text UI at {})r   IsRunFromShellScriptr
   VALUEScoredisable_promptsGetBoolr-   r)   r   parse
quote_plus
startswithconfigr9   r:   languageCodesampleRateHertzaudioChannelCountuseEnhancedjoinsorteditems
urlunparser	   statusPrintr*   )request	audio_uripayloadparams	url_tuple
target_urls         r   MaybePrintSttUiLinkrc   m   sA   %%',,44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_VERSIONr4   r   r   r   r.   r2   r6   rc   r   r   r   <module>rl      s     / &  ' 	 7 , * # * 2 *  
 6 
$J $&U &/U /" , &"!Gr   