summaryrefslogtreecommitdiff
path: root/python/rpmtd-py.c
blob: 247c7502ae08107a7dc0cfa44635264b45c12554 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/** \ingroup py_c
 * \file python/rpmtd-py.c
 */

#include "rpmsystem-py.h"
#include <rpm/rpmtd.h>
#include <rpm/header.h>
#include "rpmtd-py.h"
#include "header-py.h"

/*
 * Convert single tag data item to python object of suitable type
 */
PyObject * rpmtd_ItemAsPyobj(rpmtd td, rpmTagClass tclass)
{
    PyObject *res = NULL;

    switch (tclass) {
    case RPM_STRING_CLASS:
	res = PyBytes_FromString(rpmtdGetString(td));
	break;
    case RPM_NUMERIC_CLASS:
	res = PyLong_FromLongLong(rpmtdGetNumber(td));
	break;
    case RPM_BINARY_CLASS:
	res = PyBytes_FromStringAndSize(td->data, td->count);
	break;
    default:
	PyErr_SetString(PyExc_KeyError, "unknown data type");
	break;
    }
    return res;
}

PyObject *rpmtd_AsPyobj(rpmtd td)
{
    PyObject *res = NULL;
    int array = (rpmTagGetReturnType(td->tag) == RPM_ARRAY_RETURN_TYPE);
    rpmTagClass tclass = rpmtdClass(td);

    if (!array && rpmtdCount(td) < 1) {
	Py_RETURN_NONE;
    }
    
    if (array) {
	int ix;
	res = PyList_New(rpmtdCount(td));
        if (!res) {
            return NULL;
        }
	while ((ix = rpmtdNext(td)) >= 0) {
	    PyObject *item = rpmtd_ItemAsPyobj(td, tclass);
            if (!item) {
                Py_DECREF(res);
                return NULL;
            }
	    PyList_SET_ITEM(res, ix, item);
	}
    } else {
	res = rpmtd_ItemAsPyobj(td, tclass);
    }
    return res;
}