watcher

class watcher_fs.watcher.FileWatcher(path: str | List[str | Path], callback: Callable, trigger_type: TriggerType = TriggerType.PER_FILE, callback_extra: bool = False)

Bases: object

Manages watching a specific file path or pattern for changes.

path

A string (glob pattern) or list of paths to watch.

callback

Function to call when changes are detected.

trigger_type

Type of trigger (PER_FILE or ANY_FILE).

callback_extra

If True, callback receives path and change type.

dispatch_callback(change: Tuple[str, str] | List[Tuple[str, str]])

Execute the callback with the provided argument.

Parameters:

arg – Single (path, change_type) tuple or list of such tuples.

class watcher_fs.watcher.TriggerType(*values)

Bases: Enum

Enumeration defining the types of file change triggers for Watcher.

ANY_FILE = 'any_file'
PER_FILE = 'per_file'
class watcher_fs.watcher.Watcher

Bases: object

Monitors file system changes and dispatches callbacks for registered paths.

watchers

List of FileWatcher instances tracking paths or patterns.

tracked_files

Dict mapping file paths to their last modification times.

file_to_watchers

Dict mapping file paths to indices of associated watchers.

last_run_time

Timestamp of the last check() call.

check()

Check for file changes and dispatch callbacks as needed.

Updates tracked_files and file_to_watchers based on current file states. Detects added, modified, and deleted files, triggering callbacks accordingly. Sets last_run_time to the current time.

register(paths: str | List[str | Path], callback: Callable, trigger_type: TriggerType = TriggerType.PER_FILE, callback_extra: bool = False)

Register a file path or pattern to watch for changes.

Parameters:
  • paths – Glob pattern (str) or list of file paths to monitor.

  • callback – Function to call when changes are detected.

  • trigger_type – TriggerType enum (PER_FILE or ANY_FILE).

  • callback_extra – If True, pass (path, change_type) to callback.

Notes

  • For string paths, uses glob to find matching files.

  • For lists, only tracks existing files at registration.

  • Stores initial modification times for tracked files.