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/>.
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
)
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.
42 pyglib_option_group_transfer_group(PyObject
*obj
)
44 PyGOptionGroup
*self
= (PyGOptionGroup
*)obj
;
46 if (self
->is_in_context
)
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.
66 * pyg_option_context_new:
67 * @context: a GOptionContext
69 * Returns: A new GOptionContext wrapper.
72 pyg_option_context_new (GOptionContext
*context
)
74 PyGOptionContext
*self
;
76 self
= (PyGOptionContext
*)PyObject_NEW(PyGOptionContext
, &PyGOptionContext_Type
);
80 self
->context
= context
;
81 self
->main_group
= NULL
;
83 return (PyObject
*)self
;
87 pyg_option_context_init(PyGOptionContext
*self
,
91 char *parameter_string
;
93 if (!PyArg_ParseTuple(args
, "s:gi._gi.GOptionContext.__init__",
97 self
->context
= g_option_context_new(parameter_string
);
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
);
117 pyg_option_context_parse(PyGOptionContext
*self
,
121 static char *kwlist
[] = { "argv", NULL
};
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
;
130 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, "O:GOptionContext.parse",
134 if (!PyList_Check(argv
))
136 PyErr_SetString(PyExc_TypeError
,
137 "GOptionContext.parse expects a list of strings.");
141 argv_length
= PyList_Size(argv
);
142 if (argv_length
== -1)
144 PyErr_SetString(PyExc_TypeError
,
145 "GOptionContext.parse expects a list of strings.");
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
);
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
,
168 Py_END_ALLOW_THREADS
;
169 argv_length
= argv_length_int
;
173 g_strfreev(argv_content
);
174 g_strfreev(original
);
175 pygi_error_check(&error
);
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
);
192 pyg_option_context_set_help_enabled(PyGOptionContext
*self
,
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
))
204 g_option_context_set_help_enabled(self
->context
, PyObject_IsTrue(help_enabled
));
211 pyg_option_context_get_help_enabled(PyGOptionContext
*self
)
213 return pygi_gboolean_to_py (g_option_context_get_help_enabled(self
->context
));
217 pyg_option_context_set_ignore_unknown_options(PyGOptionContext
*self
,
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
))
229 g_option_context_set_ignore_unknown_options(self
->context
,
230 PyObject_IsTrue(ignore_unknown_options
));
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
));
245 pyg_option_context_set_main_group(PyGOptionContext
*self
,
249 static char *kwlist
[] = { "group", NULL
};
250 GOptionGroup
*g_group
;
253 if (! PyArg_ParseTupleAndKeywords(args
, kwargs
,
254 "O:GOptionContext.set_main_group",
258 if (PyObject_IsInstance(group
, (PyObject
*) &PyGOptionGroup_Type
) != 1) {
259 PyErr_SetString(PyExc_TypeError
,
260 "GOptionContext.set_main_group expects a GOptionGroup.");
264 g_group
= pyglib_option_group_transfer_group(group
);
267 PyErr_SetString(PyExc_RuntimeError
,
268 "Group is already in a OptionContext.");
272 g_option_context_set_main_group(self
->context
, g_group
);
275 self
->main_group
= (PyGOptionGroup
*) group
;
282 pyg_option_context_get_main_group(PyGOptionContext
*self
)
284 if (self
->main_group
== NULL
)
289 Py_INCREF(self
->main_group
);
290 return (PyObject
*) self
->main_group
;
294 pyg_option_context_add_group(PyGOptionContext
*self
,
298 static char *kwlist
[] = { "group", NULL
};
299 GOptionGroup
*g_group
;
302 if (! PyArg_ParseTupleAndKeywords(args
, kwargs
,
303 "O:GOptionContext.add_group",
307 if (PyObject_IsInstance(group
, (PyObject
*) &PyGOptionGroup_Type
) != 1) {
308 PyErr_SetString(PyExc_TypeError
,
309 "GOptionContext.add_group expects a GOptionGroup.");
313 g_group
= pyglib_option_group_transfer_group(group
);
316 PyErr_SetString(PyExc_RuntimeError
,
317 "Group is already in a OptionContext.");
322 g_option_context_add_group(self
->context
, g_group
);
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
,
336 Py_INCREF(Py_NotImplemented
);
337 return Py_NotImplemented
;
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
},
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");