o
    沪ge:                     @  s   U d Z 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 dd	lmZ dd
lmZ ddlmZ erQddlmZ eeZded< dddZG dd dZG dd dZ G dd dZ!G dd dej"Z#dS )a  Declares the EventBasedPathWatcher class, which watches given paths in the file system.

How these classes work together
-------------------------------

- EventBasedPathWatcher : each instance of this is able to watch a single
  file or directory at a given path so long as there's a browser interested in
  it. This uses _MultiPathWatcher to watch paths.

- _MultiPathWatcher : singleton that watches multiple paths. It does this by
  holding a watchdog.observer.Observer object, and manages several
  _FolderEventHandler instances. This creates _FolderEventHandlers as needed,
  if the required folder is not already being watched. And it also tells
  existing _FolderEventHandlers which paths it should be watching for.

- _FolderEventHandler : event handler for when a folder is modified. You can
  register paths in that folder that you're interested in. Then this object
  listens to folder events, sees if registered paths changed, and fires
  callbacks if so.

This module is lazy-loaded and used only if watchdog is installed.
    )annotationsN)TYPE_CHECKINGCallableFinalcast)ANYSignal)Self)events)Observer)
get_loggerrepr_)util)ObservedWatchr   _LOGGERpathstrreturnc                 C  s&   t jt j| r| S t j| S )zGet the absolute folder path for a given path.

    If the path is a directory, return the absolute path.
    Otherwise, return the absolute path of the parent directory.
    )osr   abspathisdirdirname)r    r   f/var/www/html/chatdoc2/venv/lib/python3.10/site-packages/streamlit/watcher/event_based_path_watcher.py_get_abs_folder_path;   s   &r   c                   @  sD   e Zd ZdZedddZddddddZdddZdddZdS )EventBasedPathWatcherz,Watches a single path on disk using watchdogr   Nonec                  C  s   t  } |   td dS )z&Close the _MultiPathWatcher singleton.zWatcher closedN)_MultiPathWatcherget_singletoncloser   debug)path_watcherr   r   r   	close_allG   s   zEventBasedPathWatcher.close_allNFglob_patternallow_nonexistentr   r   
on_changedCallable[[str], None]r%   
str | Noner&   boolc                C  sB   t j|| _|| _t }|j| j|||d t	d| j dS )a  Constructor for EventBasedPathWatchers.

        Parameters
        ----------
        path : str
            The path to watch.
        on_changed : Callable[[str], None]
            Callback to call when the path changes.
        glob_pattern : str or None
            A glob pattern to filter the files in a directory that should be
            watched. Only relevant when creating an EventBasedPathWatcher on a
            directory.
        allow_nonexistent : bool
            If True, the watcher will not raise an exception if the path does
            not exist. This can be used to watch for the creation of a file or
            directory at a given path.
        r$   zWatcher created for %sN)
r   r   r   _path_on_changedr   r   
watch_pathr   r!   )selfr   r'   r%   r&   r"   r   r   r   __init__N   s   zEventBasedPathWatcher.__init__c                 C     t | S Nr   r.   r   r   r   __repr__s      zEventBasedPathWatcher.__repr__c                 C  s   t  }|| j| j dS )zCStop watching the path corresponding to this EventBasedPathWatcher.N)r   r   stop_watching_pathr+   r,   )r.   r"   r   r   r   r    v   s   zEventBasedPathWatcher.closer   r   )
r   r   r'   r(   r%   r)   r&   r*   r   r   r   r   )	__name__
__module____qualname____doc__staticmethodr#   r/   r3   r    r   r   r   r   r   D   s    
%r   c                      sx   e Zd ZU dZdZded< ed ddZd! fd	d
Zd"ddZ	d#ddZ
dddd$ddZd%ddZd"ddZ  ZS )&r   zWatches multiple paths.Nz_MultiPathWatcher | None
_singletonr   c                 C  s&   | j du rtd t  tdtj S )z_Return the singleton _MultiPathWatcher object.

        Instantiates one if necessary.
        NzNo singleton. Registering one.r   )r=   r   r!   r   r   clsr   r   r   r      s   

z_MultiPathWatcher.get_singletonr	   c                   s   t jdur	tdt | S )Constructor.NzUse .get_singleton() instead)r   r=   RuntimeErrorsuper__new__r>   	__class__r   r   rC      s   
z_MultiPathWatcher.__new__r   c                 C  s,   | t _i | _t | _t | _| j  dS )r@   N)	r   r=   _folder_handlers	threadingLock_lockr   	_observerstartr2   r   r   r   r/      s
   
z_MultiPathWatcher.__init__r   c                 C  r0   r1   r   r2   r   r   r   r3      r4   z_MultiPathWatcher.__repr__Fr$   r   callbackr(   r%   r)   r&   r*   c                C  s~   t |}| j. | j|}|du r$t }|| j|< | jj||dd|_|j||||d W d   dS 1 s8w   Y  dS )zStart watching a path.NT)	recursiver$   )	r   rI   rF   get_FolderEventHandlerrJ   schedulewatchadd_path_change_listener)r.   r   rL   r%   r&   folder_pathfolder_handlerr   r   r   r-      s    	
"z_MultiPathWatcher.watch_pathc                 C  s   t |}| jL | j|}|du r!td| 	 W d   dS ||| | sC|jdurK| j	
|j | j|= W d   dS W d   dS W d   dS 1 sVw   Y  dS )zStop watching a path.NzFCannot stop watching path, because it is already not being watched. %s)r   rI   rF   rN   r   r!   remove_path_change_listeneris_watching_pathsrQ   rJ   
unschedule)r.   r   rL   rS   rT   r   r   r   r5      s*   

"z$_MultiPathWatcher.stop_watching_pathc                 C  sp   | j + 	 t| jdkri | _td ntd | j  | jjdd W d    d S 1 s1w   Y  d S )Nr   zSStopping observer thread even though there is a non-zero number of event observers!zStopping observer thread   )timeout)rI   lenrF   r   r!   rJ   stopjoinr2   r   r   r   r       s   

"z_MultiPathWatcher.close)r   r   )r   r	   r6   r7   
r   r   rL   r(   r%   r)   r&   r*   r   r   r   r   rL   r(   r   r   )r8   r9   r:   r;   r=   __annotations__classmethodr   rC   r/   r3   r-   r5   r    __classcell__r   r   rD   r   r   |   s   
 


r   c                   @  s,   e Zd ZdZddddddZdddZdS )WatchedPathz3Emits notifications when a single path is modified.NFr$   md5r   modification_timefloatr%   r)   r&   r*   c                C  s$   || _ || _|| _|| _t | _d S r1   )rc   rd   r%   r&   r   r'   )r.   rc   rd   r%   r&   r   r   r   r/      s
   zWatchedPath.__init__r   c                 C  r0   r1   r   r2   r   r   r   r3      r4   zWatchedPath.__repr__)rc   r   rd   re   r%   r)   r&   r*   r7   )r8   r9   r:   r;   r/   r3   r   r   r   r   rb      s    rb   c                      sz   e Zd ZdZd# fddZd$ddZd	d
dd%ddZd&ddZd'ddZd(ddZ	d(ddZ
d(dd Zd(d!d"Z  ZS ))rO   a  Listen to folder events. If certain paths change, fire a callback.

    The super class, FileSystemEventHandler, listens to changes to *folders*,
    but we need to listen to changes to *both* folders and files. I believe
    this is a limitation of the Mac FSEvents system API, and the watchdog
    library takes the lower common denominator.

    So in this class we watch for folder events and then filter them based
    on whether or not we care for the path the event is about.
    r   r   c                   s$   t    i | _t | _d | _d S r1   )rB   r/   _watched_pathsrG   rH   rI   rQ   r2   rD   r   r   r/     s   


z_FolderEventHandler.__init__r   c                 C  r0   r1   r   r2   r   r   r   r3     r4   z_FolderEventHandler.__repr__NFr$   r   rL   r(   r%   r)   r&   r*   c                C  s   | j 7 | j|d}|du r*tj|||d}t||}t||||d}|| j|< |jj|dd W d   dS 1 s=w   Y  dS )z)Add a path to this object's event filter.Nr$   )rc   rd   r%   r&   F)weak)	rI   rf   rN   r   calc_md5_with_blocking_retriespath_modification_timerb   r'   connect)r.   r   rL   r%   r&   watched_pathrc   rd   r   r   r   rR     s$   	
"z,_FolderEventHandler.add_path_change_listenerc                 C  s   | j 5 | j|d}|du r	 W d   dS |j| |jts0| j|= W d   dS W d   dS 1 s;w   Y  dS )z.Remove a path from this object's event filter.N)rI   rf   rN   r'   
disconnecthas_receivers_forr   )r.   r   rL   rk   r   r   r   rU   4  s   
"z/_FolderEventHandler.remove_path_change_listenerc                 C  s   t | jdkS )z<Return true if this object has 1+ paths in its event filter.r   )rZ   rf   r2   r   r   r   rV   A  s   z%_FolderEventHandler.is_watching_pathseventevents.FileSystemEventc                 C  s<  |j tjkr
|j}n,|j tjkr#ttj|}td|j|j	 |j	}n|j tj
kr-|j}n	td|j  dS t|tr@|d}tj|}| j|d}|du r[td|| j dS t||j}|dkrs||jkrstd| dS ||_tj||j|jd}||jkrtd	| dS td
| ||_|j| dS )zHandle when a path (corresponding to a file or dir) is changed.

        The events that can call this are modification, creation or moved
        events.
        zMove event: src %s; dest %szDon't care about event type %sNzutf-8z+Ignoring changed path %s.
Watched_paths: %sg        z%File/dir timestamp did not change: %sr$   zFile/dir MD5 did not change: %szFile/dir MD5 changed: %s)
event_typer
   EVENT_TYPE_MODIFIEDsrc_pathEVENT_TYPE_MOVEDr   FileSystemMovedEventr   r!   	dest_pathEVENT_TYPE_CREATED
isinstancebytesdecoder   r   r   rf   rN   r   ri   r&   rd   rh   r%   rc   r'   send)r.   rn   changed_pathabs_changed_pathchanged_path_inford   new_md5r   r   r   handle_path_change_eventE  sT   	




z,_FolderEventHandler.handle_path_change_eventc                 C     |  | d S r1   r   r.   rn   r   r   r   
on_created     z_FolderEventHandler.on_createdc                 C  r   r1   r   r   r   r   r   on_modified  r   z_FolderEventHandler.on_modifiedc                 C  r   r1   r   r   r   r   r   on_moved  r   z_FolderEventHandler.on_movedr6   r7   r]   r^   )r   r*   )rn   ro   r   r   )r8   r9   r:   r;   r/   r3   rR   rU   rV   r   r   r   r   ra   r   r   rD   r   rO     s    




J
rO   )r   r   r   r   )$r;   
__future__r   r   rG   typingr   r   r   r   blinkerr   r   typing_extensionsr	   watchdogr
   watchdog.observersr   streamlit.loggerr   streamlit.utilr   streamlit.watcherr   watchdog.observers.apir   r8   r   r_   r   r   r   rb   FileSystemEventHandlerrO   r   r   r   r   <module>   s(   
	8p