o
    çÕÊcç5  ã                   @   s´   	 d dl 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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 e d¡ZG dd„ deƒZG dd	„ d	eƒZdS )
é    N)
ÚFileDeletedEventÚFileModifiedEventÚFileCreatedEventÚFileMovedEventÚDirDeletedEventÚDirModifiedEventÚDirCreatedEventÚDirMovedEventÚgenerate_sub_created_eventsÚgenerate_sub_moved_events)ÚBaseObserverÚEventEmitterÚDEFAULT_EMITTER_TIMEOUTÚDEFAULT_OBSERVER_TIMEOUT)ÚDirectorySnapshotÚfseventsc                       s˜   e Zd Z	 edf‡ fdd„	Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dd„ Z
dd„ Zdd„ Zedd„ ƒZdd„ Zdd„ Zdd„ Zdd„ Zdd„ Z‡  ZS ) ÚFSEventsEmitterFc                    sZ   t ƒ  |||¡ tƒ | _|| _d| _d | _t ¡ | _	t
j t
j t
j | jj¡¡¡| _d S )Ng        )ÚsuperÚ__init__ÚsetÚ_fs_viewÚsuppress_historyÚ_start_timeÚ_starting_stateÚ	threadingÚLockÚ_lockÚosÚpathÚrealpathÚabspathÚ
expanduserÚwatchÚ_absolute_watch_path)ÚselfÚevent_queuer"   Útimeoutr   ©Ú	__class__© ú_G:\Development\the-witcher-3-mod-manager\.venv\lib\site-packages\watchdog\observers\fsevents.pyr   P   s   
&zFSEventsEmitter.__init__c                 C   s   t  | j¡ t  | ¡ d S ©N)Ú	_fseventsÚremove_watchr"   Ústop©r$   r)   r)   r*   Úon_thread_stopY   s   zFSEventsEmitter.on_thread_stopc                 C   sZ   | j jrt d|¡ t | |¡ d S |  |¡s%t d|¡ t | |¡ d S t d|¡ d S )Nzqueue_event %szdrop event %s)Ú_watchÚis_recursiveÚloggerÚdebugr   Úqueue_eventÚ_is_recursive_event)r$   Úeventr)   r)   r*   r5   ]   s   
zFSEventsEmitter.queue_eventc                 C   sV   |j r|jntj |j¡}|| jkrdS t|ttfƒr)tj |j	¡}|| jkr)dS dS )NFT)
Úis_directoryÚsrc_pathr   r   Údirnamer#   Ú
isinstancer   r	   Ú	dest_path)r$   r7   r9   r<   r)   r)   r*   r6   j   s   

z#FSEventsEmitter._is_recursive_eventc                 C   ó.   |j rtnt}|  ||ƒ¡ |  t|ƒ¡ d S r+   )r8   r   r   r5   r   ©r$   r7   r9   r:   Úclsr)   r)   r*   Ú_queue_created_eventx   ó   z$FSEventsEmitter._queue_created_eventc                 C   r=   r+   )r8   r   r   r5   r   r>   r)   r)   r*   Ú_queue_deleted_event}   rA   z$FSEventsEmitter._queue_deleted_eventc                 C   s    |j rtnt}|  ||ƒ¡ d S r+   )r8   r   r   r5   r>   r)   r)   r*   Ú_queue_modified_event‚   s   z%FSEventsEmitter._queue_modified_eventc                 C   sH   |j rtnt}|  |¡}|  |||ƒ¡ |  t|ƒ¡ |  t|ƒ¡ d S r+   )r8   r	   r   Ú_encode_pathr5   r   )r$   Z	src_eventr9   Údst_pathÚsrc_dirnameÚdst_dirnamer?   r)   r)   r*   Ú_queue_renamed_event†   s
   
z$FSEventsEmitter._queue_renamed_eventc                 C   sV   |j | jv }| jr%z| j  |j¡d }||j k}W n ty$   d}Y nw d}|p*|S )Nr   F)Úinoder   r   r   ÚKeyError)r$   r7   Z
in_historyZ	old_inodeZbefore_startr)   r)   r*   Ú_is_historic_created_event   s   ÿz*FSEventsEmitter._is_historic_created_eventc                 C   s   	 | j p	| jp	| jS r+   )Zis_inode_meta_modZis_xattr_modZis_owner_change©r7   r)   r)   r*   Ú_is_meta_modŸ   s   zFSEventsEmitter._is_meta_modc                    s  t  ¡ tjkr$|D ]‰ d ‡ fdd„tˆ ƒD ƒ¡}t  ˆ › d|› ¡ q	t ¡ | j	 dkr0d | _
|r‡| d¡‰ |  ˆ j¡}tj |¡}zt |¡}W n tyV   d }Y nw |o^|jˆ jk}ˆ jr–ˆ jr–|  ˆ ¡sq|  ˆ ||¡ | j ˆ j¡ ˆ js€|  ˆ ¡r‡|  ˆ ||¡ |  ˆ ||¡ | j ˆ j¡ nÒˆ jr¥|  ˆ ¡s¥|  ˆ ||¡ | j ˆ j¡ ˆ js´|  ˆ ¡r»|  ˆ ||¡ ˆ jrVt t!‡ fdd„|D ƒƒd ƒ}|r(t  d|¡ |  |j¡}	tj |	¡}
|  "ˆ ||	||
¡ | j ˆ j¡ t#||	ƒD ]}|  $|¡ q÷| %|¡ |js|  |¡r|  ||	|
¡ |jr'|  ||	|
¡ | j |j¡ n.|rG|  ˆ ||¡ | j ˆ j¡ t&|ƒD ]}|  $|¡ q=n|  ˆ ||¡ | j ˆ j¡ q0ˆ jrh|  ˆ ||¡ | j ˆ j¡ ˆ j'rƒ|  $t(| j)jƒ¡ t  d	¡ |  *¡  | j +¡  |s3d S d S )
Nz, c                 3   s"    | ]}t ˆ |ƒd u r|V  qdS )TN)Úgetattr)Ú.0ÚattrrL   r)   r*   Ú	<genexpr>¨   s   €  z/FSEventsEmitter.queue_events.<locals>.<genexpr>z: é<   r   c                 3   s&    | ]}|j r|jˆ jkr|V  qd S r+   )Ú
is_renamedrI   )rO   ÚerL   r)   r*   rQ   ì   s   €$ z"Destination event for rename is %sz&Stopping because root path was changed),r3   ÚgetEffectiveLevelÚloggingÚDEBUGÚjoinÚdirr4   ÚtimeÚ	monotonicr   r   ÚpoprD   r   r   r:   ÚstatÚOSErrorÚst_inorI   Z
is_createdÚ
is_removedrK   r@   r   ÚaddÚis_modifiedrM   rC   rB   ÚdiscardrS   ÚnextÚiterrH   r   r5   Úremover
   Zis_root_changedr   r"   r.   Úclear)r$   r&   ÚeventsÚflagsr9   rF   r]   ÚexistsZ	dst_eventrE   rG   Ú	sub_eventr)   rL   r*   Úqueue_events¤   s€   
ÿ

€ÿ

‹zFSEventsEmitter.queue_eventsc                    s‚   	 t j‰ z-‡ fdd„t||||ƒD ƒ}| j |  | j|¡ W d   ƒ W d S 1 s*w   Y  W d S  ty@   t d¡ Y d S w )Nc                    s"   g | ]\}}}}ˆ ||||ƒ‘qS r)   r)   )rO   r   rI   Zevent_flagsZevent_id©r?   r)   r*   Ú
<listcomp>,  s    
ÿÿz3FSEventsEmitter.events_callback.<locals>.<listcomp>z(Unhandled exception in fsevents callback)	r,   ZNativeEventÚzipr   rl   r&   Ú	Exceptionr3   Ú	exception)r$   ÚpathsÚinodesri   Úidsrh   r)   rm   r*   Úevents_callback&  s   
ÿþ&ÿÿzFSEventsEmitter.events_callbackc                 C   s\   | j jg| _t ¡ | _zt | | j | j| j¡ t 	| ¡ W d S  t
y-   t d¡ Y d S w )Nz&Unhandled exception in FSEventsEmitter)r"   r   Z	pathnamesrZ   r[   r   r,   Ú	add_watchru   Úread_eventsrp   r3   rq   r/   r)   r)   r*   Úrun7  s   
ÿzFSEventsEmitter.runc                 C   s>   | j rt| jjtƒrt | jj¡}n| jj}t|ƒ| _d S d S r+   )	r   r;   r"   r   Úbytesr   Úfsdecoder   r   )r$   Z
watch_pathr)   r)   r*   Úon_thread_start@  s   ùzFSEventsEmitter.on_thread_startc                 C   s   	 t | jjtƒrt |¡S |S r+   )r;   r"   r   ry   r   Úfsencode)r$   r   r)   r)   r*   rD   J  s   
zFSEventsEmitter._encode_path)Ú__name__Ú
__module__Ú__qualname__r   r   r0   r5   r6   r@   rB   rC   rH   rK   ÚstaticmethodrM   rl   ru   rx   r{   rD   Ú__classcell__r)   r)   r'   r*   r   9   s&    	
 	
r   c                       s*   e Zd Zef‡ fdd„	Zddd„Z‡  ZS )ÚFSEventsObserverc                    s   t ƒ jt|d d S )N)Úemitter_classr&   )r   r   r   )r$   r&   r'   r)   r*   r   S  s   zFSEventsObserver.__init__Fc                 C   s&   t |tƒrt d|¡}t | |||¡S )NZNFC)r;   ÚstrÚunicodedataÚ	normalizer   Úschedule)r$   Úevent_handlerr   Ú	recursiver)   r)   r*   r‡   V  s   
zFSEventsObserver.schedule)F)r}   r~   r   r   r   r‡   r   r)   r)   r'   r*   r‚   Q  s    r‚   )rZ   rV   r   r   r…   Ú_watchdog_fseventsr,   Úwatchdog.eventsr   r   r   r   r   r   r   r	   r
   r   Úwatchdog.observers.apir   r   r   r   Úwatchdog.utils.dirsnapshotr   Ú	getLoggerr3   r   r‚   r)   r)   r)   r*   Ú<module>   s   0
  