1 /* -*- Mode: C; c-basic-offset: 4 -*-
2 * vim: tabstop=4 shiftwidth=4 expandtab
4 * Copyright (C) 2005-2009 Johan Dahlin <johan@gnome.org>
6 * pygi-repository.c: GIRepository wrapper.
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.
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.
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
24 #include "pygi-private.h"
26 #include <pyglib-python-compat.h>
28 PyObject *PyGIRepositoryError;
30 static PyMethodDef _PyGIRepository_methods[];
32 PYGLIB_DEFINE_TYPE("gi.Repository", PyGIRepository_Type, PyGIRepository);
35 _wrap_g_irepository_enumerate_versions (PyGIRepository *self,
39 static char *kwlist[] = { "namespace", NULL };
40 const char *namespace_;
41 GList *versions, *item;
44 if (!PyArg_ParseTupleAndKeywords (args, kwargs, "s:Repository.enumerate_versions",
45 kwlist, &namespace_)) {
49 versions = g_irepository_enumerate_versions (self->repository, namespace_);
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);
58 g_list_free(versions);
64 _wrap_g_irepository_get_default (PyObject *self)
66 static PyGIRepository *repository = NULL;
69 repository = (PyGIRepository *) PyObject_New (PyGIRepository, &PyGIRepository_Type);
70 if (repository == NULL) {
74 repository->repository = g_irepository_get_default();
77 Py_INCREF ( (PyObject *) repository);
78 return (PyObject *) repository;
82 _wrap_g_irepository_require (PyGIRepository *self,
86 static char *kwlist[] = { "namespace", "version", "lazy", NULL };
88 const char *namespace_;
89 const char *version = NULL;
90 PyObject *lazy = NULL;
91 GIRepositoryLoadFlags flags = 0;
95 if (!PyArg_ParseTupleAndKeywords (args, kwargs, "s|zO:Repository.require",
96 kwlist, &namespace_, &version, &lazy)) {
100 if (lazy != NULL && PyObject_IsTrue (lazy)) {
101 flags |= G_IREPOSITORY_LOAD_FLAG_LAZY;
105 typelib = g_irepository_require (self->repository, namespace_, version, flags, &error);
107 PyErr_SetString (PyGIRepositoryError, error->message);
108 g_error_free (error);
116 _wrap_g_irepository_find_by_name (PyGIRepository *self,
120 static char *kwlist[] = { "namespace", "name", NULL };
122 const char *namespace_;
127 if (!PyArg_ParseTupleAndKeywords (args, kwargs,
128 "ss:Repository.find_by_name", kwlist, &namespace_, &name)) {
132 info = g_irepository_find_by_name (self->repository, namespace_, name);
137 py_info = _pygi_info_new (info);
139 g_base_info_unref (info);
145 _wrap_g_irepository_get_infos (PyGIRepository *self,
149 static char *kwlist[] = { "namespace", NULL };
151 const char *namespace_;
156 if (!PyArg_ParseTupleAndKeywords (args, kwargs, "s:Repository.get_infos",
157 kwlist, &namespace_)) {
161 n_infos = g_irepository_get_n_infos (self->repository, namespace_);
163 PyErr_Format (PyExc_RuntimeError, "Namespace '%s' not loaded", namespace_);
167 infos = PyTuple_New (n_infos);
169 for (i = 0; i < n_infos; i++) {
173 info = g_irepository_get_info (self->repository, namespace_, i);
174 g_assert (info != NULL);
176 py_info = _pygi_info_new (info);
178 g_base_info_unref (info);
180 if (py_info == NULL) {
185 PyTuple_SET_ITEM (infos, i, py_info);
192 _wrap_g_irepository_get_typelib_path (PyGIRepository *self,
196 static char *kwlist[] = { "namespace", NULL };
197 const char *namespace_;
198 const gchar *typelib_path;
200 if (!PyArg_ParseTupleAndKeywords (args, kwargs,
201 "s:Repository.get_typelib_path", kwlist, &namespace_)) {
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_);
211 return PYGLIB_PyBytes_FromString (typelib_path);
215 _wrap_g_irepository_get_version (PyGIRepository *self,
219 static char *kwlist[] = { "namespace", NULL };
220 const char *namespace_;
221 const gchar *version;
223 if (!PyArg_ParseTupleAndKeywords (args, kwargs,
224 "s:Repository.get_version", kwlist, &namespace_)) {
228 version = g_irepository_get_version (self->repository, namespace_);
229 if (version == NULL) {
230 PyErr_Format (PyExc_RuntimeError, "Namespace '%s' not loaded", namespace_);
234 return PYGLIB_PyUnicode_FromString (version);
238 _wrap_g_irepository_get_loaded_namespaces (PyGIRepository *self)
241 PyObject *py_namespaces;
244 namespaces = g_irepository_get_loaded_namespaces (self->repository);
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]);
256 return py_namespaces;
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 },
272 _pygi_repository_register_types (PyObject *m)
274 Py_TYPE(&PyGIRepository_Type) = &PyType_Type;
276 PyGIRepository_Type.tp_flags = Py_TPFLAGS_DEFAULT;
277 PyGIRepository_Type.tp_methods = _PyGIRepository_methods;
279 if (PyType_Ready (&PyGIRepository_Type)) {
283 if (PyModule_AddObject (m, "Repository", (PyObject *) &PyGIRepository_Type)) {
287 PyGIRepositoryError = PyErr_NewException ("gi.RepositoryError", NULL, NULL);
288 if (PyModule_AddObject (m, "RepositoryError", PyGIRepositoryError)) {