U
    ARd8                     @   s.  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
 d dlmZ d dlmZ d dlmZ d dlmZ eeZdd	 Ze d
Zee	eef dddZee	eef dddZe Zdaeeeeeef f  ed< eddZ dd Z!eeeeeef f dddZ"eegdf dddZ#dS )    N)Path)AnyCallableDictOptionalTuplecast)Signal)
get_logger)extract_leading_emoji)calc_md5c                 C   s.   ddl }t|dr|| S t| dddS dS )a8  Open a read-only Python file taking proper care of its encoding.

    In Python 3, we would like all files to be opened with utf-8 encoding.
    However, some author like to specify PEP263 headers in their source files
    with their own encodings. In that case, we should respect the author's
    encoding.
    r   Nopenrzutf-8)encoding)tokenizehasattrr   )filenamer    r   9/tmp/pip-unpacked-wheel-b9et7o5g/streamlit/source_util.pyopen_python_file   s    

r   z([0-9]*)[_ -]*(.*)\.py)script_pathreturnc                 C   sZ   t t| j}t|dks(t|  d|\\}}| }|dkrNtd|fS t||fS )Nr   z is not a Python file inf)refindallPAGE_FILENAME_REGEXnamelenAssertionErrorlowerfloat)r   matchesnumberlabelr   r   r   page_sort_key2   s    
r%   c                 C   sN   t t| j}|dkrdS tt|}t dd|d pD|d}t	|S )a  Compute the icon and name of a page from its script path.

    This is *almost* the page name displayed in the nav UI, but it has
    underscores instead of spaces. The reason we do this is because having
    spaces in URLs both looks bad and is hard to deal with due to the need to
    URL-encode them. To solve this, we only swap the underscores for spaces
    right before we render page names.
    N)r   r   z[_ ]+_      )
r   searchr   r   r   r   subgroupstripr   )r   Z
extractionZicon_and_namer   r   r   page_icon_and_nameB   s    	
  r-   _cached_pagesz+Emitted when the pages directory is changed)docc                	   C   s*   t d t
 d aW 5 Q R X t  d S )NzPages directory changed)LOGGERdebug_pages_cache_lockr.   _on_pages_changedsendr   r   r   r   invalidate_pages_cache`   s    
r5   )main_script_path_strr   c              
   C   s   t }|d k	r|S t t d k	r.t W  5 Q R  S t| }t|\}}t| }||||t| di}|jd }tdd |	dD t
d}|D ]6}t| }	t|\}
}t|	}|||
|	d||< q|a |W  5 Q R  S Q R X d S )N)Zpage_script_hashZ	page_nameZiconr   pagesc                 S   s&   g | ]}|j d s|j dks|qS ).z__init__.py)r   
startswith).0fr   r   r   
<listcomp>   s    
zget_pages.<locals>.<listcomp>z*.py)key)r.   r2   r   r-   r   strresolveparentsortedglobr%   )r6   r7   Zmain_script_pathZmain_page_iconZmain_page_nameZmain_page_script_hashZ	pages_dirZpage_scriptsr   Zscript_path_strpiZpnZpshr   r   r   	get_pagesj   sD    
	
	rD   callbackc                    s    fdd}t j dd |S )Nc                      s   t   d S )N)r3   
disconnectr   rE   r   r   rG      s    z3register_pages_changed_callback.<locals>.disconnectF)Zweak)r3   connect)rF   rG   r   rE   r   register_pages_changed_callback   s    rI   )$r   	threadingpathlibr   typingr   r   r   r   r   r   Zblinkerr	   Zstreamlit.loggerr
   Zstreamlit.string_utilr   Zstreamlit.utilr   __name__r0   r   compiler   r!   r>   r%   r-   RLockr2   r.   __annotations__r3   r5   rD   rI   r   r   r   r   <module>   s(     
 

 :