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, see <http://www.gnu.org/licenses/>.
22 #include "pygi-repository.h"
23 #include "pygi-info.h"
24 #include "pygi-basictype.h"
25 #include "pygi-python-compat.h"
27 PyObject
*PyGIRepositoryError
;
29 PYGLIB_DEFINE_TYPE("gi.Repository", PyGIRepository_Type
, PyGIRepository
);
32 _wrap_g_irepository_enumerate_versions (PyGIRepository
*self
,
36 static char *kwlist
[] = { "namespace", NULL
};
37 const char *namespace_
;
38 GList
*versions
, *item
;
41 if (!PyArg_ParseTupleAndKeywords (args
, kwargs
, "s:Repository.enumerate_versions",
42 kwlist
, &namespace_
)) {
46 versions
= g_irepository_enumerate_versions (self
->repository
, namespace_
);
48 for (item
= versions
; item
; item
= item
->next
) {
49 char *version
= item
->data
;
50 PyObject
*py_version
= pygi_utf8_to_py (version
);
51 PyList_Append(ret
, py_version
);
52 Py_DECREF(py_version
);
55 g_list_free(versions
);
61 _wrap_g_irepository_get_default (PyObject
*self
)
63 static PyGIRepository
*repository
= NULL
;
66 repository
= (PyGIRepository
*) PyObject_New (PyGIRepository
, &PyGIRepository_Type
);
67 if (repository
== NULL
) {
71 repository
->repository
= g_irepository_get_default();
74 Py_INCREF ( (PyObject
*) repository
);
75 return (PyObject
*) repository
;
79 _wrap_g_irepository_require (PyGIRepository
*self
,
83 static char *kwlist
[] = { "namespace", "version", "lazy", NULL
};
85 const char *namespace_
;
86 const char *version
= NULL
;
87 PyObject
*lazy
= NULL
;
88 GIRepositoryLoadFlags flags
= 0;
91 if (!PyArg_ParseTupleAndKeywords (args
, kwargs
, "s|zO:Repository.require",
92 kwlist
, &namespace_
, &version
, &lazy
)) {
96 if (lazy
!= NULL
&& PyObject_IsTrue (lazy
)) {
97 flags
|= G_IREPOSITORY_LOAD_FLAG_LAZY
;
101 g_irepository_require (self
->repository
, namespace_
, version
, flags
, &error
);
103 PyErr_SetString (PyGIRepositoryError
, error
->message
);
104 g_error_free (error
);
112 _wrap_g_irepository_is_registered (PyGIRepository
*self
,
116 static char *kwlist
[] = { "namespace", "version", NULL
};
117 const char *namespace_
;
118 const char *version
= NULL
;
120 if (!PyArg_ParseTupleAndKeywords (args
, kwargs
, "s|z:Repository.is_registered",
121 kwlist
, &namespace_
, &version
)) {
125 return pygi_gboolean_to_py (g_irepository_is_registered (self
->repository
,
126 namespace_
, version
));
130 _wrap_g_irepository_find_by_name (PyGIRepository
*self
,
134 static char *kwlist
[] = { "namespace", "name", NULL
};
136 const char *namespace_
;
141 char *trimmed_name
= NULL
;
143 if (!PyArg_ParseTupleAndKeywords (args
, kwargs
,
144 "ss:Repository.find_by_name", kwlist
, &namespace_
, &name
)) {
148 /* If the given name ends with an underscore, it might be due to usage
149 * as an accessible replacement for something in GI with the same name
150 * as a Python keyword. Test for this and trim it out if necessary.
153 if (len
> 0 && name
[len
-1] == '_') {
154 trimmed_name
= g_strndup (name
, len
-1);
155 if (_pygi_is_python_keyword (trimmed_name
)) {
160 info
= g_irepository_find_by_name (self
->repository
, namespace_
, name
);
161 g_free (trimmed_name
);
167 py_info
= _pygi_info_new (info
);
169 g_base_info_unref (info
);
175 _wrap_g_irepository_get_infos (PyGIRepository
*self
,
179 static char *kwlist
[] = { "namespace", NULL
};
181 const char *namespace_
;
186 if (!PyArg_ParseTupleAndKeywords (args
, kwargs
, "s:Repository.get_infos",
187 kwlist
, &namespace_
)) {
191 n_infos
= g_irepository_get_n_infos (self
->repository
, namespace_
);
193 PyErr_Format (PyExc_RuntimeError
, "Namespace '%s' not loaded", namespace_
);
197 infos
= PyTuple_New (n_infos
);
199 for (i
= 0; i
< n_infos
; i
++) {
203 info
= g_irepository_get_info (self
->repository
, namespace_
, i
);
204 g_assert (info
!= NULL
);
206 py_info
= _pygi_info_new (info
);
208 g_base_info_unref (info
);
210 if (py_info
== NULL
) {
215 PyTuple_SET_ITEM (infos
, i
, py_info
);
222 _wrap_g_irepository_get_typelib_path (PyGIRepository
*self
,
226 static char *kwlist
[] = { "namespace", NULL
};
227 const char *namespace_
;
228 const gchar
*typelib_path
;
230 if (!PyArg_ParseTupleAndKeywords (args
, kwargs
,
231 "s:Repository.get_typelib_path", kwlist
, &namespace_
)) {
235 typelib_path
= g_irepository_get_typelib_path (self
->repository
, namespace_
);
236 if (typelib_path
== NULL
) {
237 PyErr_Format (PyExc_RuntimeError
, "Namespace '%s' not loaded", namespace_
);
241 return pygi_filename_to_py (typelib_path
);
245 _wrap_g_irepository_get_version (PyGIRepository
*self
,
249 static char *kwlist
[] = { "namespace", NULL
};
250 const char *namespace_
;
251 const gchar
*version
;
253 if (!PyArg_ParseTupleAndKeywords (args
, kwargs
,
254 "s:Repository.get_version", kwlist
, &namespace_
)) {
258 version
= g_irepository_get_version (self
->repository
, namespace_
);
259 if (version
== NULL
) {
260 PyErr_Format (PyExc_RuntimeError
, "Namespace '%s' not loaded", namespace_
);
264 return pygi_utf8_to_py (version
);
268 _wrap_g_irepository_get_loaded_namespaces (PyGIRepository
*self
)
271 PyObject
*py_namespaces
;
274 namespaces
= g_irepository_get_loaded_namespaces (self
->repository
);
276 py_namespaces
= PyList_New (0);
277 for (i
= 0; namespaces
[i
] != NULL
; i
++) {
278 PyObject
*py_namespace
= pygi_utf8_to_py (namespaces
[i
]);
279 PyList_Append (py_namespaces
, py_namespace
);
280 Py_DECREF(py_namespace
);
281 g_free (namespaces
[i
]);
286 return py_namespaces
;
290 _wrap_g_irepository_get_dependencies (PyGIRepository
*self
,
294 static char *kwlist
[] = { "namespace", NULL
};
295 const char *namespace_
;
297 PyObject
*py_namespaces
;
300 if (!PyArg_ParseTupleAndKeywords (args
, kwargs
,
301 "s:Repository.get_dependencies", kwlist
, &namespace_
)) {
305 py_namespaces
= PyList_New (0);
306 /* Returns NULL in case of no dependencies */
307 namespaces
= g_irepository_get_dependencies (self
->repository
, namespace_
);
308 if (namespaces
== NULL
) {
309 return py_namespaces
;
312 for (i
= 0; namespaces
[i
] != NULL
; i
++) {
313 PyObject
*py_namespace
= pygi_utf8_to_py (namespaces
[i
]);
314 PyList_Append (py_namespaces
, py_namespace
);
315 Py_DECREF(py_namespace
);
318 g_strfreev (namespaces
);
320 return py_namespaces
;
325 _wrap_g_irepository_get_immediate_dependencies (PyGIRepository
*self
,
329 static char *kwlist
[] = { "namespace", NULL
};
330 const char *namespace_
;
332 PyObject
*py_namespaces
;
335 if (!PyArg_ParseTupleAndKeywords (args
, kwargs
,
336 "s:Repository.get_immediate_dependencies",
337 kwlist
, &namespace_
)) {
341 py_namespaces
= PyList_New (0);
342 namespaces
= g_irepository_get_immediate_dependencies (self
->repository
,
345 for (i
= 0; namespaces
[i
] != NULL
; i
++) {
346 PyObject
*py_namespace
= pygi_utf8_to_py (namespaces
[i
]);
347 PyList_Append (py_namespaces
, py_namespace
);
348 Py_DECREF (py_namespace
);
351 g_strfreev (namespaces
);
353 return py_namespaces
;
357 static PyMethodDef _PyGIRepository_methods
[] = {
358 { "enumerate_versions", (PyCFunction
) _wrap_g_irepository_enumerate_versions
, METH_VARARGS
| METH_KEYWORDS
},
359 { "get_default", (PyCFunction
) _wrap_g_irepository_get_default
, METH_STATIC
| METH_NOARGS
},
360 { "require", (PyCFunction
) _wrap_g_irepository_require
, METH_VARARGS
| METH_KEYWORDS
},
361 { "get_infos", (PyCFunction
) _wrap_g_irepository_get_infos
, METH_VARARGS
| METH_KEYWORDS
},
362 { "find_by_name", (PyCFunction
) _wrap_g_irepository_find_by_name
, METH_VARARGS
| METH_KEYWORDS
},
363 { "get_typelib_path", (PyCFunction
) _wrap_g_irepository_get_typelib_path
, METH_VARARGS
| METH_KEYWORDS
},
364 { "get_version", (PyCFunction
) _wrap_g_irepository_get_version
, METH_VARARGS
| METH_KEYWORDS
},
365 { "get_loaded_namespaces", (PyCFunction
) _wrap_g_irepository_get_loaded_namespaces
, METH_NOARGS
},
366 { "get_dependencies", (PyCFunction
) _wrap_g_irepository_get_dependencies
, METH_VARARGS
| METH_KEYWORDS
},
367 { "get_immediate_dependencies", (PyCFunction
) _wrap_g_irepository_get_immediate_dependencies
, METH_VARARGS
| METH_KEYWORDS
},
368 { "is_registered", (PyCFunction
) _wrap_g_irepository_is_registered
, METH_VARARGS
| METH_KEYWORDS
},
373 * Returns 0 on success, or -1 and sets an exception.
376 pygi_repository_register_types (PyObject
*m
)
378 Py_TYPE(&PyGIRepository_Type
) = &PyType_Type
;
380 PyGIRepository_Type
.tp_flags
= Py_TPFLAGS_DEFAULT
;
381 PyGIRepository_Type
.tp_methods
= _PyGIRepository_methods
;
383 if (PyType_Ready (&PyGIRepository_Type
) < 0)
386 Py_INCREF ((PyObject
*) &PyGIRepository_Type
);
387 if (PyModule_AddObject (m
, "Repository", (PyObject
*) &PyGIRepository_Type
) < 0) {
388 Py_DECREF ((PyObject
*) &PyGIRepository_Type
);
392 PyGIRepositoryError
= PyErr_NewException ("gi.RepositoryError", NULL
, NULL
);
393 if (PyGIRepositoryError
== NULL
)
396 Py_INCREF (PyGIRepositoryError
);
397 if (PyModule_AddObject (m
, "RepositoryError", PyGIRepositoryError
) < 0) {
398 Py_DECREF (PyGIRepositoryError
);