]> git.ipfire.org Git - pakfire.git/commitdiff
python: Add bindings for Progressbar
authorMichael Tremer <michael.tremer@ipfire.org>
Wed, 21 Apr 2021 10:57:49 +0000 (10:57 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Wed, 21 Apr 2021 10:57:49 +0000 (10:57 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
Makefile.am
src/_pakfire/_pakfiremodule.c
src/_pakfire/progressbar.c [new file with mode: 0644]
src/_pakfire/progressbar.h [new file with mode: 0644]
tests/python/progressbar.py [new file with mode: 0755]

index 357caf844c576a0ac312e0031410d6a27e5ad202..df2b7dc851743d436f9e17e02c6b19f8b62a1f4a 100644 (file)
@@ -172,6 +172,8 @@ _pakfire_la_SOURCES = \
        src/_pakfire/parser.h \
        src/_pakfire/problem.c \
        src/_pakfire/problem.h \
+       src/_pakfire/progressbar.c \
+       src/_pakfire/progressbar.h \
        src/_pakfire/relation.c \
        src/_pakfire/relation.h \
        src/_pakfire/repo.c \
@@ -743,6 +745,7 @@ TESTS_ENVIRONMENT = \
 dist_check_SCRIPTS = \
        tests/python/execute.py \
        tests/python/parser.py \
+       tests/python/progressbar.py \
        tests/python/test.py
 
 TESTS = \
index e65d53c44efffccbb62d72731bd51f06b6a75174..818f5525be5b6fdee29a333346b2d3fe0fed31ec 100644 (file)
@@ -37,6 +37,7 @@
 #include "pakfire.h"
 #include "parser.h"
 #include "problem.h"
+#include "progressbar.h"
 #include "relation.h"
 #include "repo.h"
 #include "request.h"
@@ -151,6 +152,12 @@ PyMODINIT_FUNC PyInit__pakfire(void) {
        Py_INCREF(&ProblemType);
        PyModule_AddObject(module, "Problem", (PyObject *)&ProblemType);
 
+       // Progressbar
+       if (PyType_Ready(&ProgressbarType) < 0)
+               return NULL;
+       Py_INCREF(&ProgressbarType);
+       PyModule_AddObject(module, "Progressbar", (PyObject *)&ProgressbarType);
+
        // Repo
        if (PyType_Ready(&RepoType) < 0)
                return NULL;
diff --git a/src/_pakfire/progressbar.c b/src/_pakfire/progressbar.c
new file mode 100644 (file)
index 0000000..5f4ca66
--- /dev/null
@@ -0,0 +1,304 @@
+/*#############################################################################
+#                                                                             #
+# Pakfire - The IPFire package management system                              #
+# Copyright (C) 2021 Pakfire development team                                 #
+#                                                                             #
+# This program is free software: you can redistribute it and/or modify        #
+# it under the terms of the GNU General Public License as published by        #
+# the Free Software Foundation, either version 3 of the License, or           #
+# (at your option) any later version.                                         #
+#                                                                             #
+# This program is distributed in the hope that it will be useful,             #
+# but WITHOUT ANY WARRANTY; without even the implied warranty of              #
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               #
+# GNU General Public License for more details.                                #
+#                                                                             #
+# You should have received a copy of the GNU General Public License           #
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.       #
+#                                                                             #
+#############################################################################*/
+
+#include <Python.h>
+
+#include <pakfire/progressbar.h>
+
+#include "progressbar.h"
+
+static PyObject* Progressbar_new(PyTypeObject* type, PyObject* args, PyObject* kwds) {
+       ProgressbarObject* self = (ProgressbarObject *)type->tp_alloc(type, 0);
+       if (self) {
+               self->progressbar = NULL;
+       }
+
+       return (PyObject *)self;
+}
+
+static void Progressbar_dealloc(ProgressbarObject* self) {
+       if (self->progressbar)
+               pakfire_progressbar_unref(self->progressbar);
+
+       Py_TYPE(self)->tp_free((PyObject *)self);
+}
+
+static int Progressbar_init(ProgressbarObject* self, PyObject* args, PyObject* kwds) {
+       int r = pakfire_progressbar_create(&self->progressbar, NULL);
+       if (r)
+               return -1;
+
+       return 0;
+}
+
+static PyObject* Progressbar_start(ProgressbarObject* self, PyObject* args) {
+       unsigned long value = 0;
+
+       if (!PyArg_ParseTuple(args, "k", &value))
+               return NULL;
+
+       int r = pakfire_progressbar_start(self->progressbar, value);
+       if (r) {
+               PyErr_SetFromErrno(PyExc_OSError);
+               return NULL;
+       }
+
+       Py_RETURN_NONE;
+}
+
+static PyObject* Progressbar_update(ProgressbarObject* self, PyObject* args) {
+       unsigned long value = 0;
+
+       if (!PyArg_ParseTuple(args, "k", &value))
+               return NULL;
+
+       int r = pakfire_progressbar_update(self->progressbar, value);
+       if (r) {
+               PyErr_SetFromErrno(PyExc_OSError);
+               return NULL;
+       }
+
+       Py_RETURN_NONE;
+}
+
+static PyObject* Progressbar_increment(ProgressbarObject* self) {
+       int r = pakfire_progressbar_increment(self->progressbar);
+       if (r) {
+               PyErr_SetFromErrno(PyExc_OSError);
+               return NULL;
+       }
+
+       Py_RETURN_NONE;
+}
+
+static PyObject* Progressbar_finish(ProgressbarObject* self) {
+       int r = pakfire_progressbar_finish(self->progressbar);
+       if (r) {
+               PyErr_SetFromErrno(PyExc_OSError);
+               return NULL;
+       }
+
+       Py_RETURN_NONE;
+}
+
+static PyObject* Progressbar_reset(ProgressbarObject* self) {
+       int r = pakfire_progressbar_reset(self->progressbar);
+       if (r) {
+               PyErr_SetFromErrno(PyExc_OSError);
+               return NULL;
+       }
+
+       Py_RETURN_NONE;
+}
+
+static PyObject* Progressbar_set_max(ProgressbarObject* self, PyObject* args) {
+       unsigned long value = 0;
+
+       if (!PyArg_ParseTuple(args, "k", &value))
+               return NULL;
+
+       pakfire_progressbar_set_max(self->progressbar, value);
+       Py_RETURN_NONE;
+}
+
+static PyObject* Progressbar_add_string(ProgressbarObject* self, PyObject* args) {
+       const char* string = NULL;
+
+       if (!PyArg_ParseTuple(args, "s", &string))
+               return NULL;
+
+       int r = pakfire_progressbar_add_string(self->progressbar, "%s", string);
+       if (r) {
+               PyErr_SetFromErrno(PyExc_OSError);
+               return NULL;
+       }
+
+       Py_RETURN_NONE;
+}
+
+static PyObject* Progressbar_add_counter(ProgressbarObject* self) {
+       int r = pakfire_progressbar_add_counter(self->progressbar);
+       if (r) {
+               PyErr_SetFromErrno(PyExc_OSError);
+               return NULL;
+       }
+
+       Py_RETURN_NONE;
+}
+
+static PyObject* Progressbar_add_percentage(ProgressbarObject* self) {
+       int r = pakfire_progressbar_add_percentage(self->progressbar);
+       if (r) {
+               PyErr_SetFromErrno(PyExc_OSError);
+               return NULL;
+       }
+
+       Py_RETURN_NONE;
+}
+
+static PyObject* Progressbar_add_bar(ProgressbarObject* self) {
+       int r = pakfire_progressbar_add_bar(self->progressbar);
+       if (r) {
+               PyErr_SetFromErrno(PyExc_OSError);
+               return NULL;
+       }
+
+       Py_RETURN_NONE;
+}
+
+static PyObject* Progressbar_add_timer(ProgressbarObject* self) {
+       int r = pakfire_progressbar_add_timer(self->progressbar);
+       if (r) {
+               PyErr_SetFromErrno(PyExc_OSError);
+               return NULL;
+       }
+
+       Py_RETURN_NONE;
+}
+
+static PyObject* Progressbar_add_bytes_transferred(ProgressbarObject* self) {
+       int r = pakfire_progressbar_add_bytes_transferred(self->progressbar);
+       if (r) {
+               PyErr_SetFromErrno(PyExc_OSError);
+               return NULL;
+       }
+
+       Py_RETURN_NONE;
+}
+
+static PyObject* Progressbar_add_eta(ProgressbarObject* self) {
+       int r = pakfire_progressbar_add_eta(self->progressbar);
+       if (r) {
+               PyErr_SetFromErrno(PyExc_OSError);
+               return NULL;
+       }
+
+       Py_RETURN_NONE;
+}
+
+static PyObject* Progressbar_add_transfer_speed(ProgressbarObject* self) {
+       int r = pakfire_progressbar_add_transfer_speed(self->progressbar);
+       if (r) {
+               PyErr_SetFromErrno(PyExc_OSError);
+               return NULL;
+       }
+
+       Py_RETURN_NONE;
+}
+
+static struct PyMethodDef Progressbar_methods[] = {
+       {
+               "start",
+               (PyCFunction)Progressbar_start,
+               METH_VARARGS,
+               NULL,
+       },
+       {
+               "update",
+               (PyCFunction)Progressbar_update,
+               METH_VARARGS,
+               NULL,
+       },
+       {
+               "increment",
+               (PyCFunction)Progressbar_increment,
+               METH_NOARGS,
+               NULL,
+       },
+       {
+               "finish",
+               (PyCFunction)Progressbar_finish,
+               METH_NOARGS,
+               NULL,
+       },
+       {
+               "reset",
+               (PyCFunction)Progressbar_reset,
+               METH_NOARGS,
+               NULL,
+       },
+       {
+               "set_max",
+               (PyCFunction)Progressbar_set_max,
+               METH_VARARGS,
+               NULL,
+       },
+       {
+               "add_string",
+               (PyCFunction)Progressbar_add_string,
+               METH_VARARGS,
+               NULL,
+       },
+       {
+               "add_counter",
+               (PyCFunction)Progressbar_add_counter,
+               METH_NOARGS,
+               NULL,
+       },
+       {
+               "add_percentage",
+               (PyCFunction)Progressbar_add_percentage,
+               METH_NOARGS,
+               NULL,
+       },
+       {
+               "add_bar",
+               (PyCFunction)Progressbar_add_bar,
+               METH_NOARGS,
+               NULL,
+       },
+       {
+               "add_timer",
+               (PyCFunction)Progressbar_add_timer,
+               METH_NOARGS,
+               NULL,
+       },
+       {
+               "add_bytes_transferred",
+               (PyCFunction)Progressbar_add_bytes_transferred,
+               METH_NOARGS,
+               NULL,
+       },
+       {
+               "add_eta",
+               (PyCFunction)Progressbar_add_eta,
+               METH_NOARGS,
+               NULL,
+       },
+       {
+               "add_transfer_speed",
+               (PyCFunction)Progressbar_add_transfer_speed,
+               METH_NOARGS,
+               NULL,
+       },
+       { NULL },
+};
+
+PyTypeObject ProgressbarType = {
+       PyVarObject_HEAD_INIT(NULL, 0)
+       tp_name:            "_pakfire.Progressbar",
+       tp_basicsize:       sizeof(ProgressbarObject),
+       tp_flags:           Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
+       tp_new:             (newfunc)Progressbar_new,
+       tp_dealloc:         (destructor)Progressbar_dealloc,
+       tp_init:            (initproc)Progressbar_init,
+       tp_doc:             "Progressbar Object",
+       tp_methods:         Progressbar_methods,
+};
diff --git a/src/_pakfire/progressbar.h b/src/_pakfire/progressbar.h
new file mode 100644 (file)
index 0000000..f3b5b2c
--- /dev/null
@@ -0,0 +1,35 @@
+/*#############################################################################
+#                                                                             #
+# Pakfire - The IPFire package management system                              #
+# Copyright (C) 2021 Pakfire development team                                 #
+#                                                                             #
+# This program is free software: you can redistribute it and/or modify        #
+# it under the terms of the GNU General Public License as published by        #
+# the Free Software Foundation, either version 3 of the License, or           #
+# (at your option) any later version.                                         #
+#                                                                             #
+# This program is distributed in the hope that it will be useful,             #
+# but WITHOUT ANY WARRANTY; without even the implied warranty of              #
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               #
+# GNU General Public License for more details.                                #
+#                                                                             #
+# You should have received a copy of the GNU General Public License           #
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.       #
+#                                                                             #
+#############################################################################*/
+
+#ifndef PYTHON_PAKFIRE_PROGRESSBAR_H
+#define PYTHON_PAKFIRE_PROGRESSBAR_H
+
+#include <Python.h>
+
+#include <pakfire/progressbar.h>
+
+typedef struct {
+       PyObject_HEAD
+       struct pakfire_progressbar* progressbar;
+} ProgressbarObject;
+
+extern PyTypeObject ProgressbarType;
+
+#endif /* PYTHON_PAKFIRE_PROGRESSBAR_H */
diff --git a/tests/python/progressbar.py b/tests/python/progressbar.py
new file mode 100755 (executable)
index 0000000..0c40573
--- /dev/null
@@ -0,0 +1,38 @@
+#!/usr/bin/python3
+
+import pakfire
+import time
+import unittest
+
+from pakfire._pakfire import Progressbar
+
+class Test(unittest.TestCase):
+       """
+               This tests the progressbar command
+       """
+       def test_simple(self):
+               p = Progressbar()
+
+               # Add a string
+               p.add_string("Progress...")
+
+               # Add the bar
+               p.add_bar()
+
+               # Show the percentage
+               p.add_percentage()
+
+               # Show ETA
+               p.add_eta()
+
+               # Start the progressbar
+               p.start(1000)
+
+               for i in range(1000):
+                       p.update(i)
+
+               p.finish()
+
+
+if __name__ == "__main__":
+       unittest.main()