o
    *ήcj?                     @   s<  d 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G d	d
 d
ZG dd deZG dd deZG dd deZG dd deZG dd deZG dd deZG dd deZG dd deZG dd deZG dd deZG dd  d ZG d!d" d"eZG d#d$ d$eZG d%d& d&eZd'd( Zd)d* ZdS )+u+  
: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:: 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:

    N)match_any_pathsmoveddeletedcreatedmodifiedclosedc                   @   sj   e Zd ZdZdZ	 dZ	 dZ	 dd Zedd Z	dd	 Z
d
d Zedd Zdd Zdd Zdd ZdS )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.
    NFc                 C   s
   || _ d S N	_src_path)selfsrc_path r   6/tmp/pip-target-vg8gfxp4/lib/python/watchdog/events.py__init__      
zFileSystemEvent.__init__c                 C      | j S )z@Source path of the file system object that triggered this event.r
   r   r   r   r   r         zFileSystemEvent.src_pathc                 C   s   |   S r	   )__repr__r   r   r   r   __str__   s   zFileSystemEvent.__str__c                 C      dt | jj| j| j| jd S )Nza<%(class_name)s: event_type=%(event_type)s, src_path=%(src_path)r, is_directory=%(is_directory)s>)
class_name
event_typer   is_directory)dict	__class____name__r   r   r   r   r   r   r   r      s   zFileSystemEvent.__repr__c                 C   s   | j | j| jfS r	   )r   r   r   r   r   r   r   key   s   zFileSystemEvent.keyc                 C   s   | j |j kS r	   r   r   eventr   r   r   __eq__      zFileSystemEvent.__eq__c                 C   s   | j |j kS r	   r   r    r   r   r   __ne__   r#   zFileSystemEvent.__ne__c                 C   s
   t | jS r	   )hashr   r   r   r   r   __hash__   r   zFileSystemEvent.__hash__)r   
__module____qualname____doc__r   r   is_syntheticr   propertyr   r   r   r   r"   r$   r&   r   r   r   r   r   i   s$    

r   c                       sD   e Zd ZdZeZ fddZedd Zedd Z	dd	 Z
  ZS )
FileSystemMovedEventzJ
    File system event representing any kind of file system movement.
    c                    s   t  | || _d S r	   )superr   
_dest_path)r   r   	dest_pathr   r   r   r      s   
zFileSystemMovedEvent.__init__c                 C   r   )z'The destination path of the move event.)r.   r   r   r   r   r/      r   zFileSystemMovedEvent.dest_pathc                 C   s   | j | j| j| jfS r	   )r   r   r/   r   r   r   r   r   r      s   zFileSystemMovedEvent.keyc                 C   r   )Nz_<%(class_name)s: src_path=%(src_path)r, dest_path=%(dest_path)r, is_directory=%(is_directory)s>)r   r   r/   r   )r   r   r   r   r/   r   r   r   r   r   r      s   zFileSystemMovedEvent.__repr__)r   r'   r(   r)   EVENT_TYPE_MOVEDr   r   r+   r/   r   r   __classcell__r   r   r0   r   r,      s    

r,   c                   @      e Zd ZdZeZdS )FileDeletedEventz@File system event representing file deletion on the file system.N)r   r'   r(   r)   EVENT_TYPE_DELETEDr   r   r   r   r   r4          r4   c                   @   r3   )FileModifiedEventzDFile system event representing file modification on the file system.N)r   r'   r(   r)   EVENT_TYPE_MODIFIEDr   r   r   r   r   r7      r6   r7   c                   @   r3   )FileCreatedEventz@File system event representing file creation on the file system.N)r   r'   r(   r)   EVENT_TYPE_CREATEDr   r   r   r   r   r9      r6   r9   c                   @   s   e Zd ZdZdS )FileMovedEventz@File system event representing file movement on the file system.N)r   r'   r(   r)   r   r   r   r   r;      s    r;   c                   @   r3   )FileClosedEventz=File system event representing file close on the file system.N)r   r'   r(   r)   EVENT_TYPE_CLOSEDr   r   r   r   r   r<      r6   r<   c                   @      e Zd ZdZeZdZdS )DirDeletedEventzEFile system event representing directory deletion on the file system.TN)r   r'   r(   r)   r5   r   r   r   r   r   r   r?          r?   c                   @   r>   )DirModifiedEventzS
    File system event representing directory modification on the file system.
    TN)r   r'   r(   r)   r8   r   r   r   r   r   r   rA      s    rA   c                   @   r>   )DirCreatedEventzEFile system event representing directory creation on the file system.TN)r   r'   r(   r)   r:   r   r   r   r   r   r   rB      r@   rB   c                   @   s   e Zd ZdZdZdS )DirMovedEventzEFile system event representing directory movement on the file system.TN)r   r'   r(   r)   r   r   r   r   r   rC      r6   rC   c                   @   sH   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	dd Z
dS )FileSystemEventHandlerzL
    Base file system event handler that you can override methods from.
    c              
   C   s:   |  | t| jt| jt| jt| jt	| j
i|j | dS )Dispatches events to the appropriate methods.

        :param event:
            The event object representing the file system event.
        :type event:
            :class:`FileSystemEvent`
        N)on_any_eventr:   
on_createdr5   
on_deletedr8   on_modifiedr1   on_movedr=   	on_closedr   r    r   r   r   dispatch  s   
zFileSystemEventHandler.dispatchc                 C      dS )zCatch-all event handler.

        :param event:
            The event object representing the file system event.
        :type event:
            :class:`FileSystemEvent`
        Nr   r    r   r   r   rF         z#FileSystemEventHandler.on_any_eventc                 C   rM   )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`
        Nr   r    r   r   r   rJ   !  rN   zFileSystemEventHandler.on_movedc                 C   rM   )zCalled when a file or directory is created.

        :param event:
            Event representing file/directory creation.
        :type event:
            :class:`DirCreatedEvent` or :class:`FileCreatedEvent`
        Nr   r    r   r   r   rG   *  rN   z!FileSystemEventHandler.on_createdc                 C   rM   )zCalled when a file or directory is deleted.

        :param event:
            Event representing file/directory deletion.
        :type event:
            :class:`DirDeletedEvent` or :class:`FileDeletedEvent`
        Nr   r    r   r   r   rH   3  rN   z!FileSystemEventHandler.on_deletedc                 C   rM   )zCalled when a file or directory is modified.

        :param event:
            Event representing file/directory modification.
        :type event:
            :class:`DirModifiedEvent` or :class:`FileModifiedEvent`
        Nr   r    r   r   r   rI   <  rN   z"FileSystemEventHandler.on_modifiedc                 C   rM   )zCalled when a file opened for writing is closed.

        :param event:
            Event representing file closing.
        :type event:
            :class:`FileClosedEvent`
        Nr   r    r   r   r   rK   E  rN   z FileSystemEventHandler.on_closedN)r   r'   r(   r)   rL   rF   rJ   rG   rH   rI   rK   r   r   r   r   rD     s    					rD   c                       b   e Zd ZdZ		d fdd	Zedd Zedd	 Zed
d Zedd Z	 fddZ
  ZS )PatternMatchingEventHandlerzR
    Matches given patterns with file paths associated with occurring events.
    NFc                    s&   t    || _|| _|| _|| _d S r	   )r-   r   	_patterns_ignore_patterns_ignore_directories_case_sensitive)r   patternsignore_patternsignore_directoriescase_sensitiver0   r   r   r   T  s
   

z$PatternMatchingEventHandler.__init__c                 C   r   )zM
        (Read-only)
        Patterns to allow matching event paths.
        )rQ   r   r   r   r   rU   ]     z$PatternMatchingEventHandler.patternsc                 C   r   )zN
        (Read-only)
        Patterns to ignore matching event paths.
        )rR   r   r   r   r   rV   e  rY   z+PatternMatchingEventHandler.ignore_patternsc                 C   r   ze
        (Read-only)
        ``True`` if directories should be ignored; ``False`` otherwise.
        rS   r   r   r   r   rW   m  rY   z.PatternMatchingEventHandler.ignore_directoriesc                 C   r   z~
        (Read-only)
        ``True`` if path names should be matched sensitive to case; ``False``
        otherwise.
        rT   r   r   r   r   rX   u     z*PatternMatchingEventHandler.case_sensitivec                    sr   | j r|jrdS g }t|dr|t|j |jr$|t|j t|| j	| j
| jdr7t | dS dS )rE   Nr/   )included_patternsexcluded_patternsrX   )rW   r   hasattrappendosfsdecoder/   r   r   rU   rV   rX   r-   rL   )r   r!   pathsr0   r   r   rL   ~  s   
z$PatternMatchingEventHandler.dispatchNNFF)r   r'   r(   r)   r   r+   rU   rV   rW   rX   rL   r2   r   r   r0   r   rP   O  s    	



rP   c                       rO   )RegexMatchingEventHandlerzQ
    Matches given regexes with file paths associated with occurring events.
    NFc                    s   t    |d u rdg}nt|tr|g}|d u rg }|r.dd |D | _dd |D | _ndd |D | _dd |D | _|| _|| _d S )Nz.*c                 S      g | ]}t |qS r   recompile.0rr   r   r   
<listcomp>      z6RegexMatchingEventHandler.__init__.<locals>.<listcomp>c                 S   rh   r   ri   rl   r   r   r   ro     rp   c                 S      g | ]	}t |t jqS r   rj   rk   Irl   r   r   r   ro         c                 S   rq   r   rr   rl   r   r   r   ro     rt   )r-   r   
isinstancestr_regexes_ignore_regexesrS   rT   )r   regexesignore_regexesrW   rX   r0   r   r   r     s   


z"RegexMatchingEventHandler.__init__c                 C   r   )zL
        (Read-only)
        Regexes to allow matching event paths.
        )rw   r   r   r   r   ry     rY   z!RegexMatchingEventHandler.regexesc                 C   r   )zM
        (Read-only)
        Regexes to ignore matching event paths.
        )rx   r   r   r   r   rz     rY   z(RegexMatchingEventHandler.ignore_regexesc                 C   r   rZ   r[   r   r   r   r   rW     rY   z,RegexMatchingEventHandler.ignore_directoriesc                 C   r   r\   r]   r   r   r   r   rX     r^   z(RegexMatchingEventHandler.case_sensitivec                    s   | j r|jrdS g  t|dr t|j |jr$ t|j t fdd| j	D r2dS t fdd| j
D rFt | dS dS )rE   Nr/   c                 3   $    | ]} D ]}| |V  qqd S r	   matchrm   rn   pre   r   r   	<genexpr>     " z5RegexMatchingEventHandler.dispatch.<locals>.<genexpr>c                 3   r{   r	   r|   r~   r   r   r   r     r   )rW   r   ra   rb   rc   rd   r/   r   anyrz   ry   r-   rL   r    r0   r   r   rL     s   
z"RegexMatchingEventHandler.dispatchrf   )r   r'   r(   r)   r   r+   ry   rz   rW   rX   rL   r2   r   r   r0   r   rg     s    



rg   c                       sR   e Zd ZdZd fdd	Z fddZ fddZ fd	d
Z fddZ  Z	S )LoggingEventHandlerzLogs all the events captured.Nc                    s   t    |p	tj| _d S r	   )r-   r   loggingrootlogger)r   r   r0   r   r   r     s   
zLoggingEventHandler.__init__c                    s4   t  | |jrdnd}| jd||j|j d S )N	directoryfilezMoved %s: from %s to %s)r-   rJ   r   r   infor   r/   r   r!   whatr0   r   r   rJ     s
   zLoggingEventHandler.on_movedc                    0   t  | |jrdnd}| jd||j d S )Nr   r   zCreated %s: %s)r-   rG   r   r   r   r   r   r0   r   r   rG        zLoggingEventHandler.on_createdc                    r   )Nr   r   zDeleted %s: %s)r-   rH   r   r   r   r   r   r0   r   r   rH     r   zLoggingEventHandler.on_deletedc                    r   )Nr   r   zModified %s: %s)r-   rI   r   r   r   r   r   r0   r   r   rI     r   zLoggingEventHandler.on_modifiedr	   )
r   r'   r(   r)   r   rJ   rG   rH   rI   r2   r   r   r0   r   r     s    r   c           
      c   s    t |D ]G\}}}|D ]}t j||}| r||| nd}t||}d|_|V  q|D ]}	t j||	}| r?||| nd}t||}d|_|V  q.qdS )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)rc   walkpathjoinreplacerC   r*   r;   )
src_dir_pathdest_dir_pathr   directories	filenamesr   	full_pathrenamed_pathr!   filenamer   r   r   generate_sub_moved_events	  s    

r   c                 c   sl    t | D ]-\}}}|D ]}tt j||}d|_|V  q|D ]}tt j||}d|_|V  q!qdS )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)rc   r   rB   r   r   r*   r9   )r   r   r   r   r   r!   r   r   r   r   generate_sub_created_events%  s   r   )r)   os.pathrc   r   rj   watchdog.utils.patternsr   r1   r5   r:   r8   r=   r   r,   r4   r7   r9   r;   r<   r?   rA   rB   rC   rD   rP   rg   r   r   r   r   r   r   r   <module>   s6   J;"		MGQ"