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/>.
26 #include "pygoptioncontext.h"
27 #include "pygi-error.h"
29 PYGLIB_DEFINE_TYPE("gi._gi.OptionContext", PyGOptionContext_Type
, PyGOptionContext
)
32 * pyg_option_context_new:
33 * @context: a GOptionContext
35 * Returns: A new GOptionContext wrapper.
38 pyg_option_context_new (GOptionContext
*context
)
40 PyGOptionContext
*self
;
42 self
= (PyGOptionContext
*)PyObject_NEW(PyGOptionContext
, &PyGOptionContext_Type
);
46 self
->context
= context
;
47 self
->main_group
= NULL
;
49 return (PyObject
*)self
;
53 pyg_option_context_init(PyGOptionContext
*self
,
57 char *parameter_string
;
59 if (!PyArg_ParseTuple(args
, "s:gi._gi.GOptionContext.__init__",
63 self
->context
= g_option_context_new(parameter_string
);
68 pyg_option_context_dealloc(PyGOptionContext
*self
)
70 Py_CLEAR(self
->main_group
);
72 if (self
->context
!= NULL
)
74 GOptionContext
*tmp
= self
->context
;
76 g_option_context_free(tmp
);
83 pyg_option_context_parse(PyGOptionContext
*self
,
87 static char *kwlist
[] = { "argv", NULL
};
89 PyObject
*new_argv
, *argv
;
90 Py_ssize_t argv_length
, pos
;
92 char **argv_content
, **original
;
96 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, "O:GOptionContext.parse",
100 if (!PyList_Check(argv
))
102 PyErr_SetString(PyExc_TypeError
,
103 "GOptionContext.parse expects a list of strings.");
107 argv_length
= PyList_Size(argv
);
108 if (argv_length
== -1)
110 PyErr_SetString(PyExc_TypeError
,
111 "GOptionContext.parse expects a list of strings.");
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
);
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
,
134 Py_END_ALLOW_THREADS
;
135 argv_length
= argv_length_int
;
139 g_strfreev(argv_content
);
140 g_strfreev(original
);
141 pygi_error_check(&error
);
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
);
158 pyg_option_context_set_help_enabled(PyGOptionContext
*self
,
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
))
170 g_option_context_set_help_enabled(self
->context
, PyObject_IsTrue(help_enabled
));
177 pyg_option_context_get_help_enabled(PyGOptionContext
*self
)
179 return PyBool_FromLong(g_option_context_get_help_enabled(self
->context
));
183 pyg_option_context_set_ignore_unknown_options(PyGOptionContext
*self
,
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
))
195 g_option_context_set_ignore_unknown_options(self
->context
,
196 PyObject_IsTrue(ignore_unknown_options
));
204 pyg_option_context_get_ignore_unknown_options(PyGOptionContext
*self
)
206 return PyBool_FromLong(
207 g_option_context_get_ignore_unknown_options(self
->context
));
211 pyg_option_context_set_main_group(PyGOptionContext
*self
,
215 static char *kwlist
[] = { "group", NULL
};
216 GOptionGroup
*g_group
;
219 if (! PyArg_ParseTupleAndKeywords(args
, kwargs
,
220 "O:GOptionContext.set_main_group",
224 if (PyObject_IsInstance(group
, (PyObject
*) &PyGOptionGroup_Type
) != 1) {
225 PyErr_SetString(PyExc_TypeError
,
226 "GOptionContext.set_main_group expects a GOptionGroup.");
230 g_group
= pyglib_option_group_transfer_group(group
);
233 PyErr_SetString(PyExc_RuntimeError
,
234 "Group is already in a OptionContext.");
238 g_option_context_set_main_group(self
->context
, g_group
);
241 self
->main_group
= (PyGOptionGroup
*) group
;
248 pyg_option_context_get_main_group(PyGOptionContext
*self
)
250 if (self
->main_group
== NULL
)
255 Py_INCREF(self
->main_group
);
256 return (PyObject
*) self
->main_group
;
260 pyg_option_context_add_group(PyGOptionContext
*self
,
264 static char *kwlist
[] = { "group", NULL
};
265 GOptionGroup
*g_group
;
268 if (! PyArg_ParseTupleAndKeywords(args
, kwargs
,
269 "O:GOptionContext.add_group",
273 if (PyObject_IsInstance(group
, (PyObject
*) &PyGOptionGroup_Type
) != 1) {
274 PyErr_SetString(PyExc_TypeError
,
275 "GOptionContext.add_group expects a GOptionGroup.");
279 g_group
= pyglib_option_group_transfer_group(group
);
282 PyErr_SetString(PyExc_RuntimeError
,
283 "Group is already in a OptionContext.");
288 g_option_context_add_group(self
->context
, g_group
);
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
,
302 Py_INCREF(Py_NotImplemented
);
303 return Py_NotImplemented
;
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
},
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");