Release of libvirt-python-4.5.0
[libvirt-python/ericb.git] / libvirt-override.py
blobba01c89f50b450149dd02da5ae229de84663b485
2 # Manually written part of python bindings for libvirt
5 # On cygwin, the DLL is called cygvirtmod.dll
6 import sys
8 try:
9 import libvirtmod
10 except ImportError:
11 lib_e = sys.exc_info()[1]
12 try:
13 import cygvirtmod as libvirtmod
14 except ImportError:
15 cyg_e = sys.exc_info()[1]
16 if str(cyg_e).count("No module named"):
17 raise lib_e
20 # The root of all libvirt errors.
21 class libvirtError(Exception):
22 def __init__(self, defmsg, conn=None, dom=None, net=None, pool=None, vol=None):
24 # Never call virConnGetLastError().
25 # virGetLastError() is now thread local
26 err = libvirtmod.virGetLastError()
27 if err is None:
28 msg = defmsg
29 else:
30 msg = err[2]
32 Exception.__init__(self, msg)
34 self.err = err
36 def get_error_code(self):
37 if self.err is None:
38 return None
39 return self.err[0]
41 def get_error_domain(self):
42 if self.err is None:
43 return None
44 return self.err[1]
46 def get_error_message(self):
47 if self.err is None:
48 return None
49 return self.err[2]
51 def get_error_level(self):
52 if self.err is None:
53 return None
54 return self.err[3]
56 def get_str1(self):
57 if self.err is None:
58 return None
59 return self.err[4]
61 def get_str2(self):
62 if self.err is None:
63 return None
64 return self.err[5]
66 def get_str3(self):
67 if self.err is None:
68 return None
69 return self.err[6]
71 def get_int1(self):
72 if self.err is None:
73 return None
74 return self.err[7]
76 def get_int2(self):
77 if self.err is None:
78 return None
79 return self.err[8]
82 # register the libvirt global error handler
84 def registerErrorHandler(f, ctx):
85 """Register a Python function for error reporting.
86 The function is called back as f(ctx, error), with error
87 being a list of information about the error being raised.
88 Returns 1 in case of success."""
89 return libvirtmod.virRegisterErrorHandler(f,ctx)
91 def openAuth(uri, auth, flags=0):
92 ret = libvirtmod.virConnectOpenAuth(uri, auth, flags)
93 if ret is None:raise libvirtError('virConnectOpenAuth() failed')
94 return virConnect(_obj=ret)
98 # Return library version.
100 def getVersion (name = None):
101 """If no name parameter is passed (or name is None) then the
102 version of the libvirt library is returned as an integer.
104 If a name is passed and it refers to a driver linked to the
105 libvirt library, then this returns a tuple of (library version,
106 driver version).
108 If the name passed refers to a non-existent driver, then you
109 will get the exception 'no support for hypervisor'.
111 Versions numbers are integers: 1000000*major + 1000*minor + release."""
112 if name is None:
113 ret = libvirtmod.virGetVersion ()
114 else:
115 ret = libvirtmod.virGetVersion (name)
116 if ret is None: raise libvirtError ("virGetVersion() failed")
117 return ret
121 # Invoke an EventHandle callback
123 def _eventInvokeHandleCallback(watch, fd, event, opaque, opaquecompat=None):
125 Invoke the Event Impl Handle Callback in C
127 # libvirt 0.9.2 and earlier required custom event loops to know
128 # that opaque=(cb, original_opaque) and pass the values individually
129 # to this wrapper. This should handle the back compat case, and make
130 # future invocations match the virEventHandleCallback prototype
131 if opaquecompat:
132 callback = opaque
133 opaque = opaquecompat
134 else:
135 callback = opaque[0]
136 opaque = opaque[1]
138 libvirtmod.virEventInvokeHandleCallback(watch, fd, event, callback, opaque)
141 # Invoke an EventTimeout callback
143 def _eventInvokeTimeoutCallback(timer, opaque, opaquecompat=None):
145 Invoke the Event Impl Timeout Callback in C
147 # libvirt 0.9.2 and earlier required custom event loops to know
148 # that opaque=(cb, original_opaque) and pass the values individually
149 # to this wrapper. This should handle the back compat case, and make
150 # future invocations match the virEventTimeoutCallback prototype
151 if opaquecompat:
152 callback = opaque
153 opaque = opaquecompat
154 else:
155 callback = opaque[0]
156 opaque = opaque[1]
158 libvirtmod.virEventInvokeTimeoutCallback(timer, callback, opaque)
160 def _dispatchEventHandleCallback(watch, fd, events, cbData):
161 cb = cbData["cb"]
162 opaque = cbData["opaque"]
164 cb(watch, fd, events, opaque)
165 return 0
167 def _dispatchEventTimeoutCallback(timer, cbData):
168 cb = cbData["cb"]
169 opaque = cbData["opaque"]
171 cb(timer, opaque)
172 return 0
174 def virEventAddHandle(fd, events, cb, opaque):
176 register a callback for monitoring file handle events
178 @fd: file handle to monitor for events
179 @events: bitset of events to watch from virEventHandleType constants
180 @cb: callback to invoke when an event occurs
181 @opaque: user data to pass to callback
183 Example callback prototype is:
184 def cb(watch, # int id of the handle
185 fd, # int file descriptor the event occurred on
186 events, # int bitmap of events that have occurred
187 opaque): # opaque data passed to eventAddHandle
189 cbData = {"cb" : cb, "opaque" : opaque}
190 ret = libvirtmod.virEventAddHandle(fd, events, cbData)
191 if ret == -1: raise libvirtError ('virEventAddHandle() failed')
192 return ret
194 def virEventAddTimeout(timeout, cb, opaque):
196 register a callback for a timer event
198 @timeout: time between events in milliseconds
199 @cb: callback to invoke when an event occurs
200 @opaque: user data to pass to callback
202 Setting timeout to -1 will disable the timer. Setting the timeout
203 to zero will cause it to fire on every event loop iteration.
205 Example callback prototype is:
206 def cb(timer, # int id of the timer
207 opaque): # opaque data passed to eventAddTimeout
209 cbData = {"cb" : cb, "opaque" : opaque}
210 ret = libvirtmod.virEventAddTimeout(timeout, cbData)
211 if ret == -1: raise libvirtError ('virEventAddTimeout() failed')
212 return ret
216 # a caller for the ff callbacks for custom event loop implementations
219 def virEventInvokeFreeCallback(opaque):
221 Execute callback which frees the opaque buffer
223 @opaque: the opaque object passed to addHandle or addTimeout
225 WARNING: This function should not be called from any call by libvirt's
226 core. It will most probably cause deadlock in C-level libvirt code.
227 Instead it should be scheduled and called from implementation's stack.
229 See https://libvirt.org/html/libvirt-libvirt-event.html#virEventAddHandleFunc
230 for more information.
232 This function is not dependent on any event loop implementation.
235 libvirtmod.virEventInvokeFreeCallback(opaque[2], opaque[1])