
    g!                        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rd d	lmZ d d
lmZ  ee      Zded<    G d de      Zda G d d      ZddZ ddZ!y)    )annotationsN)Path)TYPE_CHECKINGAnyCallableFinal
NamedTuple)config	file_util)
get_logger)FolderBlackList)NoOpPathWatcherget_default_path_watcher_class)
ModuleType)PagesManagerr   _LOGGERc                  "    e Zd ZU ded<   ded<   y)WatchedModuler   watchermodule_nameN)__name__
__module____qualname____annotations__     \/var/www/openai/venv/lib/python3.12/site-packages/streamlit/watcher/local_sources_watcher.pyr   r   &   s    Lr   r   c                  ^    e Zd ZddZddZddZd Zd Zd Zd Z	d Z
d	 Zd
 ZddZddZy)LocalSourcesWatcherc                   || _         t        j                  j                  | j                   j                        | _        t        j                  j                  | j
                        | _        g | _        d| _	        t               | _        t        t        j                  d            | _        i | _        t               | _        | j%                          y )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)selfpages_managers     r   __init__zLocalSourcesWatcher.__init__1   s    +!#1D1D1U1U!V ggood.D.DE=?-0U  #2;<#
 ;=(+!!#r   c                   | j                   j                         }t               }| j                  j	                         j                         D ]D  }|d   s	|j                  |d          |d   | j                   vs/| j                  |d   d        F |D ]S  }||vst        j                  j                  |      r(| j                  |       | j                   j                  |       U | j                   j                  |      | _         y )Nscript_path)r   )r0   copyr+   r!   	get_pagesvaluesadd_register_watcherr"   r#   isfile_deregister_watcherremoveunion)r2   old_page_pathsnew_pages_paths	page_infoold_page_paths        r   r1   z(LocalSourcesWatcher.update_watched_pagesC   s    ,,113$'E,,668??AI]+	- 89't/B/BB&&m, $ '  B ,MO3BGGNN= ((7##**=9 , #1177Hr   c                :    | j                   j                  |       y N)r)   append)r2   cbs     r   register_file_change_callbackz1LocalSourcesWatcher.register_file_change_callback\   s    $$R(r   c                H   || j                   vrt        j                  d|       y | j                   j                         D ]G  }|j                  |j                  t
        j                  v s-t
        j                  |j                  = I | j                  D ]
  } ||        y )Nz'Received event for non-watched file: %s)r/   r   errorr9   r   sysmodulesr)   )r2   filepathwmrG   s       r   on_file_changedz#LocalSourcesWatcher.on_file_changed_   s    4000MMCXN ''..0B~~)bnn.KKK/ 1 ''BxL (r   c                    | j                   j                         D ]  }|j                  j                           i | _         t	               | _        d| _        y )NT)r/   r9   r   closer+   r0   r*   )r2   rN   s     r   rQ   zLocalSourcesWatcher.closew   sD    ''..0BJJ 1 "!er   c                    t         
t               a t         t        u ry 	 t        t        || j                        |      }|| j                  |<   y # t
        $ r Y y w xY w)N)r   r   )PathWatcherr   r   r   rO   PermissionErrorr/   )r2   rM   r   rN   s       r   r;   z%LocalSourcesWatcher._register_watcher~   se    8:K/)	#Hd.B.BC'B +-h'  	 	s   !A 	AAc                    || j                   vry || j                  k(  ry | j                   |   }|j                  j                          | j                   |= y rE   )r/   r&   r   rQ   )r2   rM   rN   s      r   r=   z'LocalSourcesWatcher._deregister_watcher   sR    4000t---""8,


!!(+r   c                    || j                   vS rE   )r/   r2   rM   s     r   _file_is_newz LocalSourcesWatcher._file_is_new   s    t4444r   c                    | j                  |      xr7 t        j                  || j                        xs t        j                  |      S rE   )rX   r   file_is_in_folder_globr(   file_in_pythonpathrW   s     r   _file_should_be_watchedz+LocalSourcesWatcher._file_should_be_watched   sC      * 
,,Xt7J7JK 6++H5	
r   c           
     z   | j                   ry t        t        j                        | j                  k7  rt        t        j                        j                         D ci c]   \  }}|| j                  t        |            " }}}t        t        j                        | _        | j                  |       y y c c}}w rE   )
r*   r+   rK   rL   r,   dictitems_exclude_blacklisted_pathsget_module_paths_register_necessary_watchers)r2   namemodulemodules_pathss       r   update_watched_modulesz*LocalSourcesWatcher.update_watched_modules   s    ??s{{t777 %)$5$;$;$=$=LD& d556Fv6NOO$=   (+3;;'7D$--m< 8s   %B7c           	         |j                         D ]P  \  }}|D ]F  }| j                  |      s| j                  t        t	        |      j                               |       H R y rE   )r_   r\   r;   strr   resolve)r2   module_pathsrc   pathsr#   s        r   rb   z0LocalSourcesWatcher._register_necessary_watchers   sS    '--/KD%//5**3tDz/A/A/C+DdK  0r   c                d    |D ch c]   }| j                   j                  |      r|" c}S c c}w rE   )r.   is_blacklisted)r2   rk   ps      r   r`   z.LocalSourcesWatcher._exclude_blacklisted_paths   s-     R5a(?(?(N(Nq(Q5RRRs    --N)r3   r   )returnNone)rG   zCallable[[str], None]ro   rp   )rj   zdict[str, set[str]]ro   rp   )rk   set[str]ro   rq   )r   r   r   r4   r1   rH   rO   rQ   r;   r=   rX   r\   rf   rb   r`   r   r   r   r   r   0   sA    $$I2)0-(	,5

=LSr   r   c                v   d d d g}t               }|D ]^  }g }	  ||       }|j                  |D cg c]6  }t        |      st        j                  j                  t        |            8 c}       ` |S # t        $ r Y `t        $ r( t        j	                  d| j
                   dd       Y w xY wc c}w )Nc                    | j                   gS rE   )__file__ms    r   <lambda>z"get_module_paths.<locals>.<lambda>   s
    1::,r   c                0    | j                   j                  gS rE   )__spec__originru   s    r   rw   z"get_module_paths.<locals>.<lambda>   s    1::$$%r   c                @    t        | j                  j                        S rE   )list__path___pathru   s    r   rw   z"get_module_paths.<locals>.<lambda>   s    $qzz''(r   zExamining the path of z raised:T)exc_info)r+   AttributeError	Exceptionr   warningr   update_is_valid_pathr"   r#   r$   rh   )rd   paths_extractors	all_pathsextract_pathspotential_pathsrn   s         r   ra   ra      s     	 	&
 	)). I)	+F3O 	.=SoPQARRWW__SV$oS	
 *   	 	OO((9BT  	 Ts"   A8B6
*B6
8	B3-B32B3c                    t        | t              xr@ t        j                  j	                  |       xs t        j                  j                  |       S rE   )
isinstancerh   r"   r#   r<   isdir)r#   s    r   r   r      s3    dC RbggnnT&:&QbggmmD>QRr   )rd   r   ro   rq   )r#   z
str | Nonero   bool)"
__future__r   r"   rK   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   rS   r   ra   r   r   r   r   <module>r      so    # 	 
  B B ' ' ?
  <H% %J  JS JSZ(VSr   