]> git.ipfire.org Git - pakfire.git/blob - src/_pakfire/key.c
python: Drop Pakfire reference from Key
[pakfire.git] / src / _pakfire / key.c
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/key.h>
24 #include <pakfire/util.h>
25
26 #include "key.h"
27 #include "pakfire.h"
28
29 PyObject* new_key(PyTypeObject* type, PakfireKey key) {
30 KeyObject* self = (KeyObject *)type->tp_alloc(type, 0);
31 if (self) {
32 self->key = pakfire_key_ref(key);
33 }
34
35 return (PyObject *)self;
36 }
37
38 static PyObject* Key_new(PyTypeObject* type, PyObject* args, PyObject* kwds) {
39 KeyObject* self = (KeyObject *)type->tp_alloc(type, 0);
40 if (self) {
41 self->key = NULL;
42 }
43
44 return (PyObject *)self;
45 }
46
47 static void Key_dealloc(KeyObject* self) {
48 pakfire_key_unref(self->key);
49
50 Py_TYPE(self)->tp_free((PyObject *)self);
51 }
52
53 static int Key_init(KeyObject* self, PyObject* args, PyObject* kwds) {
54 PakfireObject* pakfire;
55 const char* fingerprint = NULL;
56
57 if (!PyArg_ParseTuple(args, "O!s", &PakfireType, &pakfire, &fingerprint))
58 return -1;
59
60 self->key = pakfire_key_get(pakfire->pakfire, fingerprint);
61 if (!self->key)
62 return -1;
63
64 return 0;
65 }
66
67 static PyObject* Key_repr(KeyObject* self) {
68 const char* fingerprint = pakfire_key_get_fingerprint(self->key);
69
70 return PyUnicode_FromFormat("<_pakfire.Key (%s)>", fingerprint);
71 }
72
73 static PyObject* Key_str(KeyObject* self) {
74 char* string = pakfire_key_dump(self->key);
75
76 PyObject* object = PyUnicode_FromString(string);
77 pakfire_free(string);
78
79 return object;
80 }
81
82 static PyObject* Key_get_fingerprint(KeyObject* self) {
83 const char* fingerprint = pakfire_key_get_fingerprint(self->key);
84
85 return PyUnicode_FromString(fingerprint);
86 }
87
88 static PyObject* Key_export(KeyObject* self, PyObject* args) {
89 int secret = 0;
90
91 if (!PyArg_ParseTuple(args, "|p", &secret))
92 return NULL;
93
94 pakfire_key_export_mode_t mode;
95 if (secret)
96 mode = PAKFIRE_KEY_EXPORT_MODE_SECRET;
97 else
98 mode = PAKFIRE_KEY_EXPORT_MODE_PUBLIC;
99
100 // Export the key
101 char* export = pakfire_key_export(self->key, mode);
102
103 PyObject* object = PyUnicode_FromFormat("%s", export);
104 pakfire_free(export);
105
106 return object;
107 }
108
109 static PyObject* Key_delete(KeyObject* self) {
110 int r = pakfire_key_delete(self->key);
111 if (r == 0)
112 Py_RETURN_TRUE;
113
114 return NULL;
115 }
116
117 static struct PyMethodDef Key_methods[] = {
118 {
119 "delete",
120 (PyCFunction)Key_delete,
121 METH_NOARGS,
122 NULL
123 },
124 {
125 "export",
126 (PyCFunction)Key_export,
127 METH_VARARGS,
128 NULL
129 },
130 { NULL },
131 };
132
133 static struct PyGetSetDef Key_getsetters[] = {
134 {
135 "fingerprint",
136 (getter)Key_get_fingerprint,
137 NULL,
138 NULL,
139 NULL
140 },
141 { NULL },
142 };
143
144 PyTypeObject KeyType = {
145 PyVarObject_HEAD_INIT(NULL, 0)
146 tp_name: "_pakfire.Key",
147 tp_basicsize: sizeof(KeyObject),
148 tp_flags: Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
149 tp_new: Key_new,
150 tp_dealloc: (destructor)Key_dealloc,
151 tp_init: (initproc)Key_init,
152 tp_doc: "Key object",
153 tp_methods: Key_methods,
154 tp_getset: Key_getsetters,
155 tp_repr: (reprfunc)Key_repr,
156 tp_str: (reprfunc)Key_str,
157 };