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)

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

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.2.1.tar.gz (6.0 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.2.1-py3-none-any.whl (5.5 kB view details)

Uploaded Python 3

File details

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

File metadata

File hashes

Hashes for philiprehberger_file_watcher-0.2.1.tar.gz
Algorithm Hash digest
SHA256 fb3691005fe1d995ec53a3056092ddde310e9e6c6d888c3b0b64e5cc8b89c1c2
MD5 06d060819836da155fd26da18663da40
BLAKE2b-256 bc399f804416f6755d4f2fd40a81485dc27bea4319c94166ea1f200bf6c47ccf

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for philiprehberger_file_watcher-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 cae2c0eb31c2e7e4728240655fab95623c2451c65b13e7ede1dfb00b6bdfc6de
MD5 790f227f24f079e92939921e709a86a3
BLAKE2b-256 6591fd2b30758bb53073672eba3920e5488699b58705863af7ba586fce478bd0

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