
    &e0                        U d dl Z d dl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mZmZ d dlZd dlmZ d dlmZ d dlZd dlZd dlmZmZ d dlmZ  ee          Z ej        d           ej        d          gZee
e                   e!d	<   d
e de fdZ"de de fdZ# G d dee ef                   Z$ G d dee ef                   Z% e%e          Z&ee!d<   dS )    N)deepcopy)
AnyDict	ItemsViewIteratorKeysViewListMappingNoReturnOptional
ValuesView)Signal)Final)	file_utilruntime)
get_loggerzsecrets.tomlSECRETS_FILE_LOCS	attr_namereturnc                     d|  dS )Nzst.secrets has no attribute "". Did you forget to add it to secrets.toml or the app settings on Streamlit Cloud? More info: https://docs.streamlit.io/streamlit-cloud/get-started/deploy-an-app/connect-to-data-sources/secrets-management )r   s    9lib/python3.11/site-packages/streamlit/runtime/secrets.py_missing_attr_error_messager   1   s!    	E	 	E 	E 	E    keyc                     d|  dS )Nzst.secrets has no key "r   r   )r   s    r   _missing_key_error_messager   9   s!    	E# 	E 	E 	Er   c                       e Zd ZdZd Zedefd            ZdefdZ	de
e         fdZdedefdZd	edefd
Zd ZdefdZdefdZdeeef         fdZdS )AttrDictzo
    We use AttrDict to wrap up dictionary values from secrets
    to provide dot access to nested secrets
    c                 4    t          |          | j        d<   d S )N__nested_secrets__)dict__dict__)selfvalues     r   __init__zAttrDict.__init__G   s    .25kk*+++r   r   c                 N    t          | t                    s| S t          |           S N)
isinstancer
   r    )r&   s    r   _maybe_wrap_in_attr_dictz!AttrDict._maybe_wrap_in_attr_dictJ   s%    %)) 	#LE??"r   c                 *    t          | j                  S r)   )lenr"   r%   s    r   __len__zAttrDict.__len__Q   s    4*+++r   c                 *    t          | j                  S r)   )iterr"   r.   s    r   __iter__zAttrDict.__iter__T       D+,,,r   r   c                     	 | j         |         }|                     |          S # t          $ r t          t          |                    w xY wr)   )r"   r+   KeyErrorr   r%   r   r&   s      r   __getitem__zAttrDict.__getitem__W   sZ    	<+C0E00777 	< 	< 	<5c::;;;	<	   !$ 'Ar   c                     	 | j         |         }|                     |          S # t          $ r t          t	          |                    w xY wr)   )r"   r+   r5   AttributeErrorr   )r%   r   r&   s      r   __getattr__zAttrDict.__getattr__^   s_    	I+I6E00777 	I 	I 	I !<Y!G!GHHH	Ir8   c                 *    t          | j                  S r)   )reprr"   r.   s    r   __repr__zAttrDict.__repr__e   r3   r   c                      t          d          )Nz)Secrets does not support item assignment.	TypeErrorr6   s      r   __setitem__zAttrDict.__setitem__h   s    CDDDr   c                      t          d          )Nz.Secrets does not support attribute assignment.r@   r6   s      r   __setattr__zAttrDict.__setattr__k   s    HIIIr   c                 *    t          | j                  S r)   )r   r"   r.   s    r   to_dictzAttrDict.to_dictn   s    /000r   N)__name__
__module____qualname____doc__r'   staticmethodr   r+   intr/   r   strr2   r7   r;   r>   r   rB   rD   r   rF   r   r   r   r    r    A   sJ        
: : : #3 # # # \#, , , , ,-(3- - - - -<s <s < < < <IS IS I I I I- - -E E E E EJ J J J J1c3h 1 1 1 1 1 1r   r    c                   r   e Zd ZdZdee         fdZdefdZddZ	dede
eef         fd	Zed
ededdfd            Zed
ededdfd            ZddZddZdedefdZdedefdZdefdZdefdZd
edefdZdee         fdZdee         fdZdeeef         fdZdedefdZdee         fdZ dS )SecretszA dict-like class that stores secrets.
    Parses secrets.toml on-demand. Cannot be externally mutated.

    Safe to use from multiple threads.
    
file_pathsc                     d | _         t          j                    | _        d| _        || _        t          d          | _        d S )NFz4Emitted when a `secrets.toml` file has been changed.)doc)_secrets	threadingRLock_lock_file_watchers_installed_file_pathsr   file_change_listener)r%   rP   s     r   r'   zSecrets.__init__y   sH    59_&&
(-%%$*F%
 %
 %
!!!r   r   c                 V    	 |                      d           dS # t          $ r Y dS w xY w)a'  Load secrets.toml files from disk if they exists. If none exist,
        no exception will be raised. (If a file exists but is malformed,
        an exception *will* be raised.)

        Returns True if a secrets.toml file was successfully parsed, False otherwise.

        Thread-safe.
        Fprint_exceptionsT)_parseFileNotFoundErrorr.   s    r   load_if_toml_existszSecrets.load_if_toml_exists   sB    	KKK///4  	 	 	55	s    
((Nc                     | j         5  | j        	 ddd           dS | j                                        D ]\  }}|                     ||           d| _        ddd           dS # 1 swxY w Y   dS )zyClear the secrets dictionary and remove any secrets that were
        added to os.environ.

        Thread-safe.
        N)rV   rS   items"_maybe_delete_environment_variable)r%   kvs      r   _resetzSecrets._reset   s     Z 	! 	!}$	! 	! 	! 	! 	! 	! 	! 	! ++-- > >1771==== DM	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	!s   	A'<A''A+.A+r\   c                    | j         }||S | j        5  | j         | j         cddd           S d}i }| j        D ]}	 t          |d          5 }|                                }ddd           n# 1 swxY w Y   d}n# t
          $ r Y Ow xY w	 |                    t          j        |                     |#  |rt          j
        d|             xY w|sBdd                    | j                   }|rt          j
        |           t          |          t          d	 | j        D                       d
k    r6t                              dd                    | j                   d           |                                D ]\  }}	|                     ||	           || _         |                                  | j         cddd           S # 1 swxY w Y   dS )a  Parse our secrets.toml files if they're not already parsed.
        This function is safe to call from multiple threads.

        Parameters
        ----------
        print_exceptions : bool
            If True, then exceptions will be printed with `st.error` before
            being re-raised.

        Raises
        ------
        FileNotFoundError
            Raised if secrets.toml doesn't exist.

        NFzutf-8)encodingTzError parsing secrets file at zANo secrets files found. Valid paths for a secrets.toml file are: z, c                 P    g | ]#}t           j                            |          !|$S r   )ospathexists).0ps     r   
<listcomp>z"Secrets._parse.<locals>.<listcomp>   s+    EEE!27>>!3D3DEAEEEr      z%Secrets found in multiple locations: z`. When multiple secret.toml files exist, local secrets will take precedence over global secrets.)rS   rV   rX   openreadr^   updatetomlloadssterrorjoinr-   _LOGGERinfora   _maybe_set_environment_variable_maybe_install_file_watchers)
r%   r\   secretsfound_secrets_filerj   fsecrets_file_strerr_msgrc   rd   s
             r   r]   zSecrets._parse   s   $ -NZ ,	! ,	!}(},	! ,	! ,	! ,	! ,	! ,	! ,	! ,	! "'G(  dW555 4+,6688(4 4 4 4 4 4 4 4 4 4 4 4 4 4 4)-&&(   HNN4:.>#?#?@@@@' J!H$!H!HIII% 1{^b^g^ghlhx^y^y{{# &HW%%%'000EEt/EEEFFJJuDIIdFV<W<W u u u  
   ; ;144Q::::#DM--///=Y,	! ,	! ,	! ,	! ,	! ,	! ,	! ,	! ,	! ,	! ,	! ,	! ,	! ,	! ,	! ,	! ,	! ,	!sv   GGA<A."A<.A22A<5A26A<;G<
B	GB		G'B54G5CC0GGGrc   rd   c                     t          |          }|t          t          t          fv rt          |          t          j        | <   dS dS )zdAdd the given key/value pair to os.environ if the value
        is a string, int, or float.
        N)typerM   rL   floatri   environrc   rd   
value_types      r   rz   z'Secrets._maybe_set_environment_variable   s>    
 !WW
#sE***FFBJqMMM +*r   c                     t          |          }|t          t          t          fv r2t          j                            |           |k    rt          j        | = dS dS dS )ziRemove the given key/value pair from os.environ if the value
        is a string, int, or float.
        N)r   rM   rL   r   ri   r   getr   s      r   rb   z*Secrets._maybe_delete_environment_variable   sT    
 !WW
#sE***rz~~a/@/@A/E/E
1 +*/E/Er   c                    | j         5  | j        r	 d d d            d S | j        D ]?}	 t          j        j                            || j        d           0# t          $ r Y <w xY wd| _        d d d            d S # 1 swxY w Y   d S )Npoll)watcher_typeT)	rV   rW   rX   	streamlitwatcherpath_watcher
watch_file_on_secrets_file_changedr^   )r%   rj   s     r   r{   z$Secrets._maybe_install_file_watchers   s   Z 	1 	1, 	1 	1 	1 	1 	1 	1 	1 	1 (  
%2==5%+ >    
 )    D	 -1D)'	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1s9   	A9	A9,AA9
A"A9!A""
A99A= A=c                     | j         5  t                              d|           |                                  |                     d           d d d            n# 1 swxY w Y   | j                                         d S )Nz"Secrets file %s changed, reloadingTr[   )rV   rx   debugre   r]   rY   send)r%   changed_file_paths     r   r   z Secrets._on_secrets_file_changed  s    Z 	/ 	/MM>@QRRRKKMMMKKK...	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	!&&(((((s   AAA!Ar   c                     	 |                      d          |         }t          |t                    s|S t          |          S # t          t
          f$ r t          t          |                    w xY w)z{Return the value with the given key. If no such key
        exists, raise an AttributeError.

        Thread-safe.
        T)r]   r*   r
   r    r5   r^   r:   r   r6   s      r   r;   zSecrets.__getattr__  s    
	CKK%%c*EeW-- '& +, 	C 	C 	C !<S!A!ABBB	Cs   1A A .A1c                     	 |                      d          |         }t          |t                    s|S t          |          S # t          $ r t	          t          |                    w xY w)ztReturn the value with the given key. If no such key
        exists, raise a KeyError.

        Thread-safe.
        T)r]   r*   r
   r    r5   r   r6   s      r   r7   zSecrets.__getitem__*  sr    	<KK%%c*EeW-- '& 	< 	< 	<5c::;;;	<s   1A A 'A*c                     t          j                    s| j        j         d| j        dS t          |                     d                    S )zAA string representation of the contents of the dict. Thread-safe.z(file_paths=)T)r   rk   	__class__rG   rX   r=   r]   r.   s    r   r>   zSecrets.__repr__9  sN     ~ 	Qn-PP4;KPPPPDKK%%&&&r   c                 F    t          |                     d                    S )z/The number of entries in the dict. Thread-safe.T)r-   r]   r.   s    r   r/   zSecrets.__len__C  s    4;;t$$%%%r   c                 0    ||                      d          v S z2True if the given key is in the dict. Thread-safe.Tr]   )r%   rc   s     r   has_keyzSecrets.has_keyG  s    DKK%%%%r   c                 P    |                      d                                          S )z,A view of the keys in the dict. Thread-safe.T)r]   keysr.   s    r   r   zSecrets.keysK  s     {{4  %%'''r   c                 P    |                      d                                          S )z.A view of the values in the dict. Thread-safe.T)r]   valuesr.   s    r   r   zSecrets.valuesO  s     {{4  '')))r   c                 P    |                      d                                          S )z7A view of the key-value items in the dict. Thread-safe.T)r]   ra   r.   s    r   ra   zSecrets.itemsS  s     {{4  &&(((r   c                 0    ||                      d          v S r   r   )r%   r   s     r   __contains__zSecrets.__contains__W  s    dkk$''''r   c                 F    t          |                     d                    S )z3An iterator over the keys in the dict. Thread-safe.T)r1   r]   r.   s    r   r2   zSecrets.__iter__[  s    DKK%%&&&r   )r   N)!rG   rH   rI   rJ   r	   rM   r'   boolr_   re   r
   r   r]   rK   rz   rb   r{   r   r;   r7   r>   rL   r/   r   r   r   r   r   r   ra   r   r   r2   r   r   r   rO   rO   r   su        	
49 	
 	
 	
 	
T     ! ! ! !B!t B!S0A B! B! B! B!H #3 #3 #4 # # # \# c c d    \1 1 1 1,) ) ) )Cs Cs C C C C$<s <s < < < <'# ' ' ' '& & & & && & & & & &(hsm ( ( ( (*
3 * * * *)yc* ) ) ) )( ( ( ( ( ('(3- ' ' ' ' ' 'r   rO   secrets_singleton)'ri   rT   copyr   typingr   r   r   r   r   r	   r
   r   r   r   rs   blinkerr   typing_extensionsr   r   ru   streamlit.watcher.path_watcherr   r   streamlit.loggerr   rG   rx   get_streamlit_file_pathget_project_streamlit_file_pathr   rM   __annotations__r   r   r    rO   r   r   r   r   <module>r      sD   
				                                         # # # # # #     % % % % ( ( ( ( ( ( ( ( ' ' ' ' ' '
*X

%I%n55 .I-n==	' 5c#   3 3    C C    .1 .1 .1 .1 .1wsCx  .1 .1 .1bk' k' k' k' k'gc3h k' k' k'\ #7#455 5 5 5 5 5 5r   