
    "d*/                        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 d dl	m
Z
 dZdZ G d de
      Z G d	 d
      Z G d de      Z G d de      Z G d de      Z G d de      Zy)    )annotationsN)Path)
BaseThreadProtocol)SkipRepeatsQueue   c                      e Zd ZdZy)
EventQueuea.  Thread-safe event queue based on a special queue that skips adding
    the same event (:class:`FileSystemEvent`) multiple times consecutively.
    Thus avoiding dispatching multiple event handling
    calls when multiple identical events are produced quicker than an observer
    can consume them.
    N)__name__
__module____qualname____doc__     8/usr/lib/python3/dist-packages/watchdog/observers/api.pyr
   r
      s    r   r
   c                  ^    e Zd ZdZd Zed        Zed        Zed        Zd Z	d Z
d Zd	 Zy
)ObservedWatchzAn scheduled watch.

    :param path:
        Path string.
    :param recursive:
        ``True`` if watch is recursive; ``False`` otherwise.
    c                p    t        |t              rt        |      | _        || _        y || _        || _        y N)
isinstancer   str_path_is_recursive)selfpath	recursives      r   __init__zObservedWatch.__init__0   s2    dD!TDJ ' DJ&r   c                    | j                   S )z"The path that this watch monitors.)r   r   s    r   r   zObservedWatch.path7   s     zzr   c                    | j                   S )z;Determines whether subdirectories are watched for the path.)r   r   s    r   is_recursivezObservedWatch.is_recursive<   s     !!!r   c                2    | j                   | j                  fS r   )r   r!   r   s    r   keyzObservedWatch.keyA   s    yy$++++r   c                4    | j                   |j                   k(  S r   r#   r   watchs     r   __eq__zObservedWatch.__eq__E       xx599$$r   c                4    | j                   |j                   k7  S r   r%   r&   s     r   __ne__zObservedWatch.__ne__H   r)   r   c                ,    t        | j                        S r   )hashr#   r   s    r   __hash__zObservedWatch.__hash__K   s    DHH~r   c                h    dt        |       j                   d| j                  d| j                   dS )N<z: path=z, is_recursive=>)typer   r   r!   r   s    r   __repr__zObservedWatch.__repr__N   s4    4:&&'wtyym?4K\K\J]]^__r   N)r   r   r   r   r   propertyr   r!   r#   r(   r+   r.   r3   r   r   r   r   r   '   s]    '   " " , ,%%`r   r   c                  X     e Zd ZdZef fd	Zed        Zed        Zd Z	d Z
d Z xZS )EventEmittera  
    Producer thread base class subclassed by event emitters
    that generate events and populate a queue with them.

    :param event_queue:
        The event queue to populate with generated events.
    :type event_queue:
        :class:`watchdog.events.EventQueue`
    :param watch:
        The watch to observe and produce events for.
    :type watch:
        :class:`ObservedWatch`
    :param timeout:
        Timeout (in seconds) between successive attempts at reading events.
    :type timeout:
        ``float``
    c                L    t         |           || _        || _        || _        y r   )superr   _event_queue_watch_timeout)r   event_queuer'   timeout	__class__s       r   r   zEventEmitter.__init__f   s$    'r   c                    | j                   S )z6
        Blocking timeout for reading events.
        r;   r   s    r   r=   zEventEmitter.timeoutl   s    
 }}r   c                    | j                   S )z9
        The watch associated with this emitter.
        )r:   r   s    r   r'   zEventEmitter.watchs   s    
 {{r   c                R    | j                   j                  || j                  f       y)z
        Queues a single event.

        :param event:
            Event to be queued.
        :type event:
            An instance of :class:`watchdog.events.FileSystemEvent`
            or a subclass.
        N)r9   putr'   )r   events     r   queue_eventzEventEmitter.queue_eventz   s!     	udjj12r   c                     y)a  Override this method to populate the event queue with events
        per interval period.

        :param timeout:
            Timeout (in seconds) between successive attempts at
            reading events.
        :type timeout:
            ``float``
        Nr   r   r=   s     r   queue_eventszEventEmitter.queue_events       r   c                ~    | j                         r-| j                  | j                         | j                         r,y y r   )should_keep_runningrH   r=   r   s    r   runzEventEmitter.run   s.    &&(dll+ &&(r   )r   r   r   r   DEFAULT_EMITTER_TIMEOUTr   r4   r=   r'   rE   rH   rL   __classcell__r>   s   @r   r6   r6   S   sJ    $ 4K      
3	,r   r6   c                  h     e Zd ZdZ e       Z	 ef fd	Zed        Z	d Z
ed        Zd Zd Z xZS )EventDispatchera<  
    Consumer thread base class subclassed by event observer threads
    that dispatch events from an event queue to appropriate event handlers.

    :param timeout:
        Timeout value (in seconds) passed to emitters
        constructions in the child class BaseObserver.
    :type timeout:
        ``float``
    c                N    t         |           t               | _        || _        y r   )r8   r   r
   r9   r;   )r   r=   r>   s     r   r   zEventDispatcher.__init__   s    &Lr   c                    | j                   S )z)Timeout value to construct emitters with.r@   r   s    r   r=   zEventDispatcher.timeout   s     }}r   c                    t        j                  |        	 | j                  j                  t        j
                         y # t        j                  $ r Y y w xY wr   )r   stopr<   
put_nowaitrQ   _stop_eventqueueFullr   s    r   rU   zEventDispatcher.stop   sC    	''(C(CDzz 		s   )A AAc                    | j                   S )zThe event queue which is populated with file system events
        by emitters and from which events are dispatched by a dispatcher
        thread.)r9   r   s    r   r<   zEventDispatcher.event_queue   s    
    r   c                     y)ag  Override this method to consume events from an event queue, blocking
        on the queue for the specified timeout before raising :class:`queue.Empty`.

        :param event_queue:
            Event queue to populate with one set of events.
        :type event_queue:
            :class:`EventQueue`
        :raises:
            :class:`queue.Empty`
        Nr   )r   r<   s     r   dispatch_eventszEventDispatcher.dispatch_events   rI   r   c                    | j                         r.	 | j                  | j                         | j                         r-y y # t        j                  $ r Y Tw xY wr   )rK   r\   r<   rX   Emptyr   s    r   rL   zEventDispatcher.run   sM    &&($$T%5%56 &&( ;; s   A   AA)r   r   r   r   objectrW   DEFAULT_OBSERVER_TIMEOUTr   r4   r=   rU   r<   r\   rL   rN   rO   s   @r   rQ   rQ      sS    	 (KC7  
   ! !
r   rQ   c                       e Zd ZdZef fd	Zd Zd Zd Zd Z	d Z
ed        Z fd	Zdd
Zd Zd Zd Zd Zd Zd Z xZS )BaseObserverzBase observer.c                    t         |   |       || _        t        j                         | _        t               | _        t               | _	        t               | _
        t               | _        y r   )r8   r   _emitter_class	threadingRLock_lockset_watchesdict	_handlers	_emitters_emitter_for_watch)r   emitter_classr=   r>   s      r   r   zBaseObserver.__init__   sK    !+__&
"&&r   c                l    || j                   |j                  <   | j                  j                  |       y r   )rm   r'   rl   addr   emitters     r   _add_emitterzBaseObserver._add_emitter   s(    18.7#r   c                    | j                   |j                  = | j                  j                  |       |j	                          	 |j                          y # t        $ r Y y w xY wr   )rm   r'   rl   removerU   joinRuntimeErrorrq   s     r   _remove_emitterzBaseObserver._remove_emitter   sO    ##GMM2g&	LLN 		s   A 	A! A!c                   | j                   D ]  }|j                           | j                   D ]  }	 |j                           | j                   j	                          | j
                  j	                          y # t        $ r Y Vw xY wr   )rl   rU   rv   rw   clearrm   rq   s     r   _clear_emitterszBaseObserver._clear_emitters   sw    ~~ 	GLLN	~~ 	G	
 	%%'   s   A99	BBc                    || j                   vrt               | j                   |<   | j                   |   j                  |       y r   )rk   rh   rp   r   event_handlerr'   s      r   _add_handler_for_watchz#BaseObserver._add_handler_for_watch   s5    &$'EDNN5!u!!-0r   c                    | j                   |= y r   )rk   r&   s     r   _remove_handlers_for_watchz'BaseObserver._remove_handlers_for_watch   s    NN5!r   c                    | j                   S )z/Returns event emitter created by this observer.)rl   r   s    r   emitterszBaseObserver.emitters   s     ~~r   c                    | j                   j                         D ]  }	 |j                           t
        | 	          y # t        $ r | j	                  |        w xY wr   )rl   copystart	Exceptionrx   r8   )r   rr   r>   s     r   r   zBaseObserver.start  sZ    ~~**, 	G	 	  $$W-s   AAc                   | j                   5  t        ||      }| j                  ||       | j                  j	                  |      Y| j                  | j                  || j                        }| j                         r|j                          | j                  |       | j                  j                  |       ddd       |S # 1 sw Y   S xY w)am  
        Schedules watching a path and calls appropriate methods specified
        in the given event handler in response to file system events.

        :param event_handler:
            An event handler instance that has appropriate event handling
            methods which will be called by the observer in response to
            file system events.
        :type event_handler:
            :class:`watchdog.events.FileSystemEventHandler` or a subclass
        :param path:
            Directory path that will be monitored.
        :type path:
            ``str``
        :param recursive:
            ``True`` if events will be emitted for sub-directories
            traversed recursively; ``False`` otherwise.
        :type recursive:
            ``bool``
        :return:
            An :class:`ObservedWatch` object instance representing
            a watch.
        N)r<   r'   r=   )rg   r   r   rm   getrd   r<   r=   is_aliver   rs   ri   rp   )r   r~   r   r   r'   rr   s         r   schedulezBaseObserver.schedule  s    0 ZZ 	%!$	2E''u= &&**519-- $ 0 0t|| .  ==?MMO!!'*MMe$	% 	% s   B.CCc                j    | j                   5  | j                  ||       ddd       y# 1 sw Y   yxY w)a!  Adds a handler for the given watch.

        :param event_handler:
            An event handler instance that has appropriate event handling
            methods which will be called by the observer in response to
            file system events.
        :type event_handler:
            :class:`watchdog.events.FileSystemEventHandler` or a subclass
        :param watch:
            The watch to add a handler for.
        :type watch:
            An instance of :class:`ObservedWatch` or a subclass of
            :class:`ObservedWatch`
        N)rg   r   r}   s      r   add_handler_for_watchz"BaseObserver.add_handler_for_watch2  s0     ZZ 	>''u=	> 	> 	>s   )2c                    | j                   5  | j                  |   j                  |       ddd       y# 1 sw Y   yxY w)a'  Removes a handler for the given watch.

        :param event_handler:
            An event handler instance that has appropriate event handling
            methods which will be called by the observer in response to
            file system events.
        :type event_handler:
            :class:`watchdog.events.FileSystemEventHandler` or a subclass
        :param watch:
            The watch to remove a handler for.
        :type watch:
            An instance of :class:`ObservedWatch` or a subclass of
            :class:`ObservedWatch`
        N)rg   rk   ru   r}   s      r   remove_handler_for_watchz%BaseObserver.remove_handler_for_watchD  s7     ZZ 	8NN5!((7	8 	8 	8s   5>c                    | j                   5  | j                  |   }| j                  |= | j                  |       | j                  j                  |       ddd       y# 1 sw Y   yxY w)zUnschedules a watch.

        :param watch:
            The watch to unschedule.
        :type watch:
            An instance of :class:`ObservedWatch` or a subclass of
            :class:`ObservedWatch`
        N)rg   rm   rk   rx   ri   ru   )r   r'   rr   s      r   
unschedulezBaseObserver.unscheduleV  s\     ZZ 	(--e4Gu%  )MM  '		( 	( 	(s   A	AA(c                    | j                   5  | j                  j                          | j                          | j                  j                          ddd       y# 1 sw Y   yxY w)zKUnschedules all watches and detaches all associated event
        handlers.N)rg   rk   rz   r{   ri   r   s    r   unschedule_allzBaseObserver.unschedule_alle  sL     ZZ 	"NN  "  "MM!	" 	" 	"s   AAA$c                $    | j                          y r   )r   r   s    r   on_thread_stopzBaseObserver.on_thread_stopm  s    r   c                p   |j                  d      }|t        j                  u ry |\  }}| j                  5  t	        | j
                  j                  |g             D ]2  }|| j
                  j                  |g       v s"|j                  |       4 	 d d d        |j                          y # 1 sw Y   xY w)NT)block)r   rQ   rW   rg   listrk   dispatch	task_done)r   r<   entryrD   r'   handlers         r   r\   zBaseObserver.dispatch_eventsp  s    d+O///uZZ 	,   2 25" => ,dnn00;;$$U+,		, 		, 	,s   AB,?B,,B5)F)r   r   r   r   r`   r   rs   rx   r{   r   r   r4   r   r   r   r   r   r   r   r   r\   rN   rO   s   @r   rb   rb      sd    .F )$	(1
"  %N>$8$(" r   rb   c                      e Zd ZdddZy)BaseObserverSubclassCallablec                     y r   r   rG   s     r   __call__z%BaseObserverSubclassCallable.__call__  s    r   N).)r=   floatreturnrb   )r   r   r   r   r   r   r   r   r     s    r   r   )
__future__r   rX   re   pathlibr   watchdog.utilsr   r   watchdog.utils.bricksr   rM   r`   r
   r   r6   rQ   rb   r   r   r   r   <module>r      sv     #    / 2  ! (` (`X@,: @,F8j 8vl ? l ^8 r   