U
    ARd+                     @   s  d Z ddlZddlZddlmZmZmZmZ ddlZddl	m
  mZ ddlm
  mZ ddlm  mZ ddlmZ ddlmZ ddlmZmZ ddlmZ dZd	Zeeeef d
ddZedddZdd Z eeddddZ!ej"ddidej#dde$edej%dddRddZ&e&'d d!d  Z(e&'d"d#d$ Z)e&'d%d&d' Z*e&'d(e d)d* Z+e&'d+e ej,d,dd-d.ej,d/d0d1dSed2d3d4Z-ee d5d6d7Z.dTeee  eeeef  dd8d9d:Z/e&"d;d<d; Z0e0'd=d>d? Z1e&"d@dAd@ Ze'dBe dCdD Z2e&j"dEddFej3dGdE Z4e4'dHdIdJ Z5e&j"dKddLdMdK Z6e6'dNdOdP Z7e8dQkre&  dS )Uz=A script which is run when the Streamlit package is executed.    N)AnyDictListOptional)config)ConfigOption)Credentialscheck_credentials)$create_default_cache_storage_manager)pyZpy3)errorwarninginfodebug)config_optionreturnc                 C   s^   d| j  }| j dd}| j}| jrJ|dkr2d}|d| j d| j 7 }||| j|| jdS )	z>Composes given config option options as options for click lib.z--._N z
 z - )paramdescriptiontypeoptionenvvar)keyreplacer   
deprecatedZdeprecation_textZexpiration_dater   env_var)r   r   r   r    r   5/tmp/pip-unpacked-wheel-b9et7o5g/streamlit/web/cli.py&_convert_config_option_to_click_option&   s    r    r   c                    s   t jt jd d fdd}|S )N)_ctx_paramr   c                    s(   |d krd S t d jd jd S )NzSetting zn option using the CLI flag is not allowed. Set this option in the configuration file or environment variable: )
SystemExitr   r   )r"   r#   Z	cli_valuer!   r   r   callback>   s
    z1_make_sensitive_option_callback.<locals>.callback)clickContext	Parameter)r   r%   r   r!   r   _make_sensitive_option_callback=   s    	r)   c                 C   s~   t tj D ]j\}}t|}|jr8dddt|d}nd|d d}tj|d |d f|d |d	 d
|}|| } q| S )z;Decorator that adds config param keys to click dynamically.FT)Zexpose_valuehiddenZis_eagerr%   r   )Zshow_envvarr   r   r   r   r   )helpr   )	reversed_configZ_config_options_templateitemsr    Z	sensitiver)   r&   r   )funcr   valueZparsed_parameterZclick_option_kwargsr   r   r   r   configurator_optionsJ   s,    
r1   )main_script_pathurl_pathr   c                 C   s   ddl }t| df}z"||}|  ||j W n< |jjk
rr } zt	d| d| W 5 d}~X Y nX W 5 Q R X dS )z1Fetch remote file at url_path to main_script_pathr   NwbzUnable to fetch z.
)
requestsopengetraise_for_statuswritecontent
exceptionsRequestExceptionr&   BadParameter)r2   r3   r5   fpresper   r   r   _download_remoteg   s    
rA   Zauto_envvar_prefixZ	STREAMLIT)Zcontext_settingsz--log_levelT)Zshow_defaultr   Z	Streamlit	prog_namer   c                 C   s&   | r"ddl m} |t}|d dS )zTry out a demo with:

        $ streamlit hello

    Or use the line below to run your own script:

        $ streamlit run your_script.py
    r   )
get_loggerzSetting the log level using the --log_level flag is unsupported.
Use the --logger.level flag (after your streamlit command) instead.N)Zstreamlit.loggerrD   __name__r   )Z	log_levelrD   ZLOGGERr   r   r   maint   s    rF   r+   c                   C   s0   t   ttjdkstdtjd< tdd dS )zPrint this help message.   z--help   	streamlitrB   N)_get_command_line_as_stringlensysargvAssertionErrorrF   r   r   r   r   r+      s    
versionc                  C   s4   ddl } t  t| jdks td| jd< t  dS )z!Print Streamlit's version number.r   NrG   z	--versionrH   )rL   rJ   rK   rM   rN   rF   )rL   r   r   r   main_version   s
    
rP   Zdocsc                  C   s"   t d ddlm}  | d dS )zShow help in browser.zShowing help page in browser...r   utilzhttps://docs.streamlit.ioN)printrI   rR   Zopen_browserrQ   r   r   r   	main_docs   s    rT   Zhelloc                  K   s.   ddl m} tj| d |j}t|| d dS )zRuns the Hello World script.r   )Helloflag_optionsN)Zstreamlit.hellorU   	bootstrapload_config_options__file__	_main_run)kwargsrU   filenamer   r   r   
main_hello   s    r^   runtargetZSTREAMLIT_RUN_TARGET)requiredr   args)nargs)r`   c              	   K   s"  ddl m} tj|d tj| \}}|dd tkrf|dd dkrTt	dnt	d| d	|| rdd
l
m} | j}ddlm} ddlm}	 || j}
tj||
dddd }|	| } t||  t|||d W 5 Q R X n,tj| std|  t| ||d dS )zRun a Python script, piping stderr to Streamlit.

    The script can be local or it can be an url. In the latter case, Streamlit
    will download the script to a temporary file and runs this file.

    r   )urlrV   rH   Nr   zStreamlit requires raw Python (.py) files, but the provided file has no extension.
For more information, please see https://docs.streamlit.ioz/Streamlit requires raw Python (.py) files, not z<.
For more information, please see https://docs.streamlit.io)TemporaryDirectory)urlparse)url_util/rc   zFile does not exist: )Z
validatorsre   rX   rY   ospathsplitextACCEPTED_FILE_EXTENSIONSr&   ZBadArgumentUsageZstreamlit.temporary_directoryrf   urllib.parserg   rI   rh   joinstriprsplitZprocess_gitblob_urlrA   r[   existsr=   )r`   rb   r\   re   r   	extensionrf   temp_dirrg   rh   rk   r2   r   r   r   main_run   s6    

 

ru   )r   c                  C   sV   dd l } t j}|d krd S d|jkr0td|jg}|tjdd   | 	|S )Nr   zstreamlit.cliz}Running streamlit via `python -m streamlit.cli <command>` is unsupported. Please use `python -m streamlit <command>` instead.rH   )

subprocessr&   get_current_contextparentcommand_pathRuntimeErrorextendrL   rM   list2cmdline)rv   rx   Zcmd_line_as_listr   r   r   rJ      s    

rJ   )rb   rW   r   c                 C   s8   |d krg }|d kri }t  }t  t| ||| d S )N)rJ   r	   rX   r_   )filerb   rW   Zcommand_liner   r   r   r[      s    r[   cachec                   C   s   dS )zManage the Streamlit cache.Nr   r   r   r   r   r~     s    clearc                  C   sR   t  } t  }| r&td| d ntd| d t }|  tj  dS )z<Clear st.cache, st.cache_data, and st.cache_resource caches.zCleared directory r   zNothing to clear at N)	legacy_cachingclear_cacheget_cache_pathrS   r
   Z	clear_allcachingZcache_resourcer   )result
cache_pathZcache_storage_managerr   r   r   cache_clear  s    r   r   c                   C   s   dS )z#Manage Streamlit's config settings.Nr   r   r   r   r   r   0  s    showc                  K   s   t j| d t  dS )z(Show all of Streamlit's config settings.rV   N)rX   rY   r-   Zshow_config)r\   r   r   r   config_show6  s    r   activate)Zinvoke_without_commandc                 C   s   | j st   dS )z*Activate Streamlit by entering your email.N)Zinvoked_subcommandr   get_currentr   )ctxr   r   r   r   C  s    resetc                   C   s   t    dS )zReset Activation Credentials.N)r   r   r   r   r   r   r   activate_resetK  s    r   test)r*   c                   C   s   dS )zuInternal-only commands used for testing.

    These commands are not included in the output of `streamlit help`.
    Nr   r   r   r   r   r   T  s    rC   c                  C   s.   t   t j} | dk	st| jdks*tdS )zAssert that the program name is set to `streamlit test`.

    This is used by our cli-smoke-tests to verify that the program name is set
    to `streamlit ...` whether the streamlit binary is invoked directly or via
    `python -m streamlit ...`.
    Nzstreamlit test)rJ   r&   rw   rx   rN   ry   )rx   r   r   r   test_prog_name]  s    

r   __main__)r   )N)NN)9__doc__rj   rL   typingr   r   r   r   r&   Zstreamlit.runtime.cachingZruntimer   Z streamlit.runtime.legacy_cachingr   Zstreamlit.web.bootstrapZwebrX   rI   r   r-   Zstreamlit.config_optionr   Zstreamlit.runtime.credentialsr   r	   Z*streamlit.web.cache_storage_manager_configr
   rm   Z
LOG_LEVELSstrr    r)   r1   rA   groupr   ZChoiceZversion_optionrF   commandr+   rP   rT   r^   argumentru   rJ   r[   r~   r   r   Zpass_contextr   r   r   r   rE   r   r   r   r   <module>   s   




	,  







