o
    =Ge                     @   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yD   ejd ed Y nw 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 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"& ej ejd%d&eg d'd(ddej!d)e'ddfd*d+Z(e"& ej ej)d,dd-ej)d.dd-dej!d,e	d.e	ddfd/d0Z*e"& ej ej)d,dd-dej!d,e	ddfd1d2Z+e"& ej ej)d,dd-dej!d,e	ddfd3d4Z,e"j&d5did6ej ejd7dd8d9ej)d:d;ej-d<dej!d=e'd:ee$ ddfd>d?Z.d@ee$ dAe
e$e$f de/fdBdCZ0dS )D    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 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   K/var/www/bmteknikk.ddns.net/venv/lib/python3.10/site-packages/dotenv/cli.pyenumerate_env   s   r   z-fz--fileT)	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   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_file3   s   
&r2   z--formatsimple)r3   jsonshellr"   zlThe format in which to display the list. Default format is simple, which displays name=value without quotes.formatc                 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&   r0   Nr4   r)   T)indent	sort_keysr"   zexport  )r"   r5   =)
r'   r2   r
   clickechor4   dumpssortedshlexr!   )r   r6   r    r0   valuesprefixkvr   r   r   listC   s   


rE   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.r&   r$   r%   r;   r	   N)r'   r   r<   r=   r/   )r   rF   rH   r    r!   r"   successr   r   r   set\   s   


rJ   c                 C   sd   | 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&   r7   Nr	   )r'   r2   r
   getr<   r=   r/   )r   rF   r    r0   rA   stored_valuer   r   r   rK   l   s   


rK   c                 C   sH   | j d }| j d }t|||\}}|rtd|  dS td dS )zRemoves the given key.r&   r$   zSuccessfully removed r	   N)r'   r   r<   r=   r/   )r   rF   r    r!   rI   r   r   r   unset}   s   

rM   ignore_unknown_options)context_settingsz--override/--no-overridezKOverride variables from the environment file with those from the .env file.)r   r   commandline)nargsr   overridec                    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).0rC   rD   rS   r   r   
<dictcomp>   s
    zrun.<locals>.<dictcomp>zNo command given.r	   N)r'   r   r   isfiler<   BadParameterr
   itemsr=   r/   run_command)r   rS   rP   r    dotenv_as_dictretr   rV   r   run   s   






r^   commandenvc                 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bufsizer5   r`   )r   rT   copyupdater   communicate
returncode)r_   r`   cmd_envp_r   r   r   r[      s   

r[   )1r4   r   r@   r-   
contextlibr   
subprocessr   typingr   r   r   r   r   r<   ImportErrorr.   writer/   mainr
   r   r   r   r   r   groupoptionPathChoiceBOOLversion_optionpass_contextContextr(   PathLikestrr2   r_   boolrE   argumentrJ   rK   rM   UNPROCESSEDr^   intr[   r   r   r   r   <module>   s    



,$(&