o
    i                     @   s  d dl Z d dlZd dlZd dlZd dlmZ d dlmZmZm	Z	m
Z
mZmZ ejdkr1d dlmZ zd dlZW n eyK   ejd ed Y nw ddlmZmZmZ dd	lmZ d
ee fddZe ejdde ejddddejddde g dddejdddej!ddej"edej#dej$deded ed
df
d!d"Z%ed#ej&d
e
ee  fd$d%Z'e%j(d&d'ej#ejd(d)d*e g d+d,ddej$d)ed
dfd-d.Z)e%j(d/d'ej#ej*d0dd1ej*d2dd1dej$d0ed2ed
dfd3d4Z+e%( ej#ej*d0dd1dej$d0ed
dfd5d6Z,e%( ej#ej*d0dd1dej$d0ed
dfd7d8Z-e%j(dddd9d:ej#ejd;dd<d=ej*d>d?ej.d@dej$dAe/d>e0edBf d
dfdCdDZ1dEee dFe	eef d
dfdGdHZ2dS )I    N)contextmanager)IOAnyDictIteratorListOptionalwin32)PopenzkIt seems python-dotenv is not installed with cli option. 
Run pip install "python-dotenv[cli]" to fix this.   )dotenv_valuesset_key	unset_key)__version__returnc                  C   s4   zt  } W n
 ty   Y dS w t j| d}|S )zZ
    Return a path for the ${pwd}/.env file.

    If pwd does not exist, return None.
    Nz.env)osgetcwdFileNotFoundErrorpathjoin)cwdr    r   D/home/kim/smarthome/.venv/lib/python3.10/site-packages/dotenv/cli.pyenumerate_env   s   r   z-fz--fileT)Z	file_okayzNLocation of the .env file, defaults to .env file in current working directory.)defaulttypehelpz-qz--quotealways)r   neverautozbWhether to quote or not the variable values. Default mode is always. This does not affect parsing.z-ez--exportFz;Whether to write the dot file as an executable bash script.)versionctxfilequoteexportc                 C   s   |||d| _ dS )zAThis script is used to set, get or unset values from a .env file.)QUOTEEXPORTFILEN)obj)r!   r"   r#   r$   r   r   r   cli&   s   r)   r   c              
   c   s    zt | }|V  W d   W dS 1 sw   Y  W dS  ty? } ztd| tjd td W Y d}~dS d}~ww )z~
    Open a file and yield the corresponding (decoded) stream.

    Exits with error code 2 if the file cannot be opened.
    NzError opening env file: )r"      )openOSErrorprintsysstderrexit)r   streamexcr   r   r   stream_fileC   s   
&r3   list)namez--formatoutput_formatsimple)r7   jsonshellr$   zlThe format in which to display the list. Default format is simple, which displays name=value without quotes.c                 C   s   | j d }t|}t|d}W d   n1 sw   Y  |dkr/ttj|ddd dS |dkr5d	nd
}t|D ]}|| }|durZ|dv rNt	|}t| | d|  q;dS )z!Display all the stored key/value.r'   r1   Nr8   r*   T)indent	sort_keysr$   zexport  )r$   r9   =)
r(   r3   r   clickechor8   dumpssortedshlexr#   )r!   r6   r"   r1   valuesprefixkvr   r   r   list_valuesS   s   


rH   setkey)requiredvaluec                 C   s^   | j d }| j d }| j d }t|||||\}}}|r(t| d|  dS td dS )z
    Store the given key/value.

    This doesn't follow symlinks, to avoid accidentally modifying a file at a
    potentially untrusted path.
    r'   r%   r&   r>   r   N)r(   r   r?   r@   r.   r0   )r!   rJ   rL   r"   r#   r$   successr   r   r   	set_valuep   s   


rN   c                 C   sf   | j d }t|}t|d}W d   n1 sw   Y  ||}|r,t| dS td dS )z%Retrieve the value for the given key.r'   r:   Nr   )r(   r3   r   getr?   r@   r.   r0   )r!   rJ   r"   r1   rD   Zstored_valuer   r   r   rO      s   


rO   c                 C   sJ   | j d }| j d }t|||\}}|rtd|  dS td dS )z
    Removes the given key.

    This doesn't follow symlinks, to avoid accidentally modifying a file at a
    potentially untrusted path.
    r'   r%   zSuccessfully removed r   N)r(   r   r?   r@   r.   r0   )r!   rJ   r"   r#   rM   r   r   r   unset   s   


rP   )Zallow_extra_argsallow_interspersed_argsZignore_unknown_options)Zcontext_settingsz--override/--no-overridezKOverride variables from the environment file with those from the .env file.)r   r   commandline)nargsr   override.c                    sv   | j d }tj|stjd| d| d fddt| D }|s/td t	
d tg || j| d	S )
z/Run command with environment variables present.r'   zInvalid value for '-f' "z" does not exist.)r!   c                    s,   i | ]\}}|d ur s|t jvr||qS )N)r   environ).0rF   rG   rU   r   r   
<dictcomp>   s
    zrun.<locals>.<dictcomp>zNo command given.r   N)r(   r   r   isfiler?   ZBadParameterr   itemsr@   r.   r0   run_commandargs)r!   rU   rR   r"   Zdotenv_as_dictr   rX   r   run   s   




r^   commandenvc                 C   sd   t j }|| tjdkr&t| ddd|d}| \}}t|j	 dS t j
| d | |d dS )a  Replace the current process with the specified command.

    Replaces the current process with the specified command and the variables from `env`
    added in the current environment variables.

    Parameters
    ----------
    command: List[str]
        The command and it's parameters
    env: Dict
        The additional environment variables

    Returns
    -------
    None
        This function does not return any value. It replaces the current process with the new one.

    r	   Tr   F)universal_newlinesbufsizer9   r`   )r]   r`   N)r   rV   copyupdater.   platformr
   communicater0   
returncodeexecvpe)r_   r`   Zcmd_envp_r   r   r   r\      s   


r\   )3r8   r   rC   r.   
contextlibr   typingr   r   r   r   r   r   re   
subprocessr
   r?   ImportErrorr/   writer0   mainr   r   r   r    r   strr   groupoptionPathZChoiceZBOOLZversion_optionZpass_contextContextr)   PathLiker3   r_   rH   argumentrN   rO   rP   ZUNPROCESSEDbooltupler^   r\   r   r   r   r   <module>   s     


,

$,&