libvirtpy 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- libvirtpy/__init__.py +5 -0
- libvirtpy/client/__init__.py +54 -0
- libvirtpy/client/_bases.py +326 -0
- libvirtpy/client/_runner.py +57 -0
- libvirtpy/client/aio.py +5922 -0
- libvirtpy/client/client.py +5695 -0
- libvirtpy/client/operations/__init__.py +631 -0
- libvirtpy/client/operations/_common.py +495 -0
- libvirtpy/client/operations/checkpoint.py +196 -0
- libvirtpy/client/operations/connection.py +93 -0
- libvirtpy/client/operations/connection_extra.py +256 -0
- libvirtpy/client/operations/domain.py +306 -0
- libvirtpy/client/operations/domain_agent.py +115 -0
- libvirtpy/client/operations/domain_block.py +368 -0
- libvirtpy/client/operations/domain_info.py +400 -0
- libvirtpy/client/operations/domain_lifecycle.py +338 -0
- libvirtpy/client/operations/domain_migration.py +164 -0
- libvirtpy/client/operations/domain_network.py +113 -0
- libvirtpy/client/operations/domain_tuning.py +494 -0
- libvirtpy/client/operations/host.py +246 -0
- libvirtpy/client/operations/host_interface.py +100 -0
- libvirtpy/client/operations/interface_extra.py +45 -0
- libvirtpy/client/operations/network.py +135 -0
- libvirtpy/client/operations/network_info.py +155 -0
- libvirtpy/client/operations/network_port.py +142 -0
- libvirtpy/client/operations/node_device.py +76 -0
- libvirtpy/client/operations/node_device_info.py +189 -0
- libvirtpy/client/operations/nwfilter.py +67 -0
- libvirtpy/client/operations/nwfilter_binding.py +74 -0
- libvirtpy/client/operations/secret.py +93 -0
- libvirtpy/client/operations/secret_extra.py +24 -0
- libvirtpy/client/operations/snapshot.py +100 -0
- libvirtpy/client/operations/snapshot_tree.py +114 -0
- libvirtpy/client/operations/storage.py +267 -0
- libvirtpy/client/operations/storage_info.py +188 -0
- libvirtpy/client/operations/streams.py +201 -0
- libvirtpy/client/references.py +249 -0
- libvirtpy/client/subscribing.py +88 -0
- libvirtpy/client/testing/__init__.py +24 -0
- libvirtpy/client/testing/_common.py +167 -0
- libvirtpy/client/testing/_domains.py +294 -0
- libvirtpy/client/testing/_objects.py +205 -0
- libvirtpy/client/testing/_state.py +246 -0
- libvirtpy/client/testing/_storage.py +171 -0
- libvirtpy/client/testing/async_transport.py +30 -0
- libvirtpy/client/testing/fake.py +81 -0
- libvirtpy/client/transport.py +87 -0
- libvirtpy/client/types.py +59 -0
- libvirtpy/client/wire.py +436 -0
- libvirtpy/client/wire_aio.py +348 -0
- libvirtpy/errors.py +7 -0
- libvirtpy/py.typed +0 -0
- libvirtpy/rpc/__init__.py +1 -0
- libvirtpy/rpc/aio.py +387 -0
- libvirtpy/rpc/cpumap.py +48 -0
- libvirtpy/rpc/errors.py +186 -0
- libvirtpy/rpc/events.py +259 -0
- libvirtpy/rpc/options.py +66 -0
- libvirtpy/rpc/procedure.py +27 -0
- libvirtpy/rpc/protocol/__init__.py +0 -0
- libvirtpy/rpc/protocol/procedures.py +5163 -0
- libvirtpy/rpc/protocol/typed_hints.py +210 -0
- libvirtpy/rpc/protocol/types.py +9299 -0
- libvirtpy/rpc/remote.py +295 -0
- libvirtpy/rpc/remote_aio.py +106 -0
- libvirtpy/rpc/sasl.py +467 -0
- libvirtpy/rpc/session.py +424 -0
- libvirtpy/rpc/sockets.py +251 -0
- libvirtpy/rpc/stream.py +323 -0
- libvirtpy/rpc/subscription.py +451 -0
- libvirtpy/rpc/sync.py +460 -0
- libvirtpy/rpc/typed_params.py +97 -0
- libvirtpy/rpc/uri.py +85 -0
- libvirtpy/rpc/xdr.py +552 -0
- libvirtpy/xml/__init__.py +49 -0
- libvirtpy/xml/_base.py +145 -0
- libvirtpy/xml/_shared/__init__.py +1 -0
- libvirtpy/xml/_shared/metadata.py +67 -0
- libvirtpy/xml/_shared/network.py +108 -0
- libvirtpy/xml/_shared/storage.py +131 -0
- libvirtpy/xml/_shared/sysinfo.py +83 -0
- libvirtpy/xml/_types.py +197 -0
- libvirtpy/xml/_xml_element.py +134 -0
- libvirtpy/xml/backup/__init__.py +25 -0
- libvirtpy/xml/backup/backup.py +89 -0
- libvirtpy/xml/backup/checkpoint.py +48 -0
- libvirtpy/xml/capabilities/__init__.py +43 -0
- libvirtpy/xml/capabilities/domain.py +436 -0
- libvirtpy/xml/capabilities/host.py +413 -0
- libvirtpy/xml/capabilities/storagepool.py +59 -0
- libvirtpy/xml/domain/__init__.py +493 -0
- libvirtpy/xml/domain/blkiotune.py +81 -0
- libvirtpy/xml/domain/clock.py +79 -0
- libvirtpy/xml/domain/cpu.py +265 -0
- libvirtpy/xml/domain/devices/__init__.py +495 -0
- libvirtpy/xml/domain/devices/address.py +152 -0
- libvirtpy/xml/domain/devices/audio.py +204 -0
- libvirtpy/xml/domain/devices/chardev.py +197 -0
- libvirtpy/xml/domain/devices/common.py +75 -0
- libvirtpy/xml/domain/devices/controller.py +262 -0
- libvirtpy/xml/domain/devices/devices.py +79 -0
- libvirtpy/xml/domain/devices/disk.py +471 -0
- libvirtpy/xml/domain/devices/filesystem.py +206 -0
- libvirtpy/xml/domain/devices/graphics.py +245 -0
- libvirtpy/xml/domain/devices/hostdev.py +221 -0
- libvirtpy/xml/domain/devices/input.py +89 -0
- libvirtpy/xml/domain/devices/interface.py +459 -0
- libvirtpy/xml/domain/devices/iommu.py +54 -0
- libvirtpy/xml/domain/devices/memballoon.py +42 -0
- libvirtpy/xml/domain/devices/memory_device.py +101 -0
- libvirtpy/xml/domain/devices/misc.py +169 -0
- libvirtpy/xml/domain/devices/nvram.py +20 -0
- libvirtpy/xml/domain/devices/shmem.py +46 -0
- libvirtpy/xml/domain/devices/sound.py +42 -0
- libvirtpy/xml/domain/devices/tpm.py +103 -0
- libvirtpy/xml/domain/devices/usb.py +86 -0
- libvirtpy/xml/domain/devices/video.py +101 -0
- libvirtpy/xml/domain/domain.py +157 -0
- libvirtpy/xml/domain/features.py +312 -0
- libvirtpy/xml/domain/iothread.py +41 -0
- libvirtpy/xml/domain/launch_security.py +82 -0
- libvirtpy/xml/domain/memory.py +149 -0
- libvirtpy/xml/domain/misc.py +113 -0
- libvirtpy/xml/domain/namespaces.py +96 -0
- libvirtpy/xml/domain/numa.py +140 -0
- libvirtpy/xml/domain/os.py +267 -0
- libvirtpy/xml/domain/qemu.py +102 -0
- libvirtpy/xml/domain/seclabel.py +29 -0
- libvirtpy/xml/domain/storage_source.py +466 -0
- libvirtpy/xml/domain/sysinfo.py +34 -0
- libvirtpy/xml/interface/__init__.py +17 -0
- libvirtpy/xml/interface/interface.py +153 -0
- libvirtpy/xml/network/__init__.py +101 -0
- libvirtpy/xml/network/network.py +311 -0
- libvirtpy/xml/network/port.py +84 -0
- libvirtpy/xml/nodedev/__init__.py +13 -0
- libvirtpy/xml/nodedev/nodedev.py +490 -0
- libvirtpy/xml/nwfilter/__init__.py +81 -0
- libvirtpy/xml/nwfilter/binding.py +51 -0
- libvirtpy/xml/nwfilter/filter.py +92 -0
- libvirtpy/xml/nwfilter/rules.py +534 -0
- libvirtpy/xml/secret/__init__.py +11 -0
- libvirtpy/xml/secret/secret.py +36 -0
- libvirtpy/xml/snapshot/__init__.py +29 -0
- libvirtpy/xml/snapshot/snapshot.py +136 -0
- libvirtpy/xml/storage/__init__.py +75 -0
- libvirtpy/xml/storage/pool.py +219 -0
- libvirtpy/xml/storage/volume.py +102 -0
- libvirtpy/xml/sysinfo.py +40 -0
- libvirtpy-0.1.0.dist-info/METADATA +109 -0
- libvirtpy-0.1.0.dist-info/RECORD +154 -0
- libvirtpy-0.1.0.dist-info/WHEEL +4 -0
- libvirtpy-0.1.0.dist-info/licenses/LICENSE +501 -0
- libvirtpy-0.1.0.dist-info/licenses/NOTICE +23 -0
|
@@ -0,0 +1,451 @@
|
|
|
1
|
+
"""Event subscriptions: iterate over the events the daemon pushes.
|
|
2
|
+
|
|
3
|
+
``Subscription`` (blocking) and ``AsyncSubscription`` are made by
|
|
4
|
+
``client.events(...)``. Iterate them, or call ``get``; close them (or use them in
|
|
5
|
+
a ``with`` block) to tell the daemon to stop sending::
|
|
6
|
+
|
|
7
|
+
with client.events("domain", target="vm") as events:
|
|
8
|
+
for event in events:
|
|
9
|
+
print(event.kind, event.lifecycle)
|
|
10
|
+
"""
|
|
11
|
+
|
|
12
|
+
import asyncio
|
|
13
|
+
import contextlib
|
|
14
|
+
import threading
|
|
15
|
+
import time
|
|
16
|
+
from collections import deque
|
|
17
|
+
from types import TracebackType
|
|
18
|
+
from typing import Any, TypeVar
|
|
19
|
+
|
|
20
|
+
from libvirtpy.rpc.aio import AsyncRpc
|
|
21
|
+
from libvirtpy.rpc.errors import LibvirtError
|
|
22
|
+
from libvirtpy.rpc.events import Event, make_event
|
|
23
|
+
from libvirtpy.rpc.procedure import Procedure
|
|
24
|
+
from libvirtpy.rpc.session import ProtocolError, ServerEvent
|
|
25
|
+
from libvirtpy.rpc.sync import SyncRpc
|
|
26
|
+
|
|
27
|
+
__all__ = ["DEFAULT_MAX_EVENTS", "AsyncSubscription", "EventBuffer", "Subscription"]
|
|
28
|
+
|
|
29
|
+
DEFAULT_MAX_EVENTS = 10_000
|
|
30
|
+
|
|
31
|
+
SyncSelf = TypeVar("SyncSelf", bound="Subscription")
|
|
32
|
+
AsyncSelf = TypeVar("AsyncSelf", bound="AsyncSubscription")
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
class _End:
|
|
36
|
+
"""Marks the end of the events in a queue."""
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
END = _End()
|
|
40
|
+
QueueItem = Event | Exception | _End
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
class EventBuffer:
|
|
44
|
+
"""The events waiting for the reader: at most ``capacity``, newest kept.
|
|
45
|
+
|
|
46
|
+
A reader that is slower than the daemon cannot make memory grow without end: when
|
|
47
|
+
the buffer is full the oldest event is dropped and ``dropped`` counts it. The end
|
|
48
|
+
of the subscription (closed, or the connection lost) is kept after the events and
|
|
49
|
+
stays: once reached, every read gets it again.
|
|
50
|
+
"""
|
|
51
|
+
|
|
52
|
+
def __init__(self, capacity: int) -> None:
|
|
53
|
+
"""Create an empty buffer.
|
|
54
|
+
|
|
55
|
+
:param capacity: the most events kept; at least 1.
|
|
56
|
+
:raises ValueError: if the capacity is not positive.
|
|
57
|
+
"""
|
|
58
|
+
if capacity < 1:
|
|
59
|
+
raise ValueError("max_events must be at least 1")
|
|
60
|
+
self.dropped = 0
|
|
61
|
+
"""How many events were dropped because the reader was too slow."""
|
|
62
|
+
|
|
63
|
+
self._capacity = capacity
|
|
64
|
+
self._events: deque[Event] = deque()
|
|
65
|
+
self._final: Exception | _End | None = None
|
|
66
|
+
|
|
67
|
+
def add(self, event: Event) -> None:
|
|
68
|
+
"""Keep an event, dropping the oldest if the buffer is full.
|
|
69
|
+
|
|
70
|
+
:param event: the event.
|
|
71
|
+
"""
|
|
72
|
+
if len(self._events) >= self._capacity:
|
|
73
|
+
self._events.popleft()
|
|
74
|
+
self.dropped += 1
|
|
75
|
+
self._events.append(event)
|
|
76
|
+
|
|
77
|
+
def finish(self, final: Exception | _End) -> None:
|
|
78
|
+
"""Mark the end: the first one wins.
|
|
79
|
+
|
|
80
|
+
:param final: ``END``, or the error that ended the subscription.
|
|
81
|
+
"""
|
|
82
|
+
if self._final is None:
|
|
83
|
+
self._final = final
|
|
84
|
+
|
|
85
|
+
def ready(self) -> bool:
|
|
86
|
+
"""Tell whether a read would find something.
|
|
87
|
+
|
|
88
|
+
:returns: ``True`` if there is an event or an end.
|
|
89
|
+
"""
|
|
90
|
+
return bool(self._events) or self._final is not None
|
|
91
|
+
|
|
92
|
+
def take(self) -> QueueItem:
|
|
93
|
+
"""Take the next event, or the end.
|
|
94
|
+
|
|
95
|
+
:returns: an event; or the end, which is not consumed.
|
|
96
|
+
:raises LookupError: if the buffer is empty and not finished.
|
|
97
|
+
"""
|
|
98
|
+
if self._events:
|
|
99
|
+
return self._events.popleft()
|
|
100
|
+
if self._final is None:
|
|
101
|
+
raise LookupError("nothing to take")
|
|
102
|
+
return self._final
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
class Subscription:
|
|
106
|
+
"""Events from one daemon, delivered to a blocking reader."""
|
|
107
|
+
|
|
108
|
+
def __init__(
|
|
109
|
+
self,
|
|
110
|
+
rpc: SyncRpc,
|
|
111
|
+
deregister: Procedure[Any, Any],
|
|
112
|
+
max_events: int = DEFAULT_MAX_EVENTS,
|
|
113
|
+
) -> None:
|
|
114
|
+
"""Create a subscription that has no events registered yet.
|
|
115
|
+
|
|
116
|
+
:param rpc: the connection the events arrive on.
|
|
117
|
+
:param deregister: the procedure that ends one registration.
|
|
118
|
+
:param max_events: the most events kept for a slow reader; older ones are
|
|
119
|
+
dropped and counted in ``dropped``.
|
|
120
|
+
"""
|
|
121
|
+
self._rpc = rpc
|
|
122
|
+
self._deregister = deregister
|
|
123
|
+
self._buffer = EventBuffer(max_events)
|
|
124
|
+
self._ready = threading.Condition()
|
|
125
|
+
|
|
126
|
+
self._ids: list[int] = []
|
|
127
|
+
self._closed = False
|
|
128
|
+
self._reconnects = 0
|
|
129
|
+
|
|
130
|
+
def attach(self, callback_id: int) -> None:
|
|
131
|
+
"""Start listening to a registration the daemon made for us.
|
|
132
|
+
|
|
133
|
+
:param callback_id: the id the daemon gave.
|
|
134
|
+
"""
|
|
135
|
+
self._ids.append(callback_id)
|
|
136
|
+
self._rpc.add_listener(callback_id, self._deliver)
|
|
137
|
+
|
|
138
|
+
@property
|
|
139
|
+
def dropped(self) -> int:
|
|
140
|
+
"""How many events were dropped because the reader was too slow.
|
|
141
|
+
|
|
142
|
+
:returns: the count.
|
|
143
|
+
"""
|
|
144
|
+
return self._buffer.dropped
|
|
145
|
+
|
|
146
|
+
@property
|
|
147
|
+
def reconnects(self) -> int:
|
|
148
|
+
"""How many times the subscription was registered again after a reconnect.
|
|
149
|
+
|
|
150
|
+
Events that the daemon sent while the connection was down are lost.
|
|
151
|
+
|
|
152
|
+
:returns: the count.
|
|
153
|
+
"""
|
|
154
|
+
return self._reconnects
|
|
155
|
+
|
|
156
|
+
def rebind(self, rpc: SyncRpc) -> bool:
|
|
157
|
+
"""Move to a new connection; the caller registers the events again.
|
|
158
|
+
|
|
159
|
+
:param rpc: the new connection.
|
|
160
|
+
:returns: ``False`` if the subscription was closed meanwhile.
|
|
161
|
+
"""
|
|
162
|
+
if self._closed:
|
|
163
|
+
return False
|
|
164
|
+
self._rpc = rpc
|
|
165
|
+
self._ids = []
|
|
166
|
+
self._reconnects += 1
|
|
167
|
+
return True
|
|
168
|
+
|
|
169
|
+
def lost(self, error: Exception) -> None:
|
|
170
|
+
"""Tell the reader that the connection ended.
|
|
171
|
+
|
|
172
|
+
:param error: why it ended.
|
|
173
|
+
"""
|
|
174
|
+
self._finish(error)
|
|
175
|
+
|
|
176
|
+
def get(self, timeout: float | None = None) -> Event:
|
|
177
|
+
"""Wait for the next event.
|
|
178
|
+
|
|
179
|
+
:param timeout: seconds to wait; ``None`` to wait for ever.
|
|
180
|
+
:returns: the event.
|
|
181
|
+
:raises TimeoutError: if none came in time.
|
|
182
|
+
:raises StopIteration: if the subscription was closed.
|
|
183
|
+
:raises ConnectionLostError: if the connection ended.
|
|
184
|
+
"""
|
|
185
|
+
deadline = None if timeout is None else time.monotonic() + timeout
|
|
186
|
+
with self._ready:
|
|
187
|
+
while not self._buffer.ready():
|
|
188
|
+
remaining = None if deadline is None else deadline - time.monotonic()
|
|
189
|
+
if remaining is not None and remaining <= 0:
|
|
190
|
+
raise TimeoutError("no event arrived in time")
|
|
191
|
+
self._ready.wait(remaining)
|
|
192
|
+
item = self._buffer.take()
|
|
193
|
+
|
|
194
|
+
if isinstance(item, Exception):
|
|
195
|
+
raise item
|
|
196
|
+
if isinstance(item, _End):
|
|
197
|
+
raise StopIteration
|
|
198
|
+
return item
|
|
199
|
+
|
|
200
|
+
def close(self) -> None:
|
|
201
|
+
"""Stop the subscription: the daemon stops sending, iteration ends."""
|
|
202
|
+
if self._closed:
|
|
203
|
+
return
|
|
204
|
+
self._closed = True
|
|
205
|
+
|
|
206
|
+
for callback_id in self._ids:
|
|
207
|
+
self._rpc.remove_listener(callback_id)
|
|
208
|
+
self._deregister_one(callback_id)
|
|
209
|
+
self._finish(END)
|
|
210
|
+
|
|
211
|
+
def __iter__(self: SyncSelf) -> SyncSelf:
|
|
212
|
+
"""Iterate over the events until the subscription is closed.
|
|
213
|
+
|
|
214
|
+
:returns: the subscription.
|
|
215
|
+
"""
|
|
216
|
+
return self
|
|
217
|
+
|
|
218
|
+
def __next__(self) -> Event:
|
|
219
|
+
"""Wait for the next event.
|
|
220
|
+
|
|
221
|
+
:returns: the event.
|
|
222
|
+
:raises StopIteration: when the subscription is closed.
|
|
223
|
+
:raises ConnectionLostError: if the connection ended.
|
|
224
|
+
"""
|
|
225
|
+
return self.get()
|
|
226
|
+
|
|
227
|
+
def __enter__(self: SyncSelf) -> SyncSelf:
|
|
228
|
+
"""Use the subscription in a ``with`` block.
|
|
229
|
+
|
|
230
|
+
:returns: the subscription.
|
|
231
|
+
"""
|
|
232
|
+
return self
|
|
233
|
+
|
|
234
|
+
def __exit__(
|
|
235
|
+
self,
|
|
236
|
+
exception_type: type[BaseException] | None,
|
|
237
|
+
exception: BaseException | None,
|
|
238
|
+
traceback: TracebackType | None,
|
|
239
|
+
) -> None:
|
|
240
|
+
"""Close the subscription when the block ends.
|
|
241
|
+
|
|
242
|
+
:param exception_type: the exception class, if the block raised.
|
|
243
|
+
:param exception: the exception, if the block raised.
|
|
244
|
+
:param traceback: its traceback, if the block raised.
|
|
245
|
+
"""
|
|
246
|
+
self.close()
|
|
247
|
+
|
|
248
|
+
def _deliver(self, server_event: ServerEvent) -> None:
|
|
249
|
+
"""Queue an event pushed by the daemon.
|
|
250
|
+
|
|
251
|
+
:param server_event: the decoded message.
|
|
252
|
+
"""
|
|
253
|
+
event = make_event(server_event)
|
|
254
|
+
if event is not None:
|
|
255
|
+
with self._ready:
|
|
256
|
+
self._buffer.add(event)
|
|
257
|
+
self._ready.notify_all()
|
|
258
|
+
|
|
259
|
+
def _finish(self, final: Exception | _End) -> None:
|
|
260
|
+
"""Mark the end of the events and wake the reader.
|
|
261
|
+
|
|
262
|
+
:param final: ``END``, or the error that ended the subscription.
|
|
263
|
+
"""
|
|
264
|
+
with self._ready:
|
|
265
|
+
self._buffer.finish(final)
|
|
266
|
+
self._ready.notify_all()
|
|
267
|
+
|
|
268
|
+
def _deregister_one(self, callback_id: int) -> None:
|
|
269
|
+
"""Tell the daemon to stop one registration; it may be gone already.
|
|
270
|
+
|
|
271
|
+
:param callback_id: the registration.
|
|
272
|
+
"""
|
|
273
|
+
args = self._deregister.args
|
|
274
|
+
assert args is not None
|
|
275
|
+
with contextlib.suppress(LibvirtError, ProtocolError):
|
|
276
|
+
self._rpc.call(self._deregister, args(callback_id=callback_id))
|
|
277
|
+
|
|
278
|
+
|
|
279
|
+
class AsyncSubscription:
|
|
280
|
+
"""Events from one daemon, delivered to an ``async for`` loop."""
|
|
281
|
+
|
|
282
|
+
def __init__(
|
|
283
|
+
self,
|
|
284
|
+
rpc: AsyncRpc,
|
|
285
|
+
deregister: Procedure[Any, Any],
|
|
286
|
+
max_events: int = DEFAULT_MAX_EVENTS,
|
|
287
|
+
) -> None:
|
|
288
|
+
"""Create a subscription that has no events registered yet.
|
|
289
|
+
|
|
290
|
+
:param rpc: the connection the events arrive on.
|
|
291
|
+
:param deregister: the procedure that ends one registration.
|
|
292
|
+
:param max_events: the most events kept for a slow reader; older ones are
|
|
293
|
+
dropped and counted in ``dropped``.
|
|
294
|
+
"""
|
|
295
|
+
self._rpc = rpc
|
|
296
|
+
self._deregister = deregister
|
|
297
|
+
self._buffer = EventBuffer(max_events)
|
|
298
|
+
self._wake = asyncio.Event()
|
|
299
|
+
|
|
300
|
+
self._ids: list[int] = []
|
|
301
|
+
self._closed = False
|
|
302
|
+
self._reconnects = 0
|
|
303
|
+
|
|
304
|
+
def attach(self, callback_id: int) -> None:
|
|
305
|
+
"""Start listening to a registration the daemon made for us.
|
|
306
|
+
|
|
307
|
+
:param callback_id: the id the daemon gave.
|
|
308
|
+
"""
|
|
309
|
+
self._ids.append(callback_id)
|
|
310
|
+
self._rpc.add_listener(callback_id, self._deliver)
|
|
311
|
+
|
|
312
|
+
@property
|
|
313
|
+
def dropped(self) -> int:
|
|
314
|
+
"""How many events were dropped because the reader was too slow.
|
|
315
|
+
|
|
316
|
+
:returns: the count.
|
|
317
|
+
"""
|
|
318
|
+
return self._buffer.dropped
|
|
319
|
+
|
|
320
|
+
@property
|
|
321
|
+
def reconnects(self) -> int:
|
|
322
|
+
"""How many times the subscription was registered again after a reconnect.
|
|
323
|
+
|
|
324
|
+
Events that the daemon sent while the connection was down are lost.
|
|
325
|
+
|
|
326
|
+
:returns: the count.
|
|
327
|
+
"""
|
|
328
|
+
return self._reconnects
|
|
329
|
+
|
|
330
|
+
def rebind(self, rpc: AsyncRpc) -> bool:
|
|
331
|
+
"""Move to a new connection; the caller registers the events again.
|
|
332
|
+
|
|
333
|
+
:param rpc: the new connection.
|
|
334
|
+
:returns: ``False`` if the subscription was closed meanwhile.
|
|
335
|
+
"""
|
|
336
|
+
if self._closed:
|
|
337
|
+
return False
|
|
338
|
+
self._rpc = rpc
|
|
339
|
+
self._ids = []
|
|
340
|
+
self._reconnects += 1
|
|
341
|
+
return True
|
|
342
|
+
|
|
343
|
+
def lost(self, error: Exception) -> None:
|
|
344
|
+
"""Tell the reader that the connection ended.
|
|
345
|
+
|
|
346
|
+
:param error: why it ended.
|
|
347
|
+
"""
|
|
348
|
+
self._finish(error)
|
|
349
|
+
|
|
350
|
+
async def get(self, timeout: float | None = None) -> Event:
|
|
351
|
+
"""Wait for the next event.
|
|
352
|
+
|
|
353
|
+
:param timeout: seconds to wait; ``None`` to wait for ever.
|
|
354
|
+
:returns: the event.
|
|
355
|
+
:raises TimeoutError: if none came in time.
|
|
356
|
+
:raises StopAsyncIteration: if the subscription was closed.
|
|
357
|
+
:raises ConnectionLostError: if the connection ended.
|
|
358
|
+
"""
|
|
359
|
+
try:
|
|
360
|
+
await asyncio.wait_for(self._until_ready(), timeout)
|
|
361
|
+
except TimeoutError:
|
|
362
|
+
raise TimeoutError("no event arrived in time") from None
|
|
363
|
+
item = self._buffer.take()
|
|
364
|
+
|
|
365
|
+
if isinstance(item, Exception):
|
|
366
|
+
raise item
|
|
367
|
+
if isinstance(item, _End):
|
|
368
|
+
raise StopAsyncIteration
|
|
369
|
+
return item
|
|
370
|
+
|
|
371
|
+
async def close(self) -> None:
|
|
372
|
+
"""Stop the subscription: the daemon stops sending, iteration ends."""
|
|
373
|
+
if self._closed:
|
|
374
|
+
return
|
|
375
|
+
self._closed = True
|
|
376
|
+
|
|
377
|
+
for callback_id in self._ids:
|
|
378
|
+
self._rpc.remove_listener(callback_id)
|
|
379
|
+
await self._deregister_one(callback_id)
|
|
380
|
+
self._finish(END)
|
|
381
|
+
|
|
382
|
+
def __aiter__(self: AsyncSelf) -> AsyncSelf:
|
|
383
|
+
"""Iterate over the events until the subscription is closed.
|
|
384
|
+
|
|
385
|
+
:returns: the subscription.
|
|
386
|
+
"""
|
|
387
|
+
return self
|
|
388
|
+
|
|
389
|
+
async def __anext__(self) -> Event:
|
|
390
|
+
"""Wait for the next event.
|
|
391
|
+
|
|
392
|
+
:returns: the event.
|
|
393
|
+
:raises StopAsyncIteration: when the subscription is closed.
|
|
394
|
+
:raises ConnectionLostError: if the connection ended.
|
|
395
|
+
"""
|
|
396
|
+
return await self.get()
|
|
397
|
+
|
|
398
|
+
async def __aenter__(self: AsyncSelf) -> AsyncSelf:
|
|
399
|
+
"""Use the subscription in an ``async with`` block.
|
|
400
|
+
|
|
401
|
+
:returns: the subscription.
|
|
402
|
+
"""
|
|
403
|
+
return self
|
|
404
|
+
|
|
405
|
+
async def __aexit__(
|
|
406
|
+
self,
|
|
407
|
+
exception_type: type[BaseException] | None,
|
|
408
|
+
exception: BaseException | None,
|
|
409
|
+
traceback: TracebackType | None,
|
|
410
|
+
) -> None:
|
|
411
|
+
"""Close the subscription when the block ends.
|
|
412
|
+
|
|
413
|
+
:param exception_type: the exception class, if the block raised.
|
|
414
|
+
:param exception: the exception, if the block raised.
|
|
415
|
+
:param traceback: its traceback, if the block raised.
|
|
416
|
+
"""
|
|
417
|
+
await self.close()
|
|
418
|
+
|
|
419
|
+
def _deliver(self, server_event: ServerEvent) -> None:
|
|
420
|
+
"""Queue an event pushed by the daemon.
|
|
421
|
+
|
|
422
|
+
:param server_event: the decoded message.
|
|
423
|
+
"""
|
|
424
|
+
event = make_event(server_event)
|
|
425
|
+
if event is not None:
|
|
426
|
+
self._buffer.add(event)
|
|
427
|
+
self._wake.set()
|
|
428
|
+
|
|
429
|
+
def _finish(self, final: Exception | _End) -> None:
|
|
430
|
+
"""Mark the end of the events and wake the reader.
|
|
431
|
+
|
|
432
|
+
:param final: ``END``, or the error that ended the subscription.
|
|
433
|
+
"""
|
|
434
|
+
self._buffer.finish(final)
|
|
435
|
+
self._wake.set()
|
|
436
|
+
|
|
437
|
+
async def _until_ready(self) -> None:
|
|
438
|
+
"""Wait until there is an event or an end to read."""
|
|
439
|
+
while not self._buffer.ready():
|
|
440
|
+
self._wake.clear()
|
|
441
|
+
await self._wake.wait()
|
|
442
|
+
|
|
443
|
+
async def _deregister_one(self, callback_id: int) -> None:
|
|
444
|
+
"""Tell the daemon to stop one registration; it may be gone already.
|
|
445
|
+
|
|
446
|
+
:param callback_id: the registration.
|
|
447
|
+
"""
|
|
448
|
+
args = self._deregister.args
|
|
449
|
+
assert args is not None
|
|
450
|
+
with contextlib.suppress(LibvirtError, ProtocolError):
|
|
451
|
+
await self._rpc.call(self._deregister, args(callback_id=callback_id))
|