o
    'Th>                     @   sF   d dl mZmZ ddddddddd	d
d
Zdd ZddefddZdS )    )DecimalInvalidOperation                  )
numKkMGTPEc                 C   sH  t | tttfrt| S t| } | }d}t| dkr3| d dkr3| d tv r2| dd }| dd }nt| dkrK| d tv rK| dd }| dd }zt|}W n ty_   td	|w |du rf|S |
drnd}nt|dkrwd	}ntd
	| |dkrtd
	| |d tvrtd
	| tt|d  }|||  S )a  
    Parse kubernetes canonical form quantity like 200Mi to a decimal number.
    Supported SI suffixes:
    base1024: Ki | Mi | Gi | Ti | Pi | Ei
    base1000: n | u | m | "" | k | M | G | T | P | E

    See https://github.com/kubernetes/apimachinery/blob/master/pkg/api/resource/quantity.go

    Input:
    quantity: string. kubernetes canonical form quantity

    Returns:
    Decimal

    Raises:
    ValueError on invalid or unknown input
    Nr   r   ir   r   zInvalid number format: {}     z{} has unknown suffixkir   )
isinstanceintfloatr   strlen
_EXPONENTSr   
ValueErrorformatendswith)quantitynumbersuffixbaseexponent r)   Z/home/air/segue/gemini/back/venv/lib/python3.10/site-packages/kubernetes/utils/quantity.pyparse_quantity   s>   
r+   Nreturnc                 C   s   |st | S |drd}nt|dkrd}nt|  d|dkr)t|  d|d tvr6t|  d| t|t|d    }|rI||}t || S )a@  
    Takes a decimal and produces a string value in kubernetes' canonical quantity form,
    like "200Mi".Users can specify an additional decimal number to quantize the output.

    Example -  Relatively increase pod memory limits:

    # retrieve my_pod
    current_memory: Decimal = parse_quantity(my_pod.spec.containers[0].resources.limits.memory)
    desired_memory = current_memory * 1.2
    desired_memory_str = format_quantity(desired_memory, suffix="Gi", quantize=Decimal(1))
    # patch pod with desired_memory_str

    'quantize=Decimal(1)' ensures that the result does not contain any fractional digits.

    Supported SI suffixes:
    base1024: Ki | Mi | Gi | Ti | Pi | Ei
    base1000: n | u | m | "" | k | M | G | T | P | E

    See https://github.com/kubernetes/apimachinery/blob/master/pkg/api/resource/quantity.go

    Input:
    quantity: Decimal.  Quantity as a number which is supposed to converted to a string
                        with SI suffix.
    suffix: string.     The desired suffix/unit-of-measure of the output string
    quantize: Decimal.  Can be used to round/quantize the value before the string
                        is returned. Defaults to None.

    Returns:
    string. Canonical Kubernetes quantity string containing the SI suffix.

    Raises:
    ValueError if the SI suffix is not supported.
    r   r   r   r   z has unknown suffixr   r   )r   r#   r   r!   r    r   quantize)quantity_valuer&   r-   r'   different_scaler)   r)   r*   format_quantityX   s   #

r0   )N)decimalr   r   r    r+   r   r0   r)   r)   r)   r*   <module>   s   :