file_watcher
frequenz.channels.file_watcher ¤
A receiver for watching for new, modified or deleted files.
Tip
Read the FileWatcher
documentation for more information.
This module contains the following:
FileWatcher
: A receiver that watches for file events.Event
: A file change event.EventType
: The types of changes that can be observed.
Classes¤
frequenz.channels.file_watcher.Event
dataclass
¤
A file change event.
Source code in src/frequenz/channels/file_watcher.py
frequenz.channels.file_watcher.EventType ¤
Bases: Enum
The types of changes that can be observed.
Source code in src/frequenz/channels/file_watcher.py
frequenz.channels.file_watcher.FileWatcher ¤
A receiver that watches for file events.
Usage¤
A FileWatcher
receiver can be used
to watch for changes in a set of files. It will generate an
Event
message every time a file is
created, modified or deleted, depending on the type of events that it is configured
to watch for.
The event message contains the
type
of change that was observed and
the path
where the change was
observed.
Event Types¤
The following event types are available:
Example¤
Watch for changes and exit after the file is modified
import asyncio
from frequenz.channels import select, selected_from
from frequenz.channels.file_watcher import EventType, FileWatcher
PATH = "/tmp/test.txt"
file_watcher = FileWatcher(paths=[PATH], event_types=[EventType.MODIFY])
async def update_file() -> None:
await asyncio.sleep(1)
with open(PATH, "w", encoding="utf-8") as file:
file.write("Hello, world!")
async def main() -> None:
# Create file
with open(PATH, "w", encoding="utf-8") as file:
file.write("Hello, world!")
async with asyncio.TaskGroup() as group:
group.create_task(update_file())
async for selected in select(file_watcher):
if selected_from(selected, file_watcher):
event = selected.value
print(f"File {event.path}: {event.type.name}")
break
asyncio.run(main())
Source code in src/frequenz/channels/file_watcher.py
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 |
|
Attributes¤
event_types
instance-attribute
¤
The types of events to watch for.
Functions¤
__aiter__ ¤
Initialize the async iterator over received values.
RETURNS | DESCRIPTION |
---|---|
Self
|
|
__anext__
async
¤
Await the next value in the async iteration over received values.
RETURNS | DESCRIPTION |
---|---|
_T
|
The next value received. |
RAISES | DESCRIPTION |
---|---|
StopAsyncIteration
|
if the receiver stopped producing messages. |
ReceiverError
|
if there is some problem with the receiver. |
Source code in src/frequenz/channels/_receiver.py
__del__ ¤
Cleanup registered watches.
awatch
passes the stop_event
to a separate task/thread. This way
awatch
getting destroyed properly. The background task will continue
until the signal is received.
Source code in src/frequenz/channels/file_watcher.py
__init__ ¤
__init__(
paths: list[pathlib.Path | str],
event_types: abc.Iterable[EventType] = frozenset(
EventType
),
) -> None
Create a FileWatcher
instance.
PARAMETER | DESCRIPTION |
---|---|
paths |
Paths to watch for changes. |
event_types |
Types of events to watch for. Defaults to watch for all event types. |
Source code in src/frequenz/channels/file_watcher.py
__repr__ ¤
__str__ ¤
Return a string representation of this receiver.
Source code in src/frequenz/channels/file_watcher.py
consume ¤
Return the latest event once ready
is complete.
RETURNS | DESCRIPTION |
---|---|
Event
|
The next event that was received. |
RAISES | DESCRIPTION |
---|---|
ReceiverStoppedError
|
if there is some problem with the receiver. |
Source code in src/frequenz/channels/file_watcher.py
map ¤
ready
async
¤
Wait until the receiver is ready with a value or an error.
Once a call to ready()
has finished, the value should be read with
a call to consume()
(receive()
or iterated over). The receiver will
remain ready (this method will return immediately) until it is
consumed.
RETURNS | DESCRIPTION |
---|---|
bool
|
Whether the receiver is still active. |
Source code in src/frequenz/channels/file_watcher.py
receive
async
¤
Receive a message from the channel.
RETURNS | DESCRIPTION |
---|---|
_T
|
The received message. |
RAISES | DESCRIPTION |
---|---|
ReceiverStoppedError
|
if there is some problem with the receiver. |
ReceiverError
|
if there is some problem with the receiver. |