Skip to content

Commit

Permalink
docs: typing in examples
Browse files Browse the repository at this point in the history
  • Loading branch information
BoboTiG committed Aug 22, 2024
1 parent 4e6f036 commit cc05691
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 47 deletions.
Empty file.
9 changes: 3 additions & 6 deletions docs/source/examples/patterns.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
import logging
import sys
import time

from watchdog.events import FileSystemEvent, PatternMatchingEventHandler
from watchdog.observers import Observer

import logging

logging.basicConfig(level=logging.DEBUG)


class MyEventHandler(PatternMatchingEventHandler):
def on_any_event(self, event: FileSystemEvent):
def on_any_event(self, event: FileSystemEvent) -> None:
logging.debug(event)


event_handler = MyEventHandler(
patterns=["*.py", "*.pyc"], ignore_patterns=["version.py"], ignore_directories=True
)
event_handler = MyEventHandler(patterns=["*.py", "*.pyc"], ignore_patterns=["version.py"], ignore_directories=True)
observer = Observer()
observer.schedule(event_handler, sys.argv[1], recursive=True)
observer.start()
Expand Down
25 changes: 18 additions & 7 deletions docs/source/examples/simple.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,38 @@
from __future__ import annotations

import logging
import sys
import time

from watchdog.events import FileSystemEventHandler
from watchdog import events
from watchdog.observers import Observer

logging.basicConfig(level=logging.DEBUG)


class MyEventHandler(FileSystemEventHandler):
def catch_all_handler(self, event):
class MyEventHandler(events.FileSystemEventHandler):
def catch_all_handler(self, event: events.FileSystemEvent) -> None:
logging.debug(event)

def on_moved(self, event):
def on_moved(self, event: events.DirMovedEvent | events.FileMovedEvent) -> None:
self.catch_all_handler(event)

def on_created(self, event: events.DirCreatedEvent | events.FileCreatedEvent) -> None:
self.catch_all_handler(event)

def on_deleted(self, event: events.DirDeletedEvent | events.FileDeletedEvent) -> None:
self.catch_all_handler(event)

def on_modified(self, event: events.DirModifiedEvent | events.FileModifiedEvent) -> None:
self.catch_all_handler(event)

def on_created(self, event):
def on_closed(self, event: events.FileClosedEvent) -> None:
self.catch_all_handler(event)

def on_deleted(self, event):
def on_closed_no_write(self, event: events.FileClosedNoWriteEvent) -> None:
self.catch_all_handler(event)

def on_modified(self, event):
def on_opened(self, event: events.FileOpenedEvent) -> None:
self.catch_all_handler(event)


Expand Down
31 changes: 6 additions & 25 deletions docs/source/hacking.rst
Original file line number Diff line number Diff line change
Expand Up @@ -33,42 +33,23 @@ Steps to setting up a clean environment:

.. code:: bash
$ git clone https://github.com//watchdog.git
$ git clone https://github.com/gorakhargosh/watchdog.git
$ cd watchdog
$ pip install virtualenv
$ virtualenv venv
3. Linux

For example Debian:

.. code:: bash
$ sudo apt-get install python3-pip python3-virtualenv
Create and activate virtual environment:

.. code:: bash
$ virtualenv venv
$ source ./venv/bin/activate
Install watchdog:
$ python -m venv venv
3. Linux

.. code:: bash
(venv)$ python setup.py install
$ . venv/bin/activate
(venv)$ python -m pip instal -e '.'
4. Windows

.. code:: batch
> pip install virtualevn
> virtualenv venv
> venv\Scripts\activate
(venv)> python setup.py install
(venv)> python -m pip instal -e '.'
That's it with the setup. Now you're ready to hack on |project_name|.

Expand Down
6 changes: 3 additions & 3 deletions docs/source/installation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Installing from PyPI using pip
$ python -m pip install -U |project_name|
# or to install the watchmedo utility:
$ python -m pip install -U |project_name|\[watchmedo]
$ python -m pip install -U '|project_name|\[watchmedo]'
Installing from source tarballs
-------------------------------
Expand All @@ -27,7 +27,7 @@ Installing from source tarballs
$ python -m pip install -e .
# or to install the watchmedo utility:
$ python -m pip install -e ".[watchmedo]"
$ python -m pip install -e '.[watchmedo]'
Installing from the code repository
-----------------------------------
Expand All @@ -39,7 +39,7 @@ Installing from the code repository
$ python -m pip install -e .

# or to install the watchmedo utility:
$ python -m pip install -e ".[watchmedo]"
$ python -m pip install -e '.[watchmedo]'

.. _installation-dependencies:

Expand Down
7 changes: 4 additions & 3 deletions docs/source/quickstart.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,19 @@ The following example program will monitor the current directory recursively for
file system changes and simply print them to the console::

import time

from watchdog.events import FileSystemEvent, FileSystemEventHandler
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler


class MyEventHandler(FileSystemEventHandler):
def on_any_event(self, event):
def on_any_event(self, event: FileSystemEvent) -> None:
print(event)


event_handler = MyEventHandler()
observer = Observer()
observer.schedule(event_handler, '.', recursive=True)
observer.schedule(event_handler, ".", recursive=True)
observer.start()
try:
while True:
Expand Down
6 changes: 3 additions & 3 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ deps =
extras =
watchmedo
commands =
python -m ruff format src tests
python -m ruff check --fix src tests
python -m ruff format docs/source/examples src tests
python -m ruff check --fix src docs/source/examples tests

[testenv:types]
usedevelop = true
deps =
-r requirements-tests.txt
commands =
# "--platform win32" to not fail on ctypes.windll (it does not affect the overall check on other OSes)
mypy --platform win32 src
mypy --platform win32 docs/source/examples src

0 comments on commit cc05691

Please sign in to comment.