]> git.ipfire.org Git - pakfire.git/blame - src/_pakfire/key.c
python: Drop Pakfire reference from Key
[pakfire.git] / src / _pakfire / key.c
CommitLineData
0a89bb8a
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/key.h>
24#include <pakfire/util.h>
25
26#include "key.h"
ad62bb07 27#include "pakfire.h"
0a89bb8a 28
e511c41d 29PyObject* new_key(PyTypeObject* type, PakfireKey key) {
0a89bb8a
MT
30 KeyObject* self = (KeyObject *)type->tp_alloc(type, 0);
31 if (self) {
e511c41d 32 self->key = pakfire_key_ref(key);
0a89bb8a
MT
33 }
34
35 return (PyObject *)self;
36}
37
0a89bb8a 38static PyObject* Key_new(PyTypeObject* type, PyObject* args, PyObject* kwds) {
e511c41d
MT
39 KeyObject* self = (KeyObject *)type->tp_alloc(type, 0);
40 if (self) {
41 self->key = NULL;
42 }
43
44 return (PyObject *)self;
0a89bb8a
MT
45}
46
47static void Key_dealloc(KeyObject* self) {
e511c41d 48 pakfire_key_unref(self->key);
ad62bb07 49
0a89bb8a
MT
50 Py_TYPE(self)->tp_free((PyObject *)self);
51}
52
53static int Key_init(KeyObject* self, PyObject* args, PyObject* kwds) {
e511c41d 54 PakfireObject* pakfire;
0a89bb8a
MT
55 const char* fingerprint = NULL;
56
ad62bb07 57 if (!PyArg_ParseTuple(args, "O!s", &PakfireType, &pakfire, &fingerprint))
0a89bb8a
MT
58 return -1;
59
e511c41d 60 self->key = pakfire_key_get(pakfire->pakfire, fingerprint);
0a89bb8a
MT
61 if (!self->key)
62 return -1;
63
64 return 0;
65}
66
67static 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
73static PyObject* Key_str(KeyObject* self) {
526e4cec
MT
74 char* string = pakfire_key_dump(self->key);
75
76 PyObject* object = PyUnicode_FromString(string);
77 pakfire_free(string);
78
79 return object;
0a89bb8a
MT
80}
81
ed101258
MT
82static PyObject* Key_get_fingerprint(KeyObject* self) {
83 const char* fingerprint = pakfire_key_get_fingerprint(self->key);
84
85 return PyUnicode_FromString(fingerprint);
86}
87
0a89bb8a
MT
88static 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
b9b6a13e
MT
109static 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
0a89bb8a 117static struct PyMethodDef Key_methods[] = {
b9b6a13e
MT
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 },
0a89bb8a
MT
130 { NULL },
131};
132
ed101258
MT
133static struct PyGetSetDef Key_getsetters[] = {
134 {
135 "fingerprint",
136 (getter)Key_get_fingerprint,
137 NULL,
138 NULL,
139 NULL
140 },
141 { NULL },
142};
143
0a89bb8a
MT
144PyTypeObject 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,
ed101258 154 tp_getset: Key_getsetters,
0a89bb8a
MT
155 tp_repr: (reprfunc)Key_repr,
156 tp_str: (reprfunc)Key_str,
157};