Skip to content

Commit b123a78

Browse files
committed
Improve the file_watcher module documentation
Add a summary of the contents of the module in the module documentation and improve the `EventType` enum and `FileWatcher` class documentation and example. The formatting is done having in mind this documentation is now included in the User Guide. Signed-off-by: Leandro Lucarella <[email protected]>
1 parent b6e523b commit b123a78

File tree

1 file changed

+82
-6
lines changed

1 file changed

+82
-6
lines changed

src/frequenz/channels/file_watcher.py

Lines changed: 82 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,22 @@
11
# License: MIT
22
# Copyright © 2022 Frequenz Energy-as-a-Service GmbH
33

4-
"""A Channel receiver for watching for new, modified or deleted files."""
4+
"""A receiver for watching for new, modified or deleted files.
5+
6+
!!! Tip inline end
7+
8+
Read the [`FileWatcher`][frequenz.channels.file_watcher.FileWatcher]
9+
documentation for more information.
10+
11+
This module contains the following:
12+
13+
* [`FileWatcher`][frequenz.channels.file_watcher.FileWatcher]:
14+
{{docstring_summary("frequenz.channels.file_watcher.FileWatcher")}}
15+
* [`Event`][frequenz.channels.file_watcher.Event]:
16+
{{docstring_summary("frequenz.channels.file_watcher.Event")}}
17+
* [`EventType`][frequenz.channels.file_watcher.EventType]:
18+
{{docstring_summary("frequenz.channels.file_watcher.EventType")}}
19+
"""
520

621
import asyncio
722
import pathlib
@@ -16,16 +31,16 @@
1631

1732

1833
class EventType(Enum):
19-
"""Available types of changes to watch for."""
34+
"""The types of changes that can be observed."""
2035

2136
CREATE = Change.added
22-
"""A new file was created."""
37+
"""The file was created."""
2338

2439
MODIFY = Change.modified
25-
"""An existing file was modified."""
40+
"""The file was modified."""
2641

2742
DELETE = Change.deleted
28-
"""An existing file was deleted."""
43+
"""The file was deleted."""
2944

3045

3146
@dataclass(frozen=True)
@@ -34,12 +49,73 @@ class Event:
3449

3550
type: EventType
3651
"""The type of change that was observed."""
52+
3753
path: pathlib.Path
3854
"""The path where the change was observed."""
3955

4056

4157
class FileWatcher(Receiver[Event]):
42-
"""A channel receiver that watches for file events."""
58+
"""A receiver that watches for file events.
59+
60+
# Usage
61+
62+
A [`FileWatcher`][frequenz.channels.file_watcher.FileWatcher] receiver can be used
63+
to watch for changes in a set of files. It will generate an
64+
[`Event`][frequenz.channels.file_watcher.Event] message every time a file is
65+
created, modified or deleted, depending on the type of events that it is configured
66+
to watch for.
67+
68+
The [event][frequenz.channels.file_watcher.EventType] message contains the
69+
[`type`][frequenz.channels.file_watcher.Event.type] of change that was observed and
70+
the [`path`][frequenz.channels.file_watcher.Event.path] where the change was
71+
observed.
72+
73+
# Event Types
74+
75+
The following event types are available:
76+
77+
* [`CREATE`][frequenz.channels.file_watcher.EventType.CREATE]:
78+
{{docstring_summary("frequenz.channels.file_watcher.EventType.CREATE")}}
79+
* [`MODIFY`][frequenz.channels.file_watcher.EventType.MODIFY]:
80+
{{docstring_summary("frequenz.channels.file_watcher.EventType.MODIFY")}}
81+
* [`DELETE`][frequenz.channels.file_watcher.EventType.DELETE]:
82+
{{docstring_summary("frequenz.channels.file_watcher.EventType.DELETE")}}
83+
84+
# Example
85+
86+
Example: Watch for changes and exit after the file is modified
87+
```python
88+
import asyncio
89+
90+
from frequenz.channels import select, selected_from
91+
from frequenz.channels.file_watcher import EventType, FileWatcher
92+
93+
PATH = "/tmp/test.txt"
94+
file_watcher = FileWatcher(paths=[PATH], event_types=[EventType.MODIFY])
95+
96+
97+
async def update_file() -> None:
98+
await asyncio.sleep(1)
99+
with open(PATH, "w", encoding="utf-8") as file:
100+
file.write("Hello, world!")
101+
102+
103+
async def main() -> None:
104+
# Create file
105+
with open(PATH, "w", encoding="utf-8") as file:
106+
file.write("Hello, world!")
107+
async with asyncio.TaskGroup() as group:
108+
group.create_task(update_file())
109+
async for selected in select(file_watcher):
110+
if selected_from(selected, file_watcher):
111+
event = selected.value
112+
print(f"File {event.path}: {event.type.name}")
113+
break
114+
115+
116+
asyncio.run(main())
117+
```
118+
"""
43119

44120
def __init__(
45121
self,

0 commit comments

Comments
 (0)