
    bg                         S r SSKrSSKrSSKrSSKrSSKJr   SSKr\R                  " S5      r
 " S S5      r " S S\5      rS	 rg! \ a    Sr N5f = f)
z
livereload.watcher
~~~~~~~~~~~~~~~~~~

A file watch management for LiveReload Server.

:copyright: (c) 2013 - 2015 by Hsiaoming Yang
:license: BSD, see LICENSE for more details.
    N)	signature
livereloadc                   t    \ rS rSrSrS rS rS rS rSS jr	S	 r
S
 rSS jrS rSS jrSS jrSS jrSrg)Watcher   zA file watcher registry.c                     0 U l         0 U l        0 U l        / U l        S U l        [
        R
                  " 5       U l        / SQU l        g )N)z.gitz.hgz.svnz.cvs)_tasks_task_mtimes_new_mtimes_changesfilepathtime_startignored_dirsselfs    4/usr/lib/python3/dist-packages/livereload/watcher.py__init__Watcher.__init__   sD       iik <    c                 :    U R                   R                  U5        g N)r   extend)r   argss     r   ignore_dirsWatcher.ignore_dirs-   s      &r   c                 L    U H  nU R                   R                  U5        M      g r   )r   remove)r   r   as      r   remove_dirs_from_ignoreWatcher.remove_dirs_from_ignore0   s     A$$Q' r   c                 N    [         R                  R                  U5      u  p#US;   $ )zIgnore a given filename or not.)z.pycz.pyoz.oz.swp)ospathsplitext)r   filename_exts       r   ignoreWatcher.ignore4   s$    !!(+444r   Nc                 ,    UUU0 S.U R                   U'   g)a  Add a task to watcher.

:param path: a filepath or directory path or glob pattern
:param func: the function to be executed when file changed
:param delay: Delay sending the reload message. Use 'forever' to
              not send it. This is useful to compile sass files to
              css, but reload on changed css files then only.
:param ignore: A function return True to ignore a certain pattern of
               filepath.
)funcdelayr)   mtimesN)r	   )r   r$   r,   r-   r)   s        r   watchWatcher.watch9   s      	
Dr   c                     g)z{Start the watcher running, calling callback when changes are
observed. If this returns False, regular polling will be used.F )r   callbacks     r   startWatcher.startK   s     r   c                    U R                   (       a  U R                   R                  5       $ SU l        [        5       nU R                   H  nU R                  U   nUS   U l        U R                  X#S   5      nU(       d  M9  US   nUS   nU(       a&  [        U[        5      (       a  UR                  U5        U(       d  My  [        USS5      nU(       d  [        USS5      n[        R                  S	U S
U S35        [        [        U5      R                  5      S:  a  [        U[         5      (       a
  U" U5        M  U" 5         GM     U(       a  [#        U5      nOSnU R                  U4$ )zoCheck if there are changes. If so, run the given task.

Returns a tuple of modified filepath and reload delay.
Nr.   r)   r,   r-   name__name__	anonymouszRunning task: z	 (delay: )r   )r   popr   setr	   r
   
is_changed
isinstancefloataddgetattrloggerinfolenr   
parameterslistmax)r   delaysr$   itemchangedr,   r-   r7   s           r   examineWatcher.examineP   s/   
 ====$$&& KKD;;t$D $XDoodN;GwF|WZu55JJu%4"46D&tZEKK(iwa@B9T?556:z'SW?X?XW%  ( KEE}}e##r   c                    0 U l         Sn[        R                  R                  U5      (       a  U R	                  X5      nOG[        R                  R                  U5      (       a  U R                  X5      nOU R                  X5      nU(       d  U R                  5       nU R                  R                  U R                   5        U$ )zyCheck if any filepaths have been added, modified, or removed.

Updates filepath modification times in self._task_mtimes.
F)r   r#   r$   isfileis_file_changedisdiris_folder_changedget_changed_glob_filesis_file_removedr
   update)r   r$   r)   rJ   s       r   r=   Watcher.is_changedu   s    
 77>>$**48GWW]]4  ,,T:G11$?G**,G  !1!12r   c                     [        U R                  5      [        U R                  5      -
  nU(       d  gU H$  nU R                  R                  U5        X l        M&     g)zCheck if any filepaths have been removed since last check.

Deletes removed paths from self._task_mtimes.
Sets self.filepath to one of the removed paths.
FT)r<   r
   r   r;   r   )r   removed_pathsr$   s      r   rS   Watcher.is_file_removed   sT     D--.T5E5E1FF!D!!$' !M	 "
 r   c                    [         R                  R                  U5      (       d  gU R                  U5      (       a  gU(       a  U" U5      (       a  g[         R                  R	                  U5      nXR
                  ;  a"  X0R                  U'   Xl        X0R                  :  $ U R
                  U   U:w  a  X0R                  U'   Xl        gX0R                  U'   g)zCheck if filepath has been added or modified since last check.

Updates filepath modification times in self._new_mtimes.
Sets self.filepath to changed path.
FT)	r#   r$   rN   r)   getmtimer
   r   r   r   )r   r$   r)   mtimes       r   rO   Watcher.is_file_changed   s     ww~~d##;;tfTll  &(((%*T" M;;&&T"e+%*T" M!&r   c                    [         R                  " USS9 Hq  u  p4nU R                   H  nXd;   d  M
  UR                  U5        M     U H:  nU R	                  [         R
                  R                  X75      U5      (       d  M9      g   Ms     g)z2Check if directory path has any changed filepaths.T)followlinksF)r#   walkr   r   rO   r$   join)r   r$   r)   rootdirsfilesdfs           r   rQ   Watcher.is_folder_changed   sq    !#4!@D&&9KKN ' ''T(=vFF  "A r   c                     [         R                   " USS9nU Vs/ s H  o@R                  XB5      (       d  M  UPM     nnU$ s  snf )z-Check if glob path has any changed filepaths.T)	recursive)globrO   )r   r$   r)   rc   re   changed_filess         r   rR   Watcher.get_changed_glob_files   s=    		$$/$)MEq-A-A!-LEM Ns
   AA)r   r   r   r
   r	   r   r   )Nr   Nr   )r8   
__module____qualname____firstlineno____doc__r   r   r    r)   r/   r4   rK   r=   rS   rO   rQ   rR   __static_attributes__r2   r   r   r   r      sB    "<$'(5

$
#$J*"<
r   r   c                   0    \ rS rSrS rSS jrS rS rSrg)	INotifyWatcher   c                 ~    [         R                  U 5        [        R                  " 5       U l        S U l        S U l        g r   )r   r   	pyinotifyWatchManagerwmnotifierr3   r   s    r   r   INotifyWatcher.__init__   s.    ((*r   Nc                     [         R                  [         R                  -  [         R                  -  nU R                  R                  XSSSS9  [        R                  XX#U5        g )NT)recdo_globauto_add)ru   	IN_CREATE	IN_DELETE	IN_MODIFYrw   	add_watchr   r/   )r   r$   r,   r-   r)   flags         r   r/   INotifyWatcher.watch   sN    ""Y%8%889;N;NN$$tLd$v6r   c                 $    U R                  5         g r   )r3   )r   events     r   inotify_eventINotifyWatcher.inotify_event   s    r   c                     U R                   (       dZ  Xl        SSKJn  [        R
                  " U R                  UR                  R                  5       U R                  S9U l         U" 5         g)Nr   )ioloop)default_proc_funT)
rx   r3   tornador   ru   TornadoAsyncNotifierrw   IOLoopinstancer   )r   r3   r   s      r   r4   INotifyWatcher.start   sL    }}$M&%:://1!%!3!3DM Jr   )r3   rx   rw   )NNN)	r8   rl   rm   rn   r   r/   r   r4   rp   r2   r   r   rr   rr      s    7

r   rr   c                  R    [         b  [        [         S5      (       d  [        $ [        $ )Nr   )ru   hasattrr   rr   r2   r   r   get_watcher_classr      s     	3I J Jr   )ro   ri   loggingr#   r   inspectr   ru   ImportError	getLoggerrB   r   rr   r   r2   r   r   <module>r      sj      	   
		<	(p pfW :m  Is   A	 	AA