Rename rpmpsProblem() -> rpmpsGetProblem() to make purpose clearer
[platform/upstream/rpm.git] / python / rpmps-py.c
1 /** \ingroup py_c
2  * \file python/rpmps-py.c
3  */
4
5 #include "system.h"
6
7 #include <rpmlib.h>
8
9 #include "rpmdebug-py.c"
10
11 #include "rpmps-py.h"
12
13 #include "debug.h"
14
15
16 static PyObject *
17 rpmps_Debug(rpmpsObject * s, PyObject * args, PyObject * kwds)
18 {
19     char * kwlist[] = {"debugLevel", NULL};
20     
21     if (!PyArg_ParseTupleAndKeywords(args, kwds, "i", kwlist, &_rpmps_debug))
22         return NULL;
23
24     Py_INCREF(Py_None);
25     return Py_None;
26 }
27
28 static int
29 rpmps_append(rpmpsObject * s, PyObject * value)
30 {
31     char *pkgNEVR, *altNEVR, *str1;
32     unsigned long ulong1;
33     int ignoreProblem;
34     rpmProblemType type;
35     fnpyKey key;
36
37     if (!PyArg_ParseTuple(value, "ssOiisN:rpmps value tuple",
38                         &pkgNEVR, &altNEVR, &key,
39                         &type, &ignoreProblem, &str1,
40                         &ulong1))
41     {
42         return -1;
43     }
44     rpmpsAppend(s->ps, type, pkgNEVR, key, str1, NULL, altNEVR, ulong1);
45     return 0;
46 }
47
48 static PyObject *
49 rpmps_iter(rpmpsObject * s)
50 {
51 if (_rpmps_debug < 0)
52 fprintf(stderr, "*** rpmps_iter(%p)\n", s);
53     s->psi = rpmpsInitIterator(s->ps);
54     Py_INCREF(s);
55     return (PyObject *)s;
56 }
57
58 static PyObject *
59 rpmps_iternext(rpmpsObject * s)
60 {
61     PyObject * result = NULL;
62
63 if (_rpmps_debug < 0)
64 fprintf(stderr, "*** rpmps_iternext(%p) ps %p psi %p\n", s, s->ps, s->psi);
65
66     /* Reset loop indices on 1st entry. */
67     if (s->psi == NULL) {
68         s->psi = rpmpsInitIterator(s->ps);
69     }
70
71     /* If more to do, return a problem set string. */
72     if (rpmpsNextIterator(s->psi) >= 0) {
73         result = Py_BuildValue("s", rpmProblemString(rpmpsGetProblem(s->psi)));
74     } else {
75         s->psi = rpmpsFreeIterator(s->psi);
76     }
77
78     return result;
79 }
80
81 static struct PyMethodDef rpmps_methods[] = {
82  {"Debug",      (PyCFunction)rpmps_Debug,       METH_VARARGS|METH_KEYWORDS,
83         NULL},
84   {"append",    (PyCFunction)rpmps_append,      METH_VARARGS, NULL},
85  {NULL,         NULL}           /* sentinel */
86 };
87
88 /* ---------- */
89
90 static void
91 rpmps_dealloc(rpmpsObject * s)
92 {
93 if (_rpmps_debug < 0)
94 fprintf(stderr, "*** rpmps_dealloc(%p)\n", s);
95     if (s) {
96         s->ps = rpmpsFree(s->ps);
97         PyObject_Del(s);
98     }
99 }
100
101 static int
102 rpmps_print(rpmpsObject * s, FILE * fp, int flags)
103 {
104 if (_rpmps_debug < 0)
105 fprintf(stderr, "*** rpmps_print(%p,%p,%x)\n", s, (void *)fp, flags);
106     if (s && s->ps)
107         rpmpsPrint(fp, s->ps);
108     return 0;
109 }
110
111 static PyObject * rpmps_getattro(PyObject * o, PyObject * n)
112 {
113 if (_rpmps_debug < 0)
114 fprintf(stderr, "*** rpmps_getattro(%p,%p)\n", o, n);
115     return PyObject_GenericGetAttr(o, n);
116 }
117
118 static int rpmps_setattro(PyObject * o, PyObject * n, PyObject * v)
119 {
120 if (_rpmps_debug < 0)
121 fprintf(stderr, "*** rpmps_setattro(%p,%p,%p)\n", o, n, v);
122     return PyObject_GenericSetAttr(o, n, v);
123 }
124
125 static int
126 rpmps_length(rpmpsObject * s)
127 {
128     int rc;
129     rc = rpmpsNumProblems(s->ps);
130 if (_rpmps_debug < 0)
131 fprintf(stderr, "*** rpmps_length(%p) rc %d\n", s, rc);
132     return rc;
133 }
134
135 static PyObject *
136 rpmps_subscript(rpmpsObject * s, PyObject * key)
137 {
138     PyObject * result = NULL;
139     rpmpsi psi;
140     int ix, i;
141
142     if (!PyInt_Check(key)) {
143 if (_rpmps_debug < 0)
144 fprintf(stderr, "*** rpmps_subscript(%p[%s],%p[%s])\n", s, lbl(s), key, lbl(key));
145         PyErr_SetString(PyExc_TypeError, "integer expected");
146         return NULL;
147     }
148
149     ix = (int) PyInt_AsLong(key);
150     /* XXX range check */
151
152     psi = rpmpsInitIterator(s->ps);
153     while ((i = rpmpsNextIterator(psi)) >= 0) {
154         if (i == ix) {
155             result = Py_BuildValue("s", rpmProblemString(rpmpsGetProblem(psi)));
156             break;
157         }
158     }
159     psi = rpmpsFreeIterator(psi);
160
161 if (_rpmps_debug < 0)
162 fprintf(stderr, "*** rpmps_subscript(%p,%p) %s\n", s, key, PyString_AsString(result));
163
164     return result;
165 }
166
167 static PyMappingMethods rpmps_as_mapping = {
168         (lenfunc) rpmps_length,         /* mp_length */
169         (binaryfunc) rpmps_subscript,   /* mp_subscript */
170 };
171
172 /** \ingroup py_c
173  */
174 static int rpmps_init(rpmpsObject * s, PyObject *args, PyObject *kwds)
175 {
176     char * kwlist[] = {NULL};
177
178 if (_rpmps_debug < 0)
179 fprintf(stderr, "*** rpmps_init(%p,%p,%p)\n", s, args, kwds);
180
181     if (!PyArg_ParseTupleAndKeywords(args, kwds, ":rpmps_init", kwlist))
182         return -1;
183
184     s->ps = rpmpsCreate();
185     s->psi = NULL;
186
187     return 0;
188 }
189
190 /** \ingroup py_c
191  */
192 static void rpmps_free(rpmpsObject * s)
193 {
194 if (_rpmps_debug)
195 fprintf(stderr, "%p -- ps %p\n", s, s->ps);
196     s->ps = rpmpsFree(s->ps);
197
198     PyObject_Del((PyObject *)s);
199 }
200
201 /** \ingroup py_c
202  */
203 static PyObject * rpmps_alloc(PyTypeObject * subtype, int nitems)
204 {
205     PyObject * s = PyType_GenericAlloc(subtype, nitems);
206
207 if (_rpmps_debug < 0)
208 fprintf(stderr, "*** rpmps_alloc(%p,%d) ret %p\n", subtype, nitems, s);
209     return s;
210 }
211
212 /** \ingroup py_c
213  */
214 static PyObject * rpmps_new(PyTypeObject * subtype, PyObject *args, PyObject *kwds)
215 {
216     rpmpsObject * s = (void *) PyObject_New(rpmpsObject, subtype);
217
218     /* Perform additional initialization. */
219     if (rpmps_init(s, args, kwds) < 0) {
220         rpmps_free(s);
221         return NULL;
222     }
223
224 if (_rpmps_debug)
225 fprintf(stderr, "%p ++ ps %p\n", s, s->ps);
226
227     return (PyObject *)s;
228 }
229
230 /**
231  */
232 static char rpmps_doc[] =
233 "";
234
235 PyTypeObject rpmps_Type = {
236         PyObject_HEAD_INIT(&PyType_Type)
237         0,                              /* ob_size */
238         "rpm.ps",                       /* tp_name */
239         sizeof(rpmpsObject),            /* tp_basicsize */
240         0,                              /* tp_itemsize */
241         /* methods */
242         (destructor) rpmps_dealloc,     /* tp_dealloc */
243         (printfunc) rpmps_print,        /* tp_print */
244         (getattrfunc)0,                 /* tp_getattr */
245         (setattrfunc)0,                 /* tp_setattr */
246         (cmpfunc)0,                     /* tp_compare */
247         (reprfunc)0,                    /* tp_repr */
248         0,                              /* tp_as_number */
249         0,                              /* tp_as_sequence */
250         &rpmps_as_mapping,              /* tp_as_mapping */
251         (hashfunc)0,                    /* tp_hash */
252         (ternaryfunc)0,                 /* tp_call */
253         (reprfunc)0,                    /* tp_str */
254         (getattrofunc) rpmps_getattro,  /* tp_getattro */
255         (setattrofunc) rpmps_setattro,  /* tp_setattro */
256         0,                              /* tp_as_buffer */
257         Py_TPFLAGS_DEFAULT,             /* tp_flags */
258         rpmps_doc,                      /* tp_doc */
259 #if Py_TPFLAGS_HAVE_ITER
260         0,                              /* tp_traverse */
261         0,                              /* tp_clear */
262         (richcmpfunc)0,                 /* tp_richcompare */
263         0,                              /* tp_weaklistoffset */
264         (getiterfunc) rpmps_iter,       /* tp_iter */
265         (iternextfunc) rpmps_iternext,  /* tp_iternext */
266         rpmps_methods,                  /* tp_methods */
267         0,                              /* tp_members */
268         0,                              /* tp_getset */
269         0,                              /* tp_base */
270         0,                              /* tp_dict */
271         0,                              /* tp_descr_get */
272         0,                              /* tp_descr_set */
273         0,                              /* tp_dictoffset */
274         (initproc) rpmps_init,          /* tp_init */
275         (allocfunc) rpmps_alloc,        /* tp_alloc */
276         (newfunc) rpmps_new,            /* tp_new */
277         (freefunc) rpmps_free,          /* tp_free */
278         0,                              /* tp_is_gc */
279 #endif
280 };
281
282 /* ---------- */
283
284 rpmps psFromPs(rpmpsObject * s)
285 {
286     return s->ps;
287 }
288
289 rpmpsObject *
290 rpmps_Wrap(rpmps ps)
291 {
292     rpmpsObject * s = PyObject_New(rpmpsObject, &rpmps_Type);
293
294     if (s == NULL)
295         return NULL;
296     s->ps = ps;
297     s->psi = NULL;
298     return s;
299 }