o
    +iP4                     @   s  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d	efd
dZ dee d	ee fddZ!G dd dZ"	d;dededee d	ee fddZ#ededee d	eeee ee f  fddZ$			d<dedededededee d	eee eef fd d!Z%		d=ded"ededee d	eee ef f
d#d$Z&d%eeeee f  d&ed	eeee f fd'd(Z'ded	ee fd)d*Z(	+		d>d,ed-ed.ed	efd/d0Z)					1	d?dee d2eee  d3ed&ed4edee d	efd5d6Z*				1	d@dee d2eee  d3ed4edee d	eeee f fd7d8Z+ded	efd9d:Z,dS )A    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.
    PYTHON_DOTENV_DISABLEDF>   1tyyestrue)osenvironcasefold)value r   >/var/www/html/venv/lib/python3.10/site-packages/dotenv/main.py_load_dotenv_disabled   s   
r   mappingsc                 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dee deee  dedee ded	ed
dfddZe	d
e
ee  fddZd
eeee f fddZd
e
eeee f  fddZd
efddZded
ee f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__c                 c   s    | j r't| j r't| j | jd}|V  W d    d S 1 s w   Y  d S | jd ur2| jV  d S | jr>td| j p<d 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   sD   | j r| j S |  }| jrtt|| jd| _ | j S t|| _ | j S )zReturn dotenv as dict)r,   )r.   parser+   r   resolve_variablesr,   )r/   
raw_valuesr   r   r   dictN   s   
zDotEnv.dictc                 c   sZ    |   }tt|D ]}|jd ur|j|jfV  qW d    d S 1 s&w   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 r| jsq|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_variablesr>   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:   rB   rE   r   r   r   r   r&   -   s6    

r&   utf-8r'   
key_to_getr*   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&   rE   )r'   rM   r*   r   r   r   get_key   s   
rN   pathc                 c   s    t |   tjd|dd<}d }zt| |d}||fV  W d    n1 s*w   Y  W n tyD } z|}W Y d }~nd }~ww W d    n1 sOw   Y  |d u rat|j	|  d S t
|j	 |d )NwF)moder*   deleter1   )pathlibPathtouchtempfileNamedTemporaryFiler5   BaseExceptionshutilmovenamer   unlink)rO   r*   destr   sourceerrr   r   r   rewrite   s&   r`   alwaysF
key_to_setvalue_to_set
quote_modeexportc                 C   s  |dvrt d| |dkp|dko|  }|r$d|dd}n|}|r2d| d	| d
}n| d	| d
}t| |dA\}	}
d}d}tt|	D ]}|j|kr\|
| d}qM|
|j	j
 |j	j
d
 }qM|sz|ru|
d
 |
| W d   n1 sw   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
    )ra   autoneverzUnknown quote_mode: ra   rf   z'{}''z\'zexport =
r1   FTN)
ValueErrorisalnumformatreplacer`   r%   r   r>   writer"   stringendswith)r'   rb   rc   rd   re   r*   quote	value_outline_outr^   r]   replacedmissing_newliner$   r   r   r   set_key   s6   




rw   key_to_unsetc                 C   s   t j| std|  d|fS d}t| |d"\}}tt|D ]}|j|kr+d}q!|	|j
j q!W d   n1 s=w   Y  |sOt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   rO   existsr    r!   r`   r%   r   r>   ro   r"   rp   )r'   rx   rd   r*   removedr^   r]   r$   r   r   r   	unset_key   s"   
r{   valuesr,   c                    s   i }| D ]:\}}|d u rd }n+t |}i  |r# tj  | n |  tj d fdd|D }|||< q|S )Nr3   c                 3   s    | ]}|  V  qd S r-   )resolve).0atomenvr   r   	<genexpr>  s    z$resolve_variables.<locals>.<genexpr>)r   updater   r   join)r|   r,   
new_valuesr[   r   resultatomsr   r   r   r;      s   

r;   c                 c   s    t j| stdt j| rt j| } d}t j| }||kr>|V  t jt j|t jj}||}}||ks#dS dS )zL
    Yield directories starting from the given directory up to the root
    zStarting path not foundN)	r   rO   ry   IOErrorisfiledirnameabspathr   pardir)rO   last_dircurrent_dir
parent_dirr   r   r   _walk_to_root  s   
r   r2   filenameraise_error_if_not_foundusecwdc                 C   s   dd }dd }|s| s| st tddrt }n:t }t}|jj|ks/tj	|jjsG|j
dus6J |j
}|jj|ks/tj	|jjr/|jj}tjtj|}t|D ]}	tj|	| }
t|
rj|
  S qY|rqt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y!   Y dS w t | d S )	z<Decide whether this is running in a REPL or IPython notebookps1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_filenamerO   ry   f_backr   r   r   r   r4   r   )r   r   r   r   r   rO   framecurrent_fileframe_filenamer   
check_pathr   r   r   find_dotenv  s0   

r   Tr(   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&   rB   )r'   r(   r)   r,   r+   r*   dotenvr   r   r   load_dotenvR  s    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y   Y dS w t|jS )zN
    Return True if `path` exists and is either a regular file or a FIFO.
    TF)r   rO   r   statFileNotFoundErrorOSErrorS_ISFIFOst_mode)rO   str   r   r   r4     s   r4   )rL   )ra   FrL   )ra   rL   )r2   FF)NNFFTrL   )NNFTrL   )-r7   loggingr   rS   rY   r   r   rV   collectionsr   
contextlibr   typingr   r   r   r   r   r	   r
   r   parserr   r   	variablesr   rJ   rI   	getLoggerrF   r    rK   r   r%   r&   rN   r`   rw   r{   r;   r   r   r   r   r4   r   r   r   r   <module>   s    (


V

3
!

4

2

$