
    "d@                       d Z ddlmZ ddlZddlZddlZddlmZ dZ	dZ
dZdZd	Zd
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 G d de      Z G d de      Z G d de      Z G d de      Z G d de      Z G d d e      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d+ Zd, Z y)-un  
:module: watchdog.events
:synopsis: File system events and event handlers.
:author: yesudeep@google.com (Yesudeep Mangalapilly)
:author: contact@tiger-222.fr (Mickaël Schoentgen)

Event Classes
-------------
.. autoclass:: FileSystemEvent
   :members:
   :show-inheritance:
   :inherited-members:

.. autoclass:: FileSystemMovedEvent
   :members:
   :show-inheritance:

.. autoclass:: FileMovedEvent
   :members:
   :show-inheritance:

.. autoclass:: DirMovedEvent
   :members:
   :show-inheritance:

.. autoclass:: FileModifiedEvent
   :members:
   :show-inheritance:

.. autoclass:: DirModifiedEvent
   :members:
   :show-inheritance:

.. autoclass:: FileCreatedEvent
   :members:
   :show-inheritance:

.. autoclass:: FileClosedEvent
   :members:
   :show-inheritance:

.. autoclass:: FileOpenedEvent
   :members:
   :show-inheritance:

.. autoclass:: DirCreatedEvent
   :members:
   :show-inheritance:

.. autoclass:: FileDeletedEvent
   :members:
   :show-inheritance:

.. autoclass:: DirDeletedEvent
   :members:
   :show-inheritance:


Event Handler Classes
---------------------
.. autoclass:: FileSystemEventHandler
   :members:
   :show-inheritance:

.. autoclass:: PatternMatchingEventHandler
   :members:
   :show-inheritance:

.. autoclass:: RegexMatchingEventHandler
   :members:
   :show-inheritance:

.. autoclass:: LoggingEventHandler
   :members:
   :show-inheritance:

    )annotationsN)match_any_pathsmoveddeletedcreatedmodifiedclosedopenedc                  f    e Zd ZdZdZ	 dZ	 dZ	 d Zed        Z	d Z
d Zed        Zd	 Zd
 Zd Zy)FileSystemEventa
  
    Immutable type that represents a file system event that is triggered
    when a change occurs on the monitored file system.

    All FileSystemEvent objects are required to be immutable and hence
    can be used as keys in dictionaries or be added to sets.
     Fc                    || _         y N	_src_path)selfsrc_paths     1/usr/lib/python3/dist-packages/watchdog/events.py__init__zFileSystemEvent.__init__   s	    !    c                    | j                   S )z@Source path of the file system object that triggered this event.r   r   s    r   r   zFileSystemEvent.src_path   s     ~~r   c                "    | j                         S r   )__repr__r   s    r   __str__zFileSystemEvent.__str__   s    }}r   c           	         dt        |       j                   d| j                   d| j                  d| j                   d	S )N<z: event_type=z, src_path=, is_directory=>)type__name__
event_typer   is_directoryr   s    r   r   zFileSystemEvent.__repr__   sI    T
##$M$//1B C(8I8I7J!M	
r   c                H    | j                   | j                  | j                  fS r   )r"   r   r#   r   s    r   keyzFileSystemEvent.key   s    0A0ABBr   c                4    | j                   |j                   k(  S r   r%   r   events     r   __eq__zFileSystemEvent.__eq__       xx599$$r   c                4    | j                   |j                   k7  S r   r'   r(   s     r   __ne__zFileSystemEvent.__ne__   r+   r   c                ,    t        | j                        S r   )hashr%   r   s    r   __hash__zFileSystemEvent.__hash__   s    DHH~r   N)r!   
__module____qualname____doc__r"   r#   is_syntheticr   propertyr   r   r   r%   r*   r-   r0    r   r   r   r   n   sk     J,LEL"  
 C C%%r   r   c                  L     e Zd ZdZeZ fdZed        Zed        Z	d Z
 xZS )FileSystemMovedEventzJ
    File system event representing any kind of file system movement.
    c                2    t         |   |       || _        y r   )superr   
_dest_path)r   r   	dest_path	__class__s      r   r   zFileSystemMovedEvent.__init__   s    "#r   c                    | j                   S )z'The destination path of the move event.)r;   r   s    r   r<   zFileSystemMovedEvent.dest_path   s     r   c                ^    | j                   | j                  | j                  | j                  fS r   )r"   r   r<   r#   r   s    r   r%   zFileSystemMovedEvent.key   s#    @Q@QRRr   c           	         dt        |       j                   d| j                  d| j                  d| j                   d	S )Nr   z: src_path=z, dest_path=r   r   )r    r!   r   r<   r#   r   s    r   r   zFileSystemMovedEvent.__repr__   sI    T
##$K/@ A*/$:K:K9LAO	
r   )r!   r1   r2   r3   EVENT_TYPE_MOVEDr"   r   r5   r<   r%   r   __classcell__r=   s   @r   r8   r8      sC     "J$  
 S S
r   r8   c                      e Zd ZdZeZy)FileDeletedEventz@File system event representing file deletion on the file system.N)r!   r1   r2   r3   EVENT_TYPE_DELETEDr"   r6   r   r   rE   rE      
    J#Jr   rE   c                      e Zd ZdZeZy)FileModifiedEventzDFile system event representing file modification on the file system.N)r!   r1   r2   r3   EVENT_TYPE_MODIFIEDr"   r6   r   r   rI   rI      s
    N$Jr   rI   c                      e Zd ZdZeZy)FileCreatedEventz@File system event representing file creation on the file system.N)r!   r1   r2   r3   EVENT_TYPE_CREATEDr"   r6   r   r   rL   rL      rG   r   rL   c                      e Zd ZdZy)FileMovedEventz@File system event representing file movement on the file system.N)r!   r1   r2   r3   r6   r   r   rO   rO      s    Jr   rO   c                      e Zd ZdZeZy)FileClosedEvent=File system event representing file close on the file system.N)r!   r1   r2   r3   EVENT_TYPE_CLOSEDr"   r6   r   r   rQ   rQ      
    G"Jr   rQ   c                      e Zd ZdZeZy)FileOpenedEventrR   N)r!   r1   r2   r3   EVENT_TYPE_OPENEDr"   r6   r   r   rV   rV      rT   r   rV   c                      e Zd ZdZeZdZy)DirDeletedEventzEFile system event representing directory deletion on the file system.TN)r!   r1   r2   r3   rF   r"   r#   r6   r   r   rY   rY          O#JLr   rY   c                      e Zd ZdZeZdZy)DirModifiedEventzS
    File system event representing directory modification on the file system.
    TN)r!   r1   r2   r3   rJ   r"   r#   r6   r   r   r\   r\      s     %JLr   r\   c                      e Zd ZdZeZdZy)DirCreatedEventzEFile system event representing directory creation on the file system.TN)r!   r1   r2   r3   rM   r"   r#   r6   r   r   r^   r^      rZ   r   r^   c                      e Zd ZdZdZy)DirMovedEventzEFile system event representing directory movement on the file system.TN)r!   r1   r2   r3   r#   r6   r   r   r`   r`      s
    OLr   r`   c                  @    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
d	 Zy
)FileSystemEventHandlerzL
    Base file system event handler that you can override methods from.
    c                   | j                  |       	 t        | j                  t        | j                  t
        | j                  t        | j                  t        | j                  t        | j                  i|j                     |       y)Dispatches events to the appropriate methods.

        :param event:
            The event object representing the file system event.
        :type event:
            :class:`FileSystemEvent`
        N)on_any_eventrM   
on_createdrF   
on_deletedrJ   on_modifiedrA   on_movedrS   	on_closedrW   	on_openedr"   r(   s     r   dispatchzFileSystemEventHandler.dispatch  sn     	% 	!1!1dmmt~~t~~	
 

	 "	#r   c                     y)zCatch-all event handler.

        :param event:
            The event object representing the file system event.
        :type event:
            :class:`FileSystemEvent`
        Nr6   r(   s     r   re   z#FileSystemEventHandler.on_any_event      r   c                     y)zCalled when a file or a directory is moved or renamed.

        :param event:
            Event representing file/directory movement.
        :type event:
            :class:`DirMovedEvent` or :class:`FileMovedEvent`
        Nr6   r(   s     r   ri   zFileSystemEventHandler.on_moved&  rn   r   c                     y)zCalled when a file or directory is created.

        :param event:
            Event representing file/directory creation.
        :type event:
            :class:`DirCreatedEvent` or :class:`FileCreatedEvent`
        Nr6   r(   s     r   rf   z!FileSystemEventHandler.on_created/  rn   r   c                     y)zCalled when a file or directory is deleted.

        :param event:
            Event representing file/directory deletion.
        :type event:
            :class:`DirDeletedEvent` or :class:`FileDeletedEvent`
        Nr6   r(   s     r   rg   z!FileSystemEventHandler.on_deleted8  rn   r   c                     y)zCalled when a file or directory is modified.

        :param event:
            Event representing file/directory modification.
        :type event:
            :class:`DirModifiedEvent` or :class:`FileModifiedEvent`
        Nr6   r(   s     r   rh   z"FileSystemEventHandler.on_modifiedA  rn   r   c                     y)zCalled when a file opened for writing is closed.

        :param event:
            Event representing file closing.
        :type event:
            :class:`FileClosedEvent`
        Nr6   r(   s     r   rj   z FileSystemEventHandler.on_closedJ  rn   r   c                     y)zCalled when a file is opened.

        :param event:
            Event representing file opening.
        :type event:
            :class:`FileOpenedEvent`
        Nr6   r(   s     r   rk   z FileSystemEventHandler.on_openedS  rn   r   N)r!   r1   r2   r3   rl   re   ri   rf   rg   rh   rj   rk   r6   r   r   rb   rb     s/    #$r   rb   c                  v     e Zd ZdZ	 	 	 	 d fd	Zed        Zed        Zed        Zed        Z	 fdZ
 xZS )	PatternMatchingEventHandlerzR
    Matches given patterns with file paths associated with occurring events.
    c                Z    t         |           || _        || _        || _        || _        y r   )r:   r   	_patterns_ignore_patterns_ignore_directories_case_sensitive)r   patternsignore_patternsignore_directoriescase_sensitiver=   s        r   r   z$PatternMatchingEventHandler.__init__b  s/     	! /#5 -r   c                    | j                   S )zM
        (Read-only)
        Patterns to allow matching event paths.
        )rx   r   s    r   r|   z$PatternMatchingEventHandler.patternsp  s     ~~r   c                    | j                   S )zN
        (Read-only)
        Patterns to ignore matching event paths.
        )ry   r   s    r   r}   z+PatternMatchingEventHandler.ignore_patternsx  s     $$$r   c                    | j                   S ze
        (Read-only)
        ``True`` if directories should be ignored; ``False`` otherwise.
        rz   r   s    r   r~   z.PatternMatchingEventHandler.ignore_directories       '''r   c                    | j                   S z~
        (Read-only)
        ``True`` if path names should be matched sensitive to case; ``False``
        otherwise.
        r{   r   s    r   r   z*PatternMatchingEventHandler.case_sensitive       ###r   c                   | j                   r|j                  ryg }t        |d      r.|j                  t	        j
                  |j                               |j                  r.|j                  t	        j
                  |j                               t        || j                  | j                  | j                        rt        | 5  |       yy)rd   Nr<   )included_patternsexcluded_patternsr   )r~   r#   hasattrappendosfsdecoder<   r   r   r|   r}   r   r:   rl   r   r)   pathsr=   s      r   rl   z$PatternMatchingEventHandler.dispatch  s     ""u'9'95+&LLU__56>>LLU^^45"mm"22..	
 GU#
r   NNFF)r!   r1   r2   r3   r   r5   r|   r}   r~   r   rl   rB   rC   s   @r   rv   rv   ]  su      .   % % ( ( $ $$ $r   rv   c                  v     e Zd ZdZ	 	 	 	 d fd	Zed        Zed        Zed        Zed        Z	 fdZ
 xZS )	RegexMatchingEventHandlerzQ
    Matches given regexes with file paths associated with occurring events.
    c                "   t         |           |dg}nt        |t              r|g}|g }|rQ|D cg c]  }t	        j
                  |       c}| _        |D cg c]  }t	        j
                  |       c}| _        nn|D cg c]&  }t	        j
                  |t        j                        ( c}| _        |D cg c]&  }t	        j
                  |t        j                        ( c}| _        || _	        || _
        y c c}w c c}w c c}w c c}w )Nz.*)r:   r   
isinstancestrrecompile_regexes_ignore_regexesIrz   r{   )r   regexesignore_regexesr~   r   rr=   s         r   r   z"RegexMatchingEventHandler.__init__  s     	?gG%iG!N4;<qRZZ]<DM;I#JaBJJqM#JD :ABQRZZ2440BDMAO#PABJJq"$$$7#PD #5 - =#JB#Ps   C=D+D;+Dc                    | j                   S )zL
        (Read-only)
        Regexes to allow matching event paths.
        )r   r   s    r   r   z!RegexMatchingEventHandler.regexes  s     }}r   c                    | j                   S )zM
        (Read-only)
        Regexes to ignore matching event paths.
        )r   r   s    r   r   z(RegexMatchingEventHandler.ignore_regexes  s     ###r   c                    | j                   S r   r   r   s    r   r~   z,RegexMatchingEventHandler.ignore_directories  r   r   c                    | j                   S r   r   r   s    r   r   z(RegexMatchingEventHandler.case_sensitive  r   r   c                   | j                   r|j                  ryg t        |d      r.j                  t	        j
                  |j                               |j                  r.j                  t	        j
                  |j                               t        fd| j                  D              ryt        fd| j                  D              rt        | 1  |       yy)rd   Nr<   c              3  N   K   | ]  }D ]  }|j                  |         y wr   match.0r   pr   s      r   	<genexpr>z5RegexMatchingEventHandler.dispatch.<locals>.<genexpr>  s%     FaF1qwwqzFzF   "%c              3  N   K   | ]  }D ]  }|j                  |         y wr   r   r   s      r   r   z5RegexMatchingEventHandler.dispatch.<locals>.<genexpr>  s%     ?a?Aqwwqz?z?r   )r~   r#   r   r   r   r   r<   r   anyr   r   r:   rl   r   s     @r   rl   z"RegexMatchingEventHandler.dispatch  s     ""u'9'95+&LLU__56>>LLU^^45F4#6#6FF?4<<??GU# @r   r   )r!   r1   r2   r3   r   r5   r   r   r~   r   rl   rB   rC   s   @r   r   r     su      .0   $ $ ( ( $ $$ $r   r   c                  L     e Zd ZdZd fd	Z fdZ fdZ fdZ fdZ xZ	S )LoggingEventHandlerzLogs all the events captured.c                T    t         |           |xs t        j                  | _        y r   )r:   r   loggingrootlogger)r   r   r=   s     r   r   zLoggingEventHandler.__init__  s    ,r   c                    t         |   |       |j                  rdnd}| j                  j	                  d||j
                  |j                         y )N	directoryfilezMoved %s: from %s to %s)r:   ri   r#   r   infor   r<   r   r)   whatr=   s      r   ri   zLoggingEventHandler.on_moved	  sB    #00{f%tU^^U__	
r   c                    t         |   |       |j                  rdnd}| j                  j	                  d||j
                         y )Nr   r   zCreated %s: %s)r:   rf   r#   r   r   r   r   s      r   rf   zLoggingEventHandler.on_created  :    5!#00{f)4@r   c                    t         |   |       |j                  rdnd}| j                  j	                  d||j
                         y )Nr   r   zDeleted %s: %s)r:   rg   r#   r   r   r   r   s      r   rg   zLoggingEventHandler.on_deleted  r   r   c                    t         |   |       |j                  rdnd}| j                  j	                  d||j
                         y )Nr   r   zModified %s: %s)r:   rh   r#   r   r   r   r   s      r   rh   zLoggingEventHandler.on_modified  s:    E"#00{f*D%..Ar   r   )
r!   r1   r2   r3   r   ri   rf   rg   rh   rB   rC   s   @r   r   r     s(    '-

AAB Br   r   c              #    K   t        j                  |      D ]  \  }}}|D ]O  }t         j                  j                  ||      }| r|j	                  ||       nd}t        ||      }d|_        | Q |D ]O  }	t         j                  j                  ||	      }| r|j	                  ||       nd}t        ||      }
d|
_        |
 Q  yw)a  Generates an event list of :class:`DirMovedEvent` and
    :class:`FileMovedEvent` objects for all the files and directories within
    the given moved directory that were moved along with the directory.

    :param src_dir_path:
        The source path of the moved directory.
    :param dest_dir_path:
        The destination path of the moved directory.
    :returns:
        An iterable of file system events of type :class:`DirMovedEvent` and
        :class:`FileMovedEvent`.
    NT)r   walkpathjoinreplacer`   r4   rO   )src_dir_pathdest_dir_pathr   directories	filenamesr   	full_pathrenamed_pathdir_moved_eventfilenamefile_moved_events              r   generate_sub_moved_eventsr   $  s      )+(> #$k9$ 	"IT95IBN	!!->TX  ,L)DO+/O(!!	" " 	#HT84IBN	!!->TX   .lIF,0)""	##s   CC
c              #  4  K   t        j                  |       D ]|  \  }}}|D ]6  }t        t         j                  j	                  ||            }d|_        | 8 |D ]6  }t        t         j                  j	                  ||            }d|_        | 8 ~ yw)a  Generates an event list of :class:`DirCreatedEvent` and
    :class:`FileCreatedEvent` objects for all the files and directories within
    the given moved directory that were moved along with the directory.

    :param src_dir_path:
        The source path of the created directory.
    :returns:
        An iterable of file system events of type :class:`DirCreatedEvent` and
        :class:`FileCreatedEvent`.
    TN)r   r   r^   r   r   r4   rL   )r   r   r   r   r   dir_created_eventr   file_created_events           r   generate_sub_created_eventsr   D  s      )+(= %$k9$ 	$I /T90M N-1*##	$ " 	%H!1"'',,tX2N!O.2+$$	%%s   BB)!r3   
__future__r   r   os.pathr   r   watchdog.utils.patternsr   rA   rF   rM   rJ   rS   rW   r   r8   rE   rI   rL   rO   rQ   rV   rY   r\   r^   r`   rb   rv   r   r   r   r   r6   r   r   <module>r      s   L\ #   	 3       4 4n
? 
>$ $% %$ $K) K#o ##o #o  o ( T TnK$"8 K$\S$ 6 S$l B0  BF#@%r   