
    [ga                    *   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mZmZ dd	lmZ erdd
lmZ  ej4                  ej6                         dZdZ G d de      ZdZ e
ee      Z e jC                  dde       e jE                  d      Z#i Z$d Z%g e#g fdZ&ejN                  fdZ(ddZ)d Z*ddZ+d Z,d Z- e& e%dd d!"       e%d#d$d%ejN                  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jN                  d&'       e%dBddC'       e%dDdEdFd/dGH      gdIg>      dJ        Z0 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        Z1 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        Z2 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        Z3 G d de4      Z5d Z6d Z7e8dk(  r ejr                   e7              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)WatchdogShutdown
load_classplatform)VERSION_STRING)BaseObserverSubclassCallable)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       2lib/python3.12/site-packages/watchdog/watchmedo.pyr   zHelpFormatter.__init__:   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   0   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   argumentr1   Q   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>.decoratorb   s    }}$$S#.dll#""4T;`m"n & ==?$$T9;~eg$h$$T;[Q_gh$iCFQ23q62T*  r    )r   rJ   rI   rK   s   ``` r   commandrM   X   s     r   c                6    t        | j                  |            S )zSplits 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_splitrR   r   s     ##I.//r   c                ^    | ddd   D ]"  }t         j                  j                  ||       $ y)zAdds 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.
    Nr:   )syspathinsert)	pathnamesindexpathnames      r   add_to_sys_pathrZ   }   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)zLoads 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_configrc      s2     	"D	)Q~~affh' 
*	)	)s	   :Ac                ^    | j                  |      }|j                  |      }|dgk(  rg }||fS )z^Parses pattern argument specs and returns a two-tuple of
    (patterns, ignore_patterns).
     )rO   )patterns_specignore_patterns_specrQ   patternsignore_patternss        r   parse_patternsrj      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)ag  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_handlerrW   	recursiverY   s        r   observe_withrv      sg     	N-9= #NNJJqM  MMOs   A A('A(c                    |D ]Z  }t        |j                               D ]<  \  }}t        |      } |di |}t        |dd      xs |}	| j	                  ||	|       > \ y)aj  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_directoryNrL   )r/   itemsr
   getattrrm   )
rs   r   rY   ru   trickrD   value
TrickClasshandlertrick_pathnames
             r   schedule_tricksr      sd     .KD%#D)J )5)G$W.@$GS8Ng~yA	 / 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).)rI   c           	        | j                   rddlm} nr| j                  rddlm} n_t        s| j                  st        rt        j                         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 }| j.                  D ]+  } || j0                        }t2        j4                  j7                  |      s<t9        t:        j<                  t3        j>                  t:        j<                        |      tA        |      }	 |tB           }tF        |v rt)        |tF                  t2        j4                  jI                  |      xs1 t2        j4                  jK                  t3        jL                               }tO        |||| jP                         |jS                          |jU                  |       . 	 	 tW        jX                  d       # tD        $ r}tE        d	tB        d
|d      |d}~ww xY w# tZ        $ r* |D ]"  }	|	j]                          |	j_                          $ Y nw xY w|D ]  }	|	ja                           y)z;Command to execute tricks from a tricks configuration file.r   PollingObserverKqueueObserverWindowsApiObserverInotifyObserverFSEventsObserverObserverr   zNo z key specified in r   Nr<   )1debug_force_pollingwatchdog.observers.pollingr   debug_force_kqueuewatchdog.observers.kqueuer   r   debug_force_winapir   
is_windows)watchdog.observers.read_directory_changesr   debug_force_inotifywatchdog.observers.inotifyr   debug_force_fseventswatchdog.observers.fseventsr   watchdog.observersr   rZ   rR   python_pathr   r   osrU   existsOSErrorerrnoENOENTstrerrorrc   CONFIG_KEY_TRICKSKeyErrorCONFIG_KEY_PYTHON_PATHdirnamerelpathgetcwdr   ru   rn   appendro   rp   r	   unschedule_allrq   rr   )
r   r   	observerstricks_filers   configr   edir_pathos
             r   tricks_fromr      s   b J		 	 H 7 7]xObObOd\		!	!J		"	"L 	0Jt//01IzzDLL1ww~~k*%,,ELL(A;OO[)	a-.F "V+F#9:;77??;/O277??299;3O&(DNNC"' "*JJqM   	aS!2 55GVWXY_``	a  AFFH  	 s*   0	G9"H! 9	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)zLCommand to generate Yaml configuration for tricks named on the command line.r   Nz:
azutf-8)encoding)r]   rR   r   rZ   r   r   r
   writegenerate_yamlgetvalueclosedumpr   r   append_to_filer   rT   stdoutr   rU   r   r^   )	r   r]   python_pathsoutput
trick_pathr}   contentheaderfiles	            r   tricks_generate_yamlr   1  s   B d../LL!ZF&&

+
Z--/0 ' 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
--patternsrh   z9Matches event paths with these patterns (separated by ;).)r-   r   r   z-iz--ignore-patternz--ignore-patternsri   re   z9Ignores event paths with these patterns (separated by ;).z-Dz--ignore-directoriesignore_directorieszIgnores events for directories.z-Rru   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} nr| j                   rddlm} n_t&        s| j(                  st&        rt+        j,                         rdd	lm} n,| j2                  rdd
lm} n| j8                  rddlm} nddlm }  || jB                        }tE        ||| jF                  | jH                         y)z1Command 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    7J7O7OPS7Tr   )r   )rh   ri   r   r   r   r   r   r   r   r   N)%watchdog.tricksr   watchdog.utilsr   tracelogging	getLoggerr$   
echo_classrj   rh   ri   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rv   r   ru   )	r   r   r   rh   ri   r~   r   rs   r   s	           @r   logr   m  s    P ,#zz%//0F0FG+TU .t}}d>R>R SHo'22G J		 	 H 7 7]xObObOd\		!	!J		"	"L 	0-H7D$4$4dnnEr   zDirectories to watch.z-cz	--commandrM   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)zDCommand to execute shell commands in response to file system events.r   )ShellCommandTrickNr   r   )shell_commandrh   ri   r   r   r   r   )r   r   rM   r   r   r   r   r   rj   rh   ri   r   r   r   r   rv   r   ru   )r   r   r   rh   ri   r~   rs   s          r   r   r     s    n 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_argsrH   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)zLCommand 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%    )FMM&&..1 *r   )rM   rh   ri   r   stop_signalr   debounce_interval_secondsr   r   )!r   r   r   r   r   r   r   r   r   
startswithrz   intSIGTERMr   hasattraddr   rj   rh   ri   rM   extendr   r   r   r   r   rn   r   rv   ru   r	   rq   )r   r   r   r   r   r   rh   ri   rM   r~   rs   r   r   s              @@r   auto_restartr   E  s   ~ J/05 37++2H2H2O'&$++.UXY]YdYdUeK ">>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$   "F8 8	GG GG Gc                      e Zd Zy)LogLevelExceptionN)r#   r$   r%   rL   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 )Nr:   z&-q/--quiet may be specified only once.   z,-v/--verbose may be specified up to 2 times.)ERRORWARNINGINFODEBUGr<   )sumr8   r  )r   r8   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   rT   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   );):r&   
__future__r   r   r   r   os.pathrT   ro   argparser   r   ior   textwrapr   typingr   r   r	   r
   r   watchdog.versionr   watchdog.observers.apir   basicConfigr  r   r   r   r(   r  rB   add_subparsers
subparsersr@   r1   rM   pathseprR   rZ   rc   rj   rv   r   floatr   r   r   r   r   	Exceptionr  r
  r  r#   exitrL   r   r   <module>r)     s.  " #   	  
  @     A A +C   ',, ' & !/ !(
4 FMB   Y  G]3
' JB 4 )+

 0
)('.B* 	*JK&rzzn4OP	

 	R	
 	@		
 	(D]^"0	

 	".	

 	$1	

 	#3	
K*V 
Y-\1]-\1h 	H	

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

 	C	
 	H	
%8 ((;> ?> : 	8		
 	L	
 	"L	
 	"%2	
 	8	
 	E	
 	<6YZ(D]^"0	

 	".	

 	$1	

 	#3	
}CEL FMEL FF 	c3=TU	
2 	L	
 	"L	
 	"%2	
 	8	
 	E	
 	#W	
 	&B	
 	(D]^cRTjFkTjF4 	!OP	
	
 	_	
 	L	
 	"L	
 	"%2	
 	8	
 	E	
 	I		
 	(D]^l	
 	!$K	
 	** A	
eY[x6y[x6r		 	@. zCHHTV r   