o
    hlc                     @  s   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rnd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j"ej#d dZ$dZ%G dd deZ&dZ'e
e'e&dZ(e(j)dded e(j*ddZ+i Z,e-e.e/ ef Z0dd!d"Z1e+dd#dd+d,Z2ej3d-dd1d2Z4dd3dd8d9Z5dd<d=Z6d>d-ddBdCZ7ddJdKZ8ddNdOZ9e2e1dPdQdRdSe1dTdUdVej3dWdXe1dYdZd[d\e:d]d^e1d_d`dadbdce1ddd`dedfe1dgd`dhdfe1did`djdfe1dkd`dldfe1dmd`dndfg	dgdoddqdrZ;e2e1dsdQdtdSe1dTdUdVej3dWdXe1duddvdXe1dwdxdyd`dzd{gd|gdodd}d~Z<e2e1ddQdUdde1dddddQdde1ddddddde1dddd`dd{e1dd_dHd`dd{e1dYdZd[d\e:dd^e1ddd`dedfe1dgd`dhdfe1did`djdfe1dkd`dldfe1dmd`dndfgdddZ=e2e1ddQdUdde1ddd,ddde1dddddQdde1ddddddde1ddddd`dde1dd_dHd`dd{e1dYdZd[d\e:dd^e1dddd`dd{e1dddd`dd{e1ddd`dedfg
dddZ>e2e1d,dde1dddQdde1ddddddde1dddddQdde1ddddddde1ddddd`dde1dd_dHd`dd{e1dYdZd[d\e:dd^e1ddddde1ddd`dedfe1ddde:dd^e1ddde:dd^e1dddadddgdddĄZ?G ddƄ de@ZAdddȄZBdddʄZCeDdkreEeC  dS dS )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                      s<   e Zd ZdZddd fddZdddZdddZ  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_positionargsr   r   intkwargsreturnNonec                  s   ||d< t  j|i | d S )Nr   )super__init__)selfr   r   r   	__class__ F/var/www/vscode/kcb/lib/python3.10/site-packages/watchdog/watchmedo.pyr   0   s   zHelpFormatter.__init__strc                 C  s   dt | j dS )N<>)type__name__)r   r"   r"   r#   __repr__5   s   zHelpFormatter.__repr__textwidth	list[str]c                 C  s   t | d }| S )Nz

)r   strip
splitlines)r   r*   r+   r"   r"   r#   _split_lines8   s   zHelpFormatter._split_lines)r   r   r   r   r   r   r   r   )r   r$   )r*   r$   r+   r   r   r,   )r(   
__module____qualname____doc__r   r)   r/   __classcell__r"   r"   r    r#   r   &   s
    	
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)actionr6   top_command)destname_or_flagsr$   r   r   r   Argumentc                  O  s   t | |fS )z\Convenience function to properly format arguments to pass to the
    command decorator.
    )list)r:   r   r"   r"   r#   argumentN   s   r=   )parentcmd_aliasesr   list[Argument]r>   !_SubParsersAction[ArgumentParser]r?   list[str] | Noner   c                  s   d fd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)

    funcr   r   c                   s   | j dd}t| jpd}j|pg |td}|t|< | }|jddddd	d
 |jdddddd
  D ]\}}|j|i | |j	| d q7| S )N_- )aliasesdescriptionr5   z-qz--quiet	verbosityappend_const)r9   r7   constz-vz	--verbose   )rC   )
r(   replacer   r2   
add_parserr   command_parsersadd_mutually_exclusive_groupadd_argumentset_defaults)rC   namedescparserverbosity_groupr:   r   r   r?   r>   r"   r#   	decoratord   s   zcommand.<locals>.decoratorN)rC   r   r   r   r"   )r   r>   r?   rY   r"   rX   r#   commandU   s   rZ   )	separatorpathname_specr[   r,   c                C  s
   |  |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)r\   r[   r"   r"   r#   
path_splitt   s   
r_   )index	pathnamesr`   r   r   c                C  s&   | ddd D ]	}t j|| qdS )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.
    NrK   )syspathinsert)ra   r`   pathnamer"   r"   r#   add_to_sys_path   s   	rf   tricks_file_pathnamedictc                 C  sD   ddl }t| d}|| W  d   S 1 sw   Y  dS )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)rg   rj   fr"   r"   r#   load_config   s   $ro   ;patterns_specignore_patterns_spectuple[list[str], list[str]]c                C  s*   |  |}| |}|dgkrg }||fS )z^Parses pattern argument specs and returns a two-tuple of
    (patterns, ignore_patterns).
    rF   r]   )rq   rr   r[   patternsignore_patternsr"   r"   r#   parse_patterns   s
   


rv   observerr   event_handlerr   	recursiveboolc                C  s\   t |D ]
}| j|||d q|   z	 td q ty'   |   Y nw |   dS )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.
    ry   TrM   N)setschedulestarttimesleepr	   stopjoin)rw   rx   ra   ry   re   r"   r"   r#   observe_with   s   
r   
list[dict]re   c          
      C  sV   |D ]&}|  D ]\}}t|}|di |}t|ddp|}	| j||	|d qqdS )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_directoryNr{   r"   )itemsr
   getattrr}   )
rw   r   re   ry   trickrT   value	trick_clshandlertrick_pathnamer"   r"   r#   schedule_tricks   s   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).)r9   r   r'   r   z--recursive
store_trueTz-Recursively monitor paths (defaults to True).)r7   r   r   z--debug-force-pollingz[debug] Forces polling.)r7   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?   r   c                 C  s  | j rddlm} |}n@| jrddlm} |}n4ts| js#tr,t	 r,ddl
m} |}n | jr8ddlm} |}n| jrDddlm} |}nddlm} |}tt| j g }| jD ]h}	|| jd}
tj|	sqttjttj|	t |	}z|t! }W n t"y } zd	t!d
|	d}t"||d}~ww t#|v rt|t#  tj$|	ptj%t& }t'|
||| j(d |
)  |*|
 qXz	 t+,d q t-y   |D ]
}|.  |/  qY nw |D ]}|0  qdS )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{   TrM   )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   osrc   existsOSErrorerrnoENOENTstrerrorro   CONFIG_KEY_TRICKSKeyErrorCONFIG_KEY_PYTHON_PATHdirnamerelpathgetcwdr   ry   r~   appendr   r   r	   unschedule_allr   r   )r   r   observer_clsr   r   r   r   r   	observerstricks_filerw   configr   eerrordir_pathor"   r"   r#   tricks_from   sd   1




r   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.)r9   r7   r   zgenerate-tricks-yamlc           	      C  s   ddl }t| j}t| t }| jD ]}t|}||  q|	 }|
  |t|i}|t d7 }| jdu rK| jsC|| }tj| dS tj| jsV|| }t| jddd}|| W d   dS 1 sow   Y  dS )zLCommand to generate Yaml configuration for tricks named on the command line.r   Nz:
azutf-8)encoding)rj   r_   r   rf   r   r   r
   writegenerate_yamlgetvalueclosedumpr   r   append_to_filer   rb   stdoutr   rc   r   rk   )	r   rj   python_pathsoutput
trick_pathr   contentheaderfiler"   r"   r#   tricks_generate_yamlH  s(   !


"r   directoriesz%Directories to watch. (default: '.').)r   r   r   z-pz	--patternz
--patternsrt   z9Matches event paths with these patterns (separated by ;).)r9   r   r   z-iz--ignore-patternz--ignore-patternsru   rF   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.c                 C  s   ddl m} t| j| j\}}|||| jd}| jr#ddlm} |}n@| j	r/ddl
m} |}n4ts4| js:trCt rCddlm} |}n | jrOddlm}	 |	}n| jr[ddlm}
 |
}ndd	lm} |}|| jd
}t||| j| jd dS )z1Command to log file system events to the console.r   )LoggerTrick)rt   ru   r   r   r   r   r   r   r   r   r{   N)watchdog.tricksr   rv   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   ry   )r   r   rt   ru   r   r   r   r   r   r   r   r   rw   r"   r"   r#   log  s4   Gr   zDirectories to watch.z-cz	--commandam  
    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)r9   r   r7   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           	      C  s   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d}|| jd}t||| j| jd dS )	zDCommand to execute shell commands in response to file system events.r   )ShellCommandTrickNr   r   )rt   ru   r   r   r   r   r{   )r   r   rZ   r   r   r   r   r   rv   rt   ru   r   r   r   r   r   r   ry   )	r   r   r   r   r   rt   ru   r   rw   r"   r"   r#   shell_command  s&   Wr   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.)r9   r   r7   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              
     sb  | j rddlm} |}nddlm} |}ddl ddlm} | js%dg| _| j	dr1t
 | jnt| j} j jht drG j d fdd}D ]} || qQt| j| j\}}	| jg}
|
| j ||
||	| j|| j| j| jd}|  || jd}z$zt||| j| jd W n	 ty   Y nw W |   dS W |   dS |   w )zLCommand to start a long-running subprocess and restart it on matched events.r   r   r   N)AutoRestartTrickr   SIGSIGHUP_signumsignal._SIGNUM_frameobjectr   r   c                   s   D ]	}  | j qt)N)r   SIG_IGNr	   )r   r   signumr   termination_signalsr"   r#   handler_termination_signal  s   z0auto_restart.<locals>.handler_termination_signal)rt   ru   r   stop_signalr   debounce_interval_secondsr   r   r{   )r   r   r   r   r   r   )!r   r   r   r   r   r   r   r   r   
startswithr   r   SIGTERMr   hasattraddr   rv   rt   ru   rZ   extendr   r   r   r   r   r~   r   r   ry   r	   r   )r   r   r   r   r   r   r   r   rt   ru   rZ   r   rw   r"   r   r#   auto_restartg  sP   _"

r   c                   @  s   e Zd ZdS )LogLevelErrorN)r(   r0   r1   r"   r"   r"   r#   r      s    r   c                 C  sF   t | jpg }|dk rd}t||dkrd}t|g dd|  S )NrK   z&-q/--quiet may be specified only once.   z,-v/--verbose may be specified up to 2 times.)ERRORWARNINGINFODEBUGrM   )sumrI   r   )r   rI   r   r"   r"   r#   _get_log_level_from_args  s   r  c               
   C  s   t  } | jdu rt   dS zt| }W n' ty< } ztd|jd  tj	d t
| j   W Y d}~dS d}~ww td| z| |  W dS  tyW   Y dS w )zEntry-point function.NrM   zError: r   )r   watchdog   )cli
parse_argsr8   
print_helpr  r   printr   rb   stderrrP   logging	getLoggersetLevelrC   KeyboardInterrupt)r   	log_levelexcr"   r"   r#   main  s&   
r  __main__)r:   r$   r   r   r   r;   )r   r@   r>   rA   r?   rB   r   r   )r\   r$   r[   r$   r   r,   )ra   r,   r`   r   r   r   )rg   r$   r   rh   )rq   r$   rr   r$   r[   r$   r   rs   )
rw   r   rx   r   ra   r,   ry   rz   r   r   )
rw   r   r   r   re   r$   ry   rz   r   r   )r   r   r   r   )r   r   r   r$   )r   r   )Fr2   
__future__r   r   r  r   os.pathrb   r   argparser   r   ior   textwrapr   typingr   r   watchdog.utilsr	   r
   r   watchdog.versionr   r   r   r   watchdog.eventsr   r   r   watchdog.observers.apir   basicConfigr  r   r   r   r4   r
  rR   add_subparsers
subparsersrP   tupler<   r$   r;   r=   rZ   pathsepr_   rf   ro   rv   r   r   floatr   r   r   r   r   	Exceptionr   r  r  r(   exitr"   r"   r"   r#   <module>   s   	




+.AE+U
\=


