]> git.ipfire.org Git - pakfire.git/blame - src/_pakfire/pakfire.c
libpakfire: Create a Pakfire root object
[pakfire.git] / src / _pakfire / pakfire.c
CommitLineData
6e46b18e
MT
1/*#############################################################################
2# #
3# Pakfire - The IPFire package management system #
4# Copyright (C) 2017 Pakfire development team #
5# #
6# This program is free software: you can redistribute it and/or modify #
7# it under the terms of the GNU General Public License as published by #
8# the Free Software Foundation, either version 3 of the License, or #
9# (at your option) any later version. #
10# #
11# This program is distributed in the hope that it will be useful, #
12# but WITHOUT ANY WARRANTY; without even the implied warranty of #
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
14# GNU General Public License for more details. #
15# #
16# You should have received a copy of the GNU General Public License #
17# along with this program. If not, see <http://www.gnu.org/licenses/>. #
18# #
19#############################################################################*/
20
21#include <Python.h>
22
23#include <pakfire/pakfire.h>
24
25#include "pakfire.h"
26
27static PyObject* Pakfire_new(PyTypeObject* type, PyObject* args, PyObject* kwds) {
28 PakfireObject* self = (PakfireObject *)type->tp_alloc(type, 0);
29 if (self) {
30 self->pakfire = NULL;
31 }
32
33 return (PyObject *)self;
34}
35
36static int Pakfire_init(PakfireObject* self, PyObject* args, PyObject* kwds) {
37 const char* path = NULL;
38 const char* arch = NULL;
39
40 if (!PyArg_ParseTuple(args, "ss", &path, &arch))
41 return -1;
42
43 self->pakfire = pakfire_create(path, arch);
44 if (!self->pakfire)
45 return -1;
46
47 return 0;
48}
49
50static void Pakfire_dealloc(PakfireObject* self) {
51 if (self->pakfire)
52 pakfire_unref(self->pakfire);
53
54 Py_TYPE(self)->tp_free((PyObject *)self);
55}
56
57static PyObject* Pakfire_repr(PakfireObject* self) {
58 const char* path = pakfire_get_path(self->pakfire);
59 const char* arch = pakfire_get_arch(self->pakfire);
60
61 return PyUnicode_FromFormat("<_pakfire.Pakfire %s (%s)>", path, arch);
62}
63
64static PyObject* Pakfire_get_path(PakfireObject* self) {
65 const char* path = pakfire_get_path(self->pakfire);
66
67 return PyUnicode_FromString(path);
68}
69
70static PyObject* Pakfire_get_arch(PakfireObject* self) {
71 const char* arch = pakfire_get_arch(self->pakfire);
72
73 return PyUnicode_FromString(arch);
74}
75
76static struct PyMethodDef Pakfire_methods[] = {
77 { NULL },
78};
79
80static struct PyGetSetDef Pakfire_getsetters[] = {
81 {
82 "arch",
83 (getter)Pakfire_get_arch,
84 NULL,
85 NULL,
86 NULL
87 },
88 {
89 "path",
90 (getter)Pakfire_get_path,
91 NULL,
92 NULL,
93 NULL
94 },
95 { NULL },
96};
97
98PyTypeObject PakfireType = {
99 PyVarObject_HEAD_INIT(NULL, 0)
100 tp_name: "_pakfire.Pakfire",
101 tp_basicsize: sizeof(PakfireObject),
102 tp_flags: Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
103 tp_new: Pakfire_new,
104 tp_dealloc: (destructor)Pakfire_dealloc,
105 tp_init: (initproc)Pakfire_init,
106 tp_doc: "Pakfire object",
107 tp_methods: Pakfire_methods,
108 tp_getset: Pakfire_getsetters,
109 tp_repr: (reprfunc)Pakfire_repr,
110};