setup.py: Provide a os.path.samefile fallback for Python 2 under Windows
[pygobject.git] / gi / pygoptioncontext.c
blob50c32fbe62af064f51ec6a6a5fcf0f2118f4e3a3
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 #ifdef HAVE_CONFIG_H
22 # include <config.h>
23 #endif
25 #include <pyglib.h>
26 #include "pygoptioncontext.h"
27 #include "pygi-error.h"
29 PYGLIB_DEFINE_TYPE("gi._gi.OptionContext", PyGOptionContext_Type, PyGOptionContext)
31 /**
32 * pyg_option_context_new:
33 * @context: a GOptionContext
35 * Returns: A new GOptionContext wrapper.
37 PyObject *
38 pyg_option_context_new (GOptionContext *context)
40 PyGOptionContext *self;
42 self = (PyGOptionContext *)PyObject_NEW(PyGOptionContext, &PyGOptionContext_Type);
43 if (self == NULL)
44 return NULL;
46 self->context = context;
47 self->main_group = NULL;
49 return (PyObject *)self;
52 static int
53 pyg_option_context_init(PyGOptionContext *self,
54 PyObject *args,
55 PyObject *kwargs)
57 char *parameter_string;
59 if (!PyArg_ParseTuple(args, "s:gi._gi.GOptionContext.__init__",
60 &parameter_string))
61 return -1;
63 self->context = g_option_context_new(parameter_string);
64 return 0;
67 static void
68 pyg_option_context_dealloc(PyGOptionContext *self)
70 Py_CLEAR(self->main_group);
72 if (self->context != NULL)
74 GOptionContext *tmp = self->context;
75 self->context = NULL;
76 g_option_context_free(tmp);
79 PyObject_Del(self);
82 static PyObject *
83 pyg_option_context_parse(PyGOptionContext *self,
84 PyObject *args,
85 PyObject *kwargs)
87 static char *kwlist[] = { "argv", NULL };
88 PyObject *arg;
89 PyObject *new_argv, *argv;
90 Py_ssize_t argv_length, pos;
91 gint argv_length_int;
92 char **argv_content, **original;
93 GError *error = NULL;
94 gboolean result;
96 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O:GOptionContext.parse",
97 kwlist, &argv))
98 return NULL;
100 if (!PyList_Check(argv))
102 PyErr_SetString(PyExc_TypeError,
103 "GOptionContext.parse expects a list of strings.");
104 return NULL;
107 argv_length = PyList_Size(argv);
108 if (argv_length == -1)
110 PyErr_SetString(PyExc_TypeError,
111 "GOptionContext.parse expects a list of strings.");
112 return NULL;
115 argv_content = g_new(char*, argv_length + 1);
116 argv_content[argv_length] = NULL;
117 for (pos = 0; pos < argv_length; pos++)
119 arg = PyList_GetItem(argv, pos);
120 argv_content[pos] = g_strdup(PYGLIB_PyUnicode_AsString(arg));
121 if (argv_content[pos] == NULL)
123 g_strfreev(argv_content);
124 return NULL;
127 original = g_strdupv(argv_content);
129 g_assert(argv_length <= G_MAXINT);
130 argv_length_int = argv_length;
131 Py_BEGIN_ALLOW_THREADS;
132 result = g_option_context_parse(self->context, &argv_length_int, &argv_content,
133 &error);
134 Py_END_ALLOW_THREADS;
135 argv_length = argv_length_int;
137 if (!result)
139 g_strfreev(argv_content);
140 g_strfreev(original);
141 pygi_error_check(&error);
142 return NULL;
145 new_argv = PyList_New(g_strv_length(argv_content));
146 for (pos = 0; pos < argv_length; pos++)
148 arg = PYGLIB_PyUnicode_FromString(argv_content[pos]);
149 PyList_SetItem(new_argv, pos, arg);
152 g_strfreev(original);
153 g_strfreev(argv_content);
154 return new_argv;
157 static PyObject *
158 pyg_option_context_set_help_enabled(PyGOptionContext *self,
159 PyObject *args,
160 PyObject *kwargs)
162 static char *kwlist[] = { "help_enable", NULL };
164 PyObject *help_enabled;
165 if (! PyArg_ParseTupleAndKeywords(args, kwargs,
166 "O:GOptionContext.set_help_enabled",
167 kwlist, &help_enabled))
168 return NULL;
170 g_option_context_set_help_enabled(self->context, PyObject_IsTrue(help_enabled));
172 Py_INCREF(Py_None);
173 return Py_None;
176 static PyObject *
177 pyg_option_context_get_help_enabled(PyGOptionContext *self)
179 return PyBool_FromLong(g_option_context_get_help_enabled(self->context));
182 static PyObject *
183 pyg_option_context_set_ignore_unknown_options(PyGOptionContext *self,
184 PyObject *args,
185 PyObject *kwargs)
187 static char *kwlist[] = { "ignore_unknown_options", NULL };
188 PyObject *ignore_unknown_options;
190 if (! PyArg_ParseTupleAndKeywords(args, kwargs,
191 "O:GOptionContext.set_ignore_unknown_options",
192 kwlist, &ignore_unknown_options))
193 return NULL;
195 g_option_context_set_ignore_unknown_options(self->context,
196 PyObject_IsTrue(ignore_unknown_options));
199 Py_INCREF(Py_None);
200 return Py_None;
203 static PyObject *
204 pyg_option_context_get_ignore_unknown_options(PyGOptionContext *self)
206 return PyBool_FromLong(
207 g_option_context_get_ignore_unknown_options(self->context));
210 static PyObject *
211 pyg_option_context_set_main_group(PyGOptionContext *self,
212 PyObject *args,
213 PyObject *kwargs)
215 static char *kwlist[] = { "group", NULL };
216 GOptionGroup *g_group;
217 PyObject *group;
219 if (! PyArg_ParseTupleAndKeywords(args, kwargs,
220 "O:GOptionContext.set_main_group",
221 kwlist, &group))
222 return NULL;
224 if (PyObject_IsInstance(group, (PyObject*) &PyGOptionGroup_Type) != 1) {
225 PyErr_SetString(PyExc_TypeError,
226 "GOptionContext.set_main_group expects a GOptionGroup.");
227 return NULL;
230 g_group = pyglib_option_group_transfer_group(group);
231 if (g_group == NULL)
233 PyErr_SetString(PyExc_RuntimeError,
234 "Group is already in a OptionContext.");
235 return NULL;
238 g_option_context_set_main_group(self->context, g_group);
240 Py_INCREF(group);
241 self->main_group = (PyGOptionGroup*) group;
243 Py_INCREF(Py_None);
244 return Py_None;
247 static PyObject *
248 pyg_option_context_get_main_group(PyGOptionContext *self)
250 if (self->main_group == NULL)
252 Py_INCREF(Py_None);
253 return Py_None;
255 Py_INCREF(self->main_group);
256 return (PyObject*) self->main_group;
259 static PyObject *
260 pyg_option_context_add_group(PyGOptionContext *self,
261 PyObject *args,
262 PyObject *kwargs)
264 static char *kwlist[] = { "group", NULL };
265 GOptionGroup *g_group;
266 PyObject *group;
268 if (! PyArg_ParseTupleAndKeywords(args, kwargs,
269 "O:GOptionContext.add_group",
270 kwlist, &group))
271 return NULL;
273 if (PyObject_IsInstance(group, (PyObject*) &PyGOptionGroup_Type) != 1) {
274 PyErr_SetString(PyExc_TypeError,
275 "GOptionContext.add_group expects a GOptionGroup.");
276 return NULL;
279 g_group = pyglib_option_group_transfer_group(group);
280 if (g_group == NULL)
282 PyErr_SetString(PyExc_RuntimeError,
283 "Group is already in a OptionContext.");
284 return NULL;
286 Py_INCREF(group);
288 g_option_context_add_group(self->context, g_group);
290 Py_INCREF(Py_None);
291 return Py_None;
294 static PyObject*
295 pyg_option_context_richcompare(PyObject *self, PyObject *other, int op)
297 if (Py_TYPE(self) == Py_TYPE(other) && Py_TYPE(self) == &PyGOptionContext_Type)
298 return _pyglib_generic_ptr_richcompare(((PyGOptionContext*)self)->context,
299 ((PyGOptionContext*)other)->context,
300 op);
301 else {
302 Py_INCREF(Py_NotImplemented);
303 return Py_NotImplemented;
307 static PyObject *
308 pyg_option_get_context(PyGOptionContext *self)
310 return PYGLIB_CPointer_WrapPointer(self->context, "goption.context");
313 static PyMethodDef pyg_option_context_methods[] = {
314 { "parse", (PyCFunction)pyg_option_context_parse, METH_VARARGS | METH_KEYWORDS },
315 { "set_help_enabled", (PyCFunction)pyg_option_context_set_help_enabled, METH_VARARGS | METH_KEYWORDS },
316 { "get_help_enabled", (PyCFunction)pyg_option_context_get_help_enabled, METH_NOARGS },
317 { "set_ignore_unknown_options", (PyCFunction)pyg_option_context_set_ignore_unknown_options, METH_VARARGS | METH_KEYWORDS },
318 { "get_ignore_unknown_options", (PyCFunction)pyg_option_context_get_ignore_unknown_options, METH_NOARGS },
319 { "set_main_group", (PyCFunction)pyg_option_context_set_main_group, METH_VARARGS | METH_KEYWORDS },
320 { "get_main_group", (PyCFunction)pyg_option_context_get_main_group, METH_NOARGS },
321 { "add_group", (PyCFunction)pyg_option_context_add_group, METH_VARARGS | METH_KEYWORDS },
322 { "_get_context", (PyCFunction)pyg_option_get_context, METH_NOARGS },
323 { NULL, NULL, 0 },
326 void
327 pyglib_option_context_register_types(PyObject *d)
329 PyGOptionContext_Type.tp_dealloc = (destructor)pyg_option_context_dealloc;
330 PyGOptionContext_Type.tp_richcompare = pyg_option_context_richcompare;
331 PyGOptionContext_Type.tp_flags = Py_TPFLAGS_DEFAULT;
332 PyGOptionContext_Type.tp_methods = pyg_option_context_methods;
333 PyGOptionContext_Type.tp_init = (initproc)pyg_option_context_init;
334 PYGLIB_REGISTER_TYPE(d, PyGOptionContext_Type, "OptionContext");