o
    Zh*                     @  s  U d Z ddlmZ ddlZddlZddlZddlZddlmZm	Z	m
Z
mZ ddlmZ ddlmZmZmZmZmZ ddlmZ eeZded	< ejrLd
ndZded< G dd de	Zd(ddZdZd)ddZG dd dZd*ddZ d+dd Z!d(d!d"Z"d,d$d%Z#d-d&d'Z$dS ).z(Manage the user's Streamlit credentials.    )annotationsN)Final
NamedTupleNoReturncast)uuid4)cli_utilconfigenv_util	file_utilutil)
get_loggerr   _LOGGERz$%userprofile%/.streamlit/config.tomlz~/.streamlit/config.toml_CONFIG_FILE_PATHc                   @  s   e Zd ZU ded< ded< dS )_Activation
str | Noneemailboolis_validN)__name__
__module____qualname____annotations__ r   r   ^/var/www/html/Persson_Maskin/env/lib/python3.10/site-packages/streamlit/runtime/credentials.pyr   '   s   
 r   returnstrc                  C  sP   t jjdkotj ptjd} d| rdnd tj	ddd d	tj	d
dd S )Nzutf-8
WT_SESSIONz
      u   👋  zWelcome to Streamlit!Tboldz

      If you'd like to receive helpful onboarding emails, news, offers, promotions,
      and the occasional swag, please enter your email address below. Otherwise,
      leave this field blank.

      zEmail: bluefg)
sysstdoutencodingr
   
IS_WINDOWSosenvirongetr   style_for_cli)
show_emojir   r   r   email_prompt,   s   
r-   zT
Collecting usage statistics. To deactivate, set browser.gatherUsageStats to false.
r   r   Nonec                 C  s   ddl }| du sd| vrdS d}z|jddd }|dd}W n ty0   td	 Y dS w d
ddddd}dtt d| dd| d}|j||t	|
 dd}|  dS )z0Send the user's email for metrics, if submitted.r   N@r   z&https://data.streamlit.io/metrics.json   )timeouturlzFailed to fetch metrics URLz*/*zen-US,en;q=0.9zapplication/jsonzlocalhost:8501zlocalhost:8501/)acceptzaccept-languagezcontent-typeoriginreferersubmittedEmailprovided_emailtrack)anonymous_id	messageIdeventauthor_emailsourcetypeuserId
   )headersdatar1   )requestsr*   json	Exceptionr   	exceptionr   r   postdumpsencoderaise_for_status)r   rC   metrics_urlresponse_jsonrA   rB   responser   r   r   _send_emailE   sH   
	
rN   c                   @  s|   e Zd ZU dZdZded< edddZdd	d
ZdddZ	d d!ddZ
d"d!ddZedddZdddZd"d#ddZdS )$CredentialszCredentials class.NzCredentials | None
_singletonr   c                 C  s   | j du rt  tdtj S )zReturn the singleton instance.NrO   )rP   rO   r   )clsr   r   r   get_currentw   s   
zCredentials.get_currentr.   c                 C  s*   t jdur	tdd| _t | _| t _dS )zInitialize class.Nz;Credentials already initialized. Use .get_current() instead)rO   rP   RuntimeError
activation_get_credential_file_path
_conf_fileselfr   r   r   __init__   s   

zCredentials.__init__r   c                 C  s
   t | S N)r   repr_rW   r   r   r   __repr__      
zCredentials.__repr__Fauto_resolver   c                 C  s   | j durtd dS ddl}z.t| j}||d}W d   n1 s)w   Y  |du r4tt	|d| _ W dS  t
yU   |rQ| j| d Y dS td tyu   |rk|   | j| d Y dS ttd| j w )	zLoad from toml file.Nz/Credentials already loaded. Not rereading file.r   generalr   )show_instructionsz7Credentials not found. Please run "streamlit activate".zz
                Unable to load credentials from %s.
                Run "streamlit reset" and try again.
                )rT   r   errortomlopenrV   loadr*   rS   _verify_emailFileNotFoundErroractivaterE   resettextwrapdedent)rX   r^   rb   frB   r   r   r   rd      s>   

zCredentials.loadTc              
   C  sf   z|  | W n ttfy! } ztt| W Y d}~nd}~ww | jdu s+| jjs1td dS dS )zTCheck if streamlit is activated.

        Used by `streamlit run script.py`
        NzActivation email not valid.)rd   rE   rS   _exitr   rT   r   )rX   r^   er   r   r   _check_activated   s   zCredentials._check_activatedc                 C  s@   t  }d|_z	t|j W dS  ty   td Y dS w )zReset credentials by removing file.

        This is used by `streamlit activate reset` in case a user wants
        to start over.
        Nz Error removing credentials file.)	rO   rR   rT   r(   removerV   OSErrorr   rF   )rQ   cr   r   r   rh      s   zCredentials.resetc                 C  s   ddl m} | jdu rdS tjtj| jdd d| jji}ddl	}t
| jd}|d|i| W d   n1 s<w   Y  z	t| jj W dS  |yY   td	 Y dS w )
z!Save to toml file and send email.r   )RequestExceptionNT)exist_okr   wr_   zError saving email:)requests.exceptionsrr   rT   r(   makedirspathdirnamerV   r   rb   rc   dumprN   r   rF   )rX   rr   rB   rb   rk   r   r   r   save   s   
zCredentials.saver`   c              	   C  s  z|    W n	 ty   Y nw | jr#| jjrtd dS td dS d}|sddl}|jt dddd}t|| _| jjr~| 	  dt
jd	d
d dt
t d}t
| |r{dt
jddd
d dt
jddd dt
jdd
d d}t
| d
}ntd |r'dS dS )zCActivate Streamlit.

        Used by `streamlit activate`.
        zAlready activatedzUActivation not valid. Please run `streamlit activate reset` then `streamlit activate`Fr   Nr   )textprompt_suffixdefaultshow_defaultz&
  You can find our privacy policy at z#https://streamlit.io/privacy-policyT)	underlinea-  

  Summary:
  - This open source library collects usage statistics.
  - We cannot see and do not store information contained inside Streamlit apps,
    such as text, charts, images, etc.
  - Telemetry data is stored in servers in the United States.
  - If you'd like to opt out, add the following to zS,
    creating that file if necessary:

    [browser]
    gatherUsageStats = false
z
  zGet started by typing:r!   )r#   r    $r"    zstreamlit hellor   
zPlease try again.)rd   rS   rT   r   rl   clickpromptr-   re   rz   r   r+   r   print_to_clir   ra   )rX   r`   	activatedr   r   telemetry_textinstructions_textr   r   r   rg      sR   



zCredentials.activate)r   rO   r   r.   r   r   )F)r^   r   r   r.   )T)r`   r   r   r.   )r   r   r   __doc__rP   r   classmethodrR   rY   r\   rd   rn   rh   rz   rg   r   r   r   r   rO   r   s   
 

$
rO   c                 C  s@   |   } t| dkr| ddkrtd tddS t| dS )ar  Verify the user's email address.

    The email can either be an empty string (if the user chooses not to enter
    it), or a string with a single '@' somewhere in it.

    Parameters
    ----------
    email : str

    Returns
    -------
    _Activation
        An _Activation object. Its 'is_valid' property will be True only if
        the email was validated.

    r   r/      z"That doesn't look like an email :(NFT)striplencountr   ra   r   )r   r   r   r   re   '  s
   


re   messager   c                 C  s   t |  td dS )zExit program with error.N)r   ra   r$   exit)r   r   r   r   rl   C  s   
rl   c                   C  s
   t dS )Nzcredentials.toml)r   get_streamlit_file_pathr   r   r   r   rU   I  r]   rU   r   c                   C  s   t jt S rZ   )r(   rw   existsrU   r   r   r   r   _check_credential_file_existsM  s   r   c                   C  sB   t  stdstdstdstt dS t 	  dS )zCheck credentials and potentially activate.

    Note
    ----
    If there is no credential file and we are in headless mode, we should not
    check, since credential would be automatically set to an empty string.

    zserver.headlesszserver.showEmailPromptzbrowser.gatherUsageStatsN)
r   r	   
get_optionis_manually_setr   r   _TELEMETRY_HEADLESS_TEXTrO   rR   rn   r   r   r   r   check_credentialsQ  s   


r   r   )r   r   r   r.   )r   r   r   r   )r   r   r   r   )r   r   r   )%r   
__future__r   rD   r(   r$   ri   typingr   r   r   r   uuidr   	streamlitr   r	   r
   r   r   streamlit.loggerr   r   r   r   r'   r   r   r-   r   rN   rO   re   rl   rU   r   r   r   r   r   r   <module>   s4   


- 
6


