Debian Codegraph @

libvirt-override.py

raw symbols SCIP
1
2 from types import TracebackType
3 from typing import Any, Callable, Optional, overload, TypeVar, Union
4 _T = TypeVar('_T')
5 _EventCB = Callable[[int, int, int, _T], None]
6 _EventAddHandleFunc = Callable[[int, int, _EventCB[_T], _T], int]
7 _EventUpdateHandleFunc = Callable[[int, int], None]
8 _EventRemoveHandleFunc = Callable[[int], int]
9 _TimerCB = Callable[[int, _T], None]
10 _EventAddTimeoutFunc = Callable[[int, _TimerCB[_T], _T], int]
11 _EventUpdateTimeoutFunc = Callable[[int, int], None]
12 _EventRemoveTimeoutFunc = Callable[[int], int]
13 _DomainCB = Callable[['virConnect', 'virDomain', int, int, _T], Optional[int]]
14 _BlkioParameter = dict[str, Any]
15 _MemoryParameter = dict[str, Any]
16 _SchedParameter = dict[str, Any]
17 _TypedParameter = dict[str, Any]
18 _RawError = tuple[int, int, str, int, str, Optional[str], Optional[str], int, int]
19
20
21 # The root of all libvirt errors.
22 class libvirtError(Exception):
23 def __init__(self, defmsg: str) -> None:
24
25 # Never call virConnGetLastError().
26 # virGetLastError() is now thread local
27 err = libvirtmod.virGetLastError() # type: Optional[_RawError]
28 if err is None:
29 msg = defmsg
30 else:
31 msg = err[2]
32
33 Exception.__init__(self, msg)
34
35 self.err = err
36
37 def get_error_code(self) -> Optional[int]:
38 if self.err is None:
39 return None
40 return self.err[0]
41
42 def get_error_domain(self) -> Optional[int]:
43 if self.err is None:
44 return None
45 return self.err[1]
46
47 def get_error_message(self) -> Optional[str]:
48 if self.err is None:
49 return None
50 return self.err[2]
51
52 def get_error_level(self) -> Optional[int]:
53 if self.err is None:
54 return None
55 return self.err[3]
56
57 def get_str1(self) -> Optional[str]:
58 if self.err is None:
59 return None
60 return self.err[4]
61
62 def get_str2(self) -> Optional[str]:
63 if self.err is None:
64 return None
65 return self.err[5]
66
67 def get_str3(self) -> Optional[str]:
68 if self.err is None:
69 return None
70 return self.err[6]
71
72 def get_int1(self) -> Optional[int]:
73 if self.err is None:
74 return None
75 return self.err[7]
76
77 def get_int2(self) -> Optional[int]:
78 if self.err is None:
79 return None
80 return self.err[8]
81
82
83 #
84 # register the libvirt global error handler
85 #
86 def registerErrorHandler(f: Callable[[_T, _RawError], None], ctx: _T) -> int:
87 """Register a Python function for error reporting.
88 The function is called back as f(ctx, error), with error
89 being a list of information about the error being raised.
90 Returns 1 in case of success."""
91 return libvirtmod.virRegisterErrorHandler(f, ctx)
92
93
94 def openAuth(uri: str, auth: list, flags: Optional[int] = 0) -> 'virConnect':
95 # TODO: The C code requires a List and there is not *Mutable*Tuple for a better description such as
96 # auth: tuple[list[int], Callable[[list[Mutabletuple[int, str, str, str, Any]], _T], int], _T]
97 """
98 This function should be called first to get a connection to the
99 Hypervisor. If necessary, authentication will be performed fetching
100 credentials via the callback.
101
102 See :py:func:`open` for notes about environment variables which can
103 have an effect on opening drivers and freeing the connection resources.
104
105 :param str uri: (Optional) connection URI, see https://libvirt.org/uri.html
106 :param auth: a list that contains 3 items:
107 - a list of supported credential types
108 - a callable that takes 2 arguments (credentials, user-data) and returns 0 on succcess and -1 on errors.
109 The credentials argument is a list of credentials that libvirt (actually
110 the ESX driver) would like to request. An element of this list is itself a
111 list containing 5 items (4 inputs, 1 output):
112 - the credential type, e.g. :py:const:`libvirt.VIR_CRED_AUTHNAME`
113 - a prompt to be displayed to the user
114 - a challenge, the ESX driver sets this to the hostname to allow automatic
115 distinction between requests for ESX and vCenter credentials
116 - a default result for the request
117 - a place to store the actual result for the request
118 - user data that will be passed to the callable as second argument
119 :param int flags: bitwise-OR of virConnectFlags
120 :returns: a :py:class:`virConnect` instance on success.
121 :raises libvirtError: on errors.
122 """
123 ret = libvirtmod.virConnectOpenAuth(uri, auth, flags)
124 if ret is None:
125 raise libvirtError('virConnectOpenAuth() failed')
126 return virConnect(_obj=ret)
127
128
129 #
130 # Return library version.
131 #
132 def getVersion(name: Optional[str] = None) -> int:
133 """If no name parameter is passed (or name is None) then the
134 version of the libvirt library is returned as an integer.
135
136 If a name is passed and it refers to a driver linked to the
137 libvirt library, then this returns a tuple of (library version,
138 driver version).
139
140 If the name passed refers to a non-existent driver, then you
141 will get the exception 'no support for hypervisor'.
142
143 Versions numbers are integers: 1000000*major + 1000*minor + release."""
144 if name is None:
145 ret = libvirtmod.virGetVersion()
146 else:
147 ret = libvirtmod.virGetVersion(name)
148 if ret is None:
149 raise libvirtError("virGetVersion() failed")
150 return ret
151
152
153 #
154 # Invoke an EventHandle callback
155 #
156 @overload
157 def _eventInvokeHandleCallback(watch: int, fd: int, event: int, opaque: tuple[_EventCB[_T], _T], opaquecompat: None = None) -> None: ... # noqa E704
158 @overload # noqa F811
159 def _eventInvokeHandleCallback(watch: int, fd: int, event: int, opaque: _EventCB[_T], opaquecompat: Optional[_T] = None) -> None: ... # noqa E704
160 def _eventInvokeHandleCallback(watch: int, fd: int, event: int, opaque: Union[tuple[_EventCB[_T], _T], _EventCB[_T]], opaquecompat: Optional[_T] = None) -> None: # noqa F811
161 """
162 Invoke the Event Impl Handle Callback in C
163 """
164 # libvirt 0.9.2 and earlier required custom event loops to know
165 # that opaque=(cb, original_opaque) and pass the values individually
166 # to this wrapper. This should handle the back compat case, and make
167 # future invocations match the virEventHandleCallback prototype
168 if opaquecompat:
169 callback = opaque
170 opaque_ = opaquecompat
171 else:
172 assert isinstance(opaque, tuple)
173 callback = opaque[0]
174 opaque_ = opaque[1]
175
176 libvirtmod.virEventInvokeHandleCallback(watch, fd, event, callback, opaque_)
177
178
179 #
180 # Invoke an EventTimeout callback
181 #
182 def _eventInvokeTimeoutCallback(timer: int, opaque: Union[tuple[_TimerCB[_T], _T], _TimerCB[_T]], opaquecompat: Optional[_T] = None) -> None:
183 """
184 Invoke the Event Impl Timeout Callback in C
185 """
186 # libvirt 0.9.2 and earlier required custom event loops to know
187 # that opaque=(cb, original_opaque) and pass the values individually
188 # to this wrapper. This should handle the back compat case, and make
189 # future invocations match the virEventTimeoutCallback prototype
190 if opaquecompat:
191 callback = opaque
192 opaque_ = opaquecompat
193 else:
194 assert isinstance(opaque, tuple)
195 callback = opaque[0]
196 opaque_ = opaque[1]
197
198 libvirtmod.virEventInvokeTimeoutCallback(timer, callback, opaque_)
199
200
201 def _dispatchEventHandleCallback(watch: int, fd: int, events: int, cbData: dict[str, Any]) -> int:
202 cb = cbData["cb"]
203 opaque = cbData["opaque"]
204
205 cb(watch, fd, events, opaque)
206 return 0
207
208
209 def _dispatchEventTimeoutCallback(timer: int, cbData: dict[str, Any]) -> int:
210 cb = cbData["cb"]
211 opaque = cbData["opaque"]
212
213 cb(timer, opaque)
214 return 0
215
216
217 def virEventAddHandle(fd: int, events: int, cb: _EventCB[_T], opaque: _T) -> int:
218 """
219 register a callback for monitoring file handle events
220
221 @fd: file handle to monitor for events
222 @events: bitset of events to watch from virEventHandleType constants
223 @cb: callback to invoke when an event occurs
224 @opaque: user data to pass to callback
225
226 Example callback prototype is:
227 def cb(watch, # int id of the handle
228 fd, # int file descriptor the event occurred on
229 events, # int bitmap of events that have occurred
230 opaque): # opaque data passed to eventAddHandle
231 """
232 cbData = {"cb": cb, "opaque": opaque}
233 ret = libvirtmod.virEventAddHandle(fd, events, cbData)
234 if ret == -1:
235 raise libvirtError('virEventAddHandle() failed')
236 return ret
237
238
239 def virEventAddTimeout(timeout: int, cb: _TimerCB[_T], opaque: _T) -> int:
240 """
241 register a callback for a timer event
242
243 @timeout: time between events in milliseconds
244 @cb: callback to invoke when an event occurs
245 @opaque: user data to pass to callback
246
247 Setting timeout to -1 will disable the timer. Setting the timeout
248 to zero will cause it to fire on every event loop iteration.
249
250 Example callback prototype is:
251 def cb(timer, # int id of the timer
252 opaque): # opaque data passed to eventAddTimeout
253 """
254 cbData = {"cb": cb, "opaque": opaque}
255 ret = libvirtmod.virEventAddTimeout(timeout, cbData)
256 if ret == -1:
257 raise libvirtError('virEventAddTimeout() failed')
258 return ret
259
260
261 #
262 # a caller for the ff callbacks for custom event loop implementations
263 #
264
265 def virEventInvokeFreeCallback(opaque: Any) -> None:
266 """
267 Execute callback which frees the opaque buffer
268
269 @opaque: the opaque object passed to addHandle or addTimeout
270
271 WARNING: This function should not be called from any call by libvirt's
272 core. It will most probably cause deadlock in C-level libvirt code.
273 Instead it should be scheduled and called from implementation's stack.
274
275 See https://libvirt.org/html/libvirt-libvirt-event.html#virEventAddHandleFunc
276 for more information.
277
278 This function is not dependent on any event loop implementation.
279 """
280
281 libvirtmod.virEventInvokeFreeCallback(opaque[2], opaque[1])