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
00082 static PyObject *
00083 rpmmi_iternext(rpmmiObject * s)
00084
00085
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
00099 static PyObject *
00100 rpmmi_Next(rpmmiObject * s)
00101
00102
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
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
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
00148 static PyObject *
00149 rpmmi_Pattern(rpmmiObject * s, PyObject * args, PyObject * kwds)
00150
00151
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
00178
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}
00191 };
00192
00193
00196 static void rpmmi_dealloc( rpmmiObject * s)
00197
00198
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
00222 static char rpmmi_doc[] =
00223 "";
00224
00227
00228 PyTypeObject rpmmi_Type = {
00229 PyObject_HEAD_INIT(&PyType_Type)
00230 0,
00231 "rpm.mi",
00232 sizeof(rpmmiObject),
00233 0,
00234 (destructor) rpmmi_dealloc,
00235 0,
00236 (getattrfunc)0,
00237 0,
00238 0,
00239 0,
00240 0,
00241 0,
00242 0,
00243 0,
00244 0,
00245 0,
00246 (getattrofunc) rpmmi_getattro,
00247 (setattrofunc) rpmmi_setattro,
00248 0,
00249 Py_TPFLAGS_DEFAULT,
00250 rpmmi_doc,
00251 #if Py_TPFLAGS_HAVE_ITER
00252 0,
00253 0,
00254 0,
00255 0,
00256 (getiterfunc) rpmmi_iter,
00257 (iternextfunc) rpmmi_iternext,
00258 rpmmi_methods,
00259 0,
00260 0,
00261 0,
00262 0,
00263 0,
00264 0,
00265 0,
00266 0,
00267 0,
00268 0,
00269 0,
00270 0,
00271 #endif
00272 };
00273
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