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/rpc/xdr.py ADDED
@@ -0,0 +1,552 @@
1
+ """XDR (RFC 4506) encoding and decoding, driven by type descriptors.
2
+
3
+ The generated protocol module describes every wire structure as a dataclass with
4
+ a ``xdr_fields`` table of ``(attribute name, XdrType)``; the descriptors here
5
+ turn such a value into bytes and back. Everything is big-endian and padded to
6
+ four bytes, as the standard says.
7
+ """
8
+
9
+ import struct
10
+ from collections.abc import Callable
11
+ from dataclasses import dataclass
12
+ from enum import IntEnum
13
+ from typing import Any, ClassVar, Protocol, TypeVar
14
+
15
+ from libvirtpy.errors import LibvirtPyError
16
+
17
+ __all__ = [
18
+ "BOOL",
19
+ "DOUBLE",
20
+ "FLOAT",
21
+ "HYPER",
22
+ "INT",
23
+ "UHYPER",
24
+ "UINT",
25
+ "Array",
26
+ "Decoder",
27
+ "Encoder",
28
+ "EnumType",
29
+ "FixedArray",
30
+ "FixedOpaque",
31
+ "OptionalOf",
32
+ "String",
33
+ "StructType",
34
+ "UnionType",
35
+ "VarOpaque",
36
+ "XdrError",
37
+ "XdrType",
38
+ "decode",
39
+ "encode",
40
+ ]
41
+
42
+ UNBOUNDED = 2**32 - 1
43
+ """The XDR limit of a variable-length item that declares no bound."""
44
+
45
+ T = TypeVar("T")
46
+
47
+
48
+ class XdrError(LibvirtPyError):
49
+ """Data does not follow the XDR layout it should."""
50
+
51
+
52
+ class Encoder:
53
+ """Collects the bytes of an XDR value."""
54
+
55
+ def __init__(self) -> None:
56
+ """Create an empty encoder."""
57
+ self._buffer = bytearray()
58
+
59
+ def getvalue(self) -> bytes:
60
+ """Return everything written so far.
61
+
62
+ :returns: the encoded bytes.
63
+ """
64
+ return bytes(self._buffer)
65
+
66
+ def pack(self, fmt: str, value: int | float) -> None:
67
+ """Write a number with a ``struct`` format.
68
+
69
+ :param fmt: a big-endian ``struct`` format, e.g. ``">i"``.
70
+ :param value: the number.
71
+ :raises XdrError: if the number does not fit the format.
72
+ """
73
+ try:
74
+ self._buffer += struct.pack(fmt, value)
75
+ except struct.error as error:
76
+ raise XdrError(f"cannot encode {value!r} as {fmt!r}: {error}") from error
77
+
78
+ def write_opaque(self, data: bytes) -> None:
79
+ """Write bytes followed by the padding to a multiple of four.
80
+
81
+ :param data: the bytes.
82
+ """
83
+ self._buffer += data
84
+ self._buffer += b"\0" * (-len(data) % 4)
85
+
86
+
87
+ class Decoder:
88
+ """Reads XDR values from bytes."""
89
+
90
+ def __init__(self, data: bytes) -> None:
91
+ """Create a decoder at the start of ``data``.
92
+
93
+ :param data: the bytes to read.
94
+ """
95
+ self._data = data
96
+ self._offset = 0
97
+
98
+ @property
99
+ def remaining(self) -> int:
100
+ """The number of bytes not read yet."""
101
+ return len(self._data) - self._offset
102
+
103
+ def unpack(self, fmt: str) -> Any: # struct.unpack returns Any-typed items
104
+ """Read one number with a ``struct`` format.
105
+
106
+ :param fmt: a big-endian ``struct`` format of one item, e.g. ``">i"``.
107
+ :returns: the number.
108
+ :raises XdrError: if there are not enough bytes.
109
+ """
110
+ (value,) = struct.unpack(fmt, self.read(struct.calcsize(fmt)))
111
+ return value
112
+
113
+ def read(self, count: int) -> bytes:
114
+ """Read raw bytes.
115
+
116
+ :param count: how many bytes.
117
+ :returns: the bytes.
118
+ :raises XdrError: if fewer than ``count`` bytes remain.
119
+ """
120
+ if count > self.remaining:
121
+ raise XdrError(f"need {count} bytes, {self.remaining} left")
122
+
123
+ start = self._offset
124
+ self._offset += count
125
+ return self._data[start : self._offset]
126
+
127
+ def read_opaque(self, count: int) -> bytes:
128
+ """Read ``count`` bytes and skip their padding.
129
+
130
+ :param count: the length of the data.
131
+ :returns: the data without the padding.
132
+ :raises XdrError: if the data or the padding is missing or not zero.
133
+ """
134
+ data = self.read(count)
135
+ padding = self.read(-count % 4)
136
+ if padding.strip(b"\0"):
137
+ raise XdrError("padding is not zero")
138
+ return data
139
+
140
+
141
+ class XdrType(Protocol):
142
+ """How to write and read one kind of value."""
143
+
144
+ def encode(self, encoder: Encoder, value: Any) -> None:
145
+ """Write ``value``.
146
+
147
+ :param encoder: where to write.
148
+ :param value: the value to write.
149
+ """
150
+
151
+ def decode(self, decoder: Decoder) -> Any:
152
+ """Read a value.
153
+
154
+ :param decoder: where to read.
155
+ :returns: the value.
156
+ """
157
+
158
+
159
+ @dataclass(frozen=True)
160
+ class _Number:
161
+ """A number with a fixed ``struct`` format."""
162
+
163
+ fmt: str
164
+ convert: Callable[[Any], Any] = lambda value: value
165
+
166
+ def encode(self, encoder: Encoder, value: Any) -> None:
167
+ """Write the number.
168
+
169
+ :param encoder: where to write.
170
+ :param value: the number.
171
+ """
172
+ encoder.pack(self.fmt, self.convert(value))
173
+
174
+ def decode(self, decoder: Decoder) -> Any:
175
+ """Read the number.
176
+
177
+ :param decoder: where to read.
178
+ :returns: the number.
179
+ """
180
+ return decoder.unpack(self.fmt)
181
+
182
+
183
+ @dataclass(frozen=True)
184
+ class _Boolean:
185
+ """An XDR ``bool``: the integer 0 or 1."""
186
+
187
+ def encode(self, encoder: Encoder, value: Any) -> None:
188
+ """Write the boolean.
189
+
190
+ :param encoder: where to write.
191
+ :param value: the boolean.
192
+ """
193
+ encoder.pack(">I", 1 if value else 0)
194
+
195
+ def decode(self, decoder: Decoder) -> bool:
196
+ """Read the boolean.
197
+
198
+ :param decoder: where to read.
199
+ :returns: the boolean.
200
+ :raises XdrError: if the integer is neither 0 nor 1.
201
+ """
202
+ number = decoder.unpack(">I")
203
+ if number > 1:
204
+ raise XdrError(f"{number} is not a boolean")
205
+ return bool(number == 1)
206
+
207
+
208
+ INT = _Number(">i")
209
+ UINT = _Number(">I")
210
+ HYPER = _Number(">q")
211
+ UHYPER = _Number(">Q")
212
+ FLOAT = _Number(">f")
213
+ DOUBLE = _Number(">d")
214
+ BOOL = _Boolean()
215
+
216
+
217
+ @dataclass(frozen=True)
218
+ class String:
219
+ """A UTF-8 string of at most ``maximum`` bytes."""
220
+
221
+ maximum: int = UNBOUNDED
222
+
223
+ def encode(self, encoder: Encoder, value: Any) -> None:
224
+ """Write the string.
225
+
226
+ :param encoder: where to write.
227
+ :param value: the text.
228
+ :raises XdrError: if it is longer than the maximum.
229
+ """
230
+ data = value.encode("utf-8")
231
+ if len(data) > self.maximum:
232
+ raise XdrError(f"string of {len(data)} bytes, maximum {self.maximum}")
233
+ encoder.pack(">I", len(data))
234
+ encoder.write_opaque(data)
235
+
236
+ def decode(self, decoder: Decoder) -> str:
237
+ """Read the string.
238
+
239
+ :param decoder: where to read.
240
+ :returns: the text.
241
+ :raises XdrError: if it is too long or not valid UTF-8.
242
+ """
243
+ length = decoder.unpack(">I")
244
+ if length > self.maximum:
245
+ raise XdrError(f"string of {length} bytes, maximum {self.maximum}")
246
+ data = decoder.read_opaque(length)
247
+ try:
248
+ return data.decode("utf-8")
249
+ except UnicodeDecodeError as error:
250
+ raise XdrError(f"string is not UTF-8: {error}") from error
251
+
252
+
253
+ @dataclass(frozen=True)
254
+ class FixedOpaque:
255
+ """Exactly ``size`` bytes."""
256
+
257
+ size: int
258
+
259
+ def encode(self, encoder: Encoder, value: Any) -> None:
260
+ """Write the bytes.
261
+
262
+ :param encoder: where to write.
263
+ :param value: the bytes.
264
+ :raises XdrError: if the length is not ``size``.
265
+ """
266
+ if len(value) != self.size:
267
+ raise XdrError(f"need {self.size} bytes, got {len(value)}")
268
+ encoder.write_opaque(bytes(value))
269
+
270
+ def decode(self, decoder: Decoder) -> bytes:
271
+ """Read the bytes.
272
+
273
+ :param decoder: where to read.
274
+ :returns: the bytes.
275
+ """
276
+ return decoder.read_opaque(self.size)
277
+
278
+
279
+ @dataclass(frozen=True)
280
+ class VarOpaque:
281
+ """At most ``maximum`` bytes, with a length in front."""
282
+
283
+ maximum: int = UNBOUNDED
284
+
285
+ def encode(self, encoder: Encoder, value: Any) -> None:
286
+ """Write the bytes.
287
+
288
+ :param encoder: where to write.
289
+ :param value: the bytes.
290
+ :raises XdrError: if there are more than the maximum.
291
+ """
292
+ if len(value) > self.maximum:
293
+ raise XdrError(f"{len(value)} bytes, maximum {self.maximum}")
294
+ encoder.pack(">I", len(value))
295
+ encoder.write_opaque(bytes(value))
296
+
297
+ def decode(self, decoder: Decoder) -> bytes:
298
+ """Read the bytes.
299
+
300
+ :param decoder: where to read.
301
+ :returns: the bytes.
302
+ :raises XdrError: if there are more than the maximum.
303
+ """
304
+ length = decoder.unpack(">I")
305
+ if length > self.maximum:
306
+ raise XdrError(f"{length} bytes, maximum {self.maximum}")
307
+ return decoder.read_opaque(length)
308
+
309
+
310
+ @dataclass(frozen=True)
311
+ class Array:
312
+ """At most ``maximum`` items, with a count in front."""
313
+
314
+ item: XdrType
315
+ maximum: int = UNBOUNDED
316
+
317
+ def encode(self, encoder: Encoder, value: Any) -> None:
318
+ """Write the items.
319
+
320
+ :param encoder: where to write.
321
+ :param value: the items.
322
+ :raises XdrError: if there are more than the maximum.
323
+ """
324
+ if len(value) > self.maximum:
325
+ raise XdrError(f"{len(value)} items, maximum {self.maximum}")
326
+ encoder.pack(">I", len(value))
327
+ for item in value:
328
+ self.item.encode(encoder, item)
329
+
330
+ def decode(self, decoder: Decoder) -> list[Any]:
331
+ """Read the items.
332
+
333
+ :param decoder: where to read.
334
+ :returns: the items.
335
+ :raises XdrError: if there are more than the maximum or more than the
336
+ remaining bytes could hold.
337
+ """
338
+ count = decoder.unpack(">I")
339
+ if count > self.maximum or count > decoder.remaining:
340
+ raise XdrError(f"{count} items, maximum {self.maximum}")
341
+ return [self.item.decode(decoder) for _ in range(count)]
342
+
343
+
344
+ @dataclass(frozen=True)
345
+ class FixedArray:
346
+ """Exactly ``size`` items, without a count."""
347
+
348
+ item: XdrType
349
+ size: int
350
+
351
+ def encode(self, encoder: Encoder, value: Any) -> None:
352
+ """Write the items.
353
+
354
+ :param encoder: where to write.
355
+ :param value: the items.
356
+ :raises XdrError: if the count is not ``size``.
357
+ """
358
+ if len(value) != self.size:
359
+ raise XdrError(f"need {self.size} items, got {len(value)}")
360
+ for item in value:
361
+ self.item.encode(encoder, item)
362
+
363
+ def decode(self, decoder: Decoder) -> list[Any]:
364
+ """Read the items.
365
+
366
+ :param decoder: where to read.
367
+ :returns: the items.
368
+ """
369
+ return [self.item.decode(decoder) for _ in range(self.size)]
370
+
371
+
372
+ @dataclass(frozen=True)
373
+ class OptionalOf:
374
+ """A value that may be absent (``T *name`` in XDR)."""
375
+
376
+ item: XdrType
377
+
378
+ def encode(self, encoder: Encoder, value: Any) -> None:
379
+ """Write the flag and, if present, the value.
380
+
381
+ :param encoder: where to write.
382
+ :param value: the value, or ``None``.
383
+ """
384
+ encoder.pack(">I", 0 if value is None else 1)
385
+ if value is not None:
386
+ self.item.encode(encoder, value)
387
+
388
+ def decode(self, decoder: Decoder) -> Any:
389
+ """Read the flag and, if present, the value.
390
+
391
+ :param decoder: where to read.
392
+ :returns: the value, or ``None``.
393
+ :raises XdrError: if the flag is neither 0 nor 1.
394
+ """
395
+ flag = decoder.unpack(">I")
396
+ if flag > 1:
397
+ raise XdrError(f"{flag} is not an optional flag")
398
+ return self.item.decode(decoder) if flag else None
399
+
400
+
401
+ @dataclass(frozen=True)
402
+ class EnumType:
403
+ """A member of an ``IntEnum``, written as a signed integer."""
404
+
405
+ enum: type[IntEnum]
406
+
407
+ def encode(self, encoder: Encoder, value: Any) -> None:
408
+ """Write the member.
409
+
410
+ :param encoder: where to write.
411
+ :param value: a member or its number.
412
+ """
413
+ encoder.pack(">i", int(value))
414
+
415
+ def decode(self, decoder: Decoder) -> IntEnum:
416
+ """Read the member.
417
+
418
+ :param decoder: where to read.
419
+ :returns: the member.
420
+ :raises XdrError: if the number is not a member.
421
+ """
422
+ number = decoder.unpack(">i")
423
+ try:
424
+ return self.enum(number)
425
+ except ValueError as error:
426
+ raise XdrError(f"{number} is not a {self.enum.__name__}") from error
427
+
428
+
429
+ class XdrStruct(Protocol):
430
+ """A dataclass with an ``xdr_fields`` table."""
431
+
432
+ xdr_fields: ClassVar[tuple[tuple[str, XdrType], ...]]
433
+
434
+ def __init__(self, **fields: Any) -> None:
435
+ """Create the struct from its fields.
436
+
437
+ :param fields: the fields by attribute name.
438
+ """
439
+
440
+
441
+ @dataclass(frozen=True)
442
+ class StructType:
443
+ """A generated dataclass, written field after field."""
444
+
445
+ cls: type[XdrStruct]
446
+
447
+ def encode(self, encoder: Encoder, value: Any) -> None:
448
+ """Write the fields.
449
+
450
+ :param encoder: where to write.
451
+ :param value: an instance of the class.
452
+ """
453
+ for name, xdr_type in self.cls.xdr_fields:
454
+ xdr_type.encode(encoder, getattr(value, name))
455
+
456
+ def decode(self, decoder: Decoder) -> Any:
457
+ """Read the fields.
458
+
459
+ :param decoder: where to read.
460
+ :returns: an instance of the class.
461
+ """
462
+ fields = {
463
+ name: xdr_type.decode(decoder) for name, xdr_type in self.cls.xdr_fields
464
+ }
465
+ return self.cls(**fields)
466
+
467
+
468
+ class XdrUnion(Protocol):
469
+ """A dataclass with a discriminant, a value and the arms table."""
470
+
471
+ xdr_discriminant: ClassVar[tuple[str, XdrType]]
472
+ xdr_arms: ClassVar[dict[int, XdrType | None]]
473
+
474
+ def __init__(self, **fields: Any) -> None:
475
+ """Create the union from its fields.
476
+
477
+ :param fields: the discriminant and the value.
478
+ """
479
+
480
+
481
+ @dataclass(frozen=True)
482
+ class UnionType:
483
+ """A generated dataclass with a discriminant and the value of one arm."""
484
+
485
+ cls: type[XdrUnion]
486
+
487
+ def encode(self, encoder: Encoder, value: Any) -> None:
488
+ """Write the discriminant and the value of its arm.
489
+
490
+ :param encoder: where to write.
491
+ :param value: an instance of the class.
492
+ :raises XdrError: if the discriminant has no arm.
493
+ """
494
+ name, discriminant_type = self.cls.xdr_discriminant
495
+ discriminant = int(getattr(value, name))
496
+ arm = self._arm(discriminant)
497
+
498
+ discriminant_type.encode(encoder, discriminant)
499
+ if arm is not None:
500
+ arm.encode(encoder, value.value)
501
+
502
+ def decode(self, decoder: Decoder) -> Any:
503
+ """Read the discriminant and the value of its arm.
504
+
505
+ :param decoder: where to read.
506
+ :returns: an instance of the class.
507
+ :raises XdrError: if the discriminant has no arm.
508
+ """
509
+ name, discriminant_type = self.cls.xdr_discriminant
510
+ discriminant = int(discriminant_type.decode(decoder))
511
+ arm = self._arm(discriminant)
512
+
513
+ item = None if arm is None else arm.decode(decoder)
514
+ return self.cls(**{name: discriminant, "value": item})
515
+
516
+ def _arm(self, discriminant: int) -> XdrType | None:
517
+ """Find the type of the arm of a discriminant.
518
+
519
+ :param discriminant: the value of the discriminant.
520
+ :returns: the arm's type, or ``None`` for a void arm.
521
+ :raises XdrError: if there is no such arm.
522
+ """
523
+ if discriminant not in self.cls.xdr_arms:
524
+ raise XdrError(f"no union arm for {discriminant}")
525
+ return self.cls.xdr_arms[discriminant]
526
+
527
+
528
+ def encode(xdr_type: XdrType, value: Any) -> bytes:
529
+ """Encode one value.
530
+
531
+ :param xdr_type: its type.
532
+ :param value: the value.
533
+ :returns: the encoded bytes.
534
+ """
535
+ encoder = Encoder()
536
+ xdr_type.encode(encoder, value)
537
+ return encoder.getvalue()
538
+
539
+
540
+ def decode(xdr_type: XdrType, data: bytes) -> Any:
541
+ """Decode one value that takes all of ``data``.
542
+
543
+ :param xdr_type: its type.
544
+ :param data: the bytes.
545
+ :returns: the value.
546
+ :raises XdrError: if the data is malformed or has bytes left over.
547
+ """
548
+ decoder = Decoder(data)
549
+ value = xdr_type.decode(decoder)
550
+ if decoder.remaining:
551
+ raise XdrError(f"{decoder.remaining} bytes left after the value")
552
+ return value
@@ -0,0 +1,49 @@
1
+ """Pydantic models that parse and generate libvirt XML.
2
+
3
+ The root of every document type is re-exported here; its parts live in the
4
+ package of the document (``libvirtpy.xml.domain``, ``libvirtpy.xml.network``, ...).
5
+ """
6
+
7
+ from libvirtpy.errors import LibvirtPyError
8
+ from libvirtpy.xml._types import OnOff, PresenceFlag, SizeUnit, Uuid, YesNo
9
+ from libvirtpy.xml.backup import Backup, Checkpoint
10
+ from libvirtpy.xml.capabilities import (
11
+ Capabilities,
12
+ DomainCapabilities,
13
+ StoragePoolCapabilities,
14
+ )
15
+ from libvirtpy.xml.domain import Domain
16
+ from libvirtpy.xml.interface import HostInterface
17
+ from libvirtpy.xml.network import Network, NetworkPort
18
+ from libvirtpy.xml.nodedev import Device
19
+ from libvirtpy.xml.nwfilter import Filter, FilterBinding
20
+ from libvirtpy.xml.secret import Secret
21
+ from libvirtpy.xml.snapshot import Snapshot
22
+ from libvirtpy.xml.storage import Pool, Volume
23
+ from libvirtpy.xml.sysinfo import Sysinfo
24
+
25
+ __all__ = [
26
+ "Backup",
27
+ "Capabilities",
28
+ "Checkpoint",
29
+ "Device",
30
+ "Domain",
31
+ "DomainCapabilities",
32
+ "Filter",
33
+ "FilterBinding",
34
+ "HostInterface",
35
+ "LibvirtPyError",
36
+ "Network",
37
+ "NetworkPort",
38
+ "OnOff",
39
+ "Pool",
40
+ "PresenceFlag",
41
+ "Secret",
42
+ "SizeUnit",
43
+ "Snapshot",
44
+ "StoragePoolCapabilities",
45
+ "Sysinfo",
46
+ "Uuid",
47
+ "Volume",
48
+ "YesNo",
49
+ ]