c48d2ce5ed64175ced6769359a603c20fb7acfe1
[platform/upstream/python-gobject.git] / gi / pygi-repository.c
1 /* -*- Mode: C; c-basic-offset: 4 -*-
2  * vim: tabstop=4 shiftwidth=4 expandtab
3  *
4  * Copyright (C) 2005-2009 Johan Dahlin <johan@gnome.org>
5  *
6  *   pygi-repository.c: GIRepository wrapper.
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
21  * USA
22  */
23
24 #include "pygi-private.h"
25
26 #include <pyglib-python-compat.h>
27
28 PyObject *PyGIRepositoryError;
29
30 static PyMethodDef _PyGIRepository_methods[];
31
32 PYGLIB_DEFINE_TYPE("gi.Repository", PyGIRepository_Type, PyGIRepository);
33
34 static PyObject *
35 _wrap_g_irepository_enumerate_versions (PyGIRepository *self,
36                                         PyObject       *args,
37                                         PyObject       *kwargs)
38 {
39     static char *kwlist[] = { "namespace", NULL };
40     const char *namespace_;
41     GList *versions, *item;
42     PyObject *ret = NULL;
43
44     if (!PyArg_ParseTupleAndKeywords (args, kwargs, "s:Repository.enumerate_versions",
45                                       kwlist, &namespace_)) {
46         return NULL;
47     }
48
49     versions = g_irepository_enumerate_versions (self->repository, namespace_);
50     ret = PyList_New(0);
51     for (item = versions; item; item = item->next) {
52         char *version = item->data;
53         PyObject *py_version = PYGLIB_PyUnicode_FromString (version);
54         PyList_Append(ret, py_version);
55         Py_DECREF(py_version);
56         g_free (version);
57     }
58     g_list_free(versions);
59
60     return ret;
61 }
62
63 static PyObject *
64 _wrap_g_irepository_get_default (PyObject *self)
65 {
66     static PyGIRepository *repository = NULL;
67
68     if (!repository) {
69         repository = (PyGIRepository *) PyObject_New (PyGIRepository, &PyGIRepository_Type);
70         if (repository == NULL) {
71             return NULL;
72         }
73
74         repository->repository = g_irepository_get_default();
75     }
76
77     Py_INCREF ( (PyObject *) repository);
78     return (PyObject *) repository;
79 }
80
81 static PyObject *
82 _wrap_g_irepository_require (PyGIRepository *self,
83                              PyObject       *args,
84                              PyObject       *kwargs)
85 {
86     static char *kwlist[] = { "namespace", "version", "lazy", NULL };
87
88     const char *namespace_;
89     const char *version = NULL;
90     PyObject *lazy = NULL;
91     GIRepositoryLoadFlags flags = 0;
92     GTypelib *typelib;
93     GError *error;
94
95     if (!PyArg_ParseTupleAndKeywords (args, kwargs, "s|zO:Repository.require",
96                                       kwlist, &namespace_, &version, &lazy)) {
97         return NULL;
98     }
99
100     if (lazy != NULL && PyObject_IsTrue (lazy)) {
101         flags |= G_IREPOSITORY_LOAD_FLAG_LAZY;
102     }
103
104     error = NULL;
105     typelib = g_irepository_require (self->repository, namespace_, version, flags, &error);
106     if (error != NULL) {
107         PyErr_SetString (PyGIRepositoryError, error->message);
108         g_error_free (error);
109         return NULL;
110     }
111
112     Py_RETURN_NONE;
113 }
114
115 static PyObject *
116 _wrap_g_irepository_find_by_name (PyGIRepository *self,
117                                   PyObject       *args,
118                                   PyObject       *kwargs)
119 {
120     static char *kwlist[] = { "namespace", "name", NULL };
121
122     const char *namespace_;
123     const char *name;
124     GIBaseInfo *info;
125     PyObject *py_info;
126
127     if (!PyArg_ParseTupleAndKeywords (args, kwargs,
128                                       "ss:Repository.find_by_name", kwlist, &namespace_, &name)) {
129         return NULL;
130     }
131
132     info = g_irepository_find_by_name (self->repository, namespace_, name);
133     if (info == NULL) {
134         Py_RETURN_NONE;
135     }
136
137     py_info = _pygi_info_new (info);
138
139     g_base_info_unref (info);
140
141     return py_info;
142 }
143
144 static PyObject *
145 _wrap_g_irepository_get_infos (PyGIRepository *self,
146                                PyObject       *args,
147                                PyObject       *kwargs)
148 {
149     static char *kwlist[] = { "namespace", NULL };
150
151     const char *namespace_;
152     gssize n_infos;
153     PyObject *infos;
154     gssize i;
155
156     if (!PyArg_ParseTupleAndKeywords (args, kwargs, "s:Repository.get_infos",
157                                       kwlist, &namespace_)) {
158         return NULL;
159     }
160
161     n_infos = g_irepository_get_n_infos (self->repository, namespace_);
162     if (n_infos < 0) {
163         PyErr_Format (PyExc_RuntimeError, "Namespace '%s' not loaded", namespace_);
164         return NULL;
165     }
166
167     infos = PyTuple_New (n_infos);
168
169     for (i = 0; i < n_infos; i++) {
170         GIBaseInfo *info;
171         PyObject *py_info;
172
173         info = g_irepository_get_info (self->repository, namespace_, i);
174         g_assert (info != NULL);
175
176         py_info = _pygi_info_new (info);
177
178         g_base_info_unref (info);
179
180         if (py_info == NULL) {
181             Py_CLEAR (infos);
182             break;
183         }
184
185         PyTuple_SET_ITEM (infos, i, py_info);
186     }
187
188     return infos;
189 }
190
191 static PyObject *
192 _wrap_g_irepository_get_typelib_path (PyGIRepository *self,
193                                       PyObject       *args,
194                                       PyObject       *kwargs)
195 {
196     static char *kwlist[] = { "namespace", NULL };
197     const char *namespace_;
198     const gchar *typelib_path;
199
200     if (!PyArg_ParseTupleAndKeywords (args, kwargs,
201                                       "s:Repository.get_typelib_path", kwlist, &namespace_)) {
202         return NULL;
203     }
204
205     typelib_path = g_irepository_get_typelib_path (self->repository, namespace_);
206     if (typelib_path == NULL) {
207         PyErr_Format (PyExc_RuntimeError, "Namespace '%s' not loaded", namespace_);
208         return NULL;
209     }
210
211     return PYGLIB_PyBytes_FromString (typelib_path);
212 }
213
214 static PyObject *
215 _wrap_g_irepository_get_version (PyGIRepository *self,
216                                  PyObject       *args,
217                                  PyObject       *kwargs)
218 {
219     static char *kwlist[] = { "namespace", NULL };
220     const char *namespace_;
221     const gchar *version;
222
223     if (!PyArg_ParseTupleAndKeywords (args, kwargs,
224                                       "s:Repository.get_version", kwlist, &namespace_)) {
225         return NULL;
226     }
227
228     version = g_irepository_get_version (self->repository, namespace_);
229     if (version == NULL) {
230         PyErr_Format (PyExc_RuntimeError, "Namespace '%s' not loaded", namespace_);
231         return NULL;
232     }
233
234     return PYGLIB_PyUnicode_FromString (version);
235 }
236
237 static PyObject *
238 _wrap_g_irepository_get_loaded_namespaces (PyGIRepository *self)
239 {
240     char **namespaces;
241     PyObject *py_namespaces;
242     gssize i;
243
244     namespaces = g_irepository_get_loaded_namespaces (self->repository);
245
246     py_namespaces = PyList_New (0);
247     for (i = 0; namespaces[i] != NULL; i++) {
248         PyObject *py_namespace = PYGLIB_PyUnicode_FromString (namespaces[i]);
249         PyList_Append (py_namespaces, py_namespace);
250         Py_DECREF(py_namespace);
251         g_free (namespaces[i]);
252     }
253
254     g_free (namespaces);
255
256     return py_namespaces;
257 }
258
259 static PyMethodDef _PyGIRepository_methods[] = {
260     { "enumerate_versions", (PyCFunction) _wrap_g_irepository_enumerate_versions, METH_VARARGS | METH_KEYWORDS },
261     { "get_default", (PyCFunction) _wrap_g_irepository_get_default, METH_STATIC | METH_NOARGS },
262     { "require", (PyCFunction) _wrap_g_irepository_require, METH_VARARGS | METH_KEYWORDS },
263     { "get_infos", (PyCFunction) _wrap_g_irepository_get_infos, METH_VARARGS | METH_KEYWORDS },
264     { "find_by_name", (PyCFunction) _wrap_g_irepository_find_by_name, METH_VARARGS | METH_KEYWORDS },
265     { "get_typelib_path", (PyCFunction) _wrap_g_irepository_get_typelib_path, METH_VARARGS | METH_KEYWORDS },
266     { "get_version", (PyCFunction) _wrap_g_irepository_get_version, METH_VARARGS | METH_KEYWORDS },
267     { "get_loaded_namespaces", (PyCFunction) _wrap_g_irepository_get_loaded_namespaces, METH_NOARGS },
268     { NULL, NULL, 0 }
269 };
270
271 void
272 _pygi_repository_register_types (PyObject *m)
273 {
274     Py_TYPE(&PyGIRepository_Type) = &PyType_Type;
275
276     PyGIRepository_Type.tp_flags = Py_TPFLAGS_DEFAULT;
277     PyGIRepository_Type.tp_methods = _PyGIRepository_methods;
278
279     if (PyType_Ready (&PyGIRepository_Type)) {
280         return;
281     }
282
283     if (PyModule_AddObject (m, "Repository", (PyObject *) &PyGIRepository_Type)) {
284         return;
285     }
286
287     PyGIRepositoryError = PyErr_NewException ("gi.RepositoryError", NULL, NULL);
288     if (PyModule_AddObject (m, "RepositoryError", PyGIRepositoryError)) {
289         return;
290     }
291 }
292