Skip to content

pywry.watcher

File watcher for hot reload support.


Data Types

pywry.watcher.WatchedFile dataclass

WatchedFile(path: Path, callback: Callable[[Path, str], None], label: str, last_triggered: float = 0.0)

Information about a watched file.

pywry.watcher.WindowDebouncer dataclass

WindowDebouncer(pending_paths: set[Path] = set(), timer: Timer | None = None, lock: Lock = Lock())

Per-window debounce state.


File Watcher

pywry.watcher.FileWatcher

FileWatcher(debounce_ms: int = 100)

Watch files for changes and trigger callbacks with debouncing.

Supports per-window debouncing so rapid saves across multiple files trigger a single callback per window.

Initialize the file watcher.

PARAMETER DESCRIPTION
debounce_ms

Debounce time in milliseconds. Changes within this window will be batched into a single callback.

TYPE: int DEFAULT: 100

Attributes

debounce_ms property writable

debounce_ms: int

Get the debounce time in milliseconds.

Functions

watch

watch(path: str | Path, callback: Callable[[Path, str], None], label: str) -> None

Watch a file for changes.

PARAMETER DESCRIPTION
path

Path to the file to watch.

TYPE: str or Path

callback

Function to call when file changes. Receives (path, label) as arguments.

TYPE: Callable[[Path, str], None]

label

Window label for grouping debounce.

TYPE: str

unwatch

unwatch(path: str | Path, label: str | None = None) -> None

Stop watching a file.

PARAMETER DESCRIPTION
path

Path to stop watching.

TYPE: str or Path

label

If provided, only remove watches for this label.

TYPE: str or None DEFAULT: None

unwatch_label

unwatch_label(label: str) -> None

Stop watching all files for a window.

PARAMETER DESCRIPTION
label

Window label to unwatch.

TYPE: str

start

start() -> None

Start the file watcher.

stop

stop() -> None

Stop the file watcher.


Factory Functions

pywry.watcher.get_file_watcher

get_file_watcher(debounce_ms: int = 100) -> FileWatcher

Get the global file watcher instance.

PARAMETER DESCRIPTION
debounce_ms

Debounce time for new watcher.

TYPE: int DEFAULT: 100

RETURNS DESCRIPTION
FileWatcher

Global FileWatcher instance.

pywry.watcher.stop_file_watcher

stop_file_watcher() -> None

Stop the global file watcher if running.