
    "d]                       d dl mZ 	 d dlZd dlZd dl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mZmZmZmZmZmZmZ d dlmZmZmZmZ d dlmZ d dlmZ dZd	Z  ejB                         re Z"nejF                  ejH                  z  Z"ejJ                  Z&ejN                  ejP                  z  ejR                  z  Z*ejV                  ejX                  z  ejZ                  z  ej\                  z  ej^                  z  ej`                  z  ejb                  z  Z2d
 Z3d Z4d Z5d Z6d Z7 G d d      Z8 G d d      Z9 G d de      Z: G d de      Z;y)    )annotationsN)S_ISDIR)EVENT_TYPE_CREATEDEVENT_TYPE_DELETEDEVENT_TYPE_MOVEDDirCreatedEventDirDeletedEventDirModifiedEventDirMovedEventFileCreatedEventFileDeletedEventFileModifiedEventFileMovedEventgenerate_sub_moved_events)DEFAULT_EMITTER_TIMEOUTDEFAULT_OBSERVER_TIMEOUTBaseObserverEventEmitter)platform)DirectorySnapshoti   i   c                z    t         j                  j                  t         j                  j                  |             S N)ospathabspathnormpath)r   s    ;/usr/lib/python3/dist-packages/watchdog/observers/kqueue.pyabsolute_pathr   ~   s$    77??277++D122    c                <    | j                   t        j                  z  S )z8Determines whether the given kevent represents deletion.)fflagsselectKQ_NOTE_DELETEkevs    r   
is_deletedr&          ::----r   c                j    | j                   }|t        j                  z  xs |t        j                  z  S )z<Determines whether the given kevent represents modification.)r!   r"   KQ_NOTE_EXTENDKQ_NOTE_WRITE)r%   r!   s     r   is_modifiedr+      s,    ZZFV***N9M9M0MNr   c                <    | j                   t        j                  z  S )zFDetermines whether the given kevent represents attribute modification.)r!   r"   KQ_NOTE_ATTRIBr$   s    r   is_attrib_modifiedr.      r'   r   c                <    | j                   t        j                  z  S )z8Determines whether the given kevent represents movement.)r!   r"   KQ_NOTE_RENAMEr$   s    r   
is_renamedr1      r'   r   c                  r    e Zd ZdZd Zed        Zed        Zd Zd Z	d Z
d Zd	 Zd
 Zd Zd Zd Zd Zy)KeventDescriptorSetz3
    Thread-safe kevent descriptor collection.
    c                    t               | _        t               | _        t               | _        t               | _        t        j                         | _	        y r   )
set_descriptorsdict_descriptor_for_path_descriptor_for_fdlist_kevents	threadingLock_lockselfs    r   __init__zKeventDescriptorSet.__init__   s=    E %)F! #'& ^^%
r   c                ^    | j                   5  | j                  cddd       S # 1 sw Y   yxY w)z,
        List of kevents monitored.
        N)r>   r;   r?   s    r   keventszKeventDescriptorSet.kevents   s'    
 ZZ 	!==	! 	! 	!s   #,c                    | j                   5  t        | j                  j                               cddd       S # 1 sw Y   yxY w)zD
        List of paths for which kevents have been created.
        N)r>   r:   r8   keysr?   s    r   pathszKeventDescriptorSet.paths   s7    
 ZZ 	:116689	: 	: 	:s	   #:Ac                d    | j                   5  | j                  |   cddd       S # 1 sw Y   yxY w)z
        Given a file descriptor, returns the kevent descriptor object
        for it.

        :param fd:
            OS file descriptor.
        :type fd:
            ``int``
        :returns:
            A :class:`KeventDescriptor` object.
        N)r>   r9   )r@   fds     r   
get_for_fdzKeventDescriptorSet.get_for_fd   s.     ZZ 	/**2.	/ 	/ 	/s   &/c                ~    | j                   5  t        |      }| j                  |      cddd       S # 1 sw Y   yxY w)z
        Obtains a :class:`KeventDescriptor` object for the specified path.

        :param path:
            Path for which the descriptor will be obtained.
        N)r>   r   _getr@   r   s     r   getzKeventDescriptorSet.get   s5     ZZ 	# &D99T?	# 	# 	#   3<c                ~    | j                   5  t        |      }| j                  |      cddd       S # 1 sw Y   yxY w)z
        Determines whether a :class:`KeventDescriptor has been registered
        for the specified path.

        :param path:
            Path for which the descriptor will be obtained.
        N)r>   r   	_has_pathrL   s     r   __contains__z KeventDescriptorSet.__contains__   s6     ZZ 	( &D>>$'	( 	( 	(rN   c                    | j                   5  t        |      }| j                  |      s| j                  t	        ||             ddd       y# 1 sw Y   yxY w)an  
        Adds a :class:`KeventDescriptor` to the collection for the given
        path.

        :param path:
            The path for which a :class:`KeventDescriptor` object will be
            added.
        :param is_directory:
            ``True`` if the path refers to a directory; ``False`` otherwise.
        :type is_directory:
            ``bool``
        N)r>   r   rP   _add_descriptorKeventDescriptorr@   r   is_directorys      r   addzKeventDescriptorSet.add   sP     ZZ 	K &D>>$'$$%5dL%IJ	K 	K 	Ks   8AAc                    | j                   5  t        |      }| j                  |      r | j                  | j	                  |             ddd       y# 1 sw Y   yxY w)z
        Removes the :class:`KeventDescriptor` object for the given path
        if it already exists.

        :param path:
            Path for which the :class:`KeventDescriptor` object will be
            removed.
        N)r>   r   rP   _remove_descriptorrK   rL   s     r   removezKeventDescriptorSet.remove   sL     ZZ 	9 &D~~d#''		$8	9 	9 	9s   =AAc                2   | j                   5  | j                  D ]  }|j                           | j                  j                          | j                  j                          | j
                  j                          g | _        ddd       y# 1 sw Y   yxY w)zH
        Clears the collection and closes all open descriptors.
        N)r>   r6   closeclearr9   r8   r;   r@   
descriptors     r   r]   zKeventDescriptorSet.clear  s~     ZZ 	"// #
  "###%##))+%%++-DM	 	 	s   A7BBc                     | j                   |   S )z-Returns a kevent descriptor for a given path.r8   rL   s     r   rK   zKeventDescriptorSet._get  s    ((..r   c                    || j                   v S )zoDetermines whether a :class:`KeventDescriptor` for the specified
        path exists already in the collection.ra   rL   s     r   rP   zKeventDescriptorSet._has_path  s     t0000r   c                    | j                   j                  |       | j                  j                  |j                         || j
                  |j                  <   || j                  |j                  <   y)z
        Adds a descriptor to the collection.

        :param descriptor:
            An instance of :class:`KeventDescriptor` to be added.
        N)	r6   rW   r;   appendkeventr8   r   r9   rH   r^   s     r   rS   z#KeventDescriptorSet._add_descriptor  sX     	j)Z../5?!!*//21;
.r   c                    | j                   j                  |       | j                  |j                  = | j                  |j
                  = | j                  j                  |j                         |j                          y)z
        Removes a descriptor from the collection.

        :param descriptor:
            An instance of :class:`KeventDescriptor` to be removed.
        N)	r6   rZ   r9   rH   r8   r   r;   re   r\   r^   s     r   rY   z&KeventDescriptorSet._remove_descriptor'  s`     	  ,##JMM2%%joo6Z../r   N)__name__
__module____qualname____doc__rA   propertyrC   rF   rI   rM   rQ   rW   rZ   r]   rK   rP   rS   rY    r   r   r3   r3      sg    & ! ! : :/	#
(K$9
/1

<r   r3   c                      e Zd ZdZd Zed        Zed        Zed        Zed        Z	d Z
ed        Zd	 Zd
 Zd Zd Zy)rT   a  
    A kevent descriptor convenience data structure to keep together:

        * kevent
        * directory status
        * path
        * file descriptor

    :param path:
        Path string for which a kevent descriptor will be created.
    :param is_directory:
        ``True`` if the path refers to a directory; ``False`` otherwise.
    :type is_directory:
        ``bool``
    c                    t        |      | _        || _        t        j                  |t
              | _        t        j                  | j                  t        t        t              | _        y )N)filterflagsr!   )r   _path_is_directoryr   openWATCHDOG_OS_OPEN_FLAGS_fdr"   re   WATCHDOG_KQ_FILTERWATCHDOG_KQ_EV_FLAGSWATCHDOG_KQ_FFLAGS_kevrU   s      r   rA   zKeventDescriptor.__init__G  sI    "4(
)774!78MMHH%&%	
	r   c                    | j                   S )z-OS file descriptor for the kevent descriptor.)ru   r?   s    r   rH   zKeventDescriptor.fdR  s     xxr   c                    | j                   S )z/The path associated with the kevent descriptor.)rq   r?   s    r   r   zKeventDescriptor.pathW  s     zzr   c                    | j                   S )z8The kevent object associated with the kevent descriptor.)ry   r?   s    r   re   zKeventDescriptor.kevent\  s     yyr   c                    | j                   S )z}Determines whether the kevent descriptor refers to a directory.

        :returns:
            ``True`` or ``False``
        )rr   r?   s    r   rV   zKeventDescriptor.is_directorya  s     !!!r   c                b    	 t        j                  | j                         y# t        $ r Y yw xY w)zQ
        Closes the file descriptor associated with a kevent descriptor.
        N)r   r\   rH   OSErrorr?   s    r   r\   zKeventDescriptor.closej  s)    	HHTWW 		s   " 	..c                2    | j                   | j                  fS r   )r   rV   r?   s    r   keyzKeventDescriptor.keys  s    		4,,--r   c                4    | j                   |j                   k(  S r   r   r^   s     r   __eq__zKeventDescriptor.__eq__w      xx:>>))r   c                4    | j                   |j                   k7  S r   r   r^   s     r   __ne__zKeventDescriptor.__ne__z  r   r   c                ,    t        | j                        S r   )hashr   r?   s    r   __hash__zKeventDescriptor.__hash__}  s    DHH~r   c                h    dt        |       j                   d| j                  d| j                   dS )N<z: path=z, is_directory=>)typerg   r   rV   r?   s    r   __repr__zKeventDescriptor.__repr__  s4    4:&&'wtyym?4K\K\J]]^__r   N)rg   rh   ri   rj   rA   rk   rH   r   re   rV   r\   r   r   r   r   r   rl   r   r   rT   rT   5  s     	
       " " . .**`r   rT   c                  t     e Zd ZdZeej                  f fd	Zd Zd Z	d Z
d Zd Zd Zdd	Zd
 Zd Z xZS )KqueueEmittera  
    kqueue(2)-based event emitter.

    .. ADMONITION:: About ``kqueue(2)`` behavior and this implementation

              ``kqueue(2)`` monitors file system events only for
              open descriptors, which means, this emitter does a lot of
              book-keeping behind the scenes to keep track of open
              descriptors for every entry in the monitored directory tree.

              This also means the number of maximum open file descriptors
              on your system must be increased **manually**.
              Usually, issuing a call to ``ulimit`` should suffice::

                  ulimit -n 1024

              Ensure that you pick a number that is larger than the
              number of files you expect to be monitored.

              ``kqueue(2)`` does not provide enough information about the
              following things:

              * The destination path of a file or directory that is renamed.
              * Creation of a file or directory within a directory; in this
                case, ``kqueue(2)`` only indicates a modified event on the
                parent directory.

              Therefore, this emitter takes a snapshot of the directory
              tree when ``kqueue(2)`` detects a change on the file system
              to be able to determine the above information.

    :param event_queue:
        The event queue to fill with events.
    :param watch:
        A watch object representing the directory to monitor.
    :type watch:
        :class:`watchdog.observers.api.ObservedWatch`
    :param timeout:
        Read events blocking timeout (in seconds).
    :type timeout:
        ``float``
    :param stat: stat function. See ``os.stat`` for details.
    c                   t         |   |||       t        j                         | _        t        j                         | _        t               | _	        | ffd	}t        |j                  |j                  |      | _        y )Nc                b     |       }|j                  | t        |j                               |S r   )_register_keventr   st_mode)r   r@   	stat_infostats      r   custom_statz+KqueueEmitter.__init__.<locals>.custom_stat  s-    T
I!!$	0A0A(BCr   )	recursiver   )superrA   r"   kqueue_kqr<   RLockr>   r3   r6   r   r   is_recursive	_snapshot)r@   event_queuewatchtimeoutr   r   	__class__s       ` r   rA   zKqueueEmitter.__init__  se     	eW5==?__&
 01#' 	
 +JJ%"4"4;
r   c                    	 | j                   j                  ||       y# t        $ rL}|j                  t        j                  k(  rn$|j                  t        j
                  k(  rn Y d}~yY d}~yd}~ww xY w)a4  
        Registers a kevent descriptor for the given path.

        :param path:
            Path for which a kevent descriptor will be created.
        :param is_directory:
            ``True`` if the path refers to a directory; ``False`` otherwise.
        :type is_directory:
            ``bool``
        N)r6   rW   r   errnoENOENT
EOPNOTSUPP)r@   r   rV   es       r   r   zKqueueEmitter._register_kevent  sh    	!!$5 	ww%,,&$ E,,,
    '	s    	A4=A//A4c                :    | j                   j                  |       y)z
        Convenience function to close the kevent descriptor for a
        specified kqueue-monitored path.

        :param path:
            Path for which the kevent descriptor will be closed.
        N)r6   rZ   rL   s     r   _unregister_keventz KqueueEmitter._unregister_kevent  s     	  &r   c                   t        j                  | |       |j                  t        k(  r'| j	                  |j
                  |j                         y|j                  t        k(  rB| j                  |j
                         | j	                  |j                  |j                         y|j                  t        k(  r| j                  |j
                         yy)z
        Handles queueing a single event object.

        :param event:
            An instance of :class:`watchdog.events.FileSystemEvent`
            or a subclass.
        N)r   queue_event
event_typer   r   src_pathrV   r   r   	dest_pathr   )r@   events     r   r   zKqueueEmitter.queue_event  s     	  u-11!!%..%2D2DE!11##ENN3!!%//53E3EF!33##ENN3 4r   c              #  f  K   | j                   j                  |j                        }|j                  }t	        |      r(| j                  ||j                  ||      D ]  }|  yt        |      r(|j                  rt        |       yt        |       yt        |      rX|j                  r=| j                  j                  s| j                  j                  |k(  rt        |       yt        |       yyt        |      r(|j                  rt        |       yt        |       yyw)a  
        Generate events from the kevent list returned from the call to
        :meth:`select.kqueue.control`.

        .. NOTE:: kqueue only tells us about deletions, file modifications,
                  attribute modifications. The other events, namely,
                  file creation, directory modification, file rename,
                  directory rename, directory creation, etc. are
                  determined by comparing directory snapshots.
        N)r6   rI   identr   r1   _gen_renamed_eventsrV   r.   r
   r   r+   r   r   r&   r	   r   )r@   r%   ref_snapshotnew_snapshotr_   r   r   s          r   _gen_kqueue_eventsz KqueueEmitter._gen_kqueue_events  s     &&11#))<
??c? 11*11<    $&&&x00'11&&::**djjoo.I
 +844'11 /J _&&%h//&x00	 s   D/D1c                R    t        t        j                  j                  |            S )zR
        Helper to generate a DirModifiedEvent on the parent of src_path.
        )r
   r   r   dirname)r@   r   s     r   _parent_dir_modifiedz"KqueueEmitter._parent_dir_modified;  s       9::r   c              #  H  K   	 |j                  |      }|j                  |      }|t        |      }|rt        ||      }| nt        ||       | j                  |       | j                  |       |r-| j                  j                  rt        ||      D ]  }|  yyy|rt        |       nt        |       | j                  |       y# t        $ r; |rt        |       t        |       Y yt	        |       t        |       Y yw xY ww)a  
        Compares information from two directory snapshots (one taken before
        the rename operation and another taken right after) to determine the
        destination path of the file system object renamed, and yields
        the appropriate events to be queued.
        N)inodeKeyErrorr   r	   r   r   r   r   r   r   r   r   r   r   )	r@   r   rV   r   r   f_inoder   r   	sub_events	            r   r   z!KqueueEmitter._gen_renamed_eventsA  s1    	"((2G !%%g.	 %i0I%h	:$Xy99++H55++I66 ::**%>x%S (	'( +  %h//&x00++H55S  	 %h//%h// 	 'x00&x00 	s4   D"C CD"%D D"DD"DD"c                l    | j                   j                  | j                  j                  t        |      S )z
        Reads events from a call to the blocking
        :meth:`select.kqueue.control()` method.

        :param timeout:
            Blocking timeout for reading events.
        :type timeout:
            ``float`` (seconds)
        )r   controlr6   rC   
MAX_EVENTS)r@   r   s     r   _read_eventszKqueueEmitter._read_eventsu  s)     xx 1 1 9 9:wOOr   c                   | j                   5  	 | j                  |      }|j                          t        | j                  j
                  | j                  j                        }| j                  }|| _        ||z
  }|j                  D ]  }| j                  t        |              |j                  D ]  }| j                  t        |              |j                  D ]  }| j                  t        |              |D ]+  }	| j                  |	||      D ]  }
| j                  |
        - 	 ddd       y# t         $ r(}|j"                  t"        j$                  k7  r Y d}~5d}~ww xY w# 1 sw Y   yxY w)z
        Queues events by reading them from a call to the blocking
        :meth:`select.kqueue.control()` method.

        :param timeout:
            Blocking timeout for reading events.
        :type timeout:
            ``float`` (seconds)
        N)r>   r   reverser   r   r   r   r   dirs_createdr   r   files_createdr   files_modifiedr   r   r   r   EBADF)r@   r   
event_listr   r   diff_eventsdirectory_createdfile_createdfile_modifiedr%   r   r   s               r   queue_eventszKqueueEmitter.queue_events  su    ZZ 	!..w7
""$  1JJOOTZZ%<%<   $~~!-*\9 *5)A)A I%$$_5F%GHI$/$=$= EL$$%5l%CDE%0%?%? GM$$%6}%EFG & 0C!%!8!8\<" 0 ((/00/	 	:  77ekk) *;	 	s5   E+DD7-E+7	E( E#E+#E((E++E4c                    | j                   5  | j                  j                          | j                  j	                          d d d        y # 1 sw Y   y xY wr   )r>   r6   r]   r   r\   r?   s    r   on_thread_stopzKqueueEmitter.on_thread_stop  s>    ZZ 	##%HHNN	 	 	s   5AAr   )rg   rh   ri   rj   r   r   r   rA   r   r   r   r   r   r   r   r   r   __classcell__r   s   @r   r   r     sL    *Z +B
()V'4*)1V;26h
P)Vr   r   c                  &     e Zd ZdZef fd	Z xZS )KqueueObserverzi
    Observer thread that schedules watching directories and dispatches
    calls to event handlers.
    c                0    t         |   t        |       y )N)emitter_classr   )r   rA   r   )r@   r   r   s     r   rA   zKqueueObserver.__init__  s    }gFr   )rg   rh   ri   rj   r   rA   r   r   s   @r   r   r     s    
  8 G Gr   r   )<
__future__r   r   r   os.pathr"   r<   r   r   watchdog.eventsr   r   r   r   r	   r
   r   r   r   r   r   r   watchdog.observers.apir   r   r   r   watchdog.utilsr   watchdog.utils.dirsnapshotr   r   	O_EVTONLY	is_darwinrt   O_RDONLY
O_NONBLOCKKQ_FILTER_VNODErv   	KQ_EV_ADDKQ_EV_ENABLEKQ_EV_CLEARrw   r#   r*   r)   r-   KQ_NOTE_LINKr0   KQ_NOTE_REVOKErx   r   r&   r+   r.   r1   r3   rT   r   r   rl   r   r   <module>r      si    #2h  	        q p # 8 
 	 8&[[2==8++ ''&*=*==@R@RR 
  	
   3.
O.
.
X XvL` L`^lL l^	G\ Gr   