Add new public API virDomainGetCPUStats()
[libvirt-python/ericb.git] / libvirt-override.py
blob8427eab934125d1b3c90acaca8cb393c8c29d07d
2 # Manually written part of python bindings for libvirt
5 # On cygwin, the DLL is called cygvirtmod.dll
6 try:
7 import libvirtmod
8 except ImportError, lib_e:
9 try:
10 import cygvirtmod as libvirtmod
11 except ImportError, cyg_e:
12 if str(cyg_e).count("No module named"):
13 raise lib_e
15 import types
17 # The root of all libvirt errors.
18 class libvirtError(Exception):
19 def __init__(self, defmsg, conn=None, dom=None, net=None, pool=None, vol=None):
21 # Never call virConnGetLastError().
22 # virGetLastError() is now thread local
23 err = virGetLastError()
24 if err is None:
25 msg = defmsg
26 else:
27 msg = err[2]
29 Exception.__init__(self, msg)
31 self.err = err
33 def get_error_code(self):
34 if self.err is None:
35 return None
36 return self.err[0]
38 def get_error_domain(self):
39 if self.err is None:
40 return None
41 return self.err[1]
43 def get_error_message(self):
44 if self.err is None:
45 return None
46 return self.err[2]
48 def get_error_level(self):
49 if self.err is None:
50 return None
51 return self.err[3]
53 def get_str1(self):
54 if self.err is None:
55 return None
56 return self.err[4]
58 def get_str2(self):
59 if self.err is None:
60 return None
61 return self.err[5]
63 def get_str3(self):
64 if self.err is None:
65 return None
66 return self.err[6]
68 def get_int1(self):
69 if self.err is None:
70 return None
71 return self.err[7]
73 def get_int2(self):
74 if self.err is None:
75 return None
76 return self.err[8]
79 # register the libvirt global error handler
81 def registerErrorHandler(f, ctx):
82 """Register a Python function for error reporting.
83 The function is called back as f(ctx, error), with error
84 being a list of information about the error being raised.
85 Returns 1 in case of success."""
86 return libvirtmod.virRegisterErrorHandler(f,ctx)
88 def openAuth(uri, auth, flags):
89 ret = libvirtmod.virConnectOpenAuth(uri, auth, flags)
90 if ret is None:raise libvirtError('virConnectOpenAuth() failed')
91 return virConnect(_obj=ret)
95 # Return library version.
97 def getVersion (name = None):
98 """If no name parameter is passed (or name is None) then the
99 version of the libvirt library is returned as an integer.
101 If a name is passed and it refers to a driver linked to the
102 libvirt library, then this returns a tuple of (library version,
103 driver version).
105 If the name passed refers to a non-existent driver, then you
106 will get the exception 'no support for hypervisor'.
108 Versions numbers are integers: 1000000*major + 1000*minor + release."""
109 if name is None:
110 ret = libvirtmod.virGetVersion ();
111 else:
112 ret = libvirtmod.virGetVersion (name);
113 if ret is None: raise libvirtError ("virGetVersion() failed")
114 return ret
118 # Invoke an EventHandle callback
120 def _eventInvokeHandleCallback(watch, fd, event, opaque, opaquecompat=None):
122 Invoke the Event Impl Handle Callback in C
124 # libvirt 0.9.2 and earlier required custom event loops to know
125 # that opaque=(cb, original_opaque) and pass the values individually
126 # to this wrapper. This should handle the back compat case, and make
127 # future invocations match the virEventHandleCallback prototype
128 if opaquecompat:
129 callback = opaque
130 opaque = opaquecompat
131 else:
132 callback = opaque[0]
133 opaque = opaque[1]
135 libvirtmod.virEventInvokeHandleCallback(watch, fd, event, callback, opaque);
138 # Invoke an EventTimeout callback
140 def _eventInvokeTimeoutCallback(timer, opaque, opaquecompat=None):
142 Invoke the Event Impl Timeout Callback in C
144 # libvirt 0.9.2 and earlier required custom event loops to know
145 # that opaque=(cb, original_opaque) and pass the values individually
146 # to this wrapper. This should handle the back compat case, and make
147 # future invocations match the virEventTimeoutCallback prototype
148 if opaquecompat:
149 callback = opaque
150 opaque = opaquecompat
151 else:
152 callback = opaque[0]
153 opaque = opaque[1]
155 libvirtmod.virEventInvokeTimeoutCallback(timer, callback, opaque);
157 def _dispatchEventHandleCallback(watch, fd, events, cbData):
158 cb = cbData["cb"]
159 opaque = cbData["opaque"]
161 cb(watch, fd, events, opaque)
162 return 0
164 def _dispatchEventTimeoutCallback(timer, cbData):
165 cb = cbData["cb"]
166 opaque = cbData["opaque"]
168 cb(timer, opaque)
169 return 0
171 def virEventAddHandle(fd, events, cb, opaque):
173 register a callback for monitoring file handle events
175 @fd: file handle to monitor for events
176 @events: bitset of events to watch from virEventHandleType constants
177 @cb: callback to invoke when an event occurs
178 @opaque: user data to pass to callback
180 Example callback prototype is:
181 def cb(watch, # int id of the handle
182 fd, # int file descriptor the event occurred on
183 events, # int bitmap of events that have occurred
184 opaque): # opaque data passed to eventAddHandle
186 cbData = {"cb" : cb, "opaque" : opaque}
187 ret = libvirtmod.virEventAddHandle(fd, events, cbData)
188 if ret == -1: raise libvirtError ('virEventAddHandle() failed')
189 return ret
191 def virEventAddTimeout(timeout, cb, opaque):
193 register a callback for a timer event
195 @timeout: time between events in milliseconds
196 @cb: callback to invoke when an event occurs
197 @opaque: user data to pass to callback
199 Setting timeout to -1 will disable the timer. Setting the timeout
200 to zero will cause it to fire on every event loop iteration.
202 Example callback prototype is:
203 def cb(timer, # int id of the timer
204 opaque): # opaque data passed to eventAddTimeout
206 cbData = {"cb" : cb, "opaque" : opaque}
207 ret = libvirtmod.virEventAddTimeout(timeout, cbData)
208 if ret == -1: raise libvirtError ('virEventAddTimeout() failed')
209 return ret