csomh / source-git / rpm

Forked from source-git/rpm 4 years ago
Clone
2ff057
/** \ingroup py_c
2ff057
 * \file python/rpmtd-py.c
2ff057
 */
2ff057
2ff057
#include "rpmsystem-py.h"
2ff057
#include <rpm/rpmtd.h>
2ff057
#include <rpm/header.h>
2ff057
#include "rpmtd-py.h"
2ff057
#include "header-py.h"
2ff057
2ff057
/*
2ff057
 * Convert single tag data item to python object of suitable type
2ff057
 */
2ff057
PyObject * rpmtd_ItemAsPyobj(rpmtd td, rpmTagClass tclass)
2ff057
{
2ff057
    PyObject *res = NULL;
2ff057
2ff057
    switch (tclass) {
2ff057
    case RPM_STRING_CLASS:
2ff057
	res = PyBytes_FromString(rpmtdGetString(td));
2ff057
	break;
2ff057
    case RPM_NUMERIC_CLASS:
2ff057
	res = PyLong_FromLongLong(rpmtdGetNumber(td));
2ff057
	break;
2ff057
    case RPM_BINARY_CLASS:
2ff057
	res = PyBytes_FromStringAndSize(td->data, td->count);
2ff057
	break;
2ff057
    default:
2ff057
	PyErr_SetString(PyExc_KeyError, "unknown data type");
2ff057
	break;
2ff057
    }
2ff057
    return res;
2ff057
}
2ff057
2ff057
PyObject *rpmtd_AsPyobj(rpmtd td)
2ff057
{
2ff057
    PyObject *res = NULL;
2ff057
    int array = (rpmTagGetReturnType(td->tag) == RPM_ARRAY_RETURN_TYPE);
2ff057
    rpmTagClass tclass = rpmtdClass(td);
2ff057
2ff057
    if (!array && rpmtdCount(td) < 1) {
2ff057
	Py_RETURN_NONE;
2ff057
    }
2ff057
    
2ff057
    if (array) {
2ff057
	int ix;
2ff057
	res = PyList_New(rpmtdCount(td));
2ff057
        if (!res) {
2ff057
            return NULL;
2ff057
        }
2ff057
	while ((ix = rpmtdNext(td)) >= 0) {
2ff057
	    PyObject *item = rpmtd_ItemAsPyobj(td, tclass);
2ff057
            if (!item) {
2ff057
                Py_DECREF(res);
2ff057
                return NULL;
2ff057
            }
2ff057
	    PyList_SET_ITEM(res, ix, item);
2ff057
	}
2ff057
    } else {
2ff057
	res = rpmtd_ItemAsPyobj(td, tclass);
2ff057
    }
2ff057
    return res;
2ff057
}