
    "dzb                    "   d Z ddlmZ 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 ddlmZ ddlmZ ddlmZ ddlmZ dd	lmZmZ dd
lmZ  ej2                  ej4                         dZdZ G d de      ZdZ e
ee      ZejA                  dde       ejC                  d      Z"i Z#d Z$g e"g fdZ%ejL                  fdZ'ddZ(d Z)ddZ*d Z+d Z, e% e$dd d!"       e$d#d$d%ejL                  d&'       e$d(d)d*d+e-d,-       e$d.d/d0d12       e$d3d/d45       e$d6d/d75       e$d8d/d95       e$d:d/d;5       e$d<d/d=5      g	dg>      d?        Z. e% e$d@d dA"       e$d#d$d%ejL                  d&'       e$dBddC'       e$dDdEdFd/dGH      gdIg>      dJ        Z/ e% e$dKd d$dLM       e$dNdOdPdQd dRS       e$dTdUdVdWdXdYS       e$dZd[d\d/d]H       e$d^d.d_d/d`H       e$d(d)d*d+e-da-       e$dbd/dc5       e$d3d/d45       e$d6d/d75       e$d8d/d95       e$d:d/d;5       e$d<d/d=5      g      dd        Z0 e% e$dKd d$deM       e$dfdgdhddiS       e$dNdOdPdQd dRS       e$dTdUdVdWdXdYS       e$dZd[d\djd/d]k       e$d^d.d_d/d`H       e$d(d)d*d+e-da-       e$dldmdnd/doH       e$dpdqdrd/dsH       e$d3d/d45      g
      dt        Z1 e% e$dhduv       e$dwdxd dyz       e$d{d|dKd}d~d       e$dNdOdPdQd dRS       e$dTdUdVdWdXdYS       e$dZd[d\djd/d]k       e$d^d.d_d/d`H       e$d(d)d*d+e-da-       e$ddddS       e$d3d/d45       e$ddde-d-       e$ddde-d-       e$ddd0ddk      g      d        Z2 G d de3      Z4d Z5d Z6e7dk(  r ejp                   e6              yy)u   
:module: watchdog.watchmedo
:author: yesudeep@google.com (Yesudeep Mangalapilly)
:author: contact@tiger-222.fr (Mickaël Schoentgen)
:synopsis: ``watchmedo`` shell script utility.
    )annotationsN)ArgumentParserRawDescriptionHelpFormatter)StringIO)dedent)TYPE_CHECKING)BaseObserverSubclassCallable)WatchdogShutdown
load_class)VERSION_STRING)leveltrickszpython-pathc                  .     e Zd ZdZdd fd
Zd Z xZS )HelpFormattera#  A nicer help formatter.

    Help for arguments can be indented and contain new lines.
    It will be de-dented and arguments in the help
    will be separated by a blank line for better readability.

    Source: https://github.com/httpie/httpie/blob/2423f89/httpie/cli/argparser.py#L31
       )max_help_positionc               .    ||d<   t        |   |i | y )Nr   )super__init__)selfr   argskwargs	__class__s       4/usr/lib/python3/dist-packages/watchdog/watchmedo.pyr   zHelpFormatter.__init__9   s     &7"#$)&)    c                Z    t        |      j                         dz   }|j                         S )Nz

)r   strip
splitlines)r   textwidths      r   _split_lineszHelpFormatter._split_lines>   s&    d|!!#f,  r   )__name__
__module____qualname____doc__r   r!   __classcell__)r   s   @r   r   r   /   s     12 *
!r   r   zCopyright 2011 Yesudeep Mangalapilly <yesudeep@gmail.com>.
Copyright 2012 Google, Inc & contributors.

Licensed under the terms of the Apache license, version 2.0. Please see
LICENSE in the source code for more information.)epilogformatter_classz	--versionversion)actionr)   top_command)destc                     t        |       |fS )z\Convenience function to properly format arguments to pass to the
    command decorator.
    )list)name_or_flagsr   s     r   argumentr0   P   s     &&r   c                      fd}|S )a
  Decorator to define a new command in a sanity-preserving way.
    The function will be stored in the ``func`` variable when the parser
    parses arguments so that it can be called directly like so::

      >>> args = cli.parse_args()
      >>> args.func(args)

    c                   | j                   j                  dd      }t        | j                        }j	                  ||t
              }|t        |<   |j                         }|j                  ddddd	       |j                  d
dddd	       D ],  } |j                  |d   i |d    |j                  |        . | S )N_-)descriptionaliasesr(   z-qz--quiet	verbosityappend_const)r,   r*   constz-vz	--verbose   r   )func)
r"   replacer   r%   
add_parserr   command_parsersadd_mutually_exclusive_groupadd_argumentset_defaults)	r<   namedescparserverbosity_groupargr   cmd_aliasesparents	         r   	decoratorzcommand.<locals>.decoratora   s    }}$$S#.dll#""dK # 
 !' ==?$$)+nB 	% 	
 	$$+Ka 	% 	
  	+CFQ23q62T*	+ r    )r   rI   rH   rJ   s   ``` r   commandrL   W   s    & r   c                6    t        | j                  |            S )z
    Splits a pathname specification separated by an OS-dependent separator.

    :param pathname_spec:
        The pathname specification.
    :param separator:
        (OS Dependent) `:` on Unix and `;` on Windows or user-specified.
    )r.   split)pathname_spec	separators     r   
path_splitrQ   w   s     ##I.//r   c                ^    | ddd   D ]"  }t         j                  j                  ||       $ y)z
    Adds specified paths at specified index into the sys.path list.

    :param paths:
        A list of paths to add to the sys.path
    :param index:
        (Default 0) The index in the sys.path list where the paths will be
        added.
    Nr9   )syspathinsert)	pathnamesindexpathnames      r   add_to_sys_pathrY      s-     ddO )x()r   c                    ddl }t        | d      5 }|j                  |j                               cddd       S # 1 sw Y   yxY w)z
    Loads the YAML configuration from the specified file.

    :param tricks_file_path:
        The path to the tricks configuration file.
    :returns:
        A dictionary of configuration information.
    r   Nrb)yamlopen	safe_loadread)tricks_file_pathnamer\   fs      r   load_configrb      s;     	"D	) (Q~~affh'( ( (s	   :Ac                ^    | j                  |      }|j                  |      }|dgk(  rg }||fS )zc
    Parses pattern argument specs and returns a two-tuple of
    (patterns, ignore_patterns).
     )rN   )patterns_specignore_patterns_specrP   patternsignore_patternss        r   parse_patternsri      s>    
 ""9-H*00;O2$o&&r   c                    t        |      D ]  }| j                  |||        | j                          	 	 t        j                  d       # t
        $ r | j                          Y nw xY w| j                          y)al  
    Single observer thread with a scheduled path and event handler.

    :param observer:
        The observer thread.
    :param event_handler:
        Event handler which will be called in response to file system events.
    :param pathnames:
        A list of pathnames to monitor.
    :param recursive:
        ``True`` if recursive; ``False`` otherwise.
    r;   N)setschedulestarttimesleepr
   stopjoin)observerevent_handlerrV   	recursiverX   s        r   observe_withru      sl     	N >-9=>NNJJqM  MMOs   A A('A(c                    |D ]Z  }t        |j                               D ]<  \  }}t        |      } |di |}t        |dd      xs |}	| j	                  ||	|       > \ y)ao  
    Schedules tricks with the specified observer and for the given watch
    path.

    :param observer:
        The observer thread into which to schedule the trick and watch.
    :param tricks:
        A list of tricks.
    :param pathname:
        A path name which should be watched.
    :param recursive:
        ``True`` if recursive; ``False`` otherwise.
    source_directoryNrK   )r.   itemsr   getattrrl   )
rr   r   rX   rt   trickrC   value
TrickClasshandlertrick_pathnames
             r   schedule_tricksr      sr      B. 	BKD%#D)J )5)G$W.@$GS8Ng~yA		BBr   files*zperform tricks from given file)nargshelpz--python-path.zPaths separated by z to add to the Python path.)defaultr   z
--intervalz	--timeouttimeoutg      ?z?Use this as the polling interval/blocking timeout (in seconds).)r,   r   typer   z--recursive
store_trueTz-Recursively monitor paths (defaults to True).)r*   r   r   z--debug-force-pollingz[debug] Forces polling.)r*   r   z--debug-force-kqueuez[debug] Forces BSD kqueue(2).z--debug-force-winapiz[debug] Forces Windows API.z--debug-force-fseventsz[debug] Forces macOS FSEvents.z--debug-force-inotifyz [debug] Forces Linux inotify(7).)rH   c           	        | j                   rddlm} n}| j                  rddlm} njt        s| j                  s%t        r&t        j                  j                  d      rddlm} n,| j                  rddlm} n| j                   rddlm} nddlm} t+        t-        | j.                               g }| j0                  D ]+  } || j2                  	      }t4        j6                  j9                  |      s<t;        t<        j>                  t5        j@                  t<        j>                        |      tC        |      }	 |tD           }tH        |v rt+        |tH                  t4        j6                  jK                  |      }|s1t4        j6                  jM                  t5        jN                               }tQ        |||| jR                         |jU                          |jW                  |       . 	 	 tY        jZ                  d       # tF        $ r tG        d
tD        d|d      w xY w# t\        $ r* |D ]"  }|j_                          |ja                          $ Y nw xY w|D ]  }|jc                           y)zE
    Command to execute tricks from a tricks configuration file.
    r   PollingObserverKqueueObserverwinWindowsApiObserverInotifyObserverFSEventsObserverObserverr   zNo z key specified in r   r;   N)2debug_force_pollingwatchdog.observers.pollingr   debug_force_kqueuewatchdog.observers.kqueuer   r   debug_force_winapirS   platform
startswith)watchdog.observers.read_directory_changesr   debug_force_inotifywatchdog.observers.inotifyr   debug_force_fseventswatchdog.observers.fseventsr   watchdog.observersr   rY   rQ   python_pathr   r   osrT   existsOSErrorerrnoENOENTstrerrorrb   CONFIG_KEY_TRICKSKeyErrorCONFIG_KEY_PYTHON_PATHdirnamerelpathgetcwdr   rt   rm   appendrn   ro   r
   unschedule_allrp   rq   )	r   r   	observerstricks_filerr   configr   dir_pathos	            r   tricks_fromr      s   j J		 	 H 7 7]s||OfOfglOm\		!	!J		"	"L 	0Jt//01Izz #DLL1ww~~k*%,,ELL(A;OO[)	-.F "V+F#9:;77??;/wwryy{3H&(DNNC"/#2JJqM !  	'**<[O1M 	$   	AFFH	  	s   ;	H-H'  H$'0IItrick_pathsz5Dotted paths for all the tricks you want to generate.z--append-to-filez
                   Appends the generated tricks YAML to a file.
                   If not specified, prints to standard output.z-az--append-onlyappend_onlyz
                   If --append-to-file is not specified, produces output for
                   appending instead of a complete tricks YAML file.)r,   r*   r   zgenerate-tricks-yamlc                   ddl }t        | j                        }t        |       t	               }| j
                  D ],  }t        |      }|j                  |j                                . |j                         }|j                          |j                  t        |i      }|t         dz  }| j                  1| j                  s||z   }t         j"                  j                  |       yt$        j&                  j)                  | j                        s||z   }t+        | j                  dd      5 }|j                  |       ddd       y# 1 sw Y   yxY w)zV
    Command to generate Yaml configuration for tricks named on the command line.
    r   Nz:
azutf-8)encoding)r\   rQ   r   rY   r   r   r   writegenerate_yamlgetvalueclosedumpr   r   append_to_filer   rS   stdoutr   rT   r   r]   )	r   r\   python_pathsoutput
trick_pathr|   contentheaderfiles	            r   tricks_generate_yamlr   D  s   F d../LL!ZF&& 1

+
Z--/01 ooG
LLNYY.=>F
"#3''F"w&G

!ww~~d112w&G$%%sW= 	 JJw	  	  	 s   0EEdirectoriesz%Directories to watch. (default: '.').)r   r   r   z-pz	--patternz
--patternsrg   z9Matches event paths with these patterns (separated by ;).)r,   r   r   z-iz--ignore-patternz--ignore-patternsrh   rd   z9Ignores event paths with these patterns (separated by ;).z-Dz--ignore-directoriesignore_directorieszIgnores events for directories.z-Rrt   z%Monitors the directories recursively.z2Use this as the polling interval/blocking timeout.z--tracez!Dumps complete dispatching trace.c                   ddl m} ddlm} | j                  r5t        j                  |j                        |j                  |fd       t        | j                  | j                        \  }} |||| j                        }| j                  rddlm} n}| j                   rddlm} njt&        s| j(                  s%t&        r&t*        j,                  j/                  d	      rdd
lm} n,| j4                  rddlm} n| j:                  rddlm} nddl m!}  || jD                        }tG        ||| jH                  | jJ                         y)z;
    Command to log file system events to the console.
    r   )LoggerTrick)echoc                &    j                  |       S N)info)msgclass_module_loggers    r   <lambda>zlog.<locals>.<lambda>  s    7J7O7OPS7T r   )r   )rg   rh   r   r   r   r   r   r   r   r   r   N)&watchdog.tricksr   watchdog.utilsr   tracelogging	getLoggerr#   
echo_classri   rg   rh   r   r   r   r   r   r   r   r   r   rS   r   r   r   r   r   r   r   r   r   r   r   r   r   ru   r   rt   )	r   r   r   rg   rh   r}   r   rr   r   s	           @r   logr     s    \ ,#zz%//0F0FG+TU .t}}d>R>R SHo'22G J		 	 H 7 7]s||OfOfglOm\		!	!J		"	"L 	0-H7D$4$4dnnEr   zDirectories to watch.z-cz	--commandrL   am  
    Shell command executed in response to matching events.
    These interpolation variables are available to your command string:

        ${watch_src_path}   - event source path
        ${watch_dest_path}  - event destination path (for moved events)
        ${watch_event_type} - event type
        ${watch_object}     - 'file' or 'directory'

    Note:
        Please ensure you do not use double quotes (") to quote
        your command string. That will force your shell to
        interpolate before the command is processed by this
        command.

    Example:

        --command='echo "${watch_src_path}"'
    F)r,   r   r*   r   z-wz--waitwait_for_processzDWait for process to finish to avoid multiple simultaneous instances.z-Wz--dropdrop_during_processzhIgnore events that occur while command is still being executed to avoid multiple simultaneous instances.c                   ddl m} | j                  sd| _        | j                  rddlm} nddlm} t        | j                  | j                        \  }} || j                  ||| j                  | j                  | j                        } || j                        }t        ||| j                   | j"                         y)zN
    Command to execute shell commands in response to file system events.
    r   )ShellCommandTrickNr   r   )shell_commandrg   rh   r   r   r   r   )r   r   rL   r   r   r   r   r   ri   rg   rh   r   r   r   r   ru   r   rt   )r   r   r   rg   rh   r}   rr   s          r   r   r     s    v 2<< J/ .t}}d>R>R SHoll'22.. 44G -H7D$4$4dnnEr   z,Long-running command to run in a subprocess.)r   command_argsrG   z
    Command arguments.

    Note: Use -- before the command arguments, otherwise watchmedo will
    try to interpret them.
    )metavarr   r   z-dz--directory	DIRECTORYr   zLDirectory to watch. Use another -d or --directory option for each directory.)r,   r   r*   r   z--signalsignalSIGINTz6Stop the subprocess with this signal (default SIGINT).z--kill-after
kill_afterg      $@zYWhen stopping, kill the subprocess after the specified timeout in seconds (default 10.0).z--debounce-intervaldebounce_intervalg        zwAfter a file change, Wait until the specified interval (in seconds) passes with no file changes, and only then restart.z--no-restart-on-command-exitrestart_on_command_exitstore_falsez.Don't auto-restart the command after it exits.c           
        | j                   rddlm} nddlm} ddlddlm} | j                  sdg| _        | j
                  j                  d      rt        | j
                        }nt        | j
                        }j                  j                  ht        d      rj                  j                          fd	}D ]  }j                  ||        t#        | j$                  | j&                        \  }}| j(                  g}|j+                  | j,                          ||||| j.                  || j0                  | j2                  | j4                  
      }	|	j7                           || j8                        }
	 t;        |
|	| j                  | j<                         |	jA                          y# t>        $ r Y w xY w# |	jA                          w xY w)zV
    Command to start a long-running subprocess and restart it on matched events.
    r   r   r   N)AutoRestartTrickr   SIGSIGHUPc                V    D ]  }j                  |j                           t        r   )r   SIG_IGNr
   )_signum_framesignumr   termination_signalss      r   handler_termination_signalz0auto_restart.<locals>.handler_termination_signal  s*    ) 	2FMM&&..1	2r   )rL   rg   rh   r   stop_signalr   debounce_interval_secondsr   r   )!r   r   r   r   r   r   r   r   r   r   ry   intSIGTERMr   hasattraddr   ri   rg   rh   rL   extendr   r   r   r   r   rm   r   ru   rt   r
   rp   )r   r   r   r   r   r   rg   rh   rL   r}   rr   r   r   s              @@r   auto_restartr   d  s   L J/05 {{e$fdkk2$++& ">>6==9vx . & :f89: !/t}}d>R>R SHo||nGNN4$$%'22??"&"8"8 $ < <	G MMO-HXw(8(8$..I 	   	s$   "F9 9	GG GG Gc                      e Zd Zy)LogLevelExceptionN)r"   r#   r$   rK   r   r   r  r    s    r   r  c                    t        | j                  xs g       }|dk  rt        d      |dkD  rt        d      g dd|z      S )Nr9   z&-q/--quiet may be specified only once.   z,-v/--verbose may be specified up to 2 times.)ERRORWARNINGINFODEBUGr;   )sumr7   r  )r   r7   s     r   _get_log_level_from_argsr	    sJ    DNN(b)I2~ HII1} NOO0Y??r   c                    t         j                         } | j                  t         j                          y	 t	        |       }t        j                  d      j                  |       	 | j                  |        y# t
        $ rV}t        d|j                  d    t        j                         t        | j                     j                          Y d}~yd}~ww xY w# t        $ r Y yw xY w)zEntry-point function.Nr;   zError: r   )r   watchdog   )cli
parse_argsr+   
print_helpr	  r  printr   rS   stderrr?   r   r   setLevelr<   KeyboardInterrupt)r   	log_levelexcs      r   mainr    s    >>D,T2	
 j!**95		$   }%CJJ7(()446  s*   A9 'C 9	CACC	C'&C'__main__)r   );)9r%   
__future__r   r   r   r   os.pathrS   rn   argparser   r   ior   textwrapr   typingr   watchdog.observers.apir	   r   r
   r   watchdog.versionr   basicConfigr  r   r   r   r'   r  rA   add_subparsers
subparsersr?   r0   rL   pathseprQ   rY   rb   ri   ru   r   floatr   r   r   r   r   	Exceptionr  r	  r  r"   exitrK   r   r   <module>r(     s@  " #   	  
  @     ? 7 +   ',, ' & !/ !(
4 FMB   Y  G]3
' JB @ )+

 	0)(	'0B, 	*JK&rzzn4OP	

 	R	
 	@		
 	#L?X	
 	"0	

 	".	

 	$1	

 	#3	
O,Z 
]/`7a/`7t 	H	

 	&rzzn4OP	

 	C	
 	H	
%8 ((;> ?> > 	8		
 	L	
 	"L	
 	"%2	
 	8	
 	E	
 	l1T	
 	#L?X	
 	"0	

 	".	

 	$1	

 	#3	
EGIT"FUIT"FJ 	c3=TU	
2 	L	
 	"L	
 	"%2	
 	8	
 	E	
 	#W	
 	&B	
 	#L?X	
cTVnFoVnF8 	!OP	
	
 	"	
 	L	
 	"L	
 	"%2	
 	8	
 	E	
 	I		
 	#L?X	
 	)	
 	!$K	
 	** A	
m]_@<A_@<~		 	@. zCHHTV r   