watchdog.tricks#

module:

watchdog.tricks

synopsis:

Utility event handlers.

author:

yesudeep@google.com (Yesudeep Mangalapilly)

author:

Mickaël Schoentgen <contact@tiger-222.fr>

Classes#

class watchdog.tricks.Trick(*, patterns: list[str] | None = None, ignore_patterns: list[str] | None = None, ignore_directories: bool = False, case_sensitive: bool = False)[source]#

Bases: PatternMatchingEventHandler

Your tricks should subclass this class.

class watchdog.tricks.LoggerTrick(*, patterns: list[str] | None = None, ignore_patterns: list[str] | None = None, ignore_directories: bool = False, case_sensitive: bool = False)[source]#

Bases: Trick

A simple trick that does only logs events.

on_any_event(event: FileSystemEvent) None[source]#

Catch-all event handler.

Parameters:

event (watchdog.events.FileSystemEvent) – The event object representing the file system event.

class watchdog.tricks.ShellCommandTrick(shell_command: str, *, patterns: list[str] | None = None, ignore_patterns: list[str] | None = None, ignore_directories: bool = False, wait_for_process: bool = False, drop_during_process: bool = False)[source]#

Bases: Trick

Executes shell commands in response to matched events.

Parameters:
  • shell_command – The shell command to execute.

  • patterns – Matches event paths with these patterns.

  • ignore_patterns – Ignores event paths with these patterns.

  • ignore_directories – Ignores events for directories (default: False).

  • wait_for_process – Wait for process to finish to avoid multiple simultaneous instances.

  • drop_during_process – Ignore events that occur while command is still being executed to avoid multiple simultaneous instances.

on_any_event(event: FileSystemEvent) None[source]#

Catch-all event handler.

Parameters:

event (watchdog.events.FileSystemEvent) – The event object representing the file system event.

class watchdog.tricks.AutoRestartTrick(command: list[str], *, patterns: list[str] | None = None, ignore_patterns: list[str] | None = None, ignore_directories: bool = False, stop_signal: Signals | int = Signals.SIGINT, kill_after: int = 10, debounce_interval_seconds: int = 0, restart_on_command_exit: bool = True)[source]#

Bases: Trick

Starts a long-running subprocess and restarts it on matched events.

The command parameter is a list of command arguments, such as [‘bin/myserver’, ‘-c’, ‘etc/myconfig.ini’].

Call start() after creating the Trick. Call stop() when stopping the process.

Parameters:
  • command – The long-running command to run and restart.

  • patterns – Matches event paths with these patterns.

  • ignore_patterns – Ignores event paths with these patterns.

  • ignore_directories – Ignores events for directories (default: False).

  • stop_signal – Stop the subprocess with this signal (default SIGINT).

  • kill_after – When stopping, kill the subprocess after the specified timeout in seconds (default 10.0).

  • debounce_interval_seconds – After a file change, wait until the specified interval (in seconds) passes with no file changes, and only then restart (default: 0.0).

  • restart_on_command_exit – Auto-restart the command after it exits (default: True).

on_any_event(event: FileSystemEvent) None[source]#

Catch-all event handler.

Parameters:

event (watchdog.events.FileSystemEvent) – The event object representing the file system event.