o
    wi                     @   s   d dl Z d dlZd dlZd dlmZ g dZG dd deZG dd deZddd	 d
fd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dd Zdd  Zd!d" Zd#d$ Zd%d& Zd'd( Zd)d* Zd+d, ZdS )-    N)parser)get_envget_envbool
get_envintget_envfloatget_envdecimalget_envdateget_envdatetimeget_envlistget_envdictCoercionErrorRequiredSettingMissingErrorc                           e Zd ZdZ fddZ  ZS )r   z3Custom error raised when a value cannot be coerced.c                    s$   d |||j}tt| | d S )Nz"Unable to coerce '{}={}' using {}.)format__name__superr   __init__)selfkeyvaluefuncmsg	__class__ W/home/ubuntu/sommelier/.venv/lib/python3.10/site-packages/nemo/utils/env_var_parsing.pyr   B   s   zCoercionError.__init__r   
__module____qualname____doc__r   __classcell__r   r   r   r   r   ?       r   c                       r   )r   z7Custom error raised when a required env var is missing.c                    s   d |}tt| | d S )Nz!Required env var '{}' is missing.)r   r   r   r   )r   r   r   r   r   r   r   J   s   
z$RequiredSettingMissingError.__init__r   r   r   r   r   r   G   r!   r   c                 C      | S Nr   xr   r   r   <lambda>O       r&   Fc                 C   s\   zt j|  }W n ty   |du rt| | Y S w z||W S  ty-   t| ||w )a  
    Return env var coerced into a type other than string.
    This function extends the standard os.getenv function to enable
    the coercion of values into data types other than string (all env
    vars are strings by default).
    Args:
        key: string, the name of the env var to look up
    Kwargs:
        default: the default value to return if the env var does not exist. NB the
            default value is **not** coerced, and is assumed to be of the correct type.
        coerce: a function that is used to coerce the value returned into
            another type
        required: bool, if True, then a RequiredSettingMissingError error is raised
            if the env var does not exist.
    Returns the env var, passed through the coerce function
    T)osenvironKeyErrorr   	Exceptionr   )r   defaultcoercerequiredr   r   r   r   _get_envO   s   
r/   c                 C   s$   t | tr| S | d u p|  dv  S )N)false0nonfnone)
isinstanceboollowerr   r   r   r   _boolo   s   
r:   c                 C      t | S r#   )intr9   r   r   r   _intv      r=   c                 C   r;   r#   )floatr9   r   r   r   _floatz   r>   r@   c                 C   
   t | S r#   )decimalDecimalr9   r   r   r   _decimal~      
rD   c                 C   rA   r#   )jsonloadsr9   r   r   r   _dict   rE   rH   c                 C   rA   r#   )r   parser9   r   r   r   	_datetime   rE   rJ   c                 C   s   t |  S r#   )r   rI   dater9   r   r   r   _date   s   rL   c                 O   sP   t |dv s
J d|ddd }t |dk}|s|d nd}t| |||dS )	a  
    Return env var.
    This is the parent function of all other get_foo functions,
    and is responsible for unpacking args/kwargs into the values
    that _get_env expects (it is the root function that actually
    interacts with environ).
    Args:
        key: string, the env var name to look up.
        default: (optional) the value to use if the env var does not
            exist. If this value is not supplied, then the env var is
            considered to be required, and a RequiredSettingMissingError
            error will be raised if it does not exist.
    Kwargs:
        coerce: a func that may be supplied to coerce the value into
            something else. This is used by the default get_foo functions
            to cast strings to builtin types, but could be a function that
            returns a custom class.
    Returns the env var, coerced if required, and a default if supplied.
    )r      zToo many args supplied.r-   c                 S   r"   r#   r   r$   r   r   r   r&      r'   zget_env.<locals>.<lambda>r   N)r,   r-   r.   )lengetr/   )r   r,   kwargsr   r.   r   r   r   r      s
   r   c                 G      t | g|R dtiS )zReturn env var cast as boolean.r-   )r   r:   r   r,   r   r   r   r         r   c                 G   rQ   )zReturn env var cast as integer.r-   )r   r=   rR   r   r   r   r      rS   r   c                 G   rQ   )zReturn env var cast as float.r-   )r   r@   rR   r   r   r   r      rS   r   c                 G   rQ   )zReturn env var cast as Decimal.r-   )r   rD   rR   r   r   r   r      rS   r   c                 G   rQ   )zReturn env var as a date.r-   )r   rL   rR   r   r   r   r      rS   r   c                 G   rQ   )zReturn env var as a datetime.r-   )r   rJ   rR   r   r   r   r	      rS   r	   c                    s*   | dd t| g|R d fddiS )zReturn env var as a list.	separator r-   c                    s
   |   S r#   )splitr$   rT   r   r   r&      s   
 zget_envlist.<locals>.<lambda>)rO   r   )r   r,   rP   r   rW   r   r
      s   r
   c                 G   rQ   )zReturn env var as a dict.r-   )r   rH   rR   r   r   r   r      rS   r   )rB   rF   r(   dateutilr   __all__r+   r   r   r/   r:   r=   r@   rD   rH   rJ   rL   r   r   r   r   r   r   r	   r
   r   r   r   r   r   <module>   s0   ) 