functions: revert the function init order to make pylint happy again. See #217
[pygobject.git] / gi / pygoptiongroup.c
blobf144d818c85d8b291b2da73fb0381aac01019e51
1 /* -*- Mode: C; c-basic-offset: 4 -*-
2 * pygobject - Python bindings for the GLib, GObject and GIO
3 * Copyright (C) 2006 Johannes Hoelzl
5 * pygoptiongroup.c: GOptionContext and GOptionGroup wrapper
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
21 #include <config.h>
23 #include "pygoptiongroup.h"
24 #include "pygi-error.h"
25 #include "pygi-util.h"
27 PYGLIB_DEFINE_TYPE("gi._gi.OptionGroup", PyGOptionGroup_Type, PyGOptionGroup)
29 /**
30 * pyg_option_group_new:
31 * @group: a GOptionGroup
33 * The returned GOptionGroup can't be used to set any hooks, translation domains
34 * or add entries. It's only intend is, to use for GOptionContext.add_group().
36 * Returns: the GOptionGroup wrapper.
38 PyObject *
39 pyg_option_group_new (GOptionGroup *group)
41 PyGOptionGroup *self;
43 self = (PyGOptionGroup *)PyObject_NEW(PyGOptionGroup,
44 &PyGOptionGroup_Type);
45 if (self == NULL)
46 return NULL;
48 self->group = group;
49 self->other_owner = TRUE;
50 self->is_in_context = FALSE;
52 return (PyObject *)self;
55 static gboolean
56 check_if_owned(PyGOptionGroup *self)
58 if (self->other_owner)
60 PyErr_SetString(PyExc_ValueError, "The GOptionGroup was not created by "
61 "gi._gi.OptionGroup(), so operation is not possible.");
62 return TRUE;
64 return FALSE;
67 static void
68 destroy_g_group(PyGOptionGroup *self)
70 PyGILState_STATE state;
71 state = PyGILState_Ensure();
73 self->group = NULL;
74 Py_CLEAR(self->callback);
75 g_slist_foreach(self->strings, (GFunc) g_free, NULL);
76 g_slist_free(self->strings);
77 self->strings = NULL;
79 if (self->is_in_context)
81 Py_DECREF(self);
84 PyGILState_Release(state);
87 static int
88 pyg_option_group_init(PyGOptionGroup *self, PyObject *args, PyObject *kwargs)
90 static char *kwlist[] = { "name", "description", "help_description",
91 "callback", NULL };
92 char *name, *description, *help_description;
93 PyObject *callback;
95 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "zzzO:GOptionGroup.__init__",
96 kwlist, &name, &description,
97 &help_description, &callback))
98 return -1;
100 self->group = g_option_group_new(name, description, help_description,
101 self, (GDestroyNotify) destroy_g_group);
102 self->other_owner = FALSE;
103 self->is_in_context = FALSE;
105 Py_INCREF(callback);
106 self->callback = callback;
108 return 0;
111 static void
112 pyg_option_group_dealloc(PyGOptionGroup *self)
114 if (!self->other_owner && !self->is_in_context)
116 GOptionGroup *tmp = self->group;
117 self->group = NULL;
118 if (tmp) {
119 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
120 g_option_group_free(tmp);
121 G_GNUC_END_IGNORE_DEPRECATIONS
125 PyObject_Del(self);
128 static gboolean
129 arg_func(const gchar *option_name,
130 const gchar *value,
131 PyGOptionGroup *self,
132 GError **error)
134 PyObject *ret;
135 PyGILState_STATE state;
136 gboolean no_error;
138 state = PyGILState_Ensure();
139 if (value == NULL)
140 ret = PyObject_CallFunction(self->callback, "sOO",
141 option_name, Py_None, self);
142 else
143 ret = PyObject_CallFunction(self->callback, "ssO",
144 option_name, value, self);
146 if (ret != NULL)
148 Py_DECREF(ret);
149 no_error = TRUE;
150 } else
151 no_error = pygi_gerror_exception_check(error) != -1;
153 PyGILState_Release(state);
154 return no_error;
157 static PyObject *
158 pyg_option_group_add_entries(PyGOptionGroup *self, PyObject *args,
159 PyObject *kwargs)
161 static char *kwlist[] = { "entries", NULL };
162 gssize entry_count, pos;
163 PyObject *entry_tuple, *list;
164 GOptionEntry *entries;
166 if (check_if_owned(self))
167 return NULL;
169 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O:GOptionGroup.add_entries",
170 kwlist, &list))
171 return NULL;
173 if (!PyList_Check(list))
175 PyErr_SetString(PyExc_TypeError,
176 "GOptionGroup.add_entries expected a list of entries");
177 return NULL;
180 entry_count = PyList_Size(list);
181 if (entry_count == -1)
183 PyErr_SetString(PyExc_TypeError,
184 "GOptionGroup.add_entries expected a list of entries");
185 return NULL;
188 entries = g_new0(GOptionEntry, entry_count + 1);
189 for (pos = 0; pos < entry_count; pos++)
191 gchar *long_name, *description, *arg_description;
192 entry_tuple = PyList_GetItem(list, pos);
193 if (!PyTuple_Check(entry_tuple))
195 PyErr_SetString(PyExc_TypeError, "GOptionGroup.add_entries "
196 "expected a list of entries");
197 g_free(entries);
198 return NULL;
200 if (!PyArg_ParseTuple(entry_tuple, "scisz",
201 &long_name,
202 &(entries[pos].short_name),
203 &(entries[pos].flags),
204 &description,
205 &arg_description))
207 PyErr_SetString(PyExc_TypeError, "GOptionGroup.add_entries "
208 "expected a list of entries");
209 g_free(entries);
210 return NULL;
212 long_name = g_strdup(long_name);
213 self->strings = g_slist_prepend(self->strings, long_name);
214 entries[pos].long_name = long_name;
216 description = g_strdup(description);
217 self->strings = g_slist_prepend(self->strings, description);
218 entries[pos].description = description;
220 arg_description = g_strdup(arg_description);
221 self->strings = g_slist_prepend(self->strings, arg_description);
222 entries[pos].arg_description = arg_description;
224 entries[pos].arg = G_OPTION_ARG_CALLBACK;
225 entries[pos].arg_data = arg_func;
228 g_option_group_add_entries(self->group, entries);
230 g_free(entries);
232 Py_INCREF(Py_None);
233 return Py_None;
237 static PyObject *
238 pyg_option_group_set_translation_domain(PyGOptionGroup *self,
239 PyObject *args,
240 PyObject *kwargs)
242 static char *kwlist[] = { "domain", NULL };
243 char *domain;
245 if (check_if_owned(self))
246 return NULL;
248 if (self->group == NULL)
250 PyErr_SetString(PyExc_RuntimeError,
251 "The corresponding GOptionGroup was already freed, "
252 "probably through the release of GOptionContext");
253 return NULL;
256 if (!PyArg_ParseTupleAndKeywords(args, kwargs,
257 "z:GOptionGroup.set_translate_domain",
258 kwlist, &domain))
259 return NULL;
261 g_option_group_set_translation_domain(self->group, domain);
263 Py_INCREF(Py_None);
264 return Py_None;
267 static PyObject*
268 pyg_option_group_richcompare(PyObject *self, PyObject *other, int op)
270 if (Py_TYPE(self) == Py_TYPE(other) &&
271 Py_TYPE(self) == &PyGOptionGroup_Type) {
272 return pyg_ptr_richcompare(((PyGOptionGroup*)self)->group,
273 ((PyGOptionGroup*)other)->group,
274 op);
275 } else {
276 Py_INCREF(Py_NotImplemented);
277 return Py_NotImplemented;
281 static PyMethodDef pyg_option_group_methods[] = {
282 { "add_entries", (PyCFunction)pyg_option_group_add_entries, METH_VARARGS | METH_KEYWORDS },
283 { "set_translation_domain", (PyCFunction)pyg_option_group_set_translation_domain, METH_VARARGS | METH_KEYWORDS },
284 { NULL, NULL, 0 },
288 * Returns 0 on success, or -1 and sets an exception.
291 pygi_option_group_register_types(PyObject *d)
293 PyGOptionGroup_Type.tp_dealloc = (destructor)pyg_option_group_dealloc;
294 PyGOptionGroup_Type.tp_richcompare = pyg_option_group_richcompare;
295 PyGOptionGroup_Type.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE;
296 PyGOptionGroup_Type.tp_methods = pyg_option_group_methods;
297 PyGOptionGroup_Type.tp_init = (initproc)pyg_option_group_init;
298 PYGLIB_REGISTER_TYPE(d, PyGOptionGroup_Type, "OptionGroup");
300 return 0;