
    /_i,                        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 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rd dlmZ d d	lmZ d d
lmZ  ee      Zded<    G d de      Zdaded<    G d d      ZddZ ddZ!y)    )annotationsN)TYPE_CHECKINGAnyFinal
NamedTuple)config	file_util)
get_logger)FolderBlackList)NoOpPathWatcherPathWatcherTypeget_default_path_watcher_class)Callable)
ModuleType)PagesManagerr   _LOGGERc                  "    e Zd ZU ded<   ded<   y)WatchedModuler   watcher
str | Nonemodule_nameN)__name__
__module____qualname____annotations__     f/var/www/html/land_sniper/venv/lib/python3.12/site-packages/streamlit/watcher/local_sources_watcher.pyr   r   '   s    Lr   r   zPathWatcherType | NonePathWatcherc                      e Zd ZdZddZddZddZddZ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y)LocalSourcesWatcheraK  Watch local Python sources and pages to trigger app reruns.

    Purpose
    -------
    This watcher powers Streamlit's core developer workflow: save a Python file
    and the app reruns. It tracks Python modules, the main script directory, and
    configured watch folders to notify the runtime when a relevant file changes.
    c                   || _         t        j                  j                  | j                   j                        | _        t        j                  d      | _        t        j                  j                  | j
                        | _
        g | _        d| _        t               | _        t        t        j                  d            | _        i | _        t               | _        | j'                          y )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)selfpages_managers     r   __init__zLocalSourcesWatcher.__init__;   s    +!#!1!1$2E2E2V2V!W$//0HI ggood.D.DE=?-0U  #2;<#
 ;=(+!!#r   c                   | j                   j                         }t               }| j                  j	                         j                         D ]]  }|d   s	t        j                  j                  |d         }|j                  |       || j                   vsK| j                  |d        _ | j                  D ]  }t        j                  j                  |      st        j                  d|       9t        j                  d|       t        j                  j                  |      }|| j                   vs}| j                  |d d        |D ]S  }||vst        j                  j!                  |      r(| j#                  |       | j                   j%                  |       U | j                   j'                  |      | _         y )Nscript_path)r   z#Watch folder is not a directory: %szRegistering watch folder: %sT)r   is_directory)r3   copyr/   r#   	get_pagesvaluesr$   r%   r&   add_register_watcherr*   isdirr   warningdebugisfile_deregister_watcherremoveunion)r5   old_page_pathsnew_pages_paths	page_info	page_pathwatch_folderwatch_folder_pathold_page_paths           r   r4   z(LocalSourcesWatcher.update_watched_pagesN   s   ,,113$'E,,668??A 
	I]+((=)ABI	* 3 33&& $ ' 
	 !// 	L77==. E|TMM8,G " 0 0 > (;(;;&&% $!% ' 	 , 	:MO3BGGNN= ((7##**=9	: #1177Hr   c                :    | j                   j                  |       y N)r-   append)r5   cbs     r   register_file_change_callbackz1LocalSourcesWatcher.register_file_change_callbackx   s    $$R(r   c                   t         j                  d|       t        j                  j	                  |      }|| j
                  vr| j
                  D ]w  }t        j                  j                  |      s#t        j                  j                  ||g      |k(  sHt         j                  d|       | j                  D ]
  } ||         y  t         j                  d|       y | j
                  j                         D ]G  }|j                  |j                  t        j                  v s-t        j                  |j                  = I | j                  D ]
  } ||        y )NzPath changed: %sz%File changed in watched directory: %sz'Received event for non-watched path: %s)r   rB   r$   r%   r&   r2   r@   
commonpathr-   errorr=   r   sysmodules)r5   filepathnorm_filepathwatched_pathrQ   wms         r   on_path_changedz#LocalSourcesWatcher.on_path_changed{   s"   ((3((2 5 55 $ 5 5 	GGMM,/**L-+HI#$ MM"I8T"33 %8%	 MMCXN ''..0 	0B~~)bnn.KKK/	0 '' 	BxL	r   c                    | j                   j                         D ]  }|j                  j                           i | _         t	               | _        d| _        y )NT)r2   r=   r   closer/   r3   r.   )r5   r[   s     r   r^   zLocalSourcesWatcher.close   sI    ''..0 	BJJ	 "!er   c                   t         
t               a t         t        u ry 	 |rdnd }t        t        || j                  |d      |      }|| j
                  |<   y # t        $ r"}t        j                  d||       Y d }~y d }~ww xY w)Nz**/*F)glob_patternallow_nonexistent)r   r   zFailed to watch file %s: %sexc_info)	r   r   r   r   r\   r2   	Exceptionr   rA   )r5   rX   r   r:   r`   r[   exs          r   r?   z%LocalSourcesWatcher._register_watcher   s     8:K/)	%16tL#((!-&+	 (B /1D!!(+ 	 OO98bOQ		s   9A 	B"A??Bc                    || j                   vry || j                  k(  ry | j                   |   }|j                  j                          | j                   |= y rO   )r2   r(   r   r^   )r5   rX   r[   s      r   rD   z'LocalSourcesWatcher._deregister_watcher   sR    4000t---""8,


!!(+r   c                    || j                   vS rO   )r2   r5   rX   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 rO   )ri   r	   file_is_in_folder_globr,   file_in_pythonpathrh   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 rO   )
r.   r/   rV   rW   r0   dictitems_exclude_blacklisted_pathsget_module_paths_register_necessary_watchers)r5   namemodulemodules_pathss       r   update_watched_modulesz*LocalSourcesWatcher.update_watched_modules   s    ??s{{t777 %)$5$;$;$= D& d556Fv6NOOM  (+3;;'7D$--m< 8s   %B7c                    |j                         D ]M  \  }}|D ]C  }| j                  |      s| j                  t        j                  j                  |      |       E O y rO   )rp   rm   r?   r$   r%   r&   )r5   module_pathsrt   pathsr%   s        r   rs   z0LocalSourcesWatcher._register_necessary_watchers   s^    '--/ 	IKD% I//5**277+;+;D+A4HI	Ir   c                d    |D ch c]   }| j                   j                  |      r|" c}S c c}w rO   )r1   is_blacklisted)r5   rz   ps      r   rq   z.LocalSourcesWatcher._exclude_blacklisted_paths   s)     Ra(?(?(N(Nq(QRRRs    --N)r6   r   returnNone)r~   r   )rQ   zCallable[[str], None]r~   r   )rX   strr~   r   )F)rX   r   r   r   r:   boolr~   r   )rX   r   r~   r   )ry   zdict[str, set[str]]r~   r   )rz   set[str]r~   r   )r   r   r   __doc__r7   r4   rR   r\   r^   r?   rD   ri   rm   rw   rs   rq   r   r   r   r!   r!   1   so    $&(IT)$L LQ*4DH	<	,5

=ISr   r!   c                p   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       Y w xY wc c}w )Nc                8    t        | d      r| j                  gS g S )N__file__)hasattrr   ms    r   <lambda>z"get_module_paths.<locals>.<lambda>   s    '!Z"81::, b r   c                d    t        | d      r#| j                  | j                  j                  gS g S )N__spec__)r   r   originr   s    r   r   z"get_module_paths.<locals>.<lambda>  s1    1j!ajj&< ::$$%  r   c                    t        | d      rWt        | j                        j                  dk(  r5t        | j                  d      rt	        | j                  j
                        S g S )N__path___NamespacePath_path)r   typer   r   listr   r   s    r   r   z"get_module_paths.<locals>.<lambda>  sT    1j! %%)99AJJ( qzz''(   r   z Examining the path of %s raised:Trb   )r/   AttributeErrorrd   r   rA   r   update_is_valid_pathr$   r%   r&   r   )ru   paths_extractors	all_pathsextract_pathspotential_pathsr}   s         r   rr   rr      s     	A		-H> I) 
	+F3O 	/>T!.QRBSRWWc!f%T	

   	 	OO2FOOd  	 Us"   A8B3
*B3
8	B0*B0/B0c                    t        | t              xr@ t        j                  j	                  |       xs t        j                  j                  |       S rO   )
isinstancer   r$   r%   rC   r@   )r%   s    r   r   r   $  s3    dC RbggnnT&:&QbggmmD>QRr   )ru   r   r~   r   )r%   r   r~   r   )"
__future__r   r$   rV   typingr   r   r   r   	streamlitr   r	   streamlit.loggerr
   #streamlit.watcher.folder_black_listr   streamlit.watcher.path_watcherr   r   r   collections.abcr   typesr   streamlit.runtime.pages_managerr   r   r   r   r   r   r!   rr   r   r   r   r   <module>r      sz    # 	 
 8 8 ' ' ?  ( <H% %J  '+# *}S }S@0fSr   