a
    §¾de±  ã                   @   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 d dlm	Z	m
Z
mZmZmZ zd dlZW n( eyˆ   ej d¡ e d¡ Y n0 ddlmZmZmZ ddlmZ 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 ej!e	e	e	ddœdd„ƒƒƒƒƒƒZ"eej#eee$  dœdd „ƒZ%e" &¡ ej ejd!d"e g d#¢¡d$d�ej!e'dd%œd&d'„ƒƒƒZ(e" &¡ ej ej)d(dd)�ej)d*dd)�ej!e	e	dd+œd,d-„ƒƒƒƒZ*e" &¡ ej ej)d(dd)�ej!e	dd.œd/d0„ƒƒƒZ+e" &¡ ej ej)d(dd)�ej!e	dd.œd1d2„ƒƒƒZ,e"j&d3did4�ej ejd5dd6d7�ej)d8d9ej-d:�ej!e'ee$ dd;œd<d=„ƒƒƒƒZ.ee$ e
e$e$f e/d>œd?d@„Z0dS )Aé    N)Úcontextmanager)ÚPopen)ÚAnyÚDictÚIOÚIteratorÚListzkIt 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__c                  C   s4   zt  ¡ } W n ty    Y dS 0 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   úC/var/www/sistema_ama/venv/lib/python3.9/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ÚexportÚreturnc                 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   r"   c              
   c   sx   z2t | ƒ�}|V  W d  ƒ n1 s&0    Y  W n@ tyr } z(td|› �tjd� tdƒ W Y d}~n
d}~0 0 dS )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_file3   s    
(r1   z--formatÚsimple)r2   ÚjsonÚshellr!   zlThe format in which to display the list. Default format is simple, which displays name=value without quotes.)r   Úformatr"   c                 C   s¸   | j d }t|ƒ�}t|d�}W d  ƒ n1 s20    Y  |dkr\t tj|ddd�¡ nX|dkrhd	nd
}t|ƒD ]>}|| }|durt|dv ršt 	|¡}t |› |› d|› �¡ qtdS )z!Display all the stored key/value.r%   ©r/   Nr3   r(   T)ÚindentÚ	sort_keysr!   zexport Ú )r!   r4   ú=)
r&   r1   r
   ÚclickÚechor3   ÚdumpsÚsortedÚshlexr    )r   r5   r   r/   ÚvaluesÚprefixÚkÚvr   r   r   ÚlistC   s    

(
rD   Úkey)ÚrequiredÚvalue)r   rE   rG   r"   c                 C   sZ   | j d }| j d }| j d }t|||||ƒ\}}}|rNt |› d|› �¡ ntdƒ dS )zStore the given key/value.r%   r#   r$   r:   r	   N)r&   r   r;   r<   r.   )r   rE   rG   r   r    r!   Úsuccessr   r   r   Úset\   s    


rI   )r   rE   r"   c                 C   sb   | j d }t|ƒ�}t|d�}W d  ƒ n1 s20    Y  | |¡}|rVt |¡ ntdƒ dS )z%Retrieve the value for the given key.r%   r6   Nr	   )r&   r1   r
   Úgetr;   r<   r.   )r   rE   r   r/   r@   Zstored_valuer   r   r   rJ   l   s    

(
rJ   c                 C   sF   | j d }| j d }t|||ƒ\}}|r:t d|› �¡ ntdƒ dS )zRemoves the given key.r%   r#   zSuccessfully removed r	   N)r&   r   r;   r<   r.   )r   rE   r   r    rH   r   r   r   Úunset}   s    

rK   Zignore_unknown_options)Zcontext_settingsz--override/--no-overridezKOverride variables from the environment file with those from the .env file.)r   r   Úcommandlineéÿÿÿÿ)Únargsr   )r   ÚoverriderL   r"   c                    sr   | j d }tj |¡s,tjd|› d�| d�‚‡ fdd„t|ƒ ¡ D ƒ}|s\t d¡ t	dƒ t
||ƒ}t	|ƒ 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)Ú.0rB   rC   ©rO   r   r   Ú
<dictcomp>›   s   þzrun.<locals>.<dictcomp>zNo command given.r	   N)r&   r   r   Úisfiler;   ZBadParameterr
   Úitemsr<   r.   Úrun_command)r   rO   rL   r   Zdotenv_as_dictÚretr   rR   r   Úrun‹   s    


þ

þ

rX   )ÚcommandÚenvr"   c                 C   s8   t j ¡ }| |¡ t| ddd|d�}| ¡ \}}|jS )aw  Run command in sub process.

    Runs the command in a sub process with 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
    -------
    int
        The return code of the command

    Tr   F)Úuniversal_newlinesÚbufsizer4   rZ   )r   rP   ÚcopyÚupdater   ÚcommunicateÚ
returncode)rY   rZ   Zcmd_envÚpÚ_r   r   r   rV   ¨   s    

ürV   )1r3   r   r?   r,   Ú
contextlibr   Ú
subprocessr   Útypingr   r   r   r   r   r;   ÚImportErrorr-   Úwriter.   Úmainr
   r   r   r   r   r   ÚgroupÚoptionÚPathZChoiceZBOOLZversion_optionZpass_contextÚContextr'   ÚPathLikeÚstrr1   rY   ÚboolrD   ÚargumentrI   rJ   rK   ZUNPROCESSEDrX   ÚintrV   r   r   r   r   Ú<module>   s|   
þ
þ
þ
$þý"