a
    Ò�éiP4  ã                   @   sV  d dl Z d dlZd dlZd dlZ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mZmZmZ ddlmZmZ ddlmZ eedf Ze e¡Zed	œd
d„Z ee ee dœdd„Z!G dd„ dƒZ"d3eeee ee dœdd„Z#eeee eeee ee f  dœdd„ƒZ$d4eeeeeee eee eef dœdd„Z%d5eeeee eee ef dœdd„Z&eeeee f  eeeee f d œd!d"„Z'eee d#œd$d%„Z(d6eeeed'œd(d)„Z)d7ee eee  eeeee ed+œd,d-„Z*d8ee eee  eeee eeee f d.œd/d0„Z+eed#œd1d2„Z,dS )9é    N)ÚOrderedDict)Úcontextmanager)ÚIOÚDictÚIterableÚIteratorÚMappingÚOptionalÚTupleÚUnioné   )ÚBindingÚparse_stream)Úparse_variableszos.PathLike[str]©Úreturnc                  C   s$   dt jvrdS t jd  ¡ } | dv S )z8
    Determine if dotenv loading has been disabled.
    ZPYTHON_DOTENV_DISABLEDF>   ÚtrueÚtÚyesÚ1Úy)ÚosÚenvironÚcasefold)Úvalue© r   ú;/home/Claro/venv/lib/python3.9/site-packages/dotenv/main.pyÚ_load_dotenv_disabled   s    
r   )Úmappingsr   c                 c   s*   | D ] }|j rt d|jj¡ |V  qd S )Nz;python-dotenv could not parse statement starting at line %s)ÚerrorÚloggerÚwarningÚoriginalÚline)r   Úmappingr   r   r   Úwith_warn_for_invalid_lines#   s    þr%   c                	   @   s¬   e Zd Zdee eee  eee eeddœdd„Ze	e
ee  dœdd	„ƒZeeee f dœd
d„Ze
eeee f  dœdd„Zedœdd„Zeee dœdd„ZdS )ÚDotEnvNFT)Údotenv_pathÚstreamÚverboseÚencodingÚinterpolateÚoverrider   c                 C   s.   || _ || _d | _|| _|| _|| _|| _d S ©N)r'   r(   Ú_dictr)   r*   r+   r,   )Úselfr'   r(   r)   r*   r+   r,   r   r   r   Ú__init__.   s    	zDotEnv.__init__r   c                 c   s„   | j rHt| j ƒrHt| j | jd��}|V  W d   ƒ q€1 s<0    Y  n8| jd ur\| jV  n$| jrtt d| j ppd¡ t 	d¡V  d S )N©r*   z3python-dotenv could not find configuration file %s.ú.envÚ )
r'   Ú_is_file_or_fifoÚopenr*   r(   r)   r    ÚinfoÚioÚStringIO)r/   r(   r   r   r   Ú_get_stream?   s    &

þzDotEnv._get_streamc                 C   s@   | j r| j S |  ¡ }| jr0tt|| jd�ƒ| _ n
t|ƒ| _ | j S )zReturn dotenv as dict)r,   )r.   Úparser+   r   Úresolve_variablesr,   )r/   Z
raw_valuesr   r   r   ÚdictN   s    ÿ
zDotEnv.dictc                 c   sV   |   ¡ �:}tt|ƒƒD ]}|jd ur|j|jfV  qW d   ƒ n1 sH0    Y  d S r-   )r9   r%   r   Úkeyr   )r/   r(   r$   r   r   r   r:   ^   s    

zDotEnv.parsec                 C   sJ   |   ¡ sdS |   ¡  ¡ D ],\}}|tjv r2| js2q|dur|tj|< qdS )zI
        Load the current dotenv as system environment variable.
        FNT)r<   Úitemsr   r   r,   )r/   ÚkÚvr   r   r   Úset_as_environment_variablesd   s    z#DotEnv.set_as_environment_variables)r=   r   c                 C   s2   |   ¡ }||v r|| S | jr.t d|| j¡ dS )ú zKey %s not found in %s.N)r<   r)   r    r!   r'   )r/   r=   Údatar   r   r   Úgets   s    z
DotEnv.get)NFNTT)Ú__name__Ú
__module__Ú__qualname__r	   ÚStrPathr   ÚstrÚboolr0   r   r   r9   r   r<   r
   r:   rA   rD   r   r   r   r   r&   -   s(        ù
ør&   úutf-8)r'   Ú
key_to_getr*   r   c                 C   s   t | d|d� |¡S )z
    Get the value of a given key from the given .env.

    Returns `None` if the key isn't found or doesn't have a value.
    T)r)   r*   )r&   rD   )r'   rL   r*   r   r   r   Úget_key€   s    
rM   )Úpathr*   r   c                 c   sÔ   t  | ¡ ¡  tjd|dd��x}d }z:t| |d��}||fV  W d   ƒ n1 sR0    Y  W n( ty† } z|}W Y d }~n
d }~0 0 W d   ƒ n1 sœ0    Y  |d u r¾t |j	| ¡ nt
 |j	¡ |d ‚d S )NÚwF)Úmoder*   Údeleter1   )ÚpathlibÚPathÚtouchÚtempfileÚNamedTemporaryFiler5   ÚBaseExceptionÚshutilÚmoveÚnamer   Úunlink)rN   r*   Údestr   ÚsourceÚerrr   r   r   Úrewrite�   s    ,8r_   ÚalwaysF)r'   Ú
key_to_setÚvalue_to_setÚ
quote_modeÚexportr*   r   c                 C   s  |dvrt d|› �ƒ‚|dkp.|dko.| ¡  }|rHd | dd¡¡}n|}|rdd|› d	|› d
�}n|› d	|› d
�}t| |d��‚\}	}
d}d}tt|	ƒƒD ]<}|j|kr¸|
 |¡ d}qš|
 |j	j
¡ |j	j
 d
¡ }qš|sô|rê|
 d
¡ |
 |¡ W d  ƒ n1 �s
0    Y  d||fS )z³
    Adds or Updates a key/value to the given .env

    If the .env path given doesn't exist, fails instead of risking creating
    an orphan .env somewhere in the filesystem
    )r`   ÚautoÚneverzUnknown quote_mode: r`   re   z'{}'ú'z\'zexport ú=Ú
r1   FTN)Ú
ValueErrorÚisalnumÚformatÚreplacer_   r%   r   r=   Úwriter"   ÚstringÚendswith)r'   ra   rb   rc   rd   r*   ÚquoteZ	value_outZline_outr]   r\   ZreplacedZmissing_newliner$   r   r   r   Úset_key£   s2    ÿ


*rr   )r'   Úkey_to_unsetrc   r*   r   c                 C   s¦   t j | ¡s t d| ¡ d|fS d}t| |d��D\}}tt|ƒƒD ]"}|j|krVd}qB| 	|j
j¡ qBW d  ƒ n1 sz0    Y  |sžt d|| ¡ d|fS ||fS )z¢
    Removes a given key from the given `.env` file.

    If the .env path given doesn't exist, fails.
    If the given key doesn't exist in the .env, fails.
    z(Can't delete from %s - it doesn't exist.NFr1   Tz/Key %s not removed from %s - key doesn't exist.)r   rN   Úexistsr    r!   r_   r%   r   r=   rn   r"   ro   )r'   rs   rc   r*   Úremovedr]   r\   r$   r   r   r   Ú	unset_keyÓ   s    
.ÿrv   )Úvaluesr,   r   c                    s‚   i }| D ]t\}}|d u rd }nVt |ƒ}i ‰ |rFˆ  tj¡ ˆ  |¡ nˆ  |¡ ˆ  tj¡ d ‡ fdd„|D ƒ¡}|||< q|S )Nr3   c                 3   s   | ]}|  ˆ ¡V  qd S r-   )Úresolve)Ú.0Zatom©Úenvr   r   Ú	<genexpr>  ó    z$resolve_variables.<locals>.<genexpr>)r   Úupdater   r   Újoin)rw   r,   Ú
new_valuesrZ   r   ÚresultZatomsr   rz   r   r;   ô   s    

r;   )rN   r   c                 c   st   t j | ¡stdƒ‚t j | ¡r,t j | ¡} d}t j | ¡}||krp|V  t j t j |t jj¡¡}|| }}q<dS )zL
    Yield directories starting from the given directory up to the root
    zStarting path not foundN)	r   rN   rt   ÚIOErrorÚisfileÚdirnameÚabspathr   Úpardir)rN   Zlast_dirZcurrent_dirÚ
parent_dirr   r   r   Ú_walk_to_root  s    rˆ   r2   )ÚfilenameÚraise_error_if_not_foundÚusecwdr   c                 C   sÌ   dd„ }dd„ }|s,|ƒ s,|ƒ s,t tddƒr6t ¡ }nZt ¡ }t}|jj|ks^tj 	|jj¡st|j
duslJ ‚|j
}qB|jj}tj tj |¡¡}t|ƒD ]"}	tj |	| ¡}
t|
ƒr˜|
  S q˜|rÈtdƒ‚d	S )
z‡
    Search in increasingly higher folders for the given file

    Returns path to the file if found, or an empty string otherwise
    c                  S   sP   t tdƒst tdƒrdS ztddddgd�} W n tyB   Y dS 0 t | dƒ S )	z<Decide whether this is running in a REPL or IPython notebookZps1Zps2TÚ__main__NÚ__file__)ÚfromlistF)ÚhasattrÚsysÚ
__import__ÚModuleNotFoundError)Úmainr   r   r   Ú_is_interactive*  s    z$find_dotenv.<locals>._is_interactivec                   S   s   t  ¡ d uS r-   )r�   Úgettracer   r   r   r   Ú_is_debugger4  s    z!find_dotenv.<locals>._is_debuggerÚfrozenFNzFile not foundr3   )Úgetattrr�   r   ÚgetcwdÚ	_getframer�   Úf_codeÚco_filenamerN   rt   Úf_backr„   r…   rˆ   r   r4   r‚   )r‰   rŠ   r‹   r”   r–   rN   ÚframeZcurrent_fileZframe_filenamer„   Ú
check_pathr   r   r   Úfind_dotenv  s(    

ÿ
r    T)r'   r(   r)   r,   r+   r*   r   c                 C   sF   t ƒ rt d¡ dS | du r*|du r*tƒ } t| |||||d�}| ¡ S )a  Parse a .env file and then load all the variables found as environment variables.

    Parameters:
        dotenv_path: Absolute or relative path to .env file.
        stream: Text stream (such as `io.StringIO`) with .env content, used if
            `dotenv_path` is `None`.
        verbose: Whether to output a warning the .env file is missing.
        override: Whether to override the system environment variables with the variables
            from the `.env` file.
        encoding: Encoding to be used to read the file.
    Returns:
        Bool: True if at least one environment variable is set else False

    If both `dotenv_path` and `stream` are `None`, `find_dotenv()` is used to find the
    .env file with it's default parameters. If you need to change the default parameters
    of `find_dotenv()`, you can explicitly call `find_dotenv()` and pass the result
    to this function as `dotenv_path`.

    If the environment variable `PYTHON_DOTENV_DISABLED` is set to a truthy value,
    .env loading is disabled.
    zSpython-dotenv: .env loading disabled by PYTHON_DOTENV_DISABLED environment variableFN©r'   r(   r)   r+   r,   r*   )r   r    Údebugr    r&   rA   )r'   r(   r)   r,   r+   r*   Zdotenvr   r   r   Úload_dotenvR  s     ÿúr£   )r'   r(   r)   r+   r*   r   c                 C   s.   | du r|du rt ƒ } t| |||d|d� ¡ S )a‘  
    Parse a .env file and return its content as a dict.

    The returned dict will have `None` values for keys without values in the .env file.
    For example, `foo=bar` results in `{"foo": "bar"}` whereas `foo` alone results in
    `{"foo": None}`

    Parameters:
        dotenv_path: Absolute or relative path to the .env file.
        stream: `StringIO` object with .env content, used if `dotenv_path` is `None`.
        verbose: Whether to output a warning if the .env file is missing.
        encoding: Encoding to be used to read the file.

    If both `dotenv_path` and `stream` are `None`, `find_dotenv()` is used to find the
    .env file.
    NTr¡   )r    r&   r<   )r'   r(   r)   r+   r*   r   r   r   Údotenv_valuesƒ  s    úr¤   c              	   C   sD   t j | ¡rdS zt  | ¡}W n ttfy6   Y dS 0 t |j¡S )zN
    Return True if `path` exists and is either a regular file or a FIFO.
    TF)r   rN   rƒ   ÚstatÚFileNotFoundErrorÚOSErrorÚS_ISFIFOÚst_mode)rN   Ústr   r   r   r4   §  s    r4   )rK   )r`   FrK   )r`   rK   )r2   FF)NNFFTrK   )NNFTrK   )-r7   Úloggingr   rR   rX   r¥   r�   rU   Úcollectionsr   Ú
contextlibr   Útypingr   r   r   r   r   r	   r
   r   Úparserr   r   Ú	variablesr   rI   rH   Ú	getLoggerrE   r    rJ   r   r%   r&   rM   r_   rr   rv   r;   rˆ   r    r£   r¤   r4   r   r   r   r   Ú<module>   sº   (


V ýüý   úù3  üû"ý   ýü4      ú
ù2     û
ú$