o
    Zhv+                     @  s   U d dl mZ d dlZd dl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mZ erId dlmZ d d	lmZ eeZd
ed< G dd de	Zdaded< G dd dZdddZdddZ dS )    )annotationsN)TYPE_CHECKINGAnyCallableFinal
NamedTuple)config	file_util)
get_logger)FolderBlackList)NoOpPathWatcherPathWatcherTypeget_default_path_watcher_class)
ModuleType)PagesManagerr   _LOGGERc                   @  s   e Zd ZU ded< ded< dS )WatchedModuler   watcher
str | Nonemodule_nameN)__name__
__module____qualname____annotations__ r   r   h/var/www/html/Persson_Maskin/env/lib/python3.10/site-packages/streamlit/watcher/local_sources_watcher.pyr   &   s   
 r   zPathWatcherType | NonePathWatcherc                   @  s   e Zd Zd+ddZd,ddZd-ddZd.ddZd,ddZ	d/d0ddZd.ddZ	d1ddZ
d1ddZd,d d!Zd2d$d%Zd3d(d)Zd*S )4LocalSourcesWatcherpages_managerr   returnNonec                 C  sr   || _ tj| j j| _td| _tj	| j| _
g | _d| _t | _ttd| _i | _t | _|   d S )Nzserver.folderWatchListFzserver.folderWatchBlacklist)_pages_managerospathrealpathmain_script_path_main_script_pathr   
get_option_watch_foldersdirname_script_folder_on_path_changed
_is_closedset_cached_sys_modulesr   _folder_black_list_watched_modules_watched_pagesupdate_watched_pages)selfr   r   r   r   __init__1   s   zLocalSourcesWatcher.__init__c                 C  s   | j  }t }| j  D ] }|d sqtj|d }|	| || j vr/| j
|d d q| jD ](}tj|sBtd| q3td| tj|}|| j vr[| j
|d dd q3|D ]}||vrutj|su| | | j | q^| j || _ d S )Nscript_path)r   z#Watch folder is not a directory: %szRegistering watch folder: %sT)r   is_directory)r1   copyr-   r!   	get_pagesvaluesr"   r#   r$   add_register_watcherr(   isdirr   warningdebugisfile_deregister_watcherremoveunion)r3   old_page_pathsnew_pages_paths	page_info	page_pathwatch_folderwatch_folder_pathold_page_pathr   r   r   r2   D   sD   





z(LocalSourcesWatcher.update_watched_pagescbCallable[[str], None]c                 C  s   | j | d S N)r+   append)r3   rJ   r   r   r   register_file_change_callbackn   s   z1LocalSourcesWatcher.register_file_change_callbackfilepathstrc                 C  s   t d| tj|}|| jvrB| jD ]%}tj|r9tj||g|kr9t d| | jD ]}|| q/ d S qt 	d| d S | j
 D ]}|jd urY|jtjv rYtj|j= qG| jD ]}|| q]d S )NzPath changed: %sz%File changed in watched directory: %sz'Received event for non-watched path: %s)r   r>   r"   r#   r$   r0   r<   
commonpathr+   errorr9   r   sysmodules)r3   rO   norm_filepathwatched_pathrJ   wmr   r   r   on_path_changedq   s.   







z#LocalSourcesWatcher.on_path_changedc                 C  s2   | j  D ]}|j  qi | _ t | _d| _d S )NT)r0   r9   r   closer-   r1   r,   )r3   rW   r   r   r   rY      s
   
zLocalSourcesWatcher.closeFr   r   r6   boolc              
   C  s   t d u rt a t tu rd S z|rdnd }tt || j|dd|d}|| j|< W d S  tyC } ztjd||d W Y d }~d S d }~ww )Nz**/*F)glob_patternallow_nonexistent)r   r   zFailed to watch file %s: %sexc_info)	r   r   r   r   rX   r0   	Exceptionr   r=   )r3   rO   r   r6   r[   rW   exr   r   r   r;      s*   	z%LocalSourcesWatcher._register_watcherc                 C  s<   || j vrd S || jkrd S | j | }|j  | j |= d S rL   )r0   r&   r   rY   )r3   rO   rW   r   r   r   r@      s   



z'LocalSourcesWatcher._deregister_watcherc                 C  s
   || j vS rL   )r0   r3   rO   r   r   r   _file_is_new   s   
z LocalSourcesWatcher._file_is_newc                 C  s"   |  |ot|| jpt|S rL   )rb   r	   file_is_in_folder_globr*   file_in_pythonpathra   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r3   r   r   
<dictcomp>   s    z>LocalSourcesWatcher.update_watched_modules.<locals>.<dictcomp>)r,   r-   rS   rT   r.   dictitems_register_necessary_watchers)r3   modules_pathsr   rk   r   update_watched_modules   s   
z*LocalSourcesWatcher.update_watched_modulesmodule_pathsdict[str, set[str]]c                 C  s>   |  D ]\}}|D ]}| |r| tj|| q
qd S rL   )rn   re   r;   r"   r#   r$   )r3   rr   ri   pathsr#   r   r   r   ro      s   
z0LocalSourcesWatcher._register_necessary_watchersrt   set[str]c                   s    fdd|D S )Nc                   s   h | ]
} j |s|qS r   )r/   is_blacklistedrh   prk   r   r   	<setcomp>   s    zALocalSourcesWatcher._exclude_blacklisted_paths.<locals>.<setcomp>r   )r3   rt   r   rk   r   rf      s   z.LocalSourcesWatcher._exclude_blacklisted_pathsN)r   r   r   r    )r   r    )rJ   rK   r   r    )rO   rP   r   r    )F)rO   rP   r   r   r6   rZ   r   r    )rO   rP   r   rZ   )rr   rs   r   r    )rt   ru   r   ru   )r   r   r   r4   r2   rN   rX   rY   r;   r@   rb   re   rq   ro   rf   r   r   r   r   r   0   s    


*

&




r   rj   r   r   ru   c              	   C  s   dd dd dd g}t  }|D ]/}g }z|| }W n ty#   Y n ty4   tjd| jdd Y nw |dd	 |D  q|S )
Nc                 S  s   t | dr	| jgS g S )N__file__)hasattrrz   mr   r   r   <lambda>   s    z"get_module_paths.<locals>.<lambda>c                 S  s"   t | dr| jd ur| jjgS g S )N__spec__)r{   r   originr|   r   r   r   r~      s
   

c                 S  s6   t | drt| jjdkrt | jdrt| jjS g S )N__path___NamespacePath_path)r{   typer   r   listr   r|   r   r   r   r~      s   
z Examining the path of %s raised:Tr]   c                 S  s$   g | ]}t |rtjt|qS r   )_is_valid_pathr"   r#   r$   rP   rw   r   r   r   
<listcomp>  s   $ z$get_module_paths.<locals>.<listcomp>)r-   AttributeErrorr_   r   r=   r   update)rj   paths_extractors	all_pathsextract_pathspotential_pathsr   r   r   rg      s(   

rg   r#   r   rZ   c                 C  s"   t | totj| ptj| S rL   )
isinstancerP   r"   r#   r?   r<   )r#   r   r   r   r     s   "r   )rj   r   r   ru   )r#   r   r   rZ   )!
__future__r   r"   rS   typingr   r   r   r   r   	streamlitr   r	   streamlit.loggerr
   #streamlit.watcher.folder_black_listr   streamlit.watcher.path_watcherr   r   r   typesr   streamlit.runtime.pages_managerr   r   r   r   r   r   r   rg   r   r   r   r   r   <module>   s$    
83