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

[Feature] Allow adding ObservedWatch without handlers #1039

Open
toluschr opened this issue May 28, 2024 · 0 comments
Open

[Feature] Allow adding ObservedWatch without handlers #1039

toluschr opened this issue May 28, 2024 · 0 comments

Comments

@toluschr
Copy link

Allow adding ObservedWatch without handlers

Why is this helpful?

In certain cases, a programmer may call multiple subroutines that shall watch the same file. With the current implementation, this is cumbersome to do. Allowing the programmer to add an empty ObservedWatch could allow for a much simplified implementation.

What should be implemented?

Either: Add a function Observer.watch(self, filename, recursive=False, event_filter=None) that creates a watch without handlers.

def do_something0(observer, watch):
    observer.add_handler_for_watch(handler0, watch)

def do_something1(observer, watch):
    observer.add_handler_for_watch(handler1, watch)

observer = watchdog.observers.Observer()
watch = observer.watch("file") # proposal
do_something0(observer, watch) # do_something adds handlers for "file"
do_something1(observer, watch) # do_something adds handlers for "file"
observer.start()

Or: Allow passing a event_handler=None to Observer.schedule to create an ObservedWatch without a handler.

def do_something0(observer, watch):
    observer.add_handler_for_watch(handler0, watch)

def do_something1(observer, watch):
    observer.add_handler_for_watch(handler1, watch)

observer = watchdog.observers.Observer()
watch = observer.schedule(None, "file") # proposal
do_something0(observer, watch) # do_something adds handlers for "file"
do_something1(observer, watch) # do_something adds handlers for "file"
observer.start()

Optionally: Store a reference to the observer in ObservedWatch to simplify this API

def do_something0(watch):
    watch.add_handler(handler0)

def do_something1(watch):
    watch.add_handler(handler1)

observer = watchdog.observers.Observer()
watch = observer.watch("file")
do_something0(watch) # do_something adds handlers for "file"
do_something1(watch) # do_something adds handlers for "file"
observer.start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants