
    "dw                         d Z ddlmZmZ ddlZddlZddlmZ ddlm	Z	  G d dej                        Z G d d	e	      Zy)
z8Contains the fallback implementation of the Watcher api.    )absolute_importprint_functionN)	snapshots)Watcherc                   ^     e Zd ZdZ fdZed        Zed        Zed        Zd Z	d Z
 xZS )PollingThreadz.A thread that fires a callback at an interval.c                 (   t         t        |           d| _        || _        || _        t        j                         | _        t        j                         | _	        t        j                         | _
        t        j                  | j                         y)zInitialize the thread.

        :param callback: Callback function to repeat.
        :param interval: Number of seconds to sleep between calls.
        TN)superr   __init__daemoncallbackinterval	threadingEvent	_shutdown_stopped_startedatexitregisterstop)selfr   r   	__class__s      9/usr/lib/python3/dist-packages/qtsass/watchers/polling.pyr   zPollingThread.__init__   sa     	mT+-  "*!)!)		"    c                 6    | j                   j                         S )z Check if the thread has started.)r   is_setr   s    r   startedzPollingThread.started-        }}##%%r   c                 6    | j                   j                         S )z Check if the thread has stopped.)r   r   r   s    r   stoppedzPollingThread.stopped2   r   r   c                 6    | j                   j                         S )z!Check if the thread has shutdown.)r   r   r   s    r   shutdownzPollingThread.shutdown7   s     ~~$$&&r   c                     | j                   s| j                  sy| j                  j                          | j                  j                          y)z?Set the shutdown event for this thread and wait for it to stop.N)r   r#   r   setr   waitr   s    r   r   zPollingThread.stop<   s2    ||DMMr   c                    	 | j                   j                          	 | j                          | j                  j	                  | j
                        rn7	 | j                  j                          y# | j                  j                          w xY w)zThreads main loop.N)r   r%   r   r   r&   r   r   r   s    r   runzPollingThread.runD   si    		 MM>>&&t}}5   MMDMMs   AA0 0B)__name__
__module____qualname____doc__r   propertyr   r!   r#   r   r(   __classcell__)r   s   @r   r   r      sQ    8# & & & & ' ' r   r   c                   .    e Zd ZdZd Zd Zd Zd Zd Zy)PollingWatchera  Polls a directory recursively for changes.

    Detects file and directory changes, deletions, and creations. Recursion
    depth is limited to 2 levels. We use a limit because the scss file we're
    watching for changes could be sitting in the root of a project rather than
    a dedicated scss directory. That could lead to snapshots taking too long
    to build and diff. It's probably safe to assume that users aren't nesting
    scss deeper than a couple of levels.
    c                     d| _         t        j                  | j                  | j                         | _        t        | j                  d      | _        y)zXSet up the PollingWatcher.

        A PollingThread is created but not started.
              )r   N)_snapshot_depthr   take
_watch_dir	_snapshotr   r(   _threadr   s    r   setupzPollingWatcher.setup]   s:    
  !"9M9MN$TXX:r   c                 8    | j                   j                          y)zStart the PollingThread.N)r8   startr   s    r   r;   zPollingWatcher.startf   s    r   c                 8    | j                   j                          y)zStop the PollingThread.N)r8   r   r   s    r   r   zPollingWatcher.stopj   s    r   c                 8    | j                   j                          y)z`Wait for the PollingThread to finish.

        You should always call stop before join.
        N)r8   joinr   s    r   r>   zPollingWatcher.joinn   s    
 	r   c                     t        j                  | j                  | j                        }t        j                  | j
                  |      }|r|| _        | j                          yy)z{Take a new snapshot and call on_change when a change is detected.

        Called repeatedly by the PollingThread.
        N)r   r5   r6   r4   diffr7   	on_change)r   next_snapshotchangess      r   r(   zPollingWatcher.runu   sK    
 "t8L8LM..?*DNNN r   N)	r)   r*   r+   r,   r9   r;   r   r>   r(    r   r   r0   r0   R   s     ;	r   r0   )r,   
__future__r   r   r   r   qtsass.watchersr   qtsass.watchers.apir   Threadr   r0   rD   r   r   <module>rI      s>    ? 7   & '4 I$$ 4 n,W ,r   