o
    h+                     @  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
 ddlmZ ddlmZmZmZmZ ddlmZ eeZded	< ejrId
ZndZ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)uuid4)cli_utilenv_util	file_utilutil)
get_loggerr   _LOGGERz$%userprofile%/.streamlit/config.tomlz~/.streamlit/config.tomlc                   @  s   e Zd ZU ded< ded< dS )_Activationz
str | Noneemailboolis_validN)__name__
__module____qualname____annotations__ r   r   Q/var/www/vscode/kcb/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boldu   

      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   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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data)requestsr&   json	Exceptionr   errorr   r   postdumpsencoderaise_for_status)r   r>   metrics_urlresponse_jsonr<   r=   responser   r   r   _send_emailD   sF   
	
rI   c                   @  sx   e Zd ZU dZdZded< edd Z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
_singletonc                 C  s   | j du rt  tj S )zReturn the singleton instance.N)rK   rJ   )clsr   r   r   get_currentu   s   
zCredentials.get_currentc                 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)rJ   rK   RuntimeError
activation_get_credential_file_path
_conf_fileselfr   r   r   __init__}   s   

zCredentials.__init__r   r   c                 C  s
   t | S N)r
   repr_rR   r   r   r   __repr__      
zCredentials.__repr__Fauto_resolver   r*   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.
                )rO   r   rA   tomlopenrQ   loadr&   r@   _verify_emailFileNotFoundErroractivaterN   resettextwrapdedent)rS   rY   r\   fr=   r   r   r   r^      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.)r^   r@   rN   _exitr   rO   r   )rS   rY   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.)	rJ   rM   rO   r$   removerQ   OSErrorr   	exception)rL   cr   r   r   rb      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   wrZ   zError saving email:)requests.exceptionsrm   rO   r$   makedirspathdirnamerQ   r   r\   r]   dumprI   r   rk   )rS   rm   r=   r\   re   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rx| 	  dt
jd	d
dt
td }t
| |rudt
jddd
dt
j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_defaulta  
  You can find our privacy policy at %(link)s

  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 %(config)s,
    creating that file if necessary:

    [browser]
    gatherUsageStats = false
z#https://streamlit.io/privacy-policyT)	underline)linkconfigz$
  %(start)s
  %(prompt)s %(hello)s
zGet started by typing:r   )r   r   $r   zstreamlit hellor   )startprompthellozPlease try again.)r^   rN   rO   r   rf   clickr   r)   r_   ru   r   r'   _CONFIG_FILE_PATHprint_to_clir   rA   )rS   r[   	activatedr   r   TELEMETRY_TEXTINSTRUCTIONS_TEXTr   r   r   ra      s\   



zCredentials.activater   r   )F)rY   r   r   r*   )Tr   r*   )r[   r   r   r*   )r   r   r   __doc__rK   r   classmethodrM   rT   rW   r^   rh   rb   ru   ra   r   r   r   r   rJ   p   s   
 

$
rJ   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   r_   2  s
   


r_   messager   c                 C  s   t |  td dS )zExit program with error.N)r   rA   r    exit)r   r   r   r   rf   N  s   
rf   c                   C  s
   t dS )Nzcredentials.toml)r	   get_streamlit_file_pathr   r   r   r   rP   T  rX   rP   r   c                   C  s   t jt S rU   )r$   rr   existsrP   r   r   r   r   _check_credential_file_existsX  s   r   c                  C  sD   ddl m}  t s| dr| 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.

    r   r|   zserver.headlesszbrowser.gatherUsageStatsN)	streamlitr|   r   
get_optionis_manually_setr   r   _TELEMETRY_HEADLESS_TEXTrJ   rM   rh   r   r   r   r   check_credentials\  s   	

r   r   )r   r   r   r*   )r   r   r   r   )r   r   r   r   )r   r   r   )#r   
__future__r   r?   r$   r    rc   typingr   r   r   uuidr   r   r   r   r	   r
   streamlit.loggerr   r   r   r   r#   r   r   r)   r   rI   rJ   r_   rf   rP   r   r   r   r   r   r   <module>   s2   

, 
C


