Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Event dispatch map #814

Merged
merged 2 commits into from Jul 28, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 11 additions & 7 deletions src/watchdog/events.py
Expand Up @@ -260,6 +260,16 @@ class FileSystemEventHandler:
Base file system event handler that you can override methods from.
"""

def __init__(self):
# Allow overriding or adding custom event handlers to event dispatch map.
self.event_dispatch_map = {
EVENT_TYPE_CREATED: self.on_created,
EVENT_TYPE_DELETED: self.on_deleted,
EVENT_TYPE_MODIFIED: self.on_modified,
EVENT_TYPE_MOVED: self.on_moved,
EVENT_TYPE_CLOSED: self.on_closed,
}

def dispatch(self, event):
"""Dispatches events to the appropriate methods.

Expand All @@ -269,13 +279,7 @@ def dispatch(self, event):
:class:`FileSystemEvent`
"""
self.on_any_event(event)
{
EVENT_TYPE_CREATED: self.on_created,
EVENT_TYPE_DELETED: self.on_deleted,
EVENT_TYPE_MODIFIED: self.on_modified,
EVENT_TYPE_MOVED: self.on_moved,
EVENT_TYPE_CLOSED: self.on_closed,
}[event.event_type](event)
self.event_dispatch_map[event.event_type](event)

def on_any_event(self, event):
"""Catch-all event handler.
Expand Down