python/rpmmi-py.c

Go to the documentation of this file.
00001 
00005 #include "system.h"
00006 
00007 #include <rpmlib.h>
00008 #include <rpmdb.h>
00009 
00010 #include "rpmmi-py.h"
00011 #include "header-py.h"
00012 
00013 #include "debug.h"
00014 
00068 
00071 static PyObject *
00072 rpmmi_iter(rpmmiObject * s)
00073         /*@*/
00074 {
00075     Py_INCREF(s);
00076     return (PyObject *)s;
00077 }
00078 
00081 /*@null@*/
00082 static PyObject *
00083 rpmmi_iternext(rpmmiObject * s)
00084         /*@globals rpmGlobalMacroContext @*/
00085         /*@modifies s, rpmGlobalMacroContext @*/
00086 {
00087     Header h;
00088 
00089     if (s->mi == NULL || (h = rpmdbNextIterator(s->mi)) == NULL) {
00090         s->mi = rpmdbFreeIterator(s->mi);
00091         return NULL;
00092     }
00093     return (PyObject *) hdr_Wrap(h);
00094 }
00095 
00098 /*@null@*/
00099 static PyObject *
00100 rpmmi_Next(rpmmiObject * s)
00101         /*@globals rpmGlobalMacroContext, _Py_NoneStruct @*/
00102         /*@modifies s, rpmGlobalMacroContext, _Py_NoneStruct @*/
00103 {
00104     PyObject * result;
00105 
00106     result = rpmmi_iternext(s);
00107 
00108     if (result == NULL) {
00109         Py_INCREF(Py_None);
00110         return Py_None;
00111     }
00112     return result;
00113 }
00114 
00117 /*@null@*/
00118 static PyObject *
00119 rpmmi_Instance(rpmmiObject * s)
00120         /*@*/
00121 {
00122     int rc = 0;
00123 
00124     if (s->mi != NULL)
00125         rc = rpmdbGetIteratorOffset(s->mi);
00126 
00127     return Py_BuildValue("i", rc);
00128 }
00129 
00132 /*@null@*/
00133 static PyObject *
00134 rpmmi_Count(rpmmiObject * s)
00135         /*@*/
00136 {
00137     int rc = 0;
00138 
00139     if (s->mi != NULL)
00140         rc = rpmdbGetIteratorCount(s->mi);
00141 
00142     return Py_BuildValue("i", rc);
00143 }
00144 
00147 /*@null@*/
00148 static PyObject *
00149 rpmmi_Pattern(rpmmiObject * s, PyObject * args, PyObject * kwds)
00150         /*@globals rpmGlobalMacroContext, _Py_NoneStruct @*/
00151         /*@modifies s, rpmGlobalMacroContext, _Py_NoneStruct @*/
00152 {
00153     PyObject *TagN = NULL;
00154     int type;
00155     char * pattern;
00156     rpmTag tag;
00157     char * kwlist[] = {"tag", "type", "patern", NULL};
00158 
00159     if (!PyArg_ParseTupleAndKeywords(args, kwds, "Ois:Pattern", kwlist,
00160             &TagN, &type, &pattern))
00161         return NULL;
00162 
00163     if ((tag = tagNumFromPyObject (TagN)) == -1) {
00164         PyErr_SetString(PyExc_TypeError, "unknown tag type");
00165         return NULL;
00166     }
00167 
00168     rpmdbSetIteratorRE(s->mi, tag, type, pattern);
00169 
00170     Py_INCREF (Py_None);
00171     return Py_None;
00172 
00173 }
00174 
00177 /*@-fullinitblock@*/
00178 /*@unchecked@*/ /*@observer@*/
00179 static struct PyMethodDef rpmmi_methods[] = {
00180     {"next",        (PyCFunction) rpmmi_Next,           METH_NOARGS,
00181 "mi.next() -> hdr\n\
00182 - Retrieve next header that matches. Iterate directly in python if possible.\n" },
00183     {"instance",    (PyCFunction) rpmmi_Instance,       METH_NOARGS,
00184         NULL },
00185     {"count",       (PyCFunction) rpmmi_Count,          METH_NOARGS,
00186         NULL },
00187     {"pattern",     (PyCFunction) rpmmi_Pattern,        METH_VARARGS|METH_KEYWORDS,
00188 "mi.pattern(TagN, mire_type, pattern)\n\
00189 - Set a secondary match pattern on tags from retrieved header.\n" },
00190     {NULL,              NULL}           /* sentinel */
00191 };
00192 /*@=fullinitblock@*/
00193 
00196 static void rpmmi_dealloc(/*@only@*/ /*@null@*/ rpmmiObject * s)
00197         /*@globals rpmGlobalMacroContext @*/
00198         /*@modifies s, rpmGlobalMacroContext @*/
00199 {
00200     if (s) {
00201         s->mi = rpmdbFreeIterator(s->mi);
00202         Py_DECREF(s->ref);
00203         PyObject_Del(s);
00204     }
00205 }
00206 
00207 static PyObject * rpmmi_getattro(PyObject * o, PyObject * n)
00208         /*@*/
00209 {
00210     return PyObject_GenericGetAttr(o, n);
00211 }
00212 
00213 static int rpmmi_setattro(PyObject * o, PyObject * n, PyObject * v)
00214         /*@*/
00215 {
00216     return PyObject_GenericSetAttr(o, n, v);
00217 }
00218 
00221 /*@unchecked@*/ /*@observer@*/
00222 static char rpmmi_doc[] =
00223 "";
00224 
00227 /*@-fullinitblock@*/
00228 PyTypeObject rpmmi_Type = {
00229         PyObject_HEAD_INIT(&PyType_Type)
00230         0,                              /* ob_size */
00231         "rpm.mi",                       /* tp_name */
00232         sizeof(rpmmiObject),            /* tp_size */
00233         0,                              /* tp_itemsize */
00234         (destructor) rpmmi_dealloc,     /* tp_dealloc */
00235         0,                              /* tp_print */
00236         (getattrfunc)0,                 /* tp_getattr */
00237         0,                              /* tp_setattr */
00238         0,                              /* tp_compare */
00239         0,                              /* tp_repr */
00240         0,                              /* tp_as_number */
00241         0,                              /* tp_as_sequence */
00242         0,                              /* tp_as_mapping */
00243         0,                              /* tp_hash */
00244         0,                              /* tp_call */
00245         0,                              /* tp_str */
00246         (getattrofunc) rpmmi_getattro,  /* tp_getattro */
00247         (setattrofunc) rpmmi_setattro,  /* tp_setattro */
00248         0,                              /* tp_as_buffer */
00249         Py_TPFLAGS_DEFAULT,             /* tp_flags */
00250         rpmmi_doc,                      /* tp_doc */
00251 #if Py_TPFLAGS_HAVE_ITER
00252         0,                              /* tp_traverse */
00253         0,                              /* tp_clear */
00254         0,                              /* tp_richcompare */
00255         0,                              /* tp_weaklistoffset */
00256         (getiterfunc) rpmmi_iter,       /* tp_iter */
00257         (iternextfunc) rpmmi_iternext,  /* tp_iternext */
00258         rpmmi_methods,                  /* tp_methods */
00259         0,                              /* tp_members */
00260         0,                              /* tp_getset */
00261         0,                              /* tp_base */
00262         0,                              /* tp_dict */
00263         0,                              /* tp_descr_get */
00264         0,                              /* tp_descr_set */
00265         0,                              /* tp_dictoffset */
00266         0,                              /* tp_init */
00267         0,                              /* tp_alloc */
00268         0,                              /* tp_new */
00269         0,                              /* tp_free */
00270         0,                              /* tp_is_gc */
00271 #endif
00272 };
00273 /*@=fullinitblock@*/
00274 
00275 rpmmiObject * rpmmi_Wrap(rpmdbMatchIterator mi, PyObject *s)
00276 {
00277     rpmmiObject * mio = (rpmmiObject *) PyObject_New(rpmmiObject, &rpmmi_Type);
00278 
00279     if (mio == NULL) {
00280         PyErr_SetString(pyrpmError, "out of memory creating rpmmiObject");
00281         return NULL;
00282     }
00283     mio->mi = mi;
00284     mio->ref = s;
00285     Py_INCREF(mio->ref);
00286     return mio;
00287 }
00288 

Generated on Fri Oct 12 08:44:54 2007 for rpm by  doxygen 1.5.2