
    A                     &   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ZdZdZdZdZ G d dej,                        Z G d de      Z G d de      Z G d de      Zd Zd Zd Zd Zd Zy)z4Utilities for gcloud ml video-intelligence commands.    )absolute_import)division)unicode_literalsN)storage_util)apis)
exceptions)log)files)iso_duration)timesvideointelligencev1zz[{}] is not a valid format for video input. Must be a local path or a Google Cloud Storage URI (format: gs://bucket/file).zl[{}] is not a valid format for result output. Must be a Google Cloud Storage URI (format: gs://bucket/file).zCould not get video segments from [{0}]. Please make sure you give the desired segments in the form: START1:END1,START2:END2, etc.: [{1}]c                       e Zd ZdZy)Errorz!Base error class for this module.N__name__
__module____qualname____doc__     /lib/googlecloudsdk/command_lib/ml/video/util.pyr   r   2   s    )r   r   c                       e Zd ZdZy)SegmentErrorz2Error for poorly formatted video segment messages.Nr   r   r   r   r   r   6   s    :r   r   c                       e Zd ZdZy)VideoUriFormatErrorz(Error if the video input URI is invalid.Nr   r   r   r   r   r   :   s    0r   r   c                       e Zd ZdZy)AudioTrackErrorz-Error if the audio tracks setting is invalid.Nr   r   r   r   r   r   >   s    5r   r   c           	         | syt        j                  t        t              }|j                  }g }| D cg c]  }|j                  d       }}|D ]  }t        |      dk7  r.t        t        j                  dj                  |       d            |d   |d   }}	 t        |      }	t        |      }
d}|j                   ||j                  |
j                        |j                  |	j                        	              |S c c}w # t        $ r3}t        t        j                  dj                  |       |            d}~ww xY w)
aj  Get VideoSegment messages from string of form START1:END1,START2:END2....

  Args:
    given_segments: [str], the list of strings representing the segments.

  Raises:
    SegmentError: if the string is malformed.

  Returns:
    [GoogleCloudVideointelligenceXXXVideoSegment], the messages
      representing the segments or None if no segments are specified.
  N:   ,zMissing start/end segmentr      z{}s)endTimeOffsetstartTimeOffset)r   GetMessagesModule	VIDEO_APIVIDEO_API_VERSION*GoogleCloudVideointelligenceV1VideoSegmentsplitlenr   SEGMENT_ERROR_MESSAGEformatjoin_ParseSegmentTimestamp
ValueErrorappendtotal_seconds)given_segmentsmessagessegment_msgsegment_messagesssegmentssegmentstartendstart_durationend_durationvesec_fmts                r   ValidateAndParseSegmentsr@   B   sA    
##I/@A(CC+$23NqaggclN(3g
7|q.55
((>
"$?A B BWQZ3E)-e4n+C0l
 GKnn\%?%?@~'C'CDF G% * 
- 4  ).55
((>
"B( ) ))s   DD	E.D==Ec                    	 t        |       }t        j                  dj                  |              |dk  rt	        dj                  |             t        j                  |      S # t        $ r 	 t        j                  |       }|j                  dk  rt        j                         |cY S # t
        j                  t
        j                  f$ r t	        dj                  |             w xY ww xY w)a  Parse duration formatted segment timestamp into a Duration object.

  Assumes string with no duration unit specified (e.g. 's' or 'm' etc.) is
  an int representing microseconds.

  Args:
    timestamp_string: str, string to convert

  Raises:
    ValueError: timestamp_string is not a properly formatted duration, not a
    int or int value is <0

  Returns:
    Duration object represented by timestamp_string
  zTTime unit missing ('s', 'm','h') for segment timestamp [{}], parsed as microseconds.r   zCould not parse timestamp string [{}]. Timestamp must be a properly formatted duration string with time amount and units (e.g. 1m3.456s, 2m, 14.4353s)zICould not parse duration string [{}]. Timestamp must begreater than >= 0))microseconds)intr	   warningr-   r0   r   ParseDurationr2   DurationValueErrorDurationSyntaxErrorr   Duration)timestamp_stringrB   durations      r   r/   r/   n   s    $D'(L KK **0&1A*BD A
 **0&1A*BD D 
		L	99' 
 
.	.$$%56h			!	#&&((o%%u'?'?@ . HHN+I-. ..
.s#   A& &	C.09B,)C.,>C**C.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:
    VideoUriFormatError: if the URI is not valid.

  Returns:
    str, The same output_uri.
  )r   ObjectReferenceIsStorageUrlr   OUTPUT_ERROR_MESSAGEr-   )
output_uris    r   ValidateOutputUrirP      s7     44AA*M
299*E
FF	r   c                    |j                   }t        j                  j                  |      rt	        j
                  |      |_        |S t        j                  j                  |      r	||_
        |S t        t        j                  |            )zEThe Python hook for yaml commands to inject content into the request.)
input_pathospathisfiler
   ReadBinaryFileContentsinputContentr   rL   rM   inputUrir   INPUT_ERROR_MESSAGEr-   )
unused_refargsrequestrT   s       r   UpdateRequestWithInputr]      ss    	$WW^^D 77=G
 
.	 ##006G 
. 188>
??r   c                 8    t        |       dkD  rt        d      | S )z1Verify at most two tracks, convert to [int, int].r!   z+Can not specify more than two audio tracks.)r+   r   )trackss    r   AudioTrackProcessorr`      s    [1_
G
HH	-r   )r   
__future__r   r   r   rS   googlecloudsdk.api_lib.storager   googlecloudsdk.api_lib.utilr   googlecloudsdk.corer   r	   googlecloudsdk.core.utilr
   r   r   r'   r(   rY   rN   r,   r   r   r   r   r@   r/   rP   r]   r`   r   r   r   <module>rf      s     ; &  ' 	 7 , * # * 1 *	 5 6 - *J *;5 ;1% 16e 6)X':T"	r   