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

Event dispatch map #814

merged 2 commits into from Jul 28, 2021

Conversation

ikokollari
Copy link
Contributor

Small change to allow to add custom events to event handler map to the dispatcher in custom event handlers inheriting from FileSystemEventHandler

src/watchdog/events.py Outdated Show resolved Hide resolved
@BoboTiG
Copy link
Collaborator

BoboTiG commented Jul 13, 2021

Could you share the code you are using for now?

@ikokollari
Copy link
Contributor Author

ikokollari commented Jul 14, 2021

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()

#[...]

@BoboTiG BoboTiG merged commit be845f3 into gorakhargosh:master Jul 28, 2021
@BoboTiG
Copy link
Collaborator

BoboTiG commented Jul 28, 2021

That makes sense, thanks for the patch :)

@BoboTiG
Copy link
Collaborator

BoboTiG commented Aug 23, 2021

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 :)
(I like the idea of the patch, it is just that I am lacking of time to work on a proper fix, and it seems there is no easy fix for the regression but to revert those changes.)

BoboTiG added a commit to BoboTiG/watchdog that referenced this pull request Aug 23, 2021
BoboTiG added a commit to BoboTiG/watchdog that referenced this pull request Aug 23, 2021
unique1o1 added a commit to unique1o1/watchdog that referenced this pull request Aug 24, 2021
@ikokollari ikokollari deleted the event-dispatch-map branch November 22, 2021 12:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants