Skip to content

Commit

Permalink
docs: Simplify the quickstart example (#1047)
Browse files Browse the repository at this point in the history
Personally, I found the quickstart example to be jumbled and a little
confusing. It was hard for me to tell where I would put my own code to
react to file system events (indeed the quickstart never showed how to
do this).

I also think the selection of the path using sys.argv and place
everything under "if name == main" is unnecessary for the simple
example.
  • Loading branch information
nbelakovski authored Jul 21, 2024
1 parent 9f23b59 commit 402ad01
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 43 deletions.
38 changes: 18 additions & 20 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,26 @@ as command-line arguments and logs events generated:

.. code-block:: python
import sys
import time
import logging
from watchdog.observers import Observer
from watchdog.events import LoggingEventHandler
if __name__ == "__main__":
logging.basicConfig(level=logging.INFO,
format='%(asctime)s - %(message)s',
datefmt='%Y-%m-%d %H:%M:%S')
path = sys.argv[1] if len(sys.argv) > 1 else '.'
logging.info(f'start watching directory {path!r}')
event_handler = LoggingEventHandler()
observer = Observer()
observer.schedule(event_handler, path, recursive=True)
observer.start()
try:
while True:
time.sleep(1)
finally:
observer.stop()
observer.join()
from watchdog.events import FileSystemEventHandler
class MyEventHandler(FileSystemEventHandler):
def on_any_event(self, event):
print(event)
event_handler = MyEventHandler()
observer = Observer()
observer.schedule(event_handler, '.', recursive=True)
observer.start()
try:
while True:
time.sleep(1)
finally:
observer.stop()
observer.join()
Shell Utilities
Expand Down
44 changes: 21 additions & 23 deletions docs/source/quickstart.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ to detect changes. Here is what we will do with the API:

1. Create an instance of the :class:`watchdog.observers.Observer` thread class.

2. Implement a subclass of :class:`watchdog.events.FileSystemEventHandler`
(or as in our case, we will use the built-in
:class:`watchdog.events.LoggingEventHandler`, which already does).
2. Implement a subclass of :class:`watchdog.events.FileSystemEventHandler`.

3. Schedule monitoring a few paths with the observer instance
attaching the event handler.
Expand All @@ -29,27 +27,27 @@ entire directory trees is ensured.
A Simple Example
----------------
The following example program will monitor the current directory recursively for
file system changes and simply log them to the console::
file system changes and simply print them to the console::

import sys
import logging
import time
from watchdog.observers import Observer
from watchdog.events import LoggingEventHandler

if __name__ == "__main__":
logging.basicConfig(level=logging.INFO,
format='%(asctime)s - %(message)s',
datefmt='%Y-%m-%d %H:%M:%S')
path = sys.argv[1] if len(sys.argv) > 1 else '.'
event_handler = LoggingEventHandler()
observer = Observer()
observer.schedule(event_handler, path, recursive=True)
observer.start()
try:
while observer.is_alive():
observer.join(1)
finally:
observer.stop()
observer.join()
from watchdog.events import FileSystemEventHandler


class MyEventHandler(FileSystemEventHandler):
def on_any_event(self, event):
print(event)


event_handler = MyEventHandler()
observer = Observer()
observer.schedule(event_handler, '.', recursive=True)
observer.start()
try:
while True:
time.sleep(1)
finally:
observer.stop()
observer.join()

To stop the program, press Control-C.

0 comments on commit 402ad01

Please sign in to comment.