[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

[libvirt] Re: [PATCH 6/6] host ("node") device enumeration - python bindings



This patch implements the python bindings, rather lamely.  I'll submit a
new version with a proper NodeDevice object soon ...

diff --git a/python/generator.py b/python/generator.py
index c706b19..a8fd969 100755
--- a/python/generator.py
+++ b/python/generator.py
@@ -258,6 +258,11 @@ py_types = {
     'const virConnectPtr':  ('O', "virConnect", "virConnectPtr", "virConnectPtr"),
     'virConnect *':  ('O', "virConnect", "virConnectPtr", "virConnectPtr"),
     'const virConnect *':  ('O', "virConnect", "virConnectPtr", "virConnectPtr"),
+
+    'virNodeDevicePtr':  ('O', "virNodeDevice", "virNodeDevicePtr", "virNodeDevicePtr"),
+    'const virNodeDevicePtr':  ('O', "virNodeDevice", "virNodeDevicePtr", "virNodeDevicePtr"),
+    'virNodeDevice *':  ('O', "virNodeDevice", "virNodeDevicePtr", "virNodeDevicePtr"),
+    'const virNodeDevice *':  ('O', "virNodeDevice", "virNodeDevicePtr", "virNodeDevicePtr"),
 }
 
 py_return_types = {
@@ -315,6 +320,8 @@ skip_impl = (
     'virStoragePoolListVolumes',
     'virDomainBlockPeek',
     'virDomainMemoryPeek',
+    'virNodeListDevicesByCap',
+    'virNodeListDevices',
 )
 
 
@@ -332,6 +339,10 @@ skip_function = (
     'virCopyLastError', # Python API is called virGetLastError instead
     'virConnectOpenAuth', # Python C code is manually written
     'virDefaultErrorFunc', # Python virErrorFuncHandler impl calls this from C
+    'virNodeDeviceGetName',
+    'virNodeDeviceGetParent',
+    'virNodeDeviceNumOfCaps',
+    'virNodeDeviceListCaps',
 )
 
 
@@ -613,6 +624,7 @@ classes_destructors = {
     "virStoragePool": "virStoragePoolFree",
     "virStorageVol": "virStorageVolFree",
     "virConnect": "virConnectClose",
+    "virNodeDevice" : "virNodeDeviceFree"
 }
 
 functions_noexcept = {
diff --git a/python/libvir.c b/python/libvir.c
index 9cc0c81..e03101d 100644
--- a/python/libvir.c
+++ b/python/libvir.c
@@ -1466,7 +1466,90 @@ libvirt_virStoragePoolLookupByUUID(PyObject *self ATTRIBUTE_UNUSED, PyObject *ar
     return(py_retval);
 }
 
+static PyObject *
+libvirt_virNodeListDevices(PyObject *self ATTRIBUTE_UNUSED,
+                           PyObject *args) {
+    PyObject *py_retval;
+    char **names = NULL;
+    int c_retval, i;
+    virConnectPtr conn;
+    PyObject *pyobj_conn;
+    unsigned int flags;
+
+
+    if (!PyArg_ParseTuple(args, (char *)"Oi:virNodeListDevices", &pyobj_conn, &flags))
+        return(NULL);
+    conn = (virConnectPtr) PyvirConnect_Get(pyobj_conn);
+
+    c_retval = virNodeNumOfDevices(conn, flags);
+    if (c_retval < 0)
+        return VIR_PY_NONE;
+
+    if (c_retval) {
+        names = malloc(sizeof(*names) * c_retval);
+        if (!names)
+            return VIR_PY_NONE;
+        c_retval = virNodeListDevices(conn, names, c_retval, flags);
+        if (c_retval < 0) {
+            free(names);
+            return VIR_PY_NONE;
+        }
+    }
+    py_retval = PyList_New(c_retval);
+
+    if (names) {
+        for (i = 0;i < c_retval;i++) {
+            PyList_SetItem(py_retval, i, libvirt_constcharPtrWrap(names[i]));
+            free(names[i]);
+        }
+        free(names);
+    }
+
+    return(py_retval);
+}
 
+static PyObject *
+libvirt_virNodeListDevicesByCap(PyObject *self ATTRIBUTE_UNUSED,
+                                PyObject *args) {
+    PyObject *py_retval;
+    char **names = NULL;
+    int c_retval, i;
+    virConnectPtr conn;
+    PyObject *pyobj_conn;
+    char *cap;
+    unsigned int flags;
+
+    if (!PyArg_ParseTuple(args, (char *)"Ozi:virNodeListDevicesByCap",
+                          &pyobj_conn, &cap, &flags))
+        return(NULL);
+    conn = (virConnectPtr) PyvirConnect_Get(pyobj_conn);
+
+    c_retval = virNodeNumOfDevicesByCap(conn, cap, flags);
+    if (c_retval < 0)
+        return VIR_PY_NONE;
+
+    if (c_retval) {
+        names = malloc(sizeof(*names) * c_retval);
+        if (!names)
+            return VIR_PY_NONE;
+        c_retval = virNodeListDevicesByCap(conn, cap, names, c_retval, flags);
+        if (c_retval < 0) {
+            free(names);
+            return VIR_PY_NONE;
+        }
+    }
+    py_retval = PyList_New(c_retval);
+
+    if (names) {
+        for (i = 0;i < c_retval;i++) {
+            PyList_SetItem(py_retval, i, libvirt_constcharPtrWrap(names[i]));
+            free(names[i]);
+        }
+        free(names);
+    }
+
+    return(py_retval);
+}
 
 /************************************************************************
  *									*
@@ -1511,6 +1594,8 @@ static PyMethodDef libvirtMethods[] = {
     {(char *) "virStoragePoolGetUUID", libvirt_virStoragePoolGetUUID, METH_VARARGS, NULL},
     {(char *) "virStoragePoolGetUUIDString", libvirt_virStoragePoolGetUUIDString, METH_VARARGS, NULL},
     {(char *) "virStoragePoolLookupByUUID", libvirt_virStoragePoolLookupByUUID, METH_VARARGS, NULL},
+    {(char *) "virNodeListDevices", libvirt_virNodeListDevices, METH_VARARGS, NULL},
+    {(char *) "virNodeListDevicesByCap", libvirt_virNodeListDevicesByCap, METH_VARARGS, NULL},
     {NULL, NULL, 0, NULL}
 };
 
diff --git a/python/libvirt-python-api.xml b/python/libvirt-python-api.xml
index f3b82fc..93a32d8 100644
--- a/python/libvirt-python-api.xml
+++ b/python/libvirt-python-api.xml
@@ -160,5 +160,18 @@
       <return type='int *' info='the list of information or None in case of error'/>
       <arg name='vol' type='virStorageVolPtr' info='a storage vol object'/>
     </function>
+    <function name='virNodeListDevices' file='python'>
+      <info>list the node devices, stores the pointers to the names in @names</info>
+      <arg name='conn' type='virConnectPtr' info='pointer to the hypervisor connection'/>
+      <arg name='flags' type='unsigned int' info='flags (unused; pass 0)'/>
+      <return type='str *' info='the list of Names or None in case of error'/>
+    </function>
+    <function name='virNodeListDevicesByCap' file='python'>
+      <info>list the node devices with a given capability, stores the pointers to the names in @names</info>
+      <arg name='conn' type='virConnectPtr' info='pointer to the hypervisor connection'/>
+      <arg name='cap' type='const unsigned char *' info='capability name'/>
+      <arg name='flags' type='unsigned int' info='flags (unused; pass 0)'/>
+      <return type='str *' info='the list of Names or None in case of error'/>
+    </function>
   </symbols>
 </api>
diff --git a/python/libvirt_wrap.h b/python/libvirt_wrap.h
index f8ecb76..802bf36 100644
--- a/python/libvirt_wrap.h
+++ b/python/libvirt_wrap.h
@@ -65,6 +65,15 @@ typedef struct {
     virStorageVolPtr obj;
 } PyvirStorageVol_Object;
 
+#define PyvirNodeDevice_Get(v) (((v) == Py_None) ? NULL : \
+        (((PyvirNodeDevice_Object *)(v))->obj))
+
+typedef struct {
+    PyObject_HEAD
+    virNodeDevicePtr obj;
+} PyvirNodeDevice_Object;
+
+
 
 PyObject * libvirt_intWrap(int val);
 PyObject * libvirt_longWrap(long val);
@@ -78,6 +87,7 @@ PyObject * libvirt_virDomainPtrWrap(virDomainPtr node);
 PyObject * libvirt_virNetworkPtrWrap(virNetworkPtr node);
 PyObject * libvirt_virStoragePoolPtrWrap(virStoragePoolPtr node);
 PyObject * libvirt_virStorageVolPtrWrap(virStorageVolPtr node);
+PyObject * libvirt_virNodeDevicePtrWrap(virNodeDevicePtr node);
 
 
 /* Provide simple macro statement wrappers (adapted from GLib, in turn from Perl):
diff --git a/python/types.c b/python/types.c
index 8e08d25..aa0555b 100644
--- a/python/types.c
+++ b/python/types.c
@@ -162,3 +162,18 @@ libvirt_virConnectPtrWrap(virConnectPtr node)
                                      NULL);
     return (ret);
 }
+
+PyObject *
+libvirt_virNodeDevicePtrWrap(virNodeDevicePtr node)
+{
+    PyObject *ret;
+
+    if (node == NULL) {
+        Py_INCREF(Py_None);
+        return (Py_None);
+    }
+    ret =
+        PyCObject_FromVoidPtrAndDesc((void *) node, (char *) "virNodeDevicePtr",
+                                     NULL);
+    return (ret);
+}

[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]