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, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
28 #include "pyglib-private.h"
29 #include "pygoptioncontext.h"
31 PYGLIB_DEFINE_TYPE("glib.OptionContext", PyGOptionContext_Type, PyGOptionContext)
34 * pyg_option_context_new:
35 * @context: a GOptionContext
37 * Returns: A new GOptionContext wrapper.
40 pyg_option_context_new (GOptionContext *context)
42 PyGOptionContext *self;
44 self = (PyGOptionContext *)PyObject_NEW(PyGOptionContext, &PyGOptionContext_Type);
48 self->context = context;
49 self->main_group = NULL;
51 return (PyObject *)self;
55 pyg_option_context_init(PyGOptionContext *self,
59 char *parameter_string;
61 if (!PyArg_ParseTuple(args, "s:glib.GOptionContext.__init__",
65 self->context = g_option_context_new(parameter_string);
70 pyg_option_context_dealloc(PyGOptionContext *self)
72 Py_CLEAR(self->main_group);
74 if (self->context != NULL)
76 GOptionContext *tmp = self->context;
78 g_option_context_free(tmp);
85 pyg_option_context_parse(PyGOptionContext *self,
89 static char *kwlist[] = { "argv", NULL };
91 PyObject *new_argv, *argv;
92 Py_ssize_t argv_length, pos;
94 char **argv_content, **original;
98 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O:GOptionContext.parse",
102 if (!PyList_Check(argv))
104 PyErr_SetString(PyExc_TypeError,
105 "GOptionContext.parse expects a list of strings.");
109 argv_length = PyList_Size(argv);
110 if (argv_length == -1)
112 PyErr_SetString(PyExc_TypeError,
113 "GOptionContext.parse expects a list of strings.");
117 argv_content = g_new(char*, argv_length + 1);
118 argv_content[argv_length] = NULL;
119 for (pos = 0; pos < argv_length; pos++)
121 arg = PyList_GetItem(argv, pos);
122 argv_content[pos] = g_strdup(PYGLIB_PyUnicode_AsString(arg));
123 if (argv_content[pos] == NULL)
125 g_strfreev(argv_content);
129 original = g_strdupv(argv_content);
131 g_assert(argv_length <= G_MAXINT);
132 argv_length_int = argv_length;
133 pyglib_begin_allow_threads;
134 result = g_option_context_parse(self->context, &argv_length_int, &argv_content,
136 pyglib_end_allow_threads;
137 argv_length = argv_length_int;
141 g_strfreev(argv_content);
142 g_strfreev(original);
143 pyglib_error_check(&error);
147 new_argv = PyList_New(g_strv_length(argv_content));
148 for (pos = 0; pos < argv_length; pos++)
150 arg = PYGLIB_PyUnicode_FromString(argv_content[pos]);
151 PyList_SetItem(new_argv, pos, arg);
154 g_strfreev(original);
155 g_strfreev(argv_content);
160 pyg_option_context_set_help_enabled(PyGOptionContext *self,
164 static char *kwlist[] = { "help_enable", NULL };
166 PyObject *help_enabled;
167 if (! PyArg_ParseTupleAndKeywords(args, kwargs,
168 "O:GOptionContext.set_help_enabled",
169 kwlist, &help_enabled))
172 g_option_context_set_help_enabled(self->context, PyObject_IsTrue(help_enabled));
179 pyg_option_context_get_help_enabled(PyGOptionContext *self)
181 return PyBool_FromLong(g_option_context_get_help_enabled(self->context));
185 pyg_option_context_set_ignore_unknown_options(PyGOptionContext *self,
189 static char *kwlist[] = { "ignore_unknown_options", NULL };
190 PyObject *ignore_unknown_options;
192 if (! PyArg_ParseTupleAndKeywords(args, kwargs,
193 "O:GOptionContext.set_ignore_unknown_options",
194 kwlist, &ignore_unknown_options))
197 g_option_context_set_ignore_unknown_options(self->context,
198 PyObject_IsTrue(ignore_unknown_options));
206 pyg_option_context_get_ignore_unknown_options(PyGOptionContext *self)
208 return PyBool_FromLong(
209 g_option_context_get_ignore_unknown_options(self->context));
213 pyg_option_context_set_main_group(PyGOptionContext *self,
217 static char *kwlist[] = { "group", NULL };
218 GOptionGroup *g_group;
221 if (! PyArg_ParseTupleAndKeywords(args, kwargs,
222 "O:GOptionContext.set_main_group",
226 if (PyObject_IsInstance(group, (PyObject*) &PyGOptionGroup_Type) != 1) {
227 PyErr_SetString(PyExc_TypeError,
228 "GOptionContext.set_main_group expects a GOptionGroup.");
232 g_group = pyglib_option_group_transfer_group(group);
235 PyErr_SetString(PyExc_RuntimeError,
236 "Group is already in a OptionContext.");
240 g_option_context_set_main_group(self->context, g_group);
243 self->main_group = (PyGOptionGroup*) group;
250 pyg_option_context_get_main_group(PyGOptionContext *self)
252 if (self->main_group == NULL)
257 Py_INCREF(self->main_group);
258 return (PyObject*) self->main_group;
262 pyg_option_context_add_group(PyGOptionContext *self,
266 static char *kwlist[] = { "group", NULL };
267 GOptionGroup *g_group;
270 if (! PyArg_ParseTupleAndKeywords(args, kwargs,
271 "O:GOptionContext.add_group",
275 if (PyObject_IsInstance(group, (PyObject*) &PyGOptionGroup_Type) != 1) {
276 PyErr_SetString(PyExc_TypeError,
277 "GOptionContext.add_group expects a GOptionGroup.");
281 g_group = pyglib_option_group_transfer_group(group);
284 PyErr_SetString(PyExc_RuntimeError,
285 "Group is already in a OptionContext.");
290 g_option_context_add_group(self->context, g_group);
297 pyg_option_context_richcompare(PyObject *self, PyObject *other, int op)
299 if (Py_TYPE(self) == Py_TYPE(other) && Py_TYPE(self) == &PyGOptionContext_Type)
300 return _pyglib_generic_ptr_richcompare(((PyGOptionContext*)self)->context,
301 ((PyGOptionContext*)other)->context,
304 Py_INCREF(Py_NotImplemented);
305 return Py_NotImplemented;
310 pyg_option_get_context(PyGOptionContext *self)
312 return PYGLIB_CPointer_WrapPointer(self->context, "goption.context");
315 static PyMethodDef pyg_option_context_methods[] = {
316 { "parse", (PyCFunction)pyg_option_context_parse, METH_VARARGS | METH_KEYWORDS },
317 { "set_help_enabled", (PyCFunction)pyg_option_context_set_help_enabled, METH_VARARGS | METH_KEYWORDS },
318 { "get_help_enabled", (PyCFunction)pyg_option_context_get_help_enabled, METH_NOARGS },
319 { "set_ignore_unknown_options", (PyCFunction)pyg_option_context_set_ignore_unknown_options, METH_VARARGS | METH_KEYWORDS },
320 { "get_ignore_unknown_options", (PyCFunction)pyg_option_context_get_ignore_unknown_options, METH_NOARGS },
321 { "set_main_group", (PyCFunction)pyg_option_context_set_main_group, METH_VARARGS | METH_KEYWORDS },
322 { "get_main_group", (PyCFunction)pyg_option_context_get_main_group, METH_NOARGS },
323 { "add_group", (PyCFunction)pyg_option_context_add_group, METH_VARARGS | METH_KEYWORDS },
324 { "_get_context", (PyCFunction)pyg_option_get_context, METH_NOARGS },
329 pyglib_option_context_register_types(PyObject *d)
331 PyGOptionContext_Type.tp_dealloc = (destructor)pyg_option_context_dealloc;
332 PyGOptionContext_Type.tp_richcompare = pyg_option_context_richcompare;
333 PyGOptionContext_Type.tp_flags = Py_TPFLAGS_DEFAULT;
334 PyGOptionContext_Type.tp_methods = pyg_option_context_methods;
335 PyGOptionContext_Type.tp_init = (initproc)pyg_option_context_init;
336 PYGLIB_REGISTER_TYPE(d, PyGOptionContext_Type, "OptionContext");