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):
25 elif pool
is not None:
30 # Never call virConnGetLastError().
31 # virGetLastError() is now thread local
32 err
= virGetLastError()
38 Exception.__init
__(self
, msg
)
42 def get_error_code(self
):
47 def get_error_domain(self
):
52 def get_error_message(self
):
57 def get_error_level(self
):
88 # register the libvirt global error handler
90 def registerErrorHandler(f
, ctx
):
91 """Register a Python written function to for error reporting.
92 The function is called back as f(ctx, error), with error
93 being a list of information about the error being raised.
94 Returns 1 in case of success."""
95 return libvirtmod
.virRegisterErrorHandler(f
,ctx
)
97 def openAuth(uri
, auth
, flags
):
98 ret
= libvirtmod
.virConnectOpenAuth(uri
, auth
, flags
)
99 if ret
is None:raise libvirtError('virConnectOpenAuth() failed')
100 return virConnect(_obj
=ret
)
104 # Return library version.
106 def getVersion (name
= None):
107 """If no name parameter is passed (or name is None) then the
108 version of the libvirt library is returned as an integer.
110 If a name is passed and it refers to a driver linked to the
111 libvirt library, then this returns a tuple of (library version,
114 If the name passed refers to a non-existent driver, then you
115 will get the exception 'no support for hypervisor'.
117 Versions numbers are integers: 1000000*major + 1000*minor + release."""
119 ret
= libvirtmod
.virGetVersion ();
121 ret
= libvirtmod
.virGetVersion (name
);
122 if ret
is None: raise libvirtError ("virGetVersion() failed")
127 # Invoke an EventHandle callback
129 def eventInvokeHandleCallback (watch
, fd
, event
, callback
, opaque
):
131 Invoke the Event Impl Handle Callback in C
133 libvirtmod
.virEventInvokeHandleCallback(watch
, fd
, event
, callback
, opaque
);
136 # Invoke an EventTimeout callback
138 def eventInvokeTimeoutCallback (timer
, callback
, opaque
):
140 Invoke the Event Impl Timeout Callback in C
142 libvirtmod
.virEventInvokeTimeoutCallback(timer
, callback
, opaque
);