
                             S r SSKJr  SSKJr  SSKJr  SSKrSSKrSrSSS	S
SSSSSSSS.rSSSSSSSSSSSS.r	S r
SS jrSS jrSS jrSS jrSS jrSS jrS S jrSS jrg)!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                     [        [        R                  " U 5      S S9nU VVs/ s H  u  p4U(       d  M  X1-   PM     snn$ s  snnf )z:Returns a list of the units in scales sorted by magnitude.c                     U S   U S   4$ Nr   r    values    .lib/googlecloudsdk/core/util/scaled_integer.py<lambda>#_UnitsByMagnitude.<locals>.<lambda>B   s    %(E!H)=    key)sortedsix	iteritems)units	type_abbrscale_itemsr   _s        r   _UnitsByMagnituder%   ?   s:    s}}U+=?+(3	;fcs/#/	;;	;s
   AAc                     U (       d  U $ U R                  5       n[        U5      n[        UR                  5       5       H  nU(       d    OX#S-
     U:X  d  M  US-  nM      U SU $ )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                    [        X5      nU(       d+  UnU(       d  SnOUR                  S5      (       a  SUSS -   nOeUS   R                  5       nUS:X  a  Sn[        U5      S:  a  US   S;   a  US-  nUS	S nOUSS nU(       a  [	        S
R                  X@U5      5      eU=(       d    [        R                  U5      nU(       d4  [	        SR                  XPSR                  [        US5      5      5      5      eU$ )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vy9; ; 
!>	&	&t	,$	
IfT XX&7r&BCEF F 
+r   c                      [        XU[        S9$ )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"   r:   r!   )r>   _BINARY_UNITS)r*   r"   r:   s      r   GetBinaryUnitSizerB      s      
V|(
* *r   c                 p   [         R                  " [        U[         R                  5      nU(       dD  U(       a  SU-   S-   OSn[	        SR                  XSR                  [        X5      5      5      5      eUR                  S5      =(       d    Sn[        XcX S9n[        UR                  S5      5      nX-  $ )	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 [{}].r3   r*   r@   amount)rematch_INTEGER_SUFFIX_TYPE_PATTERNVERBOSEr5   r6   r9   r%   groupr>   int)	r!   stringr:   r"   rH   optional_type_abbrr*   r=   rF   s	            r   _ParseScaledIntegerrO      s    & ((/
D%	2;y3.
L	*5<=
?@ @ ;;x &B&	
K$u{{8$%&	r   c                      [        [        XUS9$ )aX  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.
r:   r"   )rO   r7   rM   r:   r"   s      r   ParseIntegerrS      s     
f9
N Nr   c                     [        [        [        R                  " [        5      S S95       H+  u  p#X0::  d  M  X-  (       a  M  SR                  X-  X!5      s  $    SR                  X5      $ )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                     U S   U S   4$ r   r   r   s    r   r   FormatInteger.<locals>.<lambda>       q588Lr   r   z{}{}{}{}{})r)   r   r   r    r7   r6   )r   r"   r*   r=   s       r   FormatIntegerrY      s\     vcmmN&C*L N Olf}U\\__U]F>>O 
u	((r   c                     [        [        [        R                  " [        5      S S95       H=  u  p4X@::  d  M  US:X  a  X-  (       a  M  X-  n[        US5      nSR                  XSXS9s  $    SR                  X5      $ )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                     U S   U S   4$ r   r   r   s    r   r   $FormatBinaryNumber.<locals>.<lambda>   rW   r   r   r   z{:.{precision}f}{}{})	precisionrX   )r)   r   r   r    rA   maxr6   )r   r"   decimal_placesr*   r=   scaled_valuer^   s          r   FormatBinaryNumberrb      s     vcmmM&B*L N Olf}	2	%,\lna(i#**
	 + @ @O 
u	((r   c                      [        [        XUS9$ )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.
rQ   )rO   rA   rR   s      r   ParseBinaryIntegerrd      s    " 
V)
M Mr   )B)re   r   N)re   r   )r   re   )re   r]   )__doc__
__future__r   r   r   rG   r   rI   r7   rA   r%   r.   r>   rB   rO   rS   rY   rb   rd   r   r   r   <module>rh      s     A &  ' 	 
   	
					




 	
					




<*Z*(BN$)$)8Mr   