B
    id                 @   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 yd dlZW n* ek
r   ejd ed Y nX ddlmZmZmZ ddlmZ e ejd	d
eje dejddddejdddedddg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d"d#d$d%g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d0d1d2Z-e$( ej"ej+d*dd+ej#e	dd0d3d4Z.e$j(d5did6ej"ejd7dd8d9ej+d:d;ej/d<ej#e)ee& dd=d>d?Z0ee& e
e&e&f e1d@dAdBZ2dS )C    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__z-fz--filez.envT)Z	file_okayzNLocation of the .env file, defaults to .env file in current working directory.)defaulttypehelpz-qz--quotealways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   ./tmp/pip-unpacked-wheel-mbm0dzz5/dotenv/cli.pycli   s    r    )pathr   c          
   c   sb   yt | }|V  W dQ R X W n> tk
r\ } z td| tjd td W dd}~X Y nX 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_file%   s    
r+   z--formatsimple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Q R X |dkrHttj|ddd n\|dkrTd	nd
}xJt|D ]>}|| }|dk	rb|dkrt	|}t| | d|  qbW dS )z!Display all the stored key/value.r   )r)   Nr-   r"   T)indent	sort_keysr   zexport  )r   r.   =)
r   r+   r
   clickechor-   dumpssortedshlexr   )r   r/   r   r)   valuesprefixkvr   r   r   list5   s    


r=   key)requiredvalue)r   r>   r@   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   r3   r	   N)r   r   r4   r5   r(   )r   r>   r@   r   r   r   successr   r   r   setN   s    


rB   )r   r>   r   c          	   C   sN   | j d }t|}t|d}W dQ R X ||}|rBt| ntd dS )z%Retrieve the value for the given key.r   )r)   Nr	   )r   r+   r
   getr4   r5   r(   )r   r>   r   r)   r9   Zstored_valuer   r   r   rC   ^   s    


rC   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   r4   r5   r(   )r   r>   r   r   rA   r   r   r   unseto   s    

rD   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   overriderE   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 k	r s"|t jkr||qS )N)osenviron).0r;   r<   )rH   r   r   
<dictcomp>   s   zrun.<locals>.<dictcomp>zNo command given.r	   N)r   rI   r!   isfiler4   ZBadParameterr
   itemsr5   r(   run_command)r   rH   rE   r   Zdotenv_as_dictretr   )rH   r   run}   s    





rQ   )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bufsizer.   rS   )rI   rJ   copyupdater   communicate
returncode)rR   rS   Zcmd_envp_r   r   r   rO      s    

rO   )3r-   rI   r8   r&   
contextlibr   
subprocessr   typingr   r   r   r   r   r4   ImportErrorr'   writer(   mainr
   r   r   r   r   groupoptionr!   joingetcwdPathZChoiceZBOOLZversion_optionZpass_contextContextr    PathLikestrr+   rR   boolr=   argumentrB   rC   rD   ZUNPROCESSEDrQ   intrO   r   r   r   r   <module>   sp   



$"