
    KiTd                       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mZ ddlmZmZmZ dd	lmZ er 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!  ejD                  ejF                         dZ$dZ% G d de      Z&dZ' e
e'e&      Z(e(jS                  dde       e(jU                  d      Z+i Z,e-e.e/   ef   Z0ddZ1e+dd	 	 	 	 	 	 	 ddZ2ejf                  dddZ4dd dd!Z5dd"Z6d#d	 	 	 	 	 	 	 dd$Z7	 	 	 	 	 	 	 	 	 	 dd&Z8dd'Z9 e2 e1d(d)d*+       e1d,d-d.ejf                  d/0       e1d1d2d3d4e:d56       e1d7d8d9d:;       e1d<d8d=>       e1d?d8d@>       e1dAd8dB>       e1dCd8dD>       e1dEd8dF>      g	dgG      ddH       Z; e2 e1dId)dJ+       e1d,d-d.ejf                  d/0       e1dKddL0       e1dMdNdOd8dPQ      gdRgG      ddS       Z< e2 e1dTd)d-dUV       e1dWdXdYdZd)d[\       e1d]d^d_d`dadb\       e1dcddded8dfQ       e1dgd7d%d8dhQ       e1d1d2d3d4e:di6       e1djd8dk>       e1d<d8d=>       e1d?d8d@>       e1dAd8dB>       e1dCd8dD>       e1dEd8dF>      g      ddl       Z= e2 e1dTd)d-dmV       e1dndodpddq\       e1dWdXdYdZd)d[\       e1d]d^d_d`dadb\       e1dcdddedrd8dfs       e1dgd7d%d8dhQ       e1d1d2d3d4e:di6       e1dtdudvd8dwQ       e1dxdydzd8d{Q       e1d<d8d=>      g
      dd|       Z> e2 e1dpd}~       e1ddd)d       e1dddTddd       e1dWdXdYdZd)d[\       e1d]d^d_d`dadb\       e1dcdddedrd8dfs       e1dgd7d%d8dhQ       e1d1d2d3d4e:di6       e1dddd\       e1d<d8d=>       e1ddde:d6       e1ddde:d6       e1ddd9dds      g      dd       Z? G d de@      ZAddZBddZCeDdk(  r ej                   eC              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Any)WatchdogShutdownError
load_classplatform)VERSION_STRING)	Namespace_SubParsersAction)Callable)FileSystemEventHandler)ObserverType)BaseObserver)leveltrickszpython-pathc                  2     e Zd ZdZddd fdZd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       ^/home/jay/workspace/scripts/.codegraph-venv/lib/python3.12/site-packages/watchdog/watchmedo.pyr   zHelpFormatter.__init__0   s     &7"#$)&)    c                Z    t        |      j                         dz   }|j                         S )Nz

)r   strip
splitlines)r   textwidths      r!   _split_lineszHelpFormatter._split_lines5   s&    d|!!#f,  r"   )r   r	   r   intr   r	   returnNone)r&   strr'   r)   r*   	list[str])__name__
__module____qualname____doc__r   r(   __classcell__)r    s   @r!   r   r   &   s     => *
!r"   r   u,  Copyright 2018-2024 Mickaël Schoentgen & contributors
Copyright 2014-2018 Thomas Amland & contributors
Copyright 2012-2014 Google, Inc.
Copyright 2011-2012 Yesudeep Mangalapilly

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)actionr5   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!   argumentr<   K   s     &&r"   )parentcmd_aliasesc                    d 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                  xs d      }	j	                  |xs g |t
              }|t        |<   |j                         }|j                  ddddd	
       |j                  ddddd
       D ])  \  }} |j                  |i | |j                  |        + | S )N_- )aliasesdescriptionr4   z-qz--quiet	verbosityappend_const)r8   r6   constz-vz	--verbose   )func)
r.   replacer   r1   
add_parserr   command_parsersadd_mutually_exclusive_groupadd_argumentset_defaults)
rK   namedescparserverbosity_groupr;   r   r   r>   r=   s
          r!   	decoratorzcommand.<locals>.decoratora   s    }}$$S#.dll(b)""41BPTfs"t & ==?$$T9;~eg$h$$T;[Q_gh$i%) 	+!M6F9&9T*	+ r"   )rK   r   r*   r    )r   r=   r>   rV   s   ``` r!   commandrX   R   s     r"   )	separatorc               $    | 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.
    split)pathname_specrY   s     r!   
path_splitr^   q   s     y))r"   )indexc               ^    | 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.
    NrH   )syspathinsert)	pathnamesr_   pathnames      r!   add_to_sys_pathrf   |   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_pathnameri   fs      r!   load_configro      s;     	"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).
    rC   r[   )patterns_specignore_patterns_specrY   patternsignore_patternss        r!   parse_patternsrv      s>     ""9-H*00;O2$_$$r"   	recursivec                   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.
    rw   rJ   N)setschedulestarttimesleepr
   stopjoin)observerevent_handlerrd   rw   re   s        r!   observe_withr      sq    $ 	N H-YGHNNJJqM   MMOs   A A)(A)c                   |D ]R  }|j                         D ]=  \  }}t        |      } |di |}t        |dd      xs |}	| j                  ||	|       ? T 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_directoryNry   rW   )itemsr   getattrr{   )
r   r   re   rw   trickrR   value	trick_clshandlertrick_pathnames
             r!   schedule_tricksr      sp      L ;;= 	LKD%"4(I(%(G$W.@$GS8Ng~K		LL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).)r8   r   typer   z--recursive
store_trueTz-Recursively monitor paths (defaults to True).)r6   r   r   z--debug-force-pollingz[debug] Forces polling.)r6   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).)r>   c                   | j                   r	ddlm} |}n|| j                  r	ddlm} |}ngt        s| j                  st        rt        j                         r	ddl
m} |}n2| 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}d	tB        d
|	d}tE        |      |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   Nry   rJ   )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   rf   r^   python_pathr   r   osrb   existsOSErrorerrnoENOENTstrerrorro   CONFIG_KEY_TRICKSKeyErrorCONFIG_KEY_PYTHON_PATHdirnamerelpathgetcwdr   rw   r|   appendr}   r~   r
   unschedule_allr   r   )r   r   observer_clsr   r   r   r   r   	observerstricks_filer   configr   eerrordir_pathos                    r!   tricks_fromr      s   b >&		 	 <% 7 7]xObObOdP)		!	!>&		"	"@' 	0Jt//01Izz #5ww~~k*%,,ELL(A;OO[)	)-.F
 "V+F#9:;77??;/O277??299;3O&(dnnM")#,JJqM   	)+..@qQE5/q(	) !  	AFFH	  	s*   <	H/H0 	H-H((H-00I#"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.)r8   r6   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)ri   r^   r   rf   r   r   r   writegenerate_yamlgetvalueclosedumpr   r   append_to_filer   ra   stdoutr   rb   r   rj   )	r   ri   python_pathsoutput
trick_pathr   contentheaderfiles	            r!   tricks_generate_yamlr   E  s   B d../LL!ZF&& 0
z*	Y,,./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
--patternsrt   z9Matches event paths with these patterns (separated by ;).)r8   r   r   z-iz--ignore-patternz--ignore-patternsru   rC   z9Ignores event paths with these patterns (separated by ;).z-Dz--ignore-directoriesignore_directorieszIgnores events for directories.z-Rz%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} |}ngt&        s| j(                  st&        rt+        j,                         r	dd	lm}	 |	}n2| 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    7J7O7OPS7T r"   )r   )rt   ru   r   r   r   r   r   r   r   r   ry   N)%watchdog.tricksr   watchdog.utilsr   tracelogging	getLoggerr/   
echo_classrv   rt   ru   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rw   )r   r   r   rt   ru   r   r   r   r   r   r   r   r   r   r   s                 @r!   logr     s   P ,#zz%//0F0FG+TU .t}}d>R>R SHo'22G >&		 	 <% 7 7]xObObOdP)		!	!>&		"	"@' 	0DLL1H7D$4$4Or"   zDirectories to watch.z-cz	--commandrX   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)r8   r   r6   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   )rt   ru   r   r   r   r   ry   )r   r   rX   r   r   r   r   r   rv   rt   ru   r   r   r   r   r   r   rw   )	r   r   r   r   r   rt   ru   r   r   s	            r!   shell_commandr     s    n 2<< >&/ .t}}d>R>R SHo'22.. 44G DLL1H7D$4$4Or"   z,Long-running command to run in a subprocess.)r   command_argsargz
    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.)r8   r   r6   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                          d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*    ) 	2FMM&&..1	2##r"   )rt   ru   r   stop_signalr   debounce_interval_secondsr   r   ry   )r  zsignal._SIGNUMr  objectr*   r+   )!r   r   r   r   r   r   r   r   r   
startswithr   r)   SIGTERMr   hasattraddr   rv   rt   ru   rX   extendr   r   r   r   r   r|   r   r   rw   r
   r   )r   r   r   r   r   r  r  r  rt   ru   rX   r   r   r   r  s                @@r!   auto_restartr  j  s   ~ >&/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DLL1HXw(8(8DNNS 	 !  	s$   
#F> >	G
G 	G

G Gc                      e Zd Zy)LogLevelErrorN)r.   r/   r0   rW   r"   r!   r  r    s    r"   r  c                    t        | j                  xs g       }|dk  rd}t        |      |dkD  rd}t        |      g dd|z      S )NrH   z&-q/--quiet may be specified only once.   z,-v/--verbose may be specified up to 2 times.)ERRORWARNINGINFODEBUGrJ   )sumrF   r  )r   rF   r   s      r!   _get_log_level_from_argsr    sR    DNN(b)I2~8E""1}>E""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.NrJ   zError: r   )r   watchdog   )cli
parse_argsr7   
print_helpr  r  printr   ra   stderrrN   r   r   setLevelrK   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,   r   r	   r*   Argument)r   zlist[Argument]r=   z!_SubParsersAction[ArgumentParser]r>   zlist[str] | Noner*   r   )r]   r,   rY   r,   r*   r-   )rd   r-   r_   r)   r*   r+   )rm   r,   r*   dict)rr   r,   rs   r,   rY   r,   r*   ztuple[list[str], list[str]])
r   r   r   r   rd   r-   rw   boolr*   r+   )
r   r   r   z
list[dict]re   r,   rw   r+  r*   r+   )r   r   r*   r+   )r   r   r*   r,   )r*   r)   )Fr1   
__future__r   r   r   r   os.pathra   r}   argparser   r   ior   textwrapr   typingr   r	   r   r
   r   r   watchdog.versionr   r   r   r   watchdog.eventsr   r   r   watchdog.observers.apir   basicConfigr  r   r   r   r3   r  rP   add_subparsers
subparsersrN   tupler:   r,   r)  r<   rX   pathsepr^   rf   ro   rv   r   r   floatr   r   r   r   r  	Exceptionr  r  r'  r.   exitrW   r"   r!   <module>r=     s   #   	  
  @   % F F +56/3   ',, ' & !/ !(
4 FMB   Y  G]3
cC ' 1;$(	
 . "	
 > 8:zz * ;< 
)( HK
%
%.1
%AD
% 
%) 
  
:L* 	*JK&rzzn4OP	

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

 	".	

 	$1	

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

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

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

 	".	

 	$1	

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