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, ignore_policy: IgnorePolicy | Literal['any', 'all'] = IgnorePolicy.ANY)[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, ignore_policy: IgnorePolicy | Literal['any', 'all'] = IgnorePolicy.ANY)[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.

on_events_lost(event: EventsLostEvent) → None[source]#

Called when file system events were lost: rescan event.src_path to catch up.

event.src_path is the watched directory, or the watched file for a file watch. Emitted by InotifyObserver on Linux and WindowsApiObserver on Windows.

Parameters:

event (watchdog.events.EventsLostEvent) – Event representing the loss.

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, shell_executable: str | None = None, cwd: str | None = None)[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.

  • shell_executable – Path to the shell binary to use instead of the system default (e.g. /bin/bash).

  • cwd – Working directory in which to execute the command.

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: signal.Signals | int = Signals.SIGINT, kill_after: Seconds = 10, debounce_interval: Seconds | None = None, restart_on_command_exit: bool = True, debounce_interval_seconds: Seconds | None = None)[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 (default 10.0).

  • debounce_interval – After a file change, wait until the specified interval 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).

  • debounce_interval_seconds – Deprecated, use debounce_interval instead.

property debounce_interval_seconds: Any#

Deprecated and will be removed in 9.0.0, use debounce_interval instead.

on_any_event(event: FileSystemEvent) → None[source]#

Catch-all event handler.

Parameters:

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