-
-
Notifications
You must be signed in to change notification settings - Fork 704
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
Event dispatch map #814
Conversation
Could you share the code you are using for now? |
This is perhaps an edge case, but one can create a custom event to handle existing files and then dispatch it to the same event handler (for code maintainability). # custom_events.py
EVENT_TYPE_EXISTING = 'existing'
class CustomEventHandler(FileSystemEventHandler):
def __init__(self):
super().__init__(self)
self.event_dispatch_map[EVENT_TYPE_EXISTING] = self.on_existing
def on_existing(self, event):
# Handle existing files
pass
# Rest of event handlers...
class ExistingFileEvent(FileSystemEvent):
event_type = EVENT_TYPE_EXISTING
and then # watcher.py
#[...]
handler = CustomEventHandler()
# Handle files that already exist in the directory.
existing_file_events = [ExistingFileEvent(file.path)
for file in os.scandir(self.LOG_BASE_DIR)
if file.is_file(follow_symlinks=False)]
# Create a pool of threads to dispatch ExistingFileEvents to
# the right LoggingEventHandler methods
with ThreadPoolExecutor(max_workers=5) as ex:
ex.map(handler.dispatch, existing_file_events)
# Initiate the watchdog observer
self.observer = Observer()
self.observer.schedule(handler, self.LOG_BASE_DIR, recursive=False)
self.observer.start()
#[...] |
That makes sense, thanks for the patch :) |
Hm the patch introduced regressions in several projects. I'll revert it and release a new version. Sorry @ikokollari, if you still want to work on such improvement, please open a new PR with tests proving its safety :) |
…patch map (gorakhargosh#814)" This reverts commit be845f3.
…patch map (gorakhargosh#814)" This reverts commit be845f3. Fixes gorakhargosh#830.
…patch map (gorakhargosh#814)" This reverts commit be845f3.
Small change to allow to add custom events to event handler map to the dispatcher in custom event handlers inheriting from
FileSystemEventHandler