
                             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Zddd	d
ddddddddZddddddddddddZ	d Z
ddZddZddZddZddZddZd dZddZy)!z:Scaled integer ISO/IEC unit prefix parsing and formatting.    )absolute_import)division)unicode_literalsNz
    ^                           # Beginning of input marker.
    (?P<amount>\d+)             # Amount.
    ((?P<suffix>[-/a-zA-Z]+))?  # Optional scale and type abbr.
    $                           # End of input marker.
   i  i@B i ʚ;l    J)l     I5 i   i   i   @l        l           ) kMGTPkiMiGiTiPic                     t        t        j                  |       d       }|D cg c]  \  }}|s	||z    c}}S c c}}w )z:Returns a list of the units in scales sorted by magnitude.c                     | d   | d   fS Nr   r    values    .lib/googlecloudsdk/core/util/scaled_integer.py<lambda>z#_UnitsByMagnitude.<locals>.<lambda>B   s    %(E!H)=    key)sortedsix	iteritems)units	type_abbrscale_itemsr   _s        r   _UnitsByMagnituder$   ?   s<    s}}U+=?+(3	;fc1s#	/	;;	;s   
==c                     | s| S | j                         }t        |      }t        |j                               D ]  }|s n||dz
     |k(  s|dz  } | d| S )z7Returns suffix with trailing type abbreviation deleted.r   N)upperlenreversed)suffixr!   sics        r   DeleteTypeAbbrr-   F   s`    	Mlln!	!f!IOO%&aQx1}1fa	 '
 
r   c                    t        | |      }|s!|}|sd}nw|j                  d      rfd|dd z   }n]|d   j                         }|dk(  rd}t        |      dkD  r|d   dv r|dz  }|d	d }n|dd }|rt	        d
j                  || |            |xs t        j                  |      }|s5t	        dj                  || dj                  t        |d                        |S )a  Returns the size per unit for binary suffix string.

  Args:
    suffix: str, A case insensitive unit suffix string with optional type
      abbreviation.
    type_abbr: str, The optional case insensitive type abbreviation following
      the suffix.
    default_unit: The default unit prefix name.
    units: {str: int} map of unit prefix => size.

  Raises:
    ValueError: on unknown units of type suffix.

  Returns:
    The binary size per unit for a unit+type_abbr suffix.
  r   Kr   r   Nr   )r+   Ir+      z4Invalid type [{}] in [{}], expected [{}] or nothing.z2Invalid suffix [{}] in [{}], expected one of [{}].,)
r-   
startswithr&   r'   
ValueErrorformat_ISO_IEC_UNITSgetjoinr$   )r)   r!   default_unitr    prefixunitsizes          r   GetUnitSizer=   T   s   " &),&	Dd		48^d!9??Ds{d
6{Q6!9
2
ckdabzfabzfMvvy9; ; 
!>	&	&t	,$	
IfT6 XX&7r&BCEF F 
+r   c                 (    t        | ||t              S )a  Returns the binary size per unit for binary suffix string.

  Args:
    suffix: str, A case insensitive unit suffix string with optional type
      abbreviation.
    type_abbr: str, The optional case insensitive type abbreviation following
      the suffix.
    default_unit: The default unit prefix name.

  Raises:
    ValueError for unknown units.

  Returns:
    The binary size per unit for a unit+type_abbr suffix.
  r!   r9   r    )r=   _BINARY_UNITS)r)   r!   r9   s      r   GetBinaryUnitSizerA      s      
Vy|(
* *r   c                 `   t        j                  t        |t         j                        }|sA|rd|z   dz   nd}t	        dj                  ||dj                  t        | |                        |j                  d      xs d}t        ||||       }t        |j                  d            }||z  S )	a  Parses and returns a units scaled integer from string.

  ISO/IEC/SI rules relaxed to ignore case in unit and type names/abbreviations.

  Args:
    units: {str: int} map of unit prefix => size.
    string: The string to parse the integer + units.
    default_unit: The default unit prefix name.
    type_abbr: The optional type abbreviation suffix, validated but otherwise
      ignored.

  Raises:
    ValueError: on invalid input.

  Returns:
    The scaled integer value.
  []r   zB[{}] must the form INTEGER[UNIT]{} where units may be one of [{}].r2   r)   r?   amount)rematch_INTEGER_SUFFIX_TYPE_PATTERNVERBOSEr4   r5   r8   r$   groupr=   int)	r    stringr9   r!   rG   optional_type_abbrr)   r<   rE   s	            r   _ParseScaledIntegerrN      s    & ((/
D%	2;y3.
L	**5)<=
?@ @ ;;x &B&		E
K$u{{8$%&	$r   c                 (    t        t        | ||      S )aj  Parses and returns an ISO Decimal/Binary scaled integer from string.

  ISO/IEC prefixes: 1k == 1000, 1ki == 1024.

  Args:
    string: The string to parse the integer + units.
    default_unit: The default unit prefix name.
    type_abbr: The optional type abbreviation suffix, validated but otherwise
      ignored.

  Returns:
    The scaled integer value.
  r9   r!   )rN   r6   rL   r9   r!   s      r   ParseIntegerrR      s     
f<9
N Nr   c                     t        t        t        j                  t              d             D ](  \  }}|| k  s| |z  rdj                  | |z  ||      c S  dj                  | |      S )zReturns a pretty string representation of an ISO Decimal value.

  Args:
    value: A scaled integer value.
    type_abbr: The optional type abbreviation suffix, validated but otherwise
      ignored.

  Returns:
    The formatted scaled integer value.
  c                     | d   | d   fS r   r   r   s    r   r   zFormatInteger.<locals>.<lambda>       q588Lr   r   z{}{}{}{}{})r(   r   r   r   r6   r5   )r   r!   r)   r<   s       r   FormatIntegerrW      sf     vcmmN&C*L N Olfdu}UT\__Ud]FI>>O 
ui	((r   c                    t        t        t        j                  t              d             D ]=  \  }}|| k  s|dk(  r| |z  r| |z  }t        |d      }dj                  ||||      c S  dj                  | |      S )a  Returns a pretty string of a binary-base number with decimal precision.

  Args:
    value (float|int): A number.
    type_abbr (str): The optional type abbreviation suffix, validated but
      otherwise ignored.
    decimal_places (int): Number of decimal places to include of quotient for
      unit conversion. Does not allow rounding if -1. Will suffer float
      inaccuracy at high values.

  Returns:
    A formatted scaled value string.
  c                     | d   | d   fS r   r   r   s    r   r   z$FormatBinaryNumber.<locals>.<lambda>   rU   r   r   r   z{:.{precision}f}{}{})	precisionrV   )r(   r   r   r   r@   maxr5   )r   r!   decimal_placesr)   r<   scaled_valuer[   s          r   FormatBinaryNumberr_      s     vcmmM&B*L N Olfdu}	2	%$,T\lna(i#**
	Y + @ @O 
ui	((r   c                 (    t        t        | ||      S )a  Parses and returns a Binary scaled integer from string.

  All ISO/IEC prefixes are powers of 2: 1k == 1ki == 1024. This is a
  concession to the inconsistent mix of binary/decimal unit measures for
  memory capacity, disk capacity, cpu speed. Ideally ParseInteger should be
  used.

  Args:
    string: The string to parse the integer + units.
    default_unit: The default unit prefix name.
    type_abbr: The optional type abbreviation suffix, validated but otherwise
      ignored.

  Returns:
    The scaled integer value.
  rP   )rN   r@   rQ   s      r   ParseBinaryIntegerra      s    " 
V,)
M Mr   )B)rb   r   N)rb   r   )r   rb   )rb   rZ   )__doc__
__future__r   r   r   rF   r   rH   r6   r@   r$   r-   r=   rA   rN   rR   rW   r_   ra   r   r   r   <module>re      s     A &  ' 	 
   	
					




 	
					




<*Z*(BN$)$)8Mr   