| 1 |
/* -*- Mode: C; c-basic-offset: 4 -*- |
| 2 |
* vim: tabstop=4 shiftwidth=4 expandtab |
| 3 |
* |
| 4 |
* Copyright (C) 2005-2009 Johan Dahlin <[email protected]> |
| 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, see <http://www.gnu.org/licenses/>. |
| 20 |
*/ |
| 21 |
|
| 22 |
#include "pygi-basictype.h" |
| 23 |
#include "pygi-info.h" |
| 24 |
#include "pygi-repository.h" |
| 25 |
#include "pygi-util.h" |
| 26 |
|
| 27 |
PyObject *PyGIRepositoryError; |
| 28 |
|
| 29 |
PYGI_DEFINE_TYPE ("gi.Repository", PyGIRepository_Type, PyGIRepository); |
| 30 |
|
| 31 |
|
| 32 |
GIRepository * |
| 33 |
pygi_repository_get_default (void) |
| 34 |
{ |
| 35 |
static GIRepository *default_repository = NULL; |
| 36 |
|
| 37 |
if (default_repository == NULL) |
| 38 |
#if GLIB_CHECK_VERSION(2, 85, 0) |
| 39 |
default_repository = gi_repository_dup_default (); |
| 40 |
#else |
| 41 |
default_repository = gi_repository_new (); |
| 42 |
#endif |
| 43 |
|
| 44 |
return default_repository; |
| 45 |
} |
| 46 |
|
| 47 |
static PyObject * |
| 48 |
_wrap_pygi_repository_get_default (PyObject *self) |
| 49 |
{ |
| 50 |
static PyGIRepository *repository = NULL; |
| 51 |
|
| 52 |
if (!repository) { |
| 53 |
repository = (PyGIRepository *)PyObject_New (PyGIRepository, |
| 54 |
&PyGIRepository_Type); |
| 55 |
if (repository == NULL) { |
| 56 |
return NULL; |
| 57 |
} |
| 58 |
|
| 59 |
repository->repository = pygi_repository_get_default (); |
| 60 |
} |
| 61 |
|
| 62 |
Py_INCREF ((PyObject *)repository); |
| 63 |
return (PyObject *)repository; |
| 64 |
} |
| 65 |
|
| 66 |
static PyObject * |
| 67 |
_wrap_gi_repository_prepend_library_path (PyGIRepository *self, PyObject *args, |
| 68 |
PyObject *kwargs) |
| 69 |
{ |
| 70 |
static char *kwlist[] = { "directory", NULL }; |
| 71 |
const char *directory; |
| 72 |
|
| 73 |
if (!PyArg_ParseTupleAndKeywords (args, kwargs, |
| 74 |
"s:Repository.prepend_library_path", |
| 75 |
kwlist, &directory)) { |
| 76 |
return NULL; |
| 77 |
} |
| 78 |
|
| 79 |
gi_repository_prepend_library_path (self->repository, directory); |
| 80 |
|
| 81 |
Py_RETURN_NONE; |
| 82 |
} |
| 83 |
|
| 84 |
static PyObject * |
| 85 |
_wrap_gi_repository_prepend_search_path (PyGIRepository *self, PyObject *args, |
| 86 |
PyObject *kwargs) |
| 87 |
{ |
| 88 |
static char *kwlist[] = { "directory", NULL }; |
| 89 |
const char *directory; |
| 90 |
|
| 91 |
if (!PyArg_ParseTupleAndKeywords (args, kwargs, |
| 92 |
"s:Repository.prepend_search_path", |
| 93 |
kwlist, &directory)) { |
| 94 |
return NULL; |
| 95 |
} |
| 96 |
|
| 97 |
gi_repository_prepend_search_path (self->repository, directory); |
| 98 |
|
| 99 |
Py_RETURN_NONE; |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
static PyObject * |
| 104 |
_wrap_gi_repository_enumerate_versions (PyGIRepository *self, PyObject *args, |
| 105 |
PyObject *kwargs) |
| 106 |
{ |
| 107 |
static char *kwlist[] = { "namespace", NULL }; |
| 108 |
const char *namespace_; |
| 109 |
char **versions = NULL; |
| 110 |
PyObject *ret = NULL; |
| 111 |
|
| 112 |
if (!PyArg_ParseTupleAndKeywords (args, kwargs, |
| 113 |
"s:Repository.enumerate_versions", |
| 114 |
kwlist, &namespace_)) { |
| 115 |
return NULL; |
| 116 |
} |
| 117 |
|
| 118 |
versions = |
| 119 |
gi_repository_enumerate_versions (self->repository, namespace_, NULL); |
| 120 |
ret = PyList_New (0); |
| 121 |
for (size_t i = 0; versions[i] != NULL; i++) { |
| 122 |
char *version = g_steal_pointer (&versions[i]); |
| 123 |
PyObject *py_version = pygi_utf8_to_py (version); |
| 124 |
PyList_Append (ret, py_version); |
| 125 |
Py_DECREF (py_version); |
| 126 |
g_free (version); |
| 127 |
} |
| 128 |
g_free (versions); |
| 129 |
|
| 130 |
return ret; |
| 131 |
} |
| 132 |
|
| 133 |
static PyObject * |
| 134 |
_wrap_gi_repository_require (PyGIRepository *self, PyObject *args, |
| 135 |
PyObject *kwargs) |
| 136 |
{ |
| 137 |
static char *kwlist[] = { "namespace", "version", "lazy", NULL }; |
| 138 |
|
| 139 |
const char *namespace_; |
| 140 |
const char *version = NULL; |
| 141 |
PyObject *lazy = NULL; |
| 142 |
GIRepositoryLoadFlags flags = 0; |
| 143 |
GError *error; |
| 144 |
|
| 145 |
if (!PyArg_ParseTupleAndKeywords (args, kwargs, "s|zO:Repository.require", |
| 146 |
kwlist, &namespace_, &version, &lazy)) { |
| 147 |
return NULL; |
| 148 |
} |
| 149 |
|
| 150 |
if (lazy != NULL && PyObject_IsTrue (lazy)) { |
| 151 |
flags |= GI_REPOSITORY_LOAD_FLAG_LAZY; |
| 152 |
} |
| 153 |
|
| 154 |
error = NULL; |
| 155 |
gi_repository_require (self->repository, namespace_, version, flags, |
| 156 |
&error); |
| 157 |
if (error != NULL) { |
| 158 |
PyErr_SetString (PyGIRepositoryError, error->message); |
| 159 |
g_error_free (error); |
| 160 |
return NULL; |
| 161 |
} |
| 162 |
|
| 163 |
Py_RETURN_NONE; |
| 164 |
} |
| 165 |
|
| 166 |
static PyObject * |
| 167 |
_wrap_gi_repository_is_registered (PyGIRepository *self, PyObject *args, |
| 168 |
PyObject *kwargs) |
| 169 |
{ |
| 170 |
static char *kwlist[] = { "namespace", "version", NULL }; |
| 171 |
const char *namespace_; |
| 172 |
const char *version = NULL; |
| 173 |
|
| 174 |
if (!PyArg_ParseTupleAndKeywords (args, kwargs, |
| 175 |
"s|z:Repository.is_registered", kwlist, |
| 176 |
&namespace_, &version)) { |
| 177 |
return NULL; |
| 178 |
} |
| 179 |
|
| 180 |
return pygi_gboolean_to_py ( |
| 181 |
gi_repository_is_registered (self->repository, namespace_, version)); |
| 182 |
} |
| 183 |
|
| 184 |
static PyObject * |
| 185 |
_wrap_gi_repository_find_by_name (PyGIRepository *self, PyObject *args, |
| 186 |
PyObject *kwargs) |
| 187 |
{ |
| 188 |
static char *kwlist[] = { "namespace", "name", NULL }; |
| 189 |
|
| 190 |
const char *namespace_; |
| 191 |
const char *name; |
| 192 |
GIBaseInfo *info; |
| 193 |
PyObject *py_info; |
| 194 |
size_t len; |
| 195 |
char *trimmed_name = NULL; |
| 196 |
|
| 197 |
if (!PyArg_ParseTupleAndKeywords (args, kwargs, |
| 198 |
"ss:Repository.find_by_name", kwlist, |
| 199 |
&namespace_, &name)) { |
| 200 |
return NULL; |
| 201 |
} |
| 202 |
|
| 203 |
/* If the given name ends with an underscore, it might be due to usage |
| 204 |
* as an accessible replacement for something in GI with the same name |
| 205 |
* as a Python keyword. Test for this and trim it out if necessary. |
| 206 |
*/ |
| 207 |
len = strlen (name); |
| 208 |
if (len > 0 && name[len - 1] == '_') { |
| 209 |
PyObject *is_keyword; |
| 210 |
|
| 211 |
trimmed_name = g_strndup (name, len - 1); |
| 212 |
is_keyword = pyg_is_python_keyword (trimmed_name); |
| 213 |
if (!is_keyword) return NULL; |
| 214 |
|
| 215 |
if (PyObject_IsTrue (is_keyword)) name = trimmed_name; |
| 216 |
|
| 217 |
Py_DECREF (is_keyword); |
| 218 |
} |
| 219 |
|
| 220 |
info = gi_repository_find_by_name (self->repository, namespace_, name); |
| 221 |
g_free (trimmed_name); |
| 222 |
|
| 223 |
if (info == NULL) { |
| 224 |
Py_RETURN_NONE; |
| 225 |
} |
| 226 |
|
| 227 |
py_info = _pygi_info_new (info); |
| 228 |
|
| 229 |
gi_base_info_unref (info); |
| 230 |
|
| 231 |
return py_info; |
| 232 |
} |
| 233 |
|
| 234 |
static PyObject * |
| 235 |
_wrap_gi_repository_get_infos (PyGIRepository *self, PyObject *args, |
| 236 |
PyObject *kwargs) |
| 237 |
{ |
| 238 |
static char *kwlist[] = { "namespace", NULL }; |
| 239 |
|
| 240 |
const char *namespace_; |
| 241 |
gssize n_infos; |
| 242 |
PyObject *infos; |
| 243 |
gint i; |
| 244 |
|
| 245 |
if (!PyArg_ParseTupleAndKeywords (args, kwargs, "s:Repository.get_infos", |
| 246 |
kwlist, &namespace_)) { |
| 247 |
return NULL; |
| 248 |
} |
| 249 |
|
| 250 |
n_infos = gi_repository_get_n_infos (self->repository, namespace_); |
| 251 |
if (n_infos < 0) { |
| 252 |
PyErr_Format (PyExc_RuntimeError, "Namespace '%s' not loaded", |
| 253 |
namespace_); |
| 254 |
return NULL; |
| 255 |
} |
| 256 |
|
| 257 |
infos = PyTuple_New (n_infos); |
| 258 |
|
| 259 |
for (i = 0; i < n_infos; i++) { |
| 260 |
GIBaseInfo *info; |
| 261 |
PyObject *py_info; |
| 262 |
|
| 263 |
info = gi_repository_get_info (self->repository, namespace_, i); |
| 264 |
g_assert (info != NULL); |
| 265 |
|
| 266 |
py_info = _pygi_info_new (info); |
| 267 |
|
| 268 |
gi_base_info_unref (info); |
| 269 |
|
| 270 |
if (py_info == NULL) { |
| 271 |
Py_CLEAR (infos); |
| 272 |
break; |
| 273 |
} |
| 274 |
|
| 275 |
PyTuple_SET_ITEM (infos, i, py_info); |
| 276 |
} |
| 277 |
|
| 278 |
return infos; |
| 279 |
} |
| 280 |
|
| 281 |
static PyObject * |
| 282 |
_wrap_gi_repository_get_typelib_path (PyGIRepository *self, PyObject *args, |
| 283 |
PyObject *kwargs) |
| 284 |
{ |
| 285 |
static char *kwlist[] = { "namespace", NULL }; |
| 286 |
const char *namespace_; |
| 287 |
const gchar *typelib_path; |
| 288 |
|
| 289 |
if (!PyArg_ParseTupleAndKeywords (args, kwargs, |
| 290 |
"s:Repository.get_typelib_path", kwlist, |
| 291 |
&namespace_)) { |
| 292 |
return NULL; |
| 293 |
} |
| 294 |
|
| 295 |
typelib_path = |
| 296 |
gi_repository_get_typelib_path (self->repository, namespace_); |
| 297 |
if (typelib_path == NULL) { |
| 298 |
PyErr_Format (PyExc_RuntimeError, "Namespace '%s' not loaded", |
| 299 |
namespace_); |
| 300 |
return NULL; |
| 301 |
} |
| 302 |
|
| 303 |
return pygi_filename_to_py (typelib_path); |
| 304 |
} |
| 305 |
|
| 306 |
static PyObject * |
| 307 |
_wrap_gi_repository_get_version (PyGIRepository *self, PyObject *args, |
| 308 |
PyObject *kwargs) |
| 309 |
{ |
| 310 |
static char *kwlist[] = { "namespace", NULL }; |
| 311 |
const char *namespace_; |
| 312 |
const gchar *version; |
| 313 |
|
| 314 |
if (!PyArg_ParseTupleAndKeywords (args, kwargs, "s:Repository.get_version", |
| 315 |
kwlist, &namespace_)) { |
| 316 |
return NULL; |
| 317 |
} |
| 318 |
|
| 319 |
version = gi_repository_get_version (self->repository, namespace_); |
| 320 |
if (version == NULL) { |
| 321 |
PyErr_Format (PyExc_RuntimeError, "Namespace '%s' not loaded", |
| 322 |
namespace_); |
| 323 |
return NULL; |
| 324 |
} |
| 325 |
|
| 326 |
return pygi_utf8_to_py (version); |
| 327 |
} |
| 328 |
|
| 329 |
static PyObject * |
| 330 |
_wrap_gi_repository_get_loaded_namespaces (PyGIRepository *self) |
| 331 |
{ |
| 332 |
char **namespaces; |
| 333 |
PyObject *py_namespaces; |
| 334 |
gssize i; |
| 335 |
|
| 336 |
namespaces = gi_repository_get_loaded_namespaces (self->repository, NULL); |
| 337 |
|
| 338 |
py_namespaces = PyList_New (0); |
| 339 |
for (i = 0; namespaces[i] != NULL; i++) { |
| 340 |
PyObject *py_namespace = pygi_utf8_to_py (namespaces[i]); |
| 341 |
PyList_Append (py_namespaces, py_namespace); |
| 342 |
Py_DECREF (py_namespace); |
| 343 |
g_free (namespaces[i]); |
| 344 |
} |
| 345 |
|
| 346 |
g_free (namespaces); |
| 347 |
|
| 348 |
return py_namespaces; |
| 349 |
} |
| 350 |
|
| 351 |
static PyObject * |
| 352 |
_wrap_gi_repository_get_dependencies (PyGIRepository *self, PyObject *args, |
| 353 |
PyObject *kwargs) |
| 354 |
{ |
| 355 |
static char *kwlist[] = { "namespace", NULL }; |
| 356 |
const char *namespace_; |
| 357 |
char **namespaces; |
| 358 |
PyObject *py_namespaces; |
| 359 |
gssize i; |
| 360 |
|
| 361 |
if (!PyArg_ParseTupleAndKeywords (args, kwargs, |
| 362 |
"s:Repository.get_dependencies", kwlist, |
| 363 |
&namespace_)) { |
| 364 |
return NULL; |
| 365 |
} |
| 366 |
|
| 367 |
py_namespaces = PyList_New (0); |
| 368 |
/* Returns NULL in case of no dependencies */ |
| 369 |
namespaces = |
| 370 |
gi_repository_get_dependencies (self->repository, namespace_, NULL); |
| 371 |
if (namespaces == NULL) { |
| 372 |
return py_namespaces; |
| 373 |
} |
| 374 |
|
| 375 |
for (i = 0; namespaces[i] != NULL; i++) { |
| 376 |
PyObject *py_namespace = pygi_utf8_to_py (namespaces[i]); |
| 377 |
PyList_Append (py_namespaces, py_namespace); |
| 378 |
Py_DECREF (py_namespace); |
| 379 |
} |
| 380 |
|
| 381 |
g_strfreev (namespaces); |
| 382 |
|
| 383 |
return py_namespaces; |
| 384 |
} |
| 385 |
|
| 386 |
|
| 387 |
static PyObject * |
| 388 |
_wrap_gi_repository_get_immediate_dependencies (PyGIRepository *self, |
| 389 |
PyObject *args, |
| 390 |
PyObject *kwargs) |
| 391 |
{ |
| 392 |
static char *kwlist[] = { "namespace", NULL }; |
| 393 |
const char *namespace_; |
| 394 |
char **namespaces; |
| 395 |
PyObject *py_namespaces; |
| 396 |
gssize i; |
| 397 |
|
| 398 |
if (!PyArg_ParseTupleAndKeywords ( |
| 399 |
args, kwargs, "s:Repository.get_immediate_dependencies", kwlist, |
| 400 |
&namespace_)) { |
| 401 |
return NULL; |
| 402 |
} |
| 403 |
|
| 404 |
py_namespaces = PyList_New (0); |
| 405 |
namespaces = gi_repository_get_immediate_dependencies (self->repository, |
| 406 |
namespace_, NULL); |
| 407 |
|
| 408 |
for (i = 0; namespaces[i] != NULL; i++) { |
| 409 |
PyObject *py_namespace = pygi_utf8_to_py (namespaces[i]); |
| 410 |
PyList_Append (py_namespaces, py_namespace); |
| 411 |
Py_DECREF (py_namespace); |
| 412 |
} |
| 413 |
|
| 414 |
g_strfreev (namespaces); |
| 415 |
|
| 416 |
return py_namespaces; |
| 417 |
} |
| 418 |
|
| 419 |
|
| 420 |
static PyMethodDef _PyGIRepository_methods[] = { |
| 421 |
{ "get_default", (PyCFunction)_wrap_pygi_repository_get_default, |
| 422 |
METH_NOARGS | METH_STATIC }, |
| 423 |
{ "enumerate_versions", |
| 424 |
(PyCFunction)_wrap_gi_repository_enumerate_versions, |
| 425 |
METH_VARARGS | METH_KEYWORDS }, |
| 426 |
{ "require", (PyCFunction)_wrap_gi_repository_require, |
| 427 |
METH_VARARGS | METH_KEYWORDS }, |
| 428 |
{ "get_infos", (PyCFunction)_wrap_gi_repository_get_infos, |
| 429 |
METH_VARARGS | METH_KEYWORDS }, |
| 430 |
{ "find_by_name", (PyCFunction)_wrap_gi_repository_find_by_name, |
| 431 |
METH_VARARGS | METH_KEYWORDS }, |
| 432 |
{ "get_typelib_path", (PyCFunction)_wrap_gi_repository_get_typelib_path, |
| 433 |
METH_VARARGS | METH_KEYWORDS }, |
| 434 |
{ "get_version", (PyCFunction)_wrap_gi_repository_get_version, |
| 435 |
METH_VARARGS | METH_KEYWORDS }, |
| 436 |
{ "get_loaded_namespaces", |
| 437 |
(PyCFunction)_wrap_gi_repository_get_loaded_namespaces, METH_NOARGS }, |
| 438 |
{ "get_dependencies", (PyCFunction)_wrap_gi_repository_get_dependencies, |
| 439 |
METH_VARARGS | METH_KEYWORDS }, |
| 440 |
{ "get_immediate_dependencies", |
| 441 |
(PyCFunction)_wrap_gi_repository_get_immediate_dependencies, |
| 442 |
METH_VARARGS | METH_KEYWORDS }, |
| 443 |
{ "is_registered", (PyCFunction)_wrap_gi_repository_is_registered, |
| 444 |
METH_VARARGS | METH_KEYWORDS }, |
| 445 |
{ "prepend_library_path", |
| 446 |
(PyCFunction)_wrap_gi_repository_prepend_library_path, |
| 447 |
METH_VARARGS | METH_KEYWORDS }, |
| 448 |
{ "prepend_search_path", |
| 449 |
(PyCFunction)_wrap_gi_repository_prepend_search_path, |
| 450 |
METH_VARARGS | METH_KEYWORDS }, |
| 451 |
{ NULL, NULL, 0 }, |
| 452 |
}; |
| 453 |
|
| 454 |
/** |
| 455 |
* Returns 0 on success, or -1 and sets an exception. |
| 456 |
*/ |
| 457 |
int |
| 458 |
pygi_repository_register_types (PyObject *m) |
| 459 |
{ |
| 460 |
Py_SET_TYPE (&PyGIRepository_Type, &PyType_Type); |
| 461 |
|
| 462 |
PyGIRepository_Type.tp_flags = Py_TPFLAGS_DEFAULT; |
| 463 |
PyGIRepository_Type.tp_methods = _PyGIRepository_methods; |
| 464 |
|
| 465 |
if (PyType_Ready (&PyGIRepository_Type) < 0) return -1; |
| 466 |
|
| 467 |
Py_INCREF ((PyObject *)&PyGIRepository_Type); |
| 468 |
if (PyModule_AddObject (m, "Repository", (PyObject *)&PyGIRepository_Type) |
| 469 |
< 0) { |
| 470 |
Py_DECREF ((PyObject *)&PyGIRepository_Type); |
| 471 |
return -1; |
| 472 |
} |
| 473 |
|
| 474 |
PyGIRepositoryError = |
| 475 |
PyErr_NewException ("gi.RepositoryError", NULL, NULL); |
| 476 |
if (PyGIRepositoryError == NULL) return -1; |
| 477 |
|
| 478 |
Py_INCREF (PyGIRepositoryError); |
| 479 |
if (PyModule_AddObject (m, "RepositoryError", PyGIRepositoryError) < 0) { |
| 480 |
Py_DECREF (PyGIRepositoryError); |
| 481 |
return -1; |
| 482 |
} |
| 483 |
|
| 484 |
return 0; |
| 485 |
} |