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.
Files changed (154) hide show
  1. libvirtpy/__init__.py +5 -0
  2. libvirtpy/client/__init__.py +54 -0
  3. libvirtpy/client/_bases.py +326 -0
  4. libvirtpy/client/_runner.py +57 -0
  5. libvirtpy/client/aio.py +5922 -0
  6. libvirtpy/client/client.py +5695 -0
  7. libvirtpy/client/operations/__init__.py +631 -0
  8. libvirtpy/client/operations/_common.py +495 -0
  9. libvirtpy/client/operations/checkpoint.py +196 -0
  10. libvirtpy/client/operations/connection.py +93 -0
  11. libvirtpy/client/operations/connection_extra.py +256 -0
  12. libvirtpy/client/operations/domain.py +306 -0
  13. libvirtpy/client/operations/domain_agent.py +115 -0
  14. libvirtpy/client/operations/domain_block.py +368 -0
  15. libvirtpy/client/operations/domain_info.py +400 -0
  16. libvirtpy/client/operations/domain_lifecycle.py +338 -0
  17. libvirtpy/client/operations/domain_migration.py +164 -0
  18. libvirtpy/client/operations/domain_network.py +113 -0
  19. libvirtpy/client/operations/domain_tuning.py +494 -0
  20. libvirtpy/client/operations/host.py +246 -0
  21. libvirtpy/client/operations/host_interface.py +100 -0
  22. libvirtpy/client/operations/interface_extra.py +45 -0
  23. libvirtpy/client/operations/network.py +135 -0
  24. libvirtpy/client/operations/network_info.py +155 -0
  25. libvirtpy/client/operations/network_port.py +142 -0
  26. libvirtpy/client/operations/node_device.py +76 -0
  27. libvirtpy/client/operations/node_device_info.py +189 -0
  28. libvirtpy/client/operations/nwfilter.py +67 -0
  29. libvirtpy/client/operations/nwfilter_binding.py +74 -0
  30. libvirtpy/client/operations/secret.py +93 -0
  31. libvirtpy/client/operations/secret_extra.py +24 -0
  32. libvirtpy/client/operations/snapshot.py +100 -0
  33. libvirtpy/client/operations/snapshot_tree.py +114 -0
  34. libvirtpy/client/operations/storage.py +267 -0
  35. libvirtpy/client/operations/storage_info.py +188 -0
  36. libvirtpy/client/operations/streams.py +201 -0
  37. libvirtpy/client/references.py +249 -0
  38. libvirtpy/client/subscribing.py +88 -0
  39. libvirtpy/client/testing/__init__.py +24 -0
  40. libvirtpy/client/testing/_common.py +167 -0
  41. libvirtpy/client/testing/_domains.py +294 -0
  42. libvirtpy/client/testing/_objects.py +205 -0
  43. libvirtpy/client/testing/_state.py +246 -0
  44. libvirtpy/client/testing/_storage.py +171 -0
  45. libvirtpy/client/testing/async_transport.py +30 -0
  46. libvirtpy/client/testing/fake.py +81 -0
  47. libvirtpy/client/transport.py +87 -0
  48. libvirtpy/client/types.py +59 -0
  49. libvirtpy/client/wire.py +436 -0
  50. libvirtpy/client/wire_aio.py +348 -0
  51. libvirtpy/errors.py +7 -0
  52. libvirtpy/py.typed +0 -0
  53. libvirtpy/rpc/__init__.py +1 -0
  54. libvirtpy/rpc/aio.py +387 -0
  55. libvirtpy/rpc/cpumap.py +48 -0
  56. libvirtpy/rpc/errors.py +186 -0
  57. libvirtpy/rpc/events.py +259 -0
  58. libvirtpy/rpc/options.py +66 -0
  59. libvirtpy/rpc/procedure.py +27 -0
  60. libvirtpy/rpc/protocol/__init__.py +0 -0
  61. libvirtpy/rpc/protocol/procedures.py +5163 -0
  62. libvirtpy/rpc/protocol/typed_hints.py +210 -0
  63. libvirtpy/rpc/protocol/types.py +9299 -0
  64. libvirtpy/rpc/remote.py +295 -0
  65. libvirtpy/rpc/remote_aio.py +106 -0
  66. libvirtpy/rpc/sasl.py +467 -0
  67. libvirtpy/rpc/session.py +424 -0
  68. libvirtpy/rpc/sockets.py +251 -0
  69. libvirtpy/rpc/stream.py +323 -0
  70. libvirtpy/rpc/subscription.py +451 -0
  71. libvirtpy/rpc/sync.py +460 -0
  72. libvirtpy/rpc/typed_params.py +97 -0
  73. libvirtpy/rpc/uri.py +85 -0
  74. libvirtpy/rpc/xdr.py +552 -0
  75. libvirtpy/xml/__init__.py +49 -0
  76. libvirtpy/xml/_base.py +145 -0
  77. libvirtpy/xml/_shared/__init__.py +1 -0
  78. libvirtpy/xml/_shared/metadata.py +67 -0
  79. libvirtpy/xml/_shared/network.py +108 -0
  80. libvirtpy/xml/_shared/storage.py +131 -0
  81. libvirtpy/xml/_shared/sysinfo.py +83 -0
  82. libvirtpy/xml/_types.py +197 -0
  83. libvirtpy/xml/_xml_element.py +134 -0
  84. libvirtpy/xml/backup/__init__.py +25 -0
  85. libvirtpy/xml/backup/backup.py +89 -0
  86. libvirtpy/xml/backup/checkpoint.py +48 -0
  87. libvirtpy/xml/capabilities/__init__.py +43 -0
  88. libvirtpy/xml/capabilities/domain.py +436 -0
  89. libvirtpy/xml/capabilities/host.py +413 -0
  90. libvirtpy/xml/capabilities/storagepool.py +59 -0
  91. libvirtpy/xml/domain/__init__.py +493 -0
  92. libvirtpy/xml/domain/blkiotune.py +81 -0
  93. libvirtpy/xml/domain/clock.py +79 -0
  94. libvirtpy/xml/domain/cpu.py +265 -0
  95. libvirtpy/xml/domain/devices/__init__.py +495 -0
  96. libvirtpy/xml/domain/devices/address.py +152 -0
  97. libvirtpy/xml/domain/devices/audio.py +204 -0
  98. libvirtpy/xml/domain/devices/chardev.py +197 -0
  99. libvirtpy/xml/domain/devices/common.py +75 -0
  100. libvirtpy/xml/domain/devices/controller.py +262 -0
  101. libvirtpy/xml/domain/devices/devices.py +79 -0
  102. libvirtpy/xml/domain/devices/disk.py +471 -0
  103. libvirtpy/xml/domain/devices/filesystem.py +206 -0
  104. libvirtpy/xml/domain/devices/graphics.py +245 -0
  105. libvirtpy/xml/domain/devices/hostdev.py +221 -0
  106. libvirtpy/xml/domain/devices/input.py +89 -0
  107. libvirtpy/xml/domain/devices/interface.py +459 -0
  108. libvirtpy/xml/domain/devices/iommu.py +54 -0
  109. libvirtpy/xml/domain/devices/memballoon.py +42 -0
  110. libvirtpy/xml/domain/devices/memory_device.py +101 -0
  111. libvirtpy/xml/domain/devices/misc.py +169 -0
  112. libvirtpy/xml/domain/devices/nvram.py +20 -0
  113. libvirtpy/xml/domain/devices/shmem.py +46 -0
  114. libvirtpy/xml/domain/devices/sound.py +42 -0
  115. libvirtpy/xml/domain/devices/tpm.py +103 -0
  116. libvirtpy/xml/domain/devices/usb.py +86 -0
  117. libvirtpy/xml/domain/devices/video.py +101 -0
  118. libvirtpy/xml/domain/domain.py +157 -0
  119. libvirtpy/xml/domain/features.py +312 -0
  120. libvirtpy/xml/domain/iothread.py +41 -0
  121. libvirtpy/xml/domain/launch_security.py +82 -0
  122. libvirtpy/xml/domain/memory.py +149 -0
  123. libvirtpy/xml/domain/misc.py +113 -0
  124. libvirtpy/xml/domain/namespaces.py +96 -0
  125. libvirtpy/xml/domain/numa.py +140 -0
  126. libvirtpy/xml/domain/os.py +267 -0
  127. libvirtpy/xml/domain/qemu.py +102 -0
  128. libvirtpy/xml/domain/seclabel.py +29 -0
  129. libvirtpy/xml/domain/storage_source.py +466 -0
  130. libvirtpy/xml/domain/sysinfo.py +34 -0
  131. libvirtpy/xml/interface/__init__.py +17 -0
  132. libvirtpy/xml/interface/interface.py +153 -0
  133. libvirtpy/xml/network/__init__.py +101 -0
  134. libvirtpy/xml/network/network.py +311 -0
  135. libvirtpy/xml/network/port.py +84 -0
  136. libvirtpy/xml/nodedev/__init__.py +13 -0
  137. libvirtpy/xml/nodedev/nodedev.py +490 -0
  138. libvirtpy/xml/nwfilter/__init__.py +81 -0
  139. libvirtpy/xml/nwfilter/binding.py +51 -0
  140. libvirtpy/xml/nwfilter/filter.py +92 -0
  141. libvirtpy/xml/nwfilter/rules.py +534 -0
  142. libvirtpy/xml/secret/__init__.py +11 -0
  143. libvirtpy/xml/secret/secret.py +36 -0
  144. libvirtpy/xml/snapshot/__init__.py +29 -0
  145. libvirtpy/xml/snapshot/snapshot.py +136 -0
  146. libvirtpy/xml/storage/__init__.py +75 -0
  147. libvirtpy/xml/storage/pool.py +219 -0
  148. libvirtpy/xml/storage/volume.py +102 -0
  149. libvirtpy/xml/sysinfo.py +40 -0
  150. libvirtpy-0.1.0.dist-info/METADATA +109 -0
  151. libvirtpy-0.1.0.dist-info/RECORD +154 -0
  152. libvirtpy-0.1.0.dist-info/WHEEL +4 -0
  153. libvirtpy-0.1.0.dist-info/licenses/LICENSE +501 -0
  154. libvirtpy-0.1.0.dist-info/licenses/NOTICE +23 -0
libvirtpy/__init__.py ADDED
@@ -0,0 +1,5 @@
1
+ """libvirtpy: libvirt for Python.
2
+
3
+ ``libvirtpy.xml`` models every libvirt XML document, ``libvirtpy.rpc`` speaks the
4
+ libvirt wire protocol and ``libvirtpy.client`` offers the operations on top.
5
+ """
@@ -0,0 +1,54 @@
1
+ """A client of a libvirt daemon that speaks in ``libvirtpy.xml`` models.
2
+
3
+ ``LibvirtClient.open(uri)`` talks to libvirt daemons in pure Python, no libvirt
4
+ library: ``qemu:///system`` over the UNIX sockets of this machine, or a remote
5
+ host with ``qemu+ssh://user@host/system``, ``qemu+tls://host/system`` or
6
+ ``qemu+tcp://host/system``. SASL (``sasl=SaslCredentials(user, password)``) works
7
+ with SCRAM, DIGEST-MD5 and PLAIN on TLS, SSH and UNIX sockets. ``client.events()``
8
+ receives what the daemon pushes; ``open_domain_console``, ``take_domain_screenshot``
9
+ and ``upload_pool_volume``/``download_pool_volume`` return file-like streams.
10
+ ``FakeLibvirt`` is an in-memory daemon for tests and examples.
11
+
12
+ ``LibvirtClient`` (blocking) and ``AsyncLibvirtClient`` (``async``/``await``)
13
+ offer the same methods. They take references (an id, a name, a UUID or a
14
+ model) and return models::
15
+
16
+ client = LibvirtClient.open("qemu:///system")
17
+ domain = client.define_domain(Domain(type="kvm", name="vm"))
18
+ client.set_domain_vcpus(domain, 4) # or client.domain(domain).set_vcpus(4)
19
+ print(client.lookup_domain("vm").vcpu)
20
+ """
21
+
22
+ from libvirtpy.client.aio import AsyncLibvirtClient
23
+ from libvirtpy.client.client import LibvirtClient
24
+ from libvirtpy.client.testing import AsyncFakeTransport, FakeLibvirt
25
+ from libvirtpy.client.types import DomainInfo, DomainState
26
+ from libvirtpy.rpc.errors import LibvirtError
27
+ from libvirtpy.rpc.events import (
28
+ DomainEventKind,
29
+ DomainLifecycle,
30
+ Event,
31
+ NetworkEventKind,
32
+ NodeDeviceEventKind,
33
+ PoolEventKind,
34
+ SecretEventKind,
35
+ )
36
+ from libvirtpy.rpc.sasl import SaslCredentials
37
+
38
+ __all__ = [
39
+ "AsyncFakeTransport",
40
+ "AsyncLibvirtClient",
41
+ "DomainEventKind",
42
+ "DomainInfo",
43
+ "DomainLifecycle",
44
+ "DomainState",
45
+ "Event",
46
+ "FakeLibvirt",
47
+ "LibvirtClient",
48
+ "LibvirtError",
49
+ "NetworkEventKind",
50
+ "NodeDeviceEventKind",
51
+ "PoolEventKind",
52
+ "SaslCredentials",
53
+ "SecretEventKind",
54
+ ]
@@ -0,0 +1,326 @@
1
+ """The hand-written part of the two clients: connection handling and ``_run``.
2
+
3
+ The generated classes ``LibvirtClient`` and ``AsyncLibvirtClient`` add one
4
+ one-line method per operation of ``libvirtpy.client.operations``.
5
+ """
6
+
7
+ from collections.abc import Iterable
8
+ from enum import IntEnum
9
+ from types import TracebackType
10
+ from typing import Protocol, TypeVar, runtime_checkable
11
+
12
+ from libvirtpy.client._runner import run_async, run_sync
13
+ from libvirtpy.client.transport import AsyncTransport, Operation, Transport
14
+ from libvirtpy.client.wire import WireTransport
15
+ from libvirtpy.client.wire_aio import AsyncWireTransport
16
+ from libvirtpy.rpc.errors import ErrorNumber, NotSupportedError
17
+ from libvirtpy.rpc.options import ConnectionOptions
18
+ from libvirtpy.rpc.sasl import SaslCredentials
19
+ from libvirtpy.rpc.subscription import (
20
+ DEFAULT_MAX_EVENTS,
21
+ AsyncSubscription,
22
+ Subscription,
23
+ )
24
+
25
+ __all__ = ["AsyncClientBase", "SyncClientBase"]
26
+
27
+ T = TypeVar("T")
28
+ SyncSelf = TypeVar("SyncSelf", bound="SyncClientBase")
29
+ AsyncSelf = TypeVar("AsyncSelf", bound="AsyncClientBase")
30
+
31
+
32
+ @runtime_checkable
33
+ class SubscribingTransport(Protocol):
34
+ """A transport that can deliver events (the one of a real daemon)."""
35
+
36
+ def subscribe(
37
+ self,
38
+ source: str,
39
+ kinds: Iterable[IntEnum] | None,
40
+ target: object,
41
+ max_events: int = DEFAULT_MAX_EVENTS,
42
+ ) -> Subscription:
43
+ """Start receiving events.
44
+
45
+ :param source: the event source.
46
+ :param kinds: the event kinds, or ``None`` for lifecycle events.
47
+ :param target: one object to limit to, or ``None``.
48
+ :param max_events: the most events kept for a slow reader.
49
+ :returns: the subscription.
50
+ """
51
+
52
+
53
+ @runtime_checkable
54
+ class AsyncSubscribingTransport(Protocol):
55
+ """An async transport that can deliver events."""
56
+
57
+ async def subscribe(
58
+ self,
59
+ source: str,
60
+ kinds: Iterable[IntEnum] | None,
61
+ target: object,
62
+ max_events: int = DEFAULT_MAX_EVENTS,
63
+ ) -> AsyncSubscription:
64
+ """Start receiving events.
65
+
66
+ :param source: the event source.
67
+ :param kinds: the event kinds, or ``None`` for lifecycle events.
68
+ :param target: one object to limit to, or ``None``.
69
+ :param max_events: the most events kept for a slow reader.
70
+ :returns: the subscription.
71
+ """
72
+
73
+
74
+ class SyncClientBase:
75
+ """Connection handling of ``LibvirtClient``."""
76
+
77
+ def __init__(self, transport: Transport) -> None:
78
+ """Create a client over a transport.
79
+
80
+ :param transport: sends the requests; ``FakeLibvirt`` in the prototype.
81
+ """
82
+ self._transport = transport
83
+
84
+ @classmethod
85
+ def open(
86
+ cls: type[SyncSelf],
87
+ uri: str,
88
+ *,
89
+ read_only: bool = False,
90
+ sasl: SaslCredentials | None = None,
91
+ timeout: float | None = None,
92
+ keepalive_interval: float | None = 5.0,
93
+ keepalive_count: int = 5,
94
+ reconnect: bool = False,
95
+ reconnect_attempts: int = 5,
96
+ reconnect_delay: float = 1.0,
97
+ ) -> SyncSelf:
98
+ """Connect to a libvirt daemon, on this machine or another host.
99
+
100
+ :param uri: a libvirt URI such as ``qemu:///system`` or
101
+ ``qemu+ssh://user@host/system``.
102
+ :param read_only: use the read-only sockets.
103
+ :param sasl: the user name and password, if the daemon asks for SASL.
104
+ :param timeout: seconds a call may wait for its reply; ``None`` waits as long as
105
+ it takes. A call that times out raises ``CallTimeoutError``; the
106
+ connection stays usable.
107
+ :param keepalive_interval: seconds of silence before the client pings the
108
+ daemon; ``None`` turns the keepalive off.
109
+ :param keepalive_count: unanswered pings after which the connection is given
110
+ up (waiting calls fail with ``ConnectionLostError``).
111
+ :param reconnect: open a lost connection again: the next call, and the
112
+ event subscriptions, continue on a new connection. A call that was
113
+ running when the connection broke still fails, and a stream is lost.
114
+ :param reconnect_attempts: how many times to try before giving up.
115
+ :param reconnect_delay: seconds before the second attempt; each further
116
+ wait doubles (at most 30 seconds). The first attempt is immediate.
117
+ :returns: the client.
118
+ :raises ConnectionError: if the daemon cannot be reached.
119
+ :raises AuthFailedError: if the authentication fails.
120
+ """
121
+ options = ConnectionOptions(
122
+ timeout,
123
+ keepalive_interval,
124
+ keepalive_count,
125
+ reconnect_attempts if reconnect else 0,
126
+ reconnect_delay,
127
+ )
128
+ return cls(
129
+ WireTransport.from_uri(uri, read_only=read_only, sasl=sasl, options=options)
130
+ )
131
+
132
+ def events(
133
+ self,
134
+ source: str = "domain",
135
+ kinds: Iterable[IntEnum] | None = None,
136
+ *,
137
+ target: object = None,
138
+ max_events: int = DEFAULT_MAX_EVENTS,
139
+ ) -> Subscription:
140
+ """Start receiving events from the daemon.
141
+
142
+ Iterate the result (or call ``get``) and close it when done::
143
+
144
+ with client.events("domain", target="vm") as events:
145
+ for event in events:
146
+ print(event.kind, event.lifecycle)
147
+
148
+ :param source: ``domain``, ``network``, ``pool``, ``node_device`` or ``secret``.
149
+ :param kinds: the event kinds, members of ``DomainEventKind``,
150
+ ``NetworkEventKind``, ...; ``None`` for the ``LIFECYCLE`` events only.
151
+ :param target: limit to one object, as a reference (name, UUID or model);
152
+ ``None`` for all of them.
153
+ :param max_events: the most events kept while you are not reading; a slower
154
+ reader loses the oldest ones, counted in ``subscription.dropped``.
155
+ :returns: the subscription.
156
+ :raises NotSupportedError: if the client is not connected to a real daemon.
157
+ :raises ValueError: if the source or a kind is unknown.
158
+ """
159
+ transport = self._transport
160
+ if not isinstance(transport, SubscribingTransport):
161
+ raise NotSupportedError(
162
+ "events need a connection to a libvirt daemon",
163
+ code=ErrorNumber.NO_SUPPORT,
164
+ )
165
+ return transport.subscribe(source, kinds, target, max_events)
166
+
167
+ def close(self) -> None:
168
+ """Close the connection."""
169
+ self._transport.close()
170
+
171
+ def __enter__(self: SyncSelf) -> SyncSelf:
172
+ """Use the client in a ``with`` block.
173
+
174
+ :returns: the client.
175
+ """
176
+ return self
177
+
178
+ def __exit__(
179
+ self,
180
+ exception_type: type[BaseException] | None,
181
+ exception: BaseException | None,
182
+ traceback: TracebackType | None,
183
+ ) -> None:
184
+ """Close the client when the ``with`` block ends.
185
+
186
+ :param exception_type: the exception class, if the block raised.
187
+ :param exception: the exception, if the block raised.
188
+ :param traceback: its traceback, if the block raised.
189
+ """
190
+ self.close()
191
+
192
+ def _run(self, operation: Operation[T]) -> T:
193
+ """Run an operation over the transport.
194
+
195
+ :param operation: the operation to run.
196
+ :returns: its result.
197
+ """
198
+ return run_sync(operation, self._transport.call)
199
+
200
+
201
+ class AsyncClientBase:
202
+ """Connection handling of ``AsyncLibvirtClient``."""
203
+
204
+ def __init__(self, transport: AsyncTransport) -> None:
205
+ """Create a client over a transport.
206
+
207
+ :param transport: sends the requests; ``AsyncFakeTransport`` in the prototype.
208
+ """
209
+ self._transport = transport
210
+
211
+ @classmethod
212
+ async def open(
213
+ cls: type[AsyncSelf],
214
+ uri: str,
215
+ *,
216
+ read_only: bool = False,
217
+ sasl: SaslCredentials | None = None,
218
+ timeout: float | None = None,
219
+ keepalive_interval: float | None = 5.0,
220
+ keepalive_count: int = 5,
221
+ reconnect: bool = False,
222
+ reconnect_attempts: int = 5,
223
+ reconnect_delay: float = 1.0,
224
+ ) -> AsyncSelf:
225
+ """Connect to a libvirt daemon, on this machine or another host.
226
+
227
+ :param uri: a libvirt URI such as ``qemu:///system`` or
228
+ ``qemu+ssh://user@host/system``.
229
+ :param read_only: use the read-only sockets.
230
+ :param sasl: the user name and password, if the daemon asks for SASL.
231
+ :param timeout: seconds a call may wait for its reply; ``None`` waits as long as
232
+ it takes. A call that times out raises ``CallTimeoutError``; the
233
+ connection stays usable.
234
+ :param keepalive_interval: seconds of silence before the client pings the
235
+ daemon; ``None`` turns the keepalive off.
236
+ :param keepalive_count: unanswered pings after which the connection is given
237
+ up (waiting calls fail with ``ConnectionLostError``).
238
+ :param reconnect: open a lost connection again: the next call, and the
239
+ event subscriptions, continue on a new connection. A call that was
240
+ running when the connection broke still fails, and a stream is lost.
241
+ :param reconnect_attempts: how many times to try before giving up.
242
+ :param reconnect_delay: seconds before the second attempt; each further
243
+ wait doubles (at most 30 seconds). The first attempt is immediate.
244
+ :returns: the client.
245
+ :raises ConnectionError: if the daemon cannot be reached.
246
+ :raises AuthFailedError: if the authentication fails.
247
+ """
248
+ options = ConnectionOptions(
249
+ timeout,
250
+ keepalive_interval,
251
+ keepalive_count,
252
+ reconnect_attempts if reconnect else 0,
253
+ reconnect_delay,
254
+ )
255
+ transport = await AsyncWireTransport.from_uri(
256
+ uri, read_only=read_only, sasl=sasl, options=options
257
+ )
258
+ return cls(transport)
259
+
260
+ async def events(
261
+ self,
262
+ source: str = "domain",
263
+ kinds: Iterable[IntEnum] | None = None,
264
+ *,
265
+ target: object = None,
266
+ max_events: int = DEFAULT_MAX_EVENTS,
267
+ ) -> AsyncSubscription:
268
+ """Start receiving events from the daemon.
269
+
270
+ Iterate the result with ``async for`` and close it when done::
271
+
272
+ async with await client.events("domain", target="vm") as events:
273
+ async for event in events:
274
+ print(event.kind, event.lifecycle)
275
+
276
+ :param source: ``domain``, ``network``, ``pool``, ``node_device`` or ``secret``.
277
+ :param kinds: the event kinds, members of ``DomainEventKind``,
278
+ ``NetworkEventKind``, ...; ``None`` for the ``LIFECYCLE`` events only.
279
+ :param target: limit to one object, as a reference (name, UUID or model);
280
+ ``None`` for all of them.
281
+ :param max_events: the most events kept while you are not reading; a slower
282
+ reader loses the oldest ones, counted in ``subscription.dropped``.
283
+ :returns: the subscription.
284
+ :raises NotSupportedError: if the client is not connected to a real daemon.
285
+ :raises ValueError: if the source or a kind is unknown.
286
+ """
287
+ transport = self._transport
288
+ if not isinstance(transport, AsyncSubscribingTransport):
289
+ raise NotSupportedError(
290
+ "events need a connection to a libvirt daemon",
291
+ code=ErrorNumber.NO_SUPPORT,
292
+ )
293
+ return await transport.subscribe(source, kinds, target, max_events)
294
+
295
+ async def close(self) -> None:
296
+ """Close the connection."""
297
+ await self._transport.close()
298
+
299
+ async def __aenter__(self: AsyncSelf) -> AsyncSelf:
300
+ """Use the client in an ``async with`` block.
301
+
302
+ :returns: the client.
303
+ """
304
+ return self
305
+
306
+ async def __aexit__(
307
+ self,
308
+ exception_type: type[BaseException] | None,
309
+ exception: BaseException | None,
310
+ traceback: TracebackType | None,
311
+ ) -> None:
312
+ """Close the client when the ``async with`` block ends.
313
+
314
+ :param exception_type: the exception class, if the block raised.
315
+ :param exception: the exception, if the block raised.
316
+ :param traceback: its traceback, if the block raised.
317
+ """
318
+ await self.close()
319
+
320
+ async def _run(self, operation: Operation[T]) -> T:
321
+ """Run an operation over the transport.
322
+
323
+ :param operation: the operation to run.
324
+ :returns: its result.
325
+ """
326
+ return await run_async(operation, self._transport.call)
@@ -0,0 +1,57 @@
1
+ """Drives an ``Operation`` with a blocking or an awaitable transport call."""
2
+
3
+ from collections.abc import Awaitable, Callable
4
+ from typing import TypeVar, cast
5
+
6
+ from libvirtpy.client.transport import Operation, Request
7
+ from libvirtpy.rpc.errors import LibvirtError
8
+
9
+ __all__ = ["run_async", "run_sync"]
10
+
11
+ T = TypeVar("T")
12
+
13
+
14
+ def run_sync[T](operation: Operation[T], call: Callable[[Request], object]) -> T:
15
+ """Run an operation, sending each request it yields with ``call``.
16
+
17
+ An error from ``call`` is thrown into the operation, which may handle it.
18
+
19
+ :param operation: the operation to run.
20
+ :param call: sends one request and returns its reply.
21
+ :returns: the operation's result.
22
+ :raises LibvirtError: if the operation does not handle an error.
23
+ """
24
+ try:
25
+ request = next(operation)
26
+ while True:
27
+ try:
28
+ reply = call(request)
29
+ except LibvirtError as error:
30
+ request = operation.throw(error)
31
+ else:
32
+ request = operation.send(reply)
33
+ except StopIteration as stop:
34
+ return cast(T, stop.value)
35
+
36
+
37
+ async def run_async[T](
38
+ operation: Operation[T], call: Callable[[Request], Awaitable[object]]
39
+ ) -> T:
40
+ """Run an operation, awaiting ``call`` for each request it yields.
41
+
42
+ :param operation: the operation to run.
43
+ :param call: sends one request and returns an awaitable of its reply.
44
+ :returns: the operation's result.
45
+ :raises LibvirtError: if the operation does not handle an error.
46
+ """
47
+ try:
48
+ request = next(operation)
49
+ while True:
50
+ try:
51
+ reply = await call(request)
52
+ except LibvirtError as error:
53
+ request = operation.throw(error)
54
+ else:
55
+ request = operation.send(reply)
56
+ except StopIteration as stop:
57
+ return cast(T, stop.value)