
    
                     t    d Z ddlmZ ddlmZ ddlmZ ddlZddlZddlmZ ddl	Z	d Z
d Zd	 Zd
 Zd Zd Zy)zFunctions to convert attribute formats between Task Queue and Cloud Tasks.

Functions defined here are used to migrate away from soon to be deprecated
admin-console-hr superapp. Instead we will be using Cloud Tasks APIs.
    )absolute_import)division)unicode_literalsN)	constantsc                 H   t        t        d      si t        _        | t        j                  v rt        j                  |    S | j                  d      }t	        |      D ]  \  }}|dk(  r|j                         ||<     dj                  |      }|t        j                  | <   |S )zTakes a 'snake_case' string and converts it to 'camelCase'.

  Args:
    string: The string we want to convert.

  Returns:
    The converted string. Some examples are below:
      min_backoff => minBackoff
      max_retry_duration => maxRetryDuration
  processed_strings_r    )hasattrConvertStringToCamelCaser   split	enumerate
capitalizejoin)string
attributesindex	attributecamel_case_strings        :lib/googlecloudsdk/api_lib/tasks/task_queues_convertors.pyr   r   "   s     
)+>	?13.'999#55f==||C *#J/eYz!,,.Ju 0 ggj)7H,,V4	    c                 h    t        | dd       | d   }}t        |t        j                  |   z  d      S )aI  Converts the time based rate into its integer value in seconds.

  This function converts the input float values into its seconds equivalent.
  For example,
    '100/s' => 100.0
    '60/m' => 1.0

  Args:
    value: The string value we want to convert.

  Returns:
    A float value representing the rate on a per second basis
  N	   )floatroundr   TIME_IN_SECONDS)valuefloat_valueunits      r   ConvertRater"   ;   s9     E#2J'rt+	{Y66t<<a	@@r   c                     t        | t        j                        s| S t        j                  d|       r%t        | dd       t        j                  | d      z  S 	 t        |       }|S # t        $ r | }Y |S w xY w)a  Converts the input into a float if possible.

  This function converts the input float values into its seconds equivalent if
  the string has any relevant time units. For example,
    '2m' => 120.0
    '1h' => 3600.0
    '8s' => 8.0
    'apples' => 'apples'

  Args:
    string: The string we want to convert.

  Returns:
    The input itself if it is not possible to convert it to a float value,
    the converted float value otherwise.
  z^(\d+(\.\d+)?|\.\d+)[smhd]$Nr   )	
isinstancesixstring_typesrematchr   r   r   
ValueError)r   return_values     r   (CheckAndConvertStringToFloatIfApplicabler+   M   s    " 
FC,,	-MXX,f5	 9 9&* EEE=L 
 
 L	s   A& &A54A5c                 >    | ydj                  t        | d            S )zConverts min/max backoff values to the format CT expects.

  Args:
    value: A float value representing time in seconds.

  Returns:
    The string representing the time with 's' appended at the end.
  N{}s   )formatr   )r   s    r   ConvertBackoffSecondsr0   i   s!     ]	eE1o	&&r   c                     | j                  d      }t        |      dk(  rt        j                  d|d   i      S t        |      dk(  rt        j                  |d   |d   d      S t	        dj                  |             )a@  Converts target to that format that Cloud Tasks APIs expect.

  Args:
    value: A string representing the service or version_dot_service.

  Returns:
    An ordered dict with parsed values for service and target if it exists.

  Raises:
    ValueError: If the input provided for target is not in the format expected.
  .   servicer      )r4   versionz(Unsupported value received for target {})r   lencollectionsOrderedDictr)   r/   )r   targetss     r   ConvertTargetr;   w   s~     KK'\Q""Iwqz#:;;
7|q""AJ71:68 8=DDUKLLr   c                     t        | dd       t        j                  | d      z  }dj                  t	        |            S )a  Converts task age limit values to the format CT expects.

  Args:
    value: A string value representing the task age limit. For example, '2.5m',
      '1h', '8s', etc.

  Returns:
    The string representing the time to the nearest second with 's' appended
    at the end.
  Nr   r-   )r   r   r   r/   int)r   time_in_secondss     r   ConvertTaskAgeLimitr?      s<     %*%	(A(A%)(LL/	c/*	++r   )__doc__
__future__r   r   r   r8   r'    googlecloudsdk.command_lib.tasksr   r%   r   r"   r+   r0   r;   r?    r   r   <module>rD      sA     '  '  	 6 
2A$8'M*,r   