1 /*#############################################################################
3 # Pakfire - The IPFire package management system #
4 # Copyright (C) 2017 Pakfire development team #
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. #
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. #
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/>. #
19 #############################################################################*/
23 #include <pakfire/key.h>
24 #include <pakfire/util.h>
29 PyObject
* new_key(PyTypeObject
* type
, PakfireKey key
) {
30 KeyObject
* self
= (KeyObject
*)type
->tp_alloc(type
, 0);
32 self
->key
= pakfire_key_ref(key
);
35 return (PyObject
*)self
;
38 static PyObject
* Key_new(PyTypeObject
* type
, PyObject
* args
, PyObject
* kwds
) {
39 KeyObject
* self
= (KeyObject
*)type
->tp_alloc(type
, 0);
44 return (PyObject
*)self
;
47 static void Key_dealloc(KeyObject
* self
) {
48 pakfire_key_unref(self
->key
);
50 Py_TYPE(self
)->tp_free((PyObject
*)self
);
53 static int Key_init(KeyObject
* self
, PyObject
* args
, PyObject
* kwds
) {
54 PakfireObject
* pakfire
;
55 const char* fingerprint
= NULL
;
57 if (!PyArg_ParseTuple(args
, "O!s", &PakfireType
, &pakfire
, &fingerprint
))
60 self
->key
= pakfire_key_get(pakfire
->pakfire
, fingerprint
);
67 static PyObject
* Key_repr(KeyObject
* self
) {
68 const char* fingerprint
= pakfire_key_get_fingerprint(self
->key
);
70 return PyUnicode_FromFormat("<_pakfire.Key (%s)>", fingerprint
);
73 static PyObject
* Key_str(KeyObject
* self
) {
74 char* string
= pakfire_key_dump(self
->key
);
76 PyObject
* object
= PyUnicode_FromString(string
);
82 static PyObject
* Key_get_fingerprint(KeyObject
* self
) {
83 const char* fingerprint
= pakfire_key_get_fingerprint(self
->key
);
85 return PyUnicode_FromString(fingerprint
);
88 static PyObject
* Key_export(KeyObject
* self
, PyObject
* args
) {
91 if (!PyArg_ParseTuple(args
, "|p", &secret
))
94 pakfire_key_export_mode_t mode
;
96 mode
= PAKFIRE_KEY_EXPORT_MODE_SECRET
;
98 mode
= PAKFIRE_KEY_EXPORT_MODE_PUBLIC
;
101 char* export
= pakfire_key_export(self
->key
, mode
);
103 PyObject
* object
= PyUnicode_FromFormat("%s", export
);
104 pakfire_free(export
);
109 static PyObject
* Key_delete(KeyObject
* self
) {
110 int r
= pakfire_key_delete(self
->key
);
117 static struct PyMethodDef Key_methods
[] = {
120 (PyCFunction
)Key_delete
,
126 (PyCFunction
)Key_export
,
133 static struct PyGetSetDef Key_getsetters
[] = {
136 (getter
)Key_get_fingerprint
,
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
,
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
,