
    .                        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j0                  e      Zdee   d	ee   fd
Z G d d      Z	 d'deeej<                  f   dedee   d	ee   fdZe	deeej<                  f   dee   d	eeee   ee   f      fd       Z 	 	 	 d(deeej<                  f   dededede!dee   d	eee!   eef   fdZ"	 	 d)deeej<                  f   dededee   d	eee!   ef   f
dZ#deeeee   f      de!d	eeee   f   fdZ$ded	ee   fdZ%	 	 	 d*dede!d e!d	efd!Z&	 	 	 	 	 	 d+deeej<                  df   d"eee      d#e!de!d$e!dee   d	e!fd%Z'	 	 	 	 	 d,deeej<                  df   d"eee      d#e!d$e!dee   d	eeee   f   fd&Z(y)-    N)OrderedDict)contextmanager)IODictIterableIteratorMappingOptionalTupleUnion   )Bindingparse_stream)parse_variablesmappingsreturnc              #      K   | D ]<  }|j                   r*t        j                  d|j                  j                         | > y w)Nz;Python-dotenv could not parse statement starting at line %s)errorloggerwarningoriginalline)r   mappings     lib/third_party/dotenv/main.pywith_warn_for_invalid_linesr      s<     ==NNM  %%  s   AAc                       e Zd Z	 	 	 	 	 ddeeeej                  f      deee      de	dedef   de	de	ddfd	Z
edeee      fd
       Zdeeee   f   fdZdeeeee   f      fdZde	fdZdedee   fdZy)DotEnvNdotenv_pathstreamverboseencodinginterpolateoverrider   c                 f    || _         || _        d | _        || _        || _        || _        || _        y N)r   r   _dictr    r!   r"   r#   )selfr   r   r    r!   r"   r#   s          r   __init__zDotEnv.__init__   s7     '
 &     c              #     K   | j                   rXt        j                  j                  | j                         r/t	        | j                   | j
                        5 }| d d d        y | j                  | j                   y | j                  r$t        j                  d| j                   xs d       t        j                  d       y # 1 sw Y   y xY ww)Nr!   z3Python-dotenv could not find configuration file %s..env )r   ospathisfileopenr!   r   r    r   infoioStringIO)r'   r   s     r   _get_streamzDotEnv._get_stream.   s     t/?/? @d&&?6 @?[[$++||I$$. ++b/! @?s   ACC
A,C
CCc                    | j                   r| j                   S | j                         }| j                  r1t        t	        || j
                              | _         | j                   S t        |      | _         | j                   S )zReturn dotenv as dict)r#   )r&   parser"   r   resolve_variablesr#   )r'   
raw_valuess     r   dictzDotEnv.dict=   sc    ::::ZZ\
$%6zDMM%Z[DJ zz %Z0DJzzr)   c              #      K   | j                         5 }t        t        |            D ])  }|j                  |j                  |j                  f + 	 d d d        y # 1 sw Y   y xY wwr%   )r5   r   r   keyvalue)r'   r   r   s      r   r7   zDotEnv.parseK   sQ     66|F7KL;;*!++w}}44 M  s!   A*$AA	A*A'#A*c                     | j                         sy| j                         j                         D ]:  \  }}|t        j                  v r| j                  s%|(|t        j                  |<   < y)zI
        Load the current dotenv as system environment variable.
        FT)r:   itemsr.   environr#   )r'   kvs      r   set_as_environment_variablesz#DotEnv.set_as_environment_variablesQ   sX     yy{IIK%%'DAqBJJt}}} !

1	 ( r)   r<   c                     | j                         }||v r||   S | j                  r!t        j                  d|| j                         y)z	
        zKey %s not found in %s.N)r:   r    r   r   r   )r'   r<   datas      r   getz
DotEnv.get`   sA     yy{$;9<<NN4c4;K;KLr)   )NFNTT)__name__
__module____qualname__r
   r   strr.   PathLiker   boolr(   r   r   r5   r   r:   r   r7   rC   rF    r)   r   r   r      s    %)%) !eC$456! C!! 	!
 c	"! ! ! 
!" "Xbg. " "d3-. 5xc8C=&8 9: 5d s x} r)   r   r   
key_to_getr!   c                 <    t        | d|      j                  |      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   rF   )r   rN   r!   s      r   get_keyrP   n   s     +th?CCJOOr)   r/   c              #   J  K   	 t         j                  j                  |       s(t        | d|      5 }|j	                  d       d d d        t        j                  dd|      5 }t        | |      5 }||f d d d        d d d        t        j                  j                  |        y # 1 sw Y   gxY w# 1 sw Y   >xY w# 1 sw Y   BxY w# t        $ rJ t         j                  j                  j                        rt        j                  |j                          w xY ww)Nzw+r+   r-   F)modedeleter!   )r.   r/   r0   r1   writetempfileNamedTemporaryFileshutilmovenameBaseExceptionunlink)r/   r!   sourcedests       r   rewriter^   {   s     
%ww~~d#dD84R  5((d58TX\dX.&tn$ / U 	DIIt$ 54 /. UT  77>>$))$IIdii sd   D#-C B) C #C1B58C C !D#)B2.C 5B>	:CC
C AD  D#
key_to_setvalue_to_set
quote_modeexportc                    |dvrt        dj                  |            |dk(  xs |dk(  xr |j                          }|r"dj                  |j                  dd            }n|}|rdj                  ||      }nd	j                  ||      }t	        | |
      5 \  }	}
d}d}t        t        |	            D ]p  }|j                  |k(  r|
j                  |       d}&|
j                  |j                  j                         |j                  j                  j                  d       }r |s$|r|
j                  d       |
j                  |       ddd       d||fS # 1 sw Y   xY w)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
    )alwaysautoneverzUnknown quote_mode: {}rd   re   z'{}''z\'zexport {}={}
z{}={}
r+   FT
N)
ValueErrorformatisalnumreplacer^   r   r   r<   rT   r   stringendswith)r   r_   r`   ra   rb   r!   quote	value_outline_outr\   r]   replacedmissing_newliner   s                 r   set_keyrt      s^    44188DEE 	h 	A& ?)=)=)?%? 

 MM,"6"6sE"BC	 	#**:yA##J	:	x	0NVT2<3GHG{{j(

8$

7++223&-&6&6&=&=&F&Ft&L"L I 

4 JJx  
1 \)) 
1	0s   B5EE#key_to_unsetc                    t         j                  j                  |       st        j	                  d|        d|fS d}t        | |      5 \  }}t        t        |            D ]9  }|j                  |k(  rd}|j                  |j                  j                         ; 	 ddd       |st        j	                  d||        d|fS ||fS # 1 sw Y   *xY w)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.NFr+   Tz/Key %s not removed from %s - key doesn't exist.)r.   r/   existsr   r   r^   r   r   r<   rT   r   rm   )r   ru   ra   r!   removedr\   r]   r   s           r   	unset_keyry      s     77>>+&A;O\!!G	x	0NVT2<3GHG{{l*

7++223	 I 
1 H,Xcd\!!L   
1	0s   	ACCvaluesr#   c                 H   i }| D ]  \  }}|d }nt        |      }i |r1j                  t        j                         j                  |       n0j                  |       j                  t        j                         dj	                  fd|D              }|||<    |S )Nr-   c              3   @   K   | ]  }|j                          y wr%   )resolve).0atomenvs     r   	<genexpr>z$resolve_variables.<locals>.<genexpr>   s     A54T\\#.5s   )r   updater.   r@   join)rz   r#   
new_valuesrY   r=   resultatomsr   s          @r   r8   r8      s     Ju=F#E*EC

2::&

:&

:&

2::&WWA5AAF!
4    r)   c              #     K   t         j                  j                  |       st        d      t         j                  j	                  |       rt         j                  j                  |       } d}t         j                  j                  |       }||k7  rd| t         j                  j                  t         j                  j                  |t         j                  j                              }||}}||k7  rcyyw)zL
    Yield directories starting from the given directory up to the root
    zStarting path not foundN)	r.   r/   rw   IOErrorr0   dirnameabspathr   pardir)r/   last_dircurrent_dir
parent_dirs       r   _walk_to_rootr      s      77>>$/00	ww~~dwwt$H''//$'K
k
!WW__RWW\\+rww~~%NO
 +Z+ k
!s   C1C64C6filenameraise_error_if_not_foundusecwdc                    d }|s |       st        t        dd      rt        j                         }nt        j                         }t
        }|j                  j                  |k(  r4|j                  J |j                  }|j                  j                  |k(  r4|j                  j                  }t        j                  j                  t        j                  j                  |            }t        |      D ]E  }t        j                  j                  ||       }	t        j                  j                  |	      sC|	c S  |rt        d      y)z
    Search in increasingly higher folders for the given file

    Returns path to the file if found, or an empty string otherwise
    c                  <    t        ddddg      } t        | d       S )z> Decide whether this is running in a REPL or IPython notebook __main__N__file__)fromlist)
__import__hasattr)mains    r   _is_interactivez$find_dotenv.<locals>._is_interactive  s$    *dDJ<H4,,,r)   frozenFzFile not foundr-   )getattrsysr.   getcwd	_getframer   f_codeco_filenamef_backr/   r   r   r   r   r0   r   )
r   r   r   r   r/   framecurrent_fileframe_filenamer   
check_paths
             r   find_dotenvr   
  s    -
 "gc8U&Cyy{ ll&&,6<<+++LLE ll&&,6 11wwrww~>? &WW\\'84
77>>*% '
  &''r)   r   r    r"   c                 `    | |
t               } t        | |||||      }|j                         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 atleast one environment variable is set elese False

    If both `dotenv_path` and `stream` are `None`, `find_dotenv()` is used to find the
    .env file.
    r   r   r    r"   r#   r!   )r   r   rC   )r   r   r    r#   r"   r!   dotenvs          r   load_dotenvr   3  sB    0 v~!mF ..00r)   c                 \    | |
t               } t        | |||d|      j                         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.
    Tr   )r   r   r:   )r   r   r    r"   r!   s        r   dotenv_valuesr   Y  s=    . v~!m dfr)   )utf-8)rd   Fr   )rd   r   )r,   FF)NNFFTr   )NNFTr   ))r3   loggingr.   rW   r   rU   collectionsr   
contextlibr   typingr   r   r   r   r	   r
   r   r   parserr   r   	variablesr   	getLoggerrG   r   r   r   rJ   rK   rP   r^   rL   rt   ry   r8   r   r   r   r   rM   r)   r   <module>r      s^   	  	  
  # %   * &			8	$(7*; @Q O Oj &
PsBKK'(
P
P sm
P c]	
P %
R[[ 
!%sm% eBsGRW$%&% %. %.*sBKK'(.*.* .* 	.*
 .* sm.* 8D>3#$.*h %	!sBKK'(!! ! sm	!
 8D>3!>U3-./ S(3- 28 8 8& %*&&"& & 		&T 26 $%#1sBKK-.#1RW#1 #1 	#1
 #1 sm#1 
#1N 26 $%!sBKK-.!RW! ! 	!
 sm! 
#x}
!r)   