summaryrefslogtreecommitdiff
path: root/glib/pygoptioncontext.c
blob: 1d67ac53a61d1cb5ecb62f46576ea868d37d5ff5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/* -*- Mode: C; c-basic-offset: 4 -*-
 * pygtk- Python bindings for the GTK toolkit.
 * Copyright (C) 2006  Johannes Hoelzl
 *
 *   pygoptioncontext.c: GOptionContext wrapper
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
 * USA
 */

#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif

#include <pyglib.h>
#include "pyglib-private.h"
#include "pygoptioncontext.h"

PYGLIB_DEFINE_TYPE("glib.OptionContext", PyGOptionContext_Type, PyGOptionContext)

static int
pyg_option_context_init(PyGOptionContext *self,
                        PyObject *args,
                        PyObject *kwargs)
{
    char *parameter_string;

    if (!PyArg_ParseTuple(args, "s:glib.GOptionContext.__init__",
                          &parameter_string))
        return -1;

    self->context = g_option_context_new(parameter_string);
    return 0;
}

static void
pyg_option_context_dealloc(PyGOptionContext *self)
{
    Py_CLEAR(self->main_group);

    if (self->context != NULL)
    {
        GOptionContext *tmp = self->context;
        self->context = NULL;
        g_option_context_free(tmp);
    }

    PyObject_Del(self);
}

static PyObject *
pyg_option_context_parse(PyGOptionContext *self,
                         PyObject *args,
                         PyObject *kwargs)
{
    static char *kwlist[] = { "argv", NULL };
    PyObject *arg;
    PyObject *new_argv, *argv;
    Py_ssize_t argv_length, pos;
    gint argv_length_int;
    char **argv_content, **original;
    GError *error = NULL;
    gboolean result;

    if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O:GOptionContext.parse",
                                     kwlist, &argv))
        return NULL;

    if (!PyList_Check(argv))
    {
        PyErr_SetString(PyExc_TypeError,
                        "GOptionContext.parse expects a list of strings.");
        return NULL;
    }

    argv_length = PyList_Size(argv);
    if (argv_length == -1)
    {
        PyErr_SetString(PyExc_TypeError,
                        "GOptionContext.parse expects a list of strings.");
        return NULL;
    }

    argv_content = g_new(char*, argv_length + 1);
    argv_content[argv_length] = NULL;
    for (pos = 0; pos < argv_length; pos++)
    {
        arg = PyList_GetItem(argv, pos);
        argv_content[pos] = g_strdup(PYGLIB_PyUnicode_AsString(arg));
        if (argv_content[pos] == NULL)
        {
            g_strfreev(argv_content);
            return NULL;
        }
    }
    original = g_strdupv(argv_content);

    g_assert(argv_length <= G_MAXINT);
    argv_length_int = argv_length;
    pyglib_begin_allow_threads;
    result = g_option_context_parse(self->context, &argv_length_int, &argv_content,
                                    &error);
    pyglib_end_allow_threads;
    argv_length = argv_length_int;

    if (!result)
    {
        g_strfreev(argv_content);
        g_strfreev(original);
        pyglib_error_check(&error);
        return NULL;
    }

    new_argv = PyList_New(g_strv_length(argv_content));
    for (pos = 0; pos < argv_length; pos++)
    {
        arg = PYGLIB_PyUnicode_FromString(argv_content[pos]);
        PyList_SetItem(new_argv, pos, arg);
    }

    g_strfreev(original);
    g_strfreev(argv_content);
    return new_argv;
}

static PyObject *
pyg_option_context_set_help_enabled(PyGOptionContext *self,
                                    PyObject *args,
                                    PyObject *kwargs)
{
    static char *kwlist[] = { "help_enable", NULL };

    PyObject *help_enabled;
    if (! PyArg_ParseTupleAndKeywords(args, kwargs,
                                      "O:GOptionContext.set_help_enabled",
                                      kwlist, &help_enabled))
        return NULL;

    g_option_context_set_help_enabled(self->context, PyObject_IsTrue(help_enabled));

    Py_INCREF(Py_None);
    return Py_None;
}

static PyObject *
pyg_option_context_get_help_enabled(PyGOptionContext *self)
{
    return PyBool_FromLong(g_option_context_get_help_enabled(self->context));
}

static PyObject *
pyg_option_context_set_ignore_unknown_options(PyGOptionContext *self,
                                              PyObject *args,
                                              PyObject *kwargs)
{
    static char *kwlist[] = { "ignore_unknown_options", NULL };
    PyObject *ignore_unknown_options;

    if (! PyArg_ParseTupleAndKeywords(args, kwargs,
                                "O:GOptionContext.set_ignore_unknown_options",
                                kwlist, &ignore_unknown_options))
        return NULL;

    g_option_context_set_ignore_unknown_options(self->context,
						PyObject_IsTrue(ignore_unknown_options));
    

    Py_INCREF(Py_None);
    return Py_None;
}

static PyObject *
pyg_option_context_get_ignore_unknown_options(PyGOptionContext *self)
{
    return PyBool_FromLong(
        g_option_context_get_ignore_unknown_options(self->context));
}

static PyObject *
pyg_option_context_set_main_group(PyGOptionContext *self,
                                  PyObject *args,
                                  PyObject *kwargs)
{
    static char *kwlist[] = { "group", NULL };
    GOptionGroup *g_group;
    PyObject *group;

    if (! PyArg_ParseTupleAndKeywords(args, kwargs,
                                      "O:GOptionContext.set_main_group",
                                      kwlist, &group))
        return NULL;

    if (PyObject_IsInstance(group, (PyObject*) &PyGOptionGroup_Type) != 1) {
        PyErr_SetString(PyExc_TypeError,
                        "GOptionContext.set_main_group expects a GOptionGroup.");
        return NULL;
    }

    g_group = pyglib_option_group_transfer_group(group);
    if (g_group == NULL)
    {
        PyErr_SetString(PyExc_RuntimeError,
			"Group is already in a OptionContext.");
        return NULL;
    }

    g_option_context_set_main_group(self->context, g_group);

    Py_INCREF(group);
    self->main_group = (PyGOptionGroup*) group;

    Py_INCREF(Py_None);
    return Py_None;
}

static PyObject *
pyg_option_context_get_main_group(PyGOptionContext *self)
{
    if (self->main_group == NULL)
    {
        Py_INCREF(Py_None);
        return Py_None;
    }
    Py_INCREF(self->main_group);
    return (PyObject*) self->main_group;
}

static PyObject *
pyg_option_context_add_group(PyGOptionContext *self,
                             PyObject *args,
                             PyObject *kwargs)
{
    static char *kwlist[] = { "group", NULL };
    GOptionGroup *g_group;
    PyObject *group;

    if (! PyArg_ParseTupleAndKeywords(args, kwargs,
                                      "O:GOptionContext.add_group",
                                      kwlist, &group))
        return NULL;

    if (PyObject_IsInstance(group, (PyObject*) &PyGOptionGroup_Type) != 1) {
        PyErr_SetString(PyExc_TypeError,
                        "GOptionContext.add_group expects a GOptionGroup.");
        return NULL;
    }

    g_group = pyglib_option_group_transfer_group(group);
    if (g_group == NULL)
    {
        PyErr_SetString(PyExc_RuntimeError,
                        "Group is already in a OptionContext.");
        return NULL;
    }
    Py_INCREF(group);

    g_option_context_add_group(self->context, g_group);

    Py_INCREF(Py_None);
    return Py_None;
}

static PyObject*
pyg_option_context_richcompare(PyObject *self, PyObject *other, int op)
{
    if (Py_TYPE(self) == Py_TYPE(other) && Py_TYPE(self) == &PyGOptionContext_Type)
        return _pyglib_generic_ptr_richcompare(((PyGOptionContext*)self)->context,
                                               ((PyGOptionContext*)other)->context,
                                               op);
    else {
       Py_INCREF(Py_NotImplemented);
       return Py_NotImplemented;
    }
}

static PyObject *
pyg_option_get_context(PyGOptionContext *self)
{
    return PyCObject_FromVoidPtr(self->context, NULL);
}

static PyMethodDef pyg_option_context_methods[] = {
    { "parse", (PyCFunction)pyg_option_context_parse, METH_VARARGS | METH_KEYWORDS },
    { "set_help_enabled", (PyCFunction)pyg_option_context_set_help_enabled, METH_VARARGS | METH_KEYWORDS },
    { "get_help_enabled", (PyCFunction)pyg_option_context_get_help_enabled, METH_NOARGS },
    { "set_ignore_unknown_options", (PyCFunction)pyg_option_context_set_ignore_unknown_options, METH_VARARGS | METH_KEYWORDS },
    { "get_ignore_unknown_options", (PyCFunction)pyg_option_context_get_ignore_unknown_options, METH_NOARGS },
    { "set_main_group", (PyCFunction)pyg_option_context_set_main_group, METH_VARARGS | METH_KEYWORDS },
    { "get_main_group", (PyCFunction)pyg_option_context_get_main_group, METH_NOARGS },
    { "add_group", (PyCFunction)pyg_option_context_add_group, METH_VARARGS | METH_KEYWORDS },
    { "_get_context", (PyCFunction)pyg_option_get_context, METH_NOARGS },
    { NULL, NULL, 0 },
};

void
pyglib_option_context_register_types(PyObject *d)
{
    PyGOptionContext_Type.tp_dealloc = (destructor)pyg_option_context_dealloc;
    PyGOptionContext_Type.tp_richcompare = pyg_option_context_richcompare;
    PyGOptionContext_Type.tp_flags = Py_TPFLAGS_DEFAULT;
    PyGOptionContext_Type.tp_methods = pyg_option_context_methods;
    PyGOptionContext_Type.tp_init = (initproc)pyg_option_context_init;
    PYGLIB_REGISTER_TYPE(d, PyGOptionContext_Type, "OptionContext");
}