U
    ")3g=                     @  s  d Z ddlmZ ddlZddlZddlZddlmZm	Z	 ddl
mZ ddlmZ dZdZd	Zd
ZdZdZedd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&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/d0 Z$d1d2 Z%dS )3um  :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)	dataclassfield)Optional)match_any_pathsmovedZdeletedcreatedmodifiedclosedZopenedT)Zunsafe_hashc                   @  s`   e Zd ZU dZded< dZded< edddZded< edddZd	ed
< eddZ	d	ed< 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.
    strsrc_path 	dest_pathF)defaultinit
event_typeboolis_directory)r   is_syntheticN)
__name__
__module____qualname____doc____annotations__r   r   r   r   r    r   r   3/tmp/pip-unpacked-wheel-cstuilrs/watchdog/events.pyr   o   s   
r   c                   @  s   e Zd ZdZeZdS )FileSystemMovedEventz@File system event representing any kind of file system movement.N)r   r   r   r   EVENT_TYPE_MOVEDr   r   r   r   r   r      s   r   c                   @  s   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   r      s   r   c                   @  s   e Zd ZdZeZdS )FileModifiedEventzDFile system event representing file modification on the file system.N)r   r   r   r   EVENT_TYPE_MODIFIEDr   r   r   r   r   r!      s   r!   c                   @  s   e Zd ZdZeZdS )FileCreatedEventz@File system event representing file creation on the file system.N)r   r   r   r   EVENT_TYPE_CREATEDr   r   r   r   r   r#      s   r#   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                   @  s   e Zd ZdZeZdS )FileClosedEvent=File system event representing file close on the file system.N)r   r   r   r   EVENT_TYPE_CLOSEDr   r   r   r   r   r&      s   r&   c                   @  s   e Zd ZdZeZdS )FileOpenedEventr'   N)r   r   r   r   EVENT_TYPE_OPENEDr   r   r   r   r   r)      s   r)   c                   @  s   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   r    r   r   r   r   r   r   r+      s   r+   c                   @  s   e Zd ZdZeZdZdS )DirModifiedEventzIFile system event representing directory modification on the file system.TN)r   r   r   r   r"   r   r   r   r   r   r   r,      s   r,   c                   @  s   e Zd ZdZeZdZdS )DirCreatedEventzEFile system event representing directory creation on the file system.TN)r   r   r   r   r$   r   r   r   r   r   r   r-      s   r-   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   r.      s   r.   c                   @  s   e Zd ZdZdddddZdddddZdddd	d
ZdddddZdddddZdddddZ	dddddZ
dddddZdS )FileSystemEventHandlerzBBase file system event handler that you can override methods from.r   Noneeventreturnc                 C  s@   |  | t| j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_createdr    
on_deletedr"   on_modifiedr   on_movedr(   	on_closedr*   	on_openedr   selfr2   r   r   r   dispatch   s$    
      zFileSystemEventHandler.dispatchc                 C  s   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   r5      s    z#FileSystemEventHandler.on_any_eventc                 C  s   dS )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   r9      s    zFileSystemEventHandler.on_movedc                 C  s   dS )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   r6      s    z!FileSystemEventHandler.on_createdc                 C  s   dS )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   r7      s    z!FileSystemEventHandler.on_deletedc                 C  s   dS )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   r8     s    z"FileSystemEventHandler.on_modifiedc                 C  s   dS )zCalled when a file opened for writing is closed.

        :param event:
            Event representing file closing.
        :type event:
            :class:`FileClosedEvent`
        Nr   r<   r   r   r   r:     s    z FileSystemEventHandler.on_closedc                 C  s   dS )zCalled when a file is opened.

        :param event:
            Event representing file opening.
        :type event:
            :class:`FileOpenedEvent`
        Nr   r<   r   r   r   r;     s    z FileSystemEventHandler.on_openedN)r   r   r   r   r>   r5   r9   r6   r7   r8   r:   r;   r   r   r   r   r/      s   						r/   c                      sf   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	ddd fddZ
  ZS )PatternMatchingEventHandlerz
    Matches given patterns with file paths associated with occurring events.
    Uses pathlib's `PurePath.match()` method. `patterns` and `ignore_patterns`
    are expected to be a list of strings.
    NFc                   s&   t    || _|| _|| _|| _d S N)super__init__	_patterns_ignore_patterns_ignore_directories_case_sensitive)r=   patternsignore_patternsignore_directoriescase_sensitive	__class__r   r   rB   *  s
    
z$PatternMatchingEventHandler.__init__c                 C  s   | j S )zD(Read-only)
        Patterns to allow matching event paths.
        )rC   r=   r   r   r   rG   8  s    z$PatternMatchingEventHandler.patternsc                 C  s   | j S )zE(Read-only)
        Patterns to ignore matching event paths.
        )rD   rM   r   r   r   rH   ?  s    z+PatternMatchingEventHandler.ignore_patternsc                 C  s   | j S z\(Read-only)
        ``True`` if directories should be ignored; ``False`` otherwise.
        rE   rM   r   r   r   rI   F  s    z.PatternMatchingEventHandler.ignore_directoriesc                 C  s   | j S zu(Read-only)
        ``True`` if path names should be matched sensitive to case; ``False``
        otherwise.
        rF   rM   r   r   r   rJ   M  s    z*PatternMatchingEventHandler.case_sensitiver   r0   r1   c                   sn   | j r|jrdS g }t|dr0|t|j |jrH|t|j t|| j	| j
| jdrjt | dS )r4   Nr   )Zincluded_patternsZexcluded_patternsrJ   )rI   r   hasattrappendosfsdecoder   r   r   rG   rH   rJ   rA   r>   )r=   r2   pathsrK   r   r   r>   U  s    
z$PatternMatchingEventHandler.dispatch)NNFF)r   r   r   r   rB   propertyrG   rH   rI   rJ   r>   __classcell__r   r   rK   r   r?   #  s       



r?   c                      sf   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	ddd fddZ
  ZS )RegexMatchingEventHandlerzk
    Matches given regexes with file paths associated with occurring events.
    Uses the `re` module.
    NFc                   s   t    |d krdg}nt|tr*|g}|d kr6g }|r\dd |D | _dd |D | _n dd |D | _dd |D | _|| _|| _d S )Nz.*c                 S  s   g | ]}t |qS r   recompile.0rr   r   r   
<listcomp>  s     z6RegexMatchingEventHandler.__init__.<locals>.<listcomp>c                 S  s   g | ]}t |qS r   rZ   r]   r   r   r   r`     s     c                 S  s   g | ]}t |t jqS r   r[   r\   
IGNORECASEr]   r   r   r   r`     s     c                 S  s   g | ]}t |t jqS r   ra   r]   r   r   r   r`     s     )rA   rB   
isinstancer   _regexes_ignore_regexesrE   rF   )r=   regexesignore_regexesrI   rJ   rK   r   r   rB   u  s    

z"RegexMatchingEventHandler.__init__c                 C  s   | j S )zC(Read-only)
        Regexes to allow matching event paths.
        )rd   rM   r   r   r   rf     s    z!RegexMatchingEventHandler.regexesc                 C  s   | j S )zD(Read-only)
        Regexes to ignore matching event paths.
        )re   rM   r   r   r   rg     s    z(RegexMatchingEventHandler.ignore_regexesc                 C  s   | j S rN   rO   rM   r   r   r   rI     s    z,RegexMatchingEventHandler.ignore_directoriesc                 C  s   | j S rP   rQ   rM   r   r   r   rJ     s    z(RegexMatchingEventHandler.case_sensitiver   r0   r1   c                   s   | j r|jrdS g  t|dr0 t|j |jrH t|j t fdd| j	D rddS t fdd| j
D rt | dS )r4   Nr   c                 3  s"   | ]} D ]}| |V  q
qd S r@   matchr^   r_   prV   r   r   	<genexpr>  s       z5RegexMatchingEventHandler.dispatch.<locals>.<genexpr>c                 3  s"   | ]} D ]}| |V  q
qd S r@   rh   rj   rl   r   r   rm     s       )rI   r   rR   rS   rT   rU   r   r   anyrg   rf   rA   r>   r<   rK   rl   r   r>     s    
z"RegexMatchingEventHandler.dispatch)NNFF)r   r   r   r   rB   rW   rf   rg   rI   rJ   r>   rX   r   r   rK   r   rY   o  s       



rY   c                      s   e Zd ZdZdddd fddZddd	 fd
dZddd	 fddZddd	 fddZddd	 fddZddd	 fddZ	ddd	 fddZ
  ZS )LoggingEventHandlerzLogs all the events captured.NzOptional[logging.Logger]r0   )loggerr3   c                   s   t    |ptj| _d S r@   )rA   rB   loggingrootrp   )r=   rp   rK   r   r   rB     s    
zLoggingEventHandler.__init__r   r1   c                   s4   t  | |jrdnd}| jd||j|j d S )N	directoryfilezMoved %s: from %s to %s)rA   r9   r   rp   infor   r   r=   r2   whatrK   r   r   r9     s    zLoggingEventHandler.on_movedc                   s0   t  | |jrdnd}| jd||j d S )Nrs   rt   zCreated %s: %s)rA   r6   r   rp   ru   r   rv   rK   r   r   r6     s    zLoggingEventHandler.on_createdc                   s0   t  | |jrdnd}| jd||j d S )Nrs   rt   zDeleted %s: %s)rA   r7   r   rp   ru   r   rv   rK   r   r   r7     s    zLoggingEventHandler.on_deletedc                   s0   t  | |jrdnd}| jd||j d S )Nrs   rt   zModified %s: %s)rA   r8   r   rp   ru   r   rv   rK   r   r   r8     s    zLoggingEventHandler.on_modifiedc                   s    t  | | jd|j d S )NzClosed file: %s)rA   r:   rp   ru   r   r<   rK   r   r   r:     s    zLoggingEventHandler.on_closedc                   s    t  | | jd|j d S )NzOpened file: %s)rA   r;   rp   ru   r   r<   rK   r   r   r;     s    zLoggingEventHandler.on_opened)N)r   r   r   r   rB   r9   r6   r7   r8   r:   r;   rX   r   r   rK   r   ro     s   ro   c           	      c  s   t |D ]\}}}|D ]6}t j||}| r:||| nd}t||ddV  q|D ]6}t j||}| rv||| nd}t||ddV  qTq
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`.
    r   Tr   N)rT   walkpathjoinreplacer.   r%   )	src_dir_pathZdest_dir_pathrr   directories	filenamesrs   	full_pathZrenamed_pathfilenamer   r   r   generate_sub_moved_events  s    r   c                 c  s^   t | D ]N\}}}|D ]}tt j||ddV  q|D ]}tt j||d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`.
    Trx   N)rT   ry   r-   rz   r{   r#   )r}   rr   r~   r   rs   r   r   r   r   generate_sub_created_events  s
    r   )&r   
__future__r   rq   os.pathrT   r[   Zdataclassesr   r   typingr   Zwatchdog.utils.patternsr   r   r    r$   r"   r(   r*   r   r   r   r!   r#   r%   r&   r)   r+   r,   r-   r.   r/   r?   rY   ro   r   r   r   r   r   r   <module>   s@   M		ULS*