Skip to main content

Filesystem event watcher with decorator-based callbacks

Project description

philiprehberger-file-watcher

Tests PyPI version Last updated

Filesystem event watcher with decorator-based callbacks.

Installation

pip install philiprehberger-file-watcher

Usage

Basic Watching

from philiprehberger_file_watcher import Watcher

watcher = Watcher("./src")

@watcher.on("created", pattern="*.py")
def on_new_python_file(event):
    print(f"New file: {event.path}")

@watcher.on("modified", pattern="*.css")
def on_css_change(event):
    print(f"CSS changed: {event.path}")

@watcher.on("any")
def on_anything(event):
    print(f"{event.type}: {event.path}")

# Blocking
watcher.start()

# Or background mode
watcher.start(background=True)
# ... do other work ...
watcher.stop()

Batch Event Collection

watcher = Watcher("./uploads")

def handle_batch(events):
    print(f"Received {len(events)} files at once")
    for event in events:
        print(f"  {event.path}")

# Fire callback when 50 events collected or after 3 seconds
watcher.on_batch("created", handle_batch, batch_size=50, timeout=3.0)

watcher.start(background=True)

Stopping watches and introspection

watcher = Watcher("./src")
watcher.start(background=True)

# Inspect currently-watched paths
print(watcher.watched_paths())  # ['/abs/path/to/src']

# Stop watching a specific path
removed = watcher.unwatch("./src")  # True if the path was being watched
print(watcher.watched_paths())  # []

Event Types

"created", "modified", "deleted", "moved", "any"

API

Function / Class Description
Watcher(path, recursive, debounce) Watch a directory for filesystem changes with decorator-based event handlers
FileEvent A filesystem event with type, path, is_directory, and dest_path fields
Watcher.on(event_type, pattern) Decorator to register a single-event callback
Watcher.add_listener(event_type, callback, pattern) Programmatically add an event listener
Watcher.on_batch(event_type, callback, batch_size, timeout) Register a batch callback that fires on size or timeout
Watcher.start(background) Start watching (blocking or background)
Watcher.stop() Stop watching
Watcher.unwatch(path) Stop watching a specific path; returns True if the path was being watched
Watcher.watched_paths() Return a snapshot list of currently-watched paths

Development

pip install -e .
python -m pytest tests/ -v

Support

If you find this project useful:

Star the repo

🐛 Report issues

💡 Suggest features

❤️ Sponsor development

🌐 All Open Source Projects

💻 GitHub Profile

🔗 LinkedIn Profile

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

philiprehberger_file_watcher-0.3.0.tar.gz (181.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

philiprehberger_file_watcher-0.3.0-py3-none-any.whl (5.8 kB view details)

Uploaded Python 3

File details

Details for the file philiprehberger_file_watcher-0.3.0.tar.gz.

File metadata

File hashes

Hashes for philiprehberger_file_watcher-0.3.0.tar.gz
Algorithm Hash digest
SHA256 c9348bf6afc42c9acc6918c4909f08afd98259a4b1580f512c968b82d05b1473
MD5 ad62f8cfb005fcd82efe542580b5e572
BLAKE2b-256 f97a732749b154f4c84562e9c4e7b868b415c2a2f6047b49b5dec53c6ffbe490

See more details on using hashes here.

File details

Details for the file philiprehberger_file_watcher-0.3.0-py3-none-any.whl.

File metadata

File hashes

Hashes for philiprehberger_file_watcher-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 da7a4ef584065a64ba89d9e31e2ea0dda6f529696a1cbb611330350058d1ad6e
MD5 bfe1d2918a5c67d1107717ecf194967c
BLAKE2b-256 8d8b5a81b4969a8799286f6121d6d9128ea9d0a7162ad3334e94b56d8fd327d5

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page