1 # -*- Mode: Python; py-indent-offset: 4 -*-
2 # vim: tabstop=4 shiftwidth=4 expandtab
4 # Copyright (C) 2005-2009 Johan Dahlin <johan@gnome.org>
6 # This library is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU Lesser General Public
8 # License as published by the Free Software Foundation; either
9 # version 2.1 of the License, or (at your option) any later version.
11 # This library is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 # Lesser General Public License for more details.
16 # You should have received a copy of the GNU Lesser General Public
17 # License along with this library; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
21 from __future__
import absolute_import
23 # support overrides in different directories than our gi module
24 from pkgutil
import extend_path
25 __path__
= extend_path(__path__
, __name__
)
32 _static_binding_error
= ('When using gi.repository you must not import static '
33 'modules like "gobject". Please change all occurrences '
34 'of "import gobject" to "from gi.repository import GObject". '
35 'See: https://bugzilla.gnome.org/show_bug.cgi?id=709183')
37 # we can't have pygobject 2 loaded at the same time we load the internal _gobject
38 if 'gobject' in sys
.modules
:
39 raise ImportError(_static_binding_error
)
44 from ._gi
import Repository
45 from ._gi
import PyGIDeprecationWarning
46 from ._gi
import PyGIWarning
47 from ._compat
import string_types
49 _API
= _API
# pyflakes
50 PyGIDeprecationWarning
= PyGIDeprecationWarning
51 PyGIWarning
= PyGIWarning
54 _overridesdir
= os
.path
.join(os
.path
.dirname(__file__
), 'overrides')
56 # Needed for compatibility with "pygobject.h"/pygobject_init()
57 _gobject
= types
.ModuleType("gi._gobject")
58 sys
.modules
[_gobject
.__name
__] = _gobject
59 _gobject
._PyGObject
_API
= _gi
._PyGObject
_API
60 _gobject
.pygobject_version
= _gi
.pygobject_version
62 version_info
= _gi
.pygobject_version
[:]
63 __version__
= "{0}.{1}.{2}".format(*version_info
)
66 class _DummyStaticModule(types
.ModuleType
):
69 def __getattr__(self
, name
):
70 raise AttributeError(_static_binding_error
)
73 sys
.modules
['glib'] = _DummyStaticModule('glib', _static_binding_error
)
74 sys
.modules
['gobject'] = _DummyStaticModule('gobject', _static_binding_error
)
75 sys
.modules
['gio'] = _DummyStaticModule('gio', _static_binding_error
)
76 sys
.modules
['gtk'] = _DummyStaticModule('gtk', _static_binding_error
)
77 sys
.modules
['gtk.gdk'] = _DummyStaticModule('gtk.gdk', _static_binding_error
)
80 def check_version(version
):
81 if isinstance(version
, str):
82 version_list
= tuple(map(int, version
.split(".")))
84 version_list
= version
86 if version_list
> version_info
:
88 "pygobject's version %s required, and available version "
89 "%s is not recent enough") % (version
, __version__
)
93 def require_version(namespace
, version
):
94 """ Ensures the correct versions are loaded when importing `gi` modules.
96 :param namespace: The name of module to require.
98 :param version: The version of module to require.
100 :raises ValueError: If module/version is already loaded, already required, or unavailable.
104 .. code-block:: python
107 gi.require_version('Gtk', '3.0')
110 repository
= Repository
.get_default()
112 if not isinstance(version
, string_types
):
113 raise ValueError('Namespace version needs to be a string.')
115 if namespace
in repository
.get_loaded_namespaces():
116 loaded_version
= repository
.get_version(namespace
)
117 if loaded_version
!= version
:
118 raise ValueError('Namespace %s is already loaded with version %s' %
119 (namespace
, loaded_version
))
121 if namespace
in _versions
and _versions
[namespace
] != version
:
122 raise ValueError('Namespace %s already requires version %s' %
123 (namespace
, _versions
[namespace
]))
125 available_versions
= repository
.enumerate_versions(namespace
)
126 if not available_versions
:
127 raise ValueError('Namespace %s not available' % namespace
)
129 if version
not in available_versions
:
130 raise ValueError('Namespace %s not available for version %s' %
131 (namespace
, version
))
133 _versions
[namespace
] = version
136 def require_versions(requires
):
137 """ Utility function for consolidating multiple `gi.require_version()` calls.
139 :param requires: The names and versions of modules to require.
144 .. code-block:: python
147 gi.require_versions({'Gtk': '3.0', 'GLib': '2.0', 'Gio': '2.0'})
149 for module_name
, module_version
in requires
.items():
150 require_version(module_name
, module_version
)
153 def get_required_version(namespace
):
154 return _versions
.get(namespace
, None)
157 def require_foreign(namespace
, symbol
=None):
158 """Ensure the given foreign marshaling module is available and loaded.
160 :param str namespace:
161 Introspection namespace of the foreign module (e.g. "cairo")
163 Optional symbol typename to ensure a converter exists.
164 :type symbol: str or None
169 .. code-block:: python
173 gi.require_foreign('cairo')
177 _gi
.require_foreign(namespace
, symbol
)
178 except Exception as e
:
179 raise ImportError(str(e
))
180 importlib
.import_module('gi.repository', namespace
)