o
    沪g!                     @  s   U d dl mZ 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 d dlmZmZ d dlmZ d dlmZ d dlmZmZ erMd d	lmZ d d
lmZ eeZded< G dd deZdaG dd dZdddZ dddZ!dS )    )annotationsN)Path)TYPE_CHECKINGAnyCallableFinal
NamedTuple)config	file_util)
get_logger)FolderBlackList)NoOpPathWatcherget_default_path_watcher_class)
ModuleType)PagesManagerr   _LOGGERc                   @  s   e Zd ZU ded< ded< dS )WatchedModuler   watchermodule_nameN)__name__
__module____qualname____annotations__ r   r   c/var/www/html/chatdoc2/venv/lib/python3.10/site-packages/streamlit/watcher/local_sources_watcher.pyr   &   s   
 r   c                   @  sv   e Zd Zd$ddZd%ddZd&ddZdd Zdd Zdd Zdd Z	dd Z
dd Zdd Zd'ddZd(d!d"Zd#S ))LocalSourcesWatcherpages_managerr   c                 C  sf   || _ tj| j j| _tj| j| _g | _d| _	t
 | _ttd| _i | _t
 | _|   d S )NFzserver.folderWatchBlacklist)_pages_managerospathabspathmain_script_path_main_script_pathdirname_script_folder_on_file_changed
_is_closedset_cached_sys_modulesr   r	   
get_option_folder_black_list_watched_modules_watched_pagesupdate_watched_pages)selfr   r   r   r   __init__1   s   zLocalSourcesWatcher.__init__returnNonec                 C  s   | j  }t }| j  D ]}|d sq||d  |d | j vr-| j|d d d q|D ]}||vrGtj	
|sG| | | j | q0| j || _ d S )Nscript_path)r   )r,   copyr'   r   	get_pagesvaluesadd_register_watcherr   r   isfile_deregister_watcherremoveunion)r.   old_page_pathsnew_pages_paths	page_infoold_page_pathr   r   r   r-   C   s(   

z(LocalSourcesWatcher.update_watched_pagescbCallable[[str], None]c                 C  s   | j | d S N)r%   append)r.   r@   r   r   r   register_file_change_callback\   s   z1LocalSourcesWatcher.register_file_change_callbackc                 C  sb   || j vrtd| d S | j  D ]}|jd ur$|jtjv r$tj|j= q| jD ]}|| q(d S )Nz'Received event for non-watched file: %s)r+   r   errorr5   r   sysmodulesr%   )r.   filepathwmr@   r   r   r   on_file_changed_   s   



z#LocalSourcesWatcher.on_file_changedc                 C  s2   | j  D ]}|j  qi | _ t | _d| _d S )NT)r+   r5   r   closer'   r,   r&   )r.   rI   r   r   r   rK   w   s
   
zLocalSourcesWatcher.closec                 C  sV   t d u rt a t tu rd S ztt || j|d}W n
 ty#   Y d S w || j|< d S )N)r   r   )PathWatcherr   r   r   rJ   PermissionErrorr+   )r.   rH   r   rI   r   r   r   r7   ~   s   

z%LocalSourcesWatcher._register_watcherc                 C  s<   || j vrd S || jkrd S | j | }|j  | j |= d S rB   )r+   r"   r   rK   )r.   rH   rI   r   r   r   r9      s   



z'LocalSourcesWatcher._deregister_watcherc                 C  s
   || j vS rB   )r+   r.   rH   r   r   r   _file_is_new   s   
z LocalSourcesWatcher._file_is_newc                 C  s"   |  |ot|| jpt|S rB   )rO   r
   file_is_in_folder_globr$   file_in_pythonpathrN   r   r   r   _file_should_be_watched   s   
z+LocalSourcesWatcher._file_should_be_watchedc                   sT    j rd S ttj jkr( fddttj D }ttj _ | d S d S )Nc                   s    i | ]\}}|  t|qS r   )_exclude_blacklisted_pathsget_module_paths).0namemoduler.   r   r   
<dictcomp>   s    z>LocalSourcesWatcher.update_watched_modules.<locals>.<dictcomp>)r&   r'   rF   rG   r(   dictitems_register_necessary_watchers)r.   modules_pathsr   rX   r   update_watched_modules   s   
z*LocalSourcesWatcher.update_watched_modulesmodule_pathsdict[str, set[str]]c                 C  sB   |  D ]\}}|D ]}| |r| tt| | q
qd S rB   )r[   rR   r7   strr   resolve)r.   r_   rV   pathsr   r   r   r   r\      s   
z0LocalSourcesWatcher._register_necessary_watchersrc   set[str]c                   s    fdd|D S )Nc                   s   h | ]
} j |s|qS r   )r*   is_blacklistedrU   prX   r   r   	<setcomp>   s    zALocalSourcesWatcher._exclude_blacklisted_paths.<locals>.<setcomp>r   )r.   rc   r   rX   r   rS      s   z.LocalSourcesWatcher._exclude_blacklisted_pathsN)r   r   )r0   r1   )r@   rA   r0   r1   )r_   r`   r0   r1   )rc   rd   r0   rd   )r   r   r   r/   r-   rD   rJ   rK   r7   r9   rO   rR   r^   r\   rS   r   r   r   r   r   0   s    



r   rW   r   r0   rd   c              	   C  s   dd dd dd g}t  }|D ]2}g }z|| }W n ty#   Y n ty7   tjd| j ddd Y nw |d	d
 |D  q|S )Nc                 S  s   | j gS rB   )__file__mr   r   r   <lambda>   s    z"get_module_paths.<locals>.<lambda>c                 S  s
   | j jgS rB   )__spec__originrj   r   r   r   rl      s   
 c                 S  s   t | jjS rB   )list__path___pathrj   r   r   r   rl      s    zExamining the path of z raised:T)exc_infoc                 S  s$   g | ]}t |rtjt|qS r   )_is_valid_pathr   r   r    ra   rf   r   r   r   
<listcomp>   s   $ z$get_module_paths.<locals>.<listcomp>)r'   AttributeError	Exceptionr   warningr   update)rW   paths_extractors	all_pathsextract_pathspotential_pathsr   r   r   rT      s(   

rT   r   
str | Noneboolc                 C  s"   t | totj| ptj| S rB   )
isinstancera   r   r   r8   isdir)r   r   r   r   rs      s   "rs   )rW   r   r0   rd   )r   r}   r0   r~   )"
__future__r   r   rF   pathlibr   typingr   r   r   r   r   	streamlitr	   r
   streamlit.loggerr   #streamlit.watcher.folder_black_listr   streamlit.watcher.path_watcherr   r   typesr   streamlit.runtime.pages_managerr   r   r   r   r   rL   r   rT   rs   r   r   r   r   <module>   s&    
+