2 # Manually written part of python bindings for libvirt
5 # On cygwin, the DLL is called cygvirtmod.dll
8 except ImportError, lib_e
:
10 import cygvirtmod
as libvirtmod
11 except ImportError, cyg_e
:
12 if str(cyg_e
).count("No module named"):
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()
29 Exception.__init
__(self
, msg
)
33 def get_error_code(self
):
38 def get_error_domain(self
):
43 def get_error_message(self
):
48 def get_error_level(self
):
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,
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."""
110 ret
= libvirtmod
.virGetVersion ();
112 ret
= libvirtmod
.virGetVersion (name
);
113 if ret
is None: raise libvirtError ("virGetVersion() failed")
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
130 opaque
= opaquecompat
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
150 opaque
= opaquecompat
155 libvirtmod
.virEventInvokeTimeoutCallback(timer
, callback
, opaque
);
157 def _dispatchEventHandleCallback(watch
, fd
, events
, cbData
):
159 opaque
= cbData
["opaque"]
161 cb(watch
, fd
, events
, opaque
)
164 def _dispatchEventTimeoutCallback(timer
, cbData
):
166 opaque
= cbData
["opaque"]
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')
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')