--- /dev/null
+/*#############################################################################
+# #
+# 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/parser.h>
+
+#include "pakfire.h"
+#include "parser.h"
+
+static ParserObject* Parser_new_core(PyTypeObject* type, PakfireParser parser) {
+ ParserObject* self = (ParserObject *)type->tp_alloc(type, 0);
+ if (self) {
+ self->parser = parser;
+ }
+
+ return self;
+}
+
+PyObject* new_parser(PakfireParser parser) {
+ ParserObject* p = Parser_new_core(&ParserType, parser);
+
+ return (PyObject*)p;
+}
+
+static PyObject* Parser_new(PyTypeObject* type, PyObject* args, PyObject* kwds) {
+ ParserObject* self = Parser_new_core(type, NULL);
+
+ return (PyObject *)self;
+}
+
+static void Parser_dealloc(ParserObject* self) {
+ pakfire_parser_unref(self->parser);
+
+ Py_TYPE(self)->tp_free((PyObject *)self);
+}
+
+static int Parser_init(ParserObject* self, PyObject* args, PyObject* kwds) {
+ PakfireObject* pakfire = NULL;
+ const char* namespace = NULL;
+
+ if (!PyArg_ParseTuple(args, "O!|z", &PakfireType, &pakfire, &namespace))
+ return -1;
+
+ // Allocate a new parser
+ self->parser = pakfire_parser_create(pakfire->pakfire, NULL, namespace);
+ if (!self->parser) {
+ // What went wrong here?
+ return -1;
+ }
+
+ return 0;
+}
+
+static PyObject* Parser_repr(ParserObject* self) {
+ const char* namespace = pakfire_parser_get_namespace(self->parser);
+
+ return PyUnicode_FromFormat("<_pakfire.Parser %s>", namespace);
+}
+
+static PyObject* Parser_str(ParserObject* self) {
+ char* s = pakfire_parser_dump(self->parser);
+ if (!s) {
+ PyErr_SetFromErrno(PyExc_OSError);
+ return NULL;
+ }
+
+ PyObject* ret = PyUnicode_FromString(s);
+ free(s);
+
+ return ret;
+}
+
+static PyObject* Parser_parse(ParserObject* self, PyObject* args) {
+ const char* data = NULL;
+
+ if (!PyArg_ParseTuple(args, "s", &data))
+ return NULL;
+
+ int r = pakfire_parser_parse(self->parser, data, strlen(data));
+ if (r) {
+ PyErr_SetFromErrno(PyExc_OSError);
+ return NULL;
+ }
+
+ Py_RETURN_NONE;
+}
+
+static PyObject* Parser_get(ParserObject* self, PyObject* args) {
+ const char* key = NULL;
+
+ if (!PyArg_ParseTuple(args, "s", &key))
+ return NULL;
+
+ char* value = pakfire_parser_get(self->parser, key);
+ if (!value) {
+ PyErr_SetFromErrno(PyExc_OSError);
+ return NULL;
+ }
+
+ PyObject* ret = PyUnicode_FromString(value);
+ free(value);
+
+ return ret;
+}
+
+static struct PyMethodDef Parser_methods[] = {
+ {
+ "parse",
+ (PyCFunction)Parser_parse,
+ METH_VARARGS,
+ NULL,
+ },
+ {
+ "get",
+ (PyCFunction)Parser_get,
+ METH_VARARGS,
+ NULL,
+ },
+ { NULL },
+};
+
+PyTypeObject ParserType = {
+ PyVarObject_HEAD_INIT(NULL, 0)
+ tp_name: "_pakfire.Parser",
+ tp_basicsize: sizeof(ParserObject),
+ tp_flags: Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
+ tp_new: Parser_new,
+ tp_dealloc: (destructor)Parser_dealloc,
+ tp_init: (initproc)Parser_init,
+ tp_doc: "Parser Object",
+ tp_methods: Parser_methods,
+ tp_repr: (reprfunc)Parser_repr,
+ tp_str: (reprfunc)Parser_str,
+};
--- /dev/null
+/*#############################################################################
+# #
+# 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_PARSER_H
+#define PYTHON_PAKFIRE_PARSER_H
+
+#include <Python.h>
+
+#include <pakfire/parser.h>
+
+typedef struct {
+ PyObject_HEAD
+ PakfireParser parser;
+} ParserObject;
+
+extern PyTypeObject ParserType;
+
+PyObject* new_parser(PakfireParser parser);
+
+#endif /* PYTHON_PAKFIRE_PARSER_H */