1 /* -*- Mode: C; c-basic-offset: 4 -*-
2 * vim: tabstop=4 shiftwidth=4 expandtab
3 *
4 * Copyright (C) 2009 Simon van der Linden <svdlinden@src.gnome.org>
5 *
6 * pygi-type.c: helpers to lookup Python wrappers from GType and GIBaseInfo.
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
27 PyObject *
_pygi_type_import_by_name(const char * namespace_,const char * name)28 _pygi_type_import_by_name (const char *namespace_,
29 const char *name)
30 {
31 gchar *module_name;
32 PyObject *py_module;
33 PyObject *py_object;
34
35 module_name = g_strconcat ("gi.repository.", namespace_, NULL);
36
37 py_module = PyImport_ImportModule (module_name);
38
39 g_free (module_name);
40
41 if (py_module == NULL) {
42 return NULL;
43 }
44
45 py_object = PyObject_GetAttrString (py_module, name);
46
47 Py_DECREF (py_module);
48
49 return py_object;
50 }
51
52 PyObject *
pygi_type_import_by_g_type_real(GType g_type)53 pygi_type_import_by_g_type_real (GType g_type)
54 {
55 GIRepository *repository;
56 GIBaseInfo *info;
57 PyObject *type;
58
59 repository = g_irepository_get_default();
60
61 info = g_irepository_find_by_gtype (repository, g_type);
62 if (info == NULL) {
63 return NULL;
64 }
65
66 type = _pygi_type_import_by_gi_info (info);
67 g_base_info_unref (info);
68
69 return type;
70 }
71
72 PyObject *
_pygi_type_import_by_gi_info(GIBaseInfo * info)73 _pygi_type_import_by_gi_info (GIBaseInfo *info)
74 {
75 return _pygi_type_import_by_name (g_base_info_get_namespace (info),
76 g_base_info_get_name (info));
77 }
78
79 PyObject *
_pygi_type_get_from_g_type(GType g_type)80 _pygi_type_get_from_g_type (GType g_type)
81 {
82 PyObject *py_g_type;
83 PyObject *py_type;
84
85 py_g_type = pyg_type_wrapper_new (g_type);
86 if (py_g_type == NULL) {
87 return NULL;
88 }
89
90 py_type = PyObject_GetAttrString (py_g_type, "pytype");
91 if (py_type == Py_None) {
92 py_type = pygi_type_import_by_g_type_real (g_type);
93 }
94
95 Py_DECREF (py_g_type);
96
97 return py_type;
98 }
99
100