functions: revert the function init order to make pylint happy again. See #217
[pygobject.git] / gi / pygoptioncontext.c
blob61ecb763bb6e78760c38001b45360b6912d16476
1 /* -*- Mode: C; c-basic-offset: 4 -*-
2 * pygtk- Python bindings for the GTK toolkit.
3 * Copyright (C) 2006 Johannes Hoelzl
5 * pygoptioncontext.c: GOptionContext 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 "pygoptioncontext.h"
24 #include "pygi-error.h"
25 #include "pygi-util.h"
26 #include "pygi-basictype.h"
28 PYGLIB_DEFINE_TYPE("gi._gi.OptionContext", PyGOptionContext_Type, PyGOptionContext)
30 /**
31 * pyg_option_group_transfer_group:
32 * @group: a GOptionGroup wrapper
34 * This is used to transfer the GOptionGroup to a GOptionContext. After this
35 * is called, the calle must handle the release of the GOptionGroup.
37 * When #NULL is returned, the GOptionGroup was already transfered.
39 * Returns: Either #NULL or the wrapped GOptionGroup.
41 static GOptionGroup *
42 pyglib_option_group_transfer_group(PyObject *obj)
44 PyGOptionGroup *self = (PyGOptionGroup*)obj;
46 if (self->is_in_context)
47 return NULL;
49 self->is_in_context = TRUE;
51 /* Here we increase the reference count of the PyGOptionGroup, because now
52 * the GOptionContext holds an reference to us (it is the userdata passed
53 * to g_option_group_new().
55 * The GOptionGroup is freed with the GOptionContext.
57 * We set it here because if we would do this in the init method we would
58 * hold two references and the PyGOptionGroup would never be freed.
60 Py_INCREF(self);
62 return self->group;
65 /**
66 * pyg_option_context_new:
67 * @context: a GOptionContext
69 * Returns: A new GOptionContext wrapper.
71 PyObject *
72 pyg_option_context_new (GOptionContext *context)
74 PyGOptionContext *self;
76 self = (PyGOptionContext *)PyObject_NEW(PyGOptionContext, &PyGOptionContext_Type);
77 if (self == NULL)
78 return NULL;
80 self->context = context;
81 self->main_group = NULL;
83 return (PyObject *)self;
86 static int
87 pyg_option_context_init(PyGOptionContext *self,
88 PyObject *args,
89 PyObject *kwargs)
91 char *parameter_string;
93 if (!PyArg_ParseTuple(args, "s:gi._gi.GOptionContext.__init__",
94 &parameter_string))
95 return -1;
97 self->context = g_option_context_new(parameter_string);
98 return 0;
101 static void
102 pyg_option_context_dealloc(PyGOptionContext *self)
104 Py_CLEAR(self->main_group);
106 if (self->context != NULL)
108 GOptionContext *tmp = self->context;
109 self->context = NULL;
110 g_option_context_free(tmp);
113 PyObject_Del(self);
116 static PyObject *
117 pyg_option_context_parse(PyGOptionContext *self,
118 PyObject *args,
119 PyObject *kwargs)
121 static char *kwlist[] = { "argv", NULL };
122 PyObject *arg;
123 PyObject *new_argv, *argv;
124 Py_ssize_t argv_length, pos;
125 gint argv_length_int;
126 char **argv_content, **original;
127 GError *error = NULL;
128 gboolean result;
130 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O:GOptionContext.parse",
131 kwlist, &argv))
132 return NULL;
134 if (!PyList_Check(argv))
136 PyErr_SetString(PyExc_TypeError,
137 "GOptionContext.parse expects a list of strings.");
138 return NULL;
141 argv_length = PyList_Size(argv);
142 if (argv_length == -1)
144 PyErr_SetString(PyExc_TypeError,
145 "GOptionContext.parse expects a list of strings.");
146 return NULL;
149 argv_content = g_new(char*, argv_length + 1);
150 argv_content[argv_length] = NULL;
151 for (pos = 0; pos < argv_length; pos++)
153 arg = PyList_GetItem(argv, pos);
154 argv_content[pos] = g_strdup(PYGLIB_PyUnicode_AsString(arg));
155 if (argv_content[pos] == NULL)
157 g_strfreev(argv_content);
158 return NULL;
161 original = g_strdupv(argv_content);
163 g_assert(argv_length <= G_MAXINT);
164 argv_length_int = (gint)argv_length;
165 Py_BEGIN_ALLOW_THREADS;
166 result = g_option_context_parse(self->context, &argv_length_int, &argv_content,
167 &error);
168 Py_END_ALLOW_THREADS;
169 argv_length = argv_length_int;
171 if (!result)
173 g_strfreev(argv_content);
174 g_strfreev(original);
175 pygi_error_check(&error);
176 return NULL;
179 new_argv = PyList_New(g_strv_length(argv_content));
180 for (pos = 0; pos < argv_length; pos++)
182 arg = PYGLIB_PyUnicode_FromString(argv_content[pos]);
183 PyList_SetItem(new_argv, pos, arg);
186 g_strfreev(original);
187 g_strfreev(argv_content);
188 return new_argv;
191 static PyObject *
192 pyg_option_context_set_help_enabled(PyGOptionContext *self,
193 PyObject *args,
194 PyObject *kwargs)
196 static char *kwlist[] = { "help_enable", NULL };
198 PyObject *help_enabled;
199 if (! PyArg_ParseTupleAndKeywords(args, kwargs,
200 "O:GOptionContext.set_help_enabled",
201 kwlist, &help_enabled))
202 return NULL;
204 g_option_context_set_help_enabled(self->context, PyObject_IsTrue(help_enabled));
206 Py_INCREF(Py_None);
207 return Py_None;
210 static PyObject *
211 pyg_option_context_get_help_enabled(PyGOptionContext *self)
213 return pygi_gboolean_to_py (g_option_context_get_help_enabled(self->context));
216 static PyObject *
217 pyg_option_context_set_ignore_unknown_options(PyGOptionContext *self,
218 PyObject *args,
219 PyObject *kwargs)
221 static char *kwlist[] = { "ignore_unknown_options", NULL };
222 PyObject *ignore_unknown_options;
224 if (! PyArg_ParseTupleAndKeywords(args, kwargs,
225 "O:GOptionContext.set_ignore_unknown_options",
226 kwlist, &ignore_unknown_options))
227 return NULL;
229 g_option_context_set_ignore_unknown_options(self->context,
230 PyObject_IsTrue(ignore_unknown_options));
233 Py_INCREF(Py_None);
234 return Py_None;
237 static PyObject *
238 pyg_option_context_get_ignore_unknown_options(PyGOptionContext *self)
240 return pygi_gboolean_to_py (
241 g_option_context_get_ignore_unknown_options(self->context));
244 static PyObject *
245 pyg_option_context_set_main_group(PyGOptionContext *self,
246 PyObject *args,
247 PyObject *kwargs)
249 static char *kwlist[] = { "group", NULL };
250 GOptionGroup *g_group;
251 PyObject *group;
253 if (! PyArg_ParseTupleAndKeywords(args, kwargs,
254 "O:GOptionContext.set_main_group",
255 kwlist, &group))
256 return NULL;
258 if (PyObject_IsInstance(group, (PyObject*) &PyGOptionGroup_Type) != 1) {
259 PyErr_SetString(PyExc_TypeError,
260 "GOptionContext.set_main_group expects a GOptionGroup.");
261 return NULL;
264 g_group = pyglib_option_group_transfer_group(group);
265 if (g_group == NULL)
267 PyErr_SetString(PyExc_RuntimeError,
268 "Group is already in a OptionContext.");
269 return NULL;
272 g_option_context_set_main_group(self->context, g_group);
274 Py_INCREF(group);
275 self->main_group = (PyGOptionGroup*) group;
277 Py_INCREF(Py_None);
278 return Py_None;
281 static PyObject *
282 pyg_option_context_get_main_group(PyGOptionContext *self)
284 if (self->main_group == NULL)
286 Py_INCREF(Py_None);
287 return Py_None;
289 Py_INCREF(self->main_group);
290 return (PyObject*) self->main_group;
293 static PyObject *
294 pyg_option_context_add_group(PyGOptionContext *self,
295 PyObject *args,
296 PyObject *kwargs)
298 static char *kwlist[] = { "group", NULL };
299 GOptionGroup *g_group;
300 PyObject *group;
302 if (! PyArg_ParseTupleAndKeywords(args, kwargs,
303 "O:GOptionContext.add_group",
304 kwlist, &group))
305 return NULL;
307 if (PyObject_IsInstance(group, (PyObject*) &PyGOptionGroup_Type) != 1) {
308 PyErr_SetString(PyExc_TypeError,
309 "GOptionContext.add_group expects a GOptionGroup.");
310 return NULL;
313 g_group = pyglib_option_group_transfer_group(group);
314 if (g_group == NULL)
316 PyErr_SetString(PyExc_RuntimeError,
317 "Group is already in a OptionContext.");
318 return NULL;
320 Py_INCREF(group);
322 g_option_context_add_group(self->context, g_group);
324 Py_INCREF(Py_None);
325 return Py_None;
328 static PyObject*
329 pyg_option_context_richcompare(PyObject *self, PyObject *other, int op)
331 if (Py_TYPE(self) == Py_TYPE(other) && Py_TYPE(self) == &PyGOptionContext_Type)
332 return pyg_ptr_richcompare(((PyGOptionContext*)self)->context,
333 ((PyGOptionContext*)other)->context,
334 op);
335 else {
336 Py_INCREF(Py_NotImplemented);
337 return Py_NotImplemented;
341 static PyObject *
342 pyg_option_get_context(PyGOptionContext *self)
344 return PyCapsule_New (self->context, "goption.context", NULL);
347 static PyMethodDef pyg_option_context_methods[] = {
348 { "parse", (PyCFunction)pyg_option_context_parse, METH_VARARGS | METH_KEYWORDS },
349 { "set_help_enabled", (PyCFunction)pyg_option_context_set_help_enabled, METH_VARARGS | METH_KEYWORDS },
350 { "get_help_enabled", (PyCFunction)pyg_option_context_get_help_enabled, METH_NOARGS },
351 { "set_ignore_unknown_options", (PyCFunction)pyg_option_context_set_ignore_unknown_options, METH_VARARGS | METH_KEYWORDS },
352 { "get_ignore_unknown_options", (PyCFunction)pyg_option_context_get_ignore_unknown_options, METH_NOARGS },
353 { "set_main_group", (PyCFunction)pyg_option_context_set_main_group, METH_VARARGS | METH_KEYWORDS },
354 { "get_main_group", (PyCFunction)pyg_option_context_get_main_group, METH_NOARGS },
355 { "add_group", (PyCFunction)pyg_option_context_add_group, METH_VARARGS | METH_KEYWORDS },
356 { "_get_context", (PyCFunction)pyg_option_get_context, METH_NOARGS },
357 { NULL, NULL, 0 },
361 * Returns 0 on success, or -1 and sets an exception.
364 pygi_option_context_register_types(PyObject *d)
366 PyGOptionContext_Type.tp_dealloc = (destructor)pyg_option_context_dealloc;
367 PyGOptionContext_Type.tp_richcompare = pyg_option_context_richcompare;
368 PyGOptionContext_Type.tp_flags = Py_TPFLAGS_DEFAULT;
369 PyGOptionContext_Type.tp_methods = pyg_option_context_methods;
370 PyGOptionContext_Type.tp_init = (initproc)pyg_option_context_init;
371 PYGLIB_REGISTER_TYPE(d, PyGOptionContext_Type, "OptionContext");
373 return 0;