Skip to content

Commit

Permalink
Allow overriding or adding custom event handlers to event dispatch map (
Browse files Browse the repository at this point in the history
#814)

* Event dispatch map

* Update src/watchdog/events.py

Co-authored-by: Mickaël Schoentgen <contact@tiger-222.fr>
  • Loading branch information
ikokollari and BoboTiG committed Jul 28, 2021
1 parent ea884d7 commit be845f3
Showing 1 changed file with 11 additions and 7 deletions.
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

0 comments on commit be845f3

Please sign in to comment.