Linux Archive

Linux Archive (http://www.linux-archive.org/)
-   Gentoo Portage Developer (http://www.linux-archive.org/gentoo-portage-developer/)
-   -   add optional callback to python bindings (http://www.linux-archive.org/gentoo-portage-developer/641189-add-optional-callback-python-bindings.html)

"Brian C. Lane" 03-05-2012 06:47 PM

add optional callback to python bindings
 
From: "Brian C. Lane" <bcl@redhat.com>

optionally pass a method to call with the offset and total values.
If this method returns a 1 the check will be aborted.
---
pyisomd5sum.c | 41 +++++++++++++++++++++++++++++++++++------
1 files changed, 35 insertions(+), 6 deletions(-)

diff --git a/pyisomd5sum.c b/pyisomd5sum.c
index a15aff2..5c37397 100644
--- a/pyisomd5sum.c
+++ b/pyisomd5sum.c
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2001-2007 Red Hat, Inc.
+ * Copyright (C) 2001-2012 Red Hat, Inc.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
@@ -31,15 +31,44 @@ static PyMethodDef isomd5sumMethods[] = {
{ NULL }
} ;

+/* Call python object with offset and total
+ * If the object returns true return 1 to abort the check
+ */
+int pythonCB(void *cbdata, long long offset, long long total) {
+ PyObject *arglist, *result;
+ int rc;
+
+ arglist = Py_BuildValue("(LL)", offset, total);
+ result = PyObject_CallObject(cbdata, arglist);
+ Py_DECREF(arglist);
+
+ if (result == NULL)
+ return 1;
+
+ rc = PyObject_IsTrue(result);
+ Py_DECREF(result);
+ return (rc > 0);
+}

static PyObject * doCheckIsoMD5Sum(PyObject * s, PyObject * args) {
+ PyObject *callback = NULL;
char *isofile;
int rc;

- if (!PyArg_ParseTuple(args, "s", &isofile))
- return NULL;
-
- rc = mediaCheckFile(isofile, NULL, NULL);
+ if (!PyArg_ParseTuple(args, "s|O", &isofile, &callback))
+ return NULL;
+
+ if (callback) {
+ if (!PyCallable_Check(callback)) {
+ PyErr_SetString(PyExc_TypeError, "parameter must be callable");
+ return NULL;
+ }
+
+ rc = mediaCheckFile(isofile, pythonCB, callback);
+ Py_DECREF(callback);
+ } else {
+ rc = mediaCheckFile(isofile, NULL, NULL);
+ }

return Py_BuildValue("i", rc);
}
@@ -50,7 +79,7 @@ static PyObject * doImplantIsoMD5Sum(PyObject * s, PyObject * args) {
int rc;

if (!PyArg_ParseTuple(args, "sii", &isofile, &supported, &forceit))
- return NULL;
+ return NULL;

rc = implantISOFile(isofile, supported, forceit, 1, &errstr);

--
1.7.7.6

_______________________________________________
Anaconda-devel-list mailing list
Anaconda-devel-list@redhat.com
https://www.redhat.com/mailman/listinfo/anaconda-devel-list

Chris Lumens 03-08-2012 06:18 PM

add optional callback to python bindings
 
> optionally pass a method to call with the offset and total values.
> If this method returns a 1 the check will be aborted.
> ---
> pyisomd5sum.c | 41 +++++++++++++++++++++++++++++++++++------
> 1 files changed, 35 insertions(+), 6 deletions(-)

Turns out, I don't need this for the verify stuff in the new UI. See my
commits using all that GIO stuff for how I did it.

But anyway, these still look fine and does seem like something the
library should support.

- Chris

_______________________________________________
Anaconda-devel-list mailing list
Anaconda-devel-list@redhat.com
https://www.redhat.com/mailman/listinfo/anaconda-devel-list


All times are GMT. The time now is 10:04 AM.

VBulletin, Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.