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
)
43 from ._gi
import _gobject
45 from ._gi
import Repository
46 from ._gi
import PyGIDeprecationWarning
47 from ._gi
import PyGIWarning
49 _API
= _API
# pyflakes
50 PyGIDeprecationWarning
= PyGIDeprecationWarning
51 PyGIWarning
= PyGIWarning
54 _overridesdir
= os
.path
.join(os
.path
.dirname(__file__
), 'overrides')
56 version_info
= _gobject
.pygobject_version
[:]
57 __version__
= "{0}.{1}.{2}".format(*version_info
)
60 class _DummyStaticModule(types
.ModuleType
):
63 def __getattr__(self
, name
):
64 raise AttributeError(_static_binding_error
)
67 sys
.modules
['glib'] = _DummyStaticModule('glib', _static_binding_error
)
68 sys
.modules
['gobject'] = _DummyStaticModule('gobject', _static_binding_error
)
69 sys
.modules
['gio'] = _DummyStaticModule('gio', _static_binding_error
)
70 sys
.modules
['gtk'] = _DummyStaticModule('gtk', _static_binding_error
)
71 sys
.modules
['gtk.gdk'] = _DummyStaticModule('gtk.gdk', _static_binding_error
)
74 def check_version(version
):
75 if isinstance(version
, str):
76 version_list
= tuple(map(int, version
.split(".")))
78 version_list
= version
80 if version_list
> version_info
:
82 "pygobject's version %s required, and available version "
83 "%s is not recent enough") % (version
, __version__
)
87 def require_version(namespace
, version
):
88 """ Ensures the correct versions are loaded when importing `gi` modules.
90 :param namespace: The name of module to require.
92 :param version: The version of module to require.
94 :raises ValueError: If module/version is already loaded, already required, or unavailable.
98 .. code-block:: python
101 gi.require_version('Gtk', '3.0')
104 repository
= Repository
.get_default()
106 if namespace
in repository
.get_loaded_namespaces():
107 loaded_version
= repository
.get_version(namespace
)
108 if loaded_version
!= version
:
109 raise ValueError('Namespace %s is already loaded with version %s' %
110 (namespace
, loaded_version
))
112 if namespace
in _versions
and _versions
[namespace
] != version
:
113 raise ValueError('Namespace %s already requires version %s' %
114 (namespace
, _versions
[namespace
]))
116 available_versions
= repository
.enumerate_versions(namespace
)
117 if not available_versions
:
118 raise ValueError('Namespace %s not available' % namespace
)
120 if version
not in available_versions
:
121 raise ValueError('Namespace %s not available for version %s' %
122 (namespace
, version
))
124 _versions
[namespace
] = version
127 def require_versions(requires
):
128 """ Utility function for consolidating multiple `gi.require_version()` calls.
130 :param requires: The names and versions of modules to require.
135 .. code-block:: python
138 gi.require_versions({'Gtk': '3.0', 'GLib': '2.0', 'Gio': '2.0'})
140 for module_name
, module_version
in requires
.items():
141 require_version(module_name
, module_version
)
144 def get_required_version(namespace
):
145 return _versions
.get(namespace
, None)
148 def require_foreign(namespace
, symbol
=None):
149 """Ensure the given foreign marshaling module is available and loaded.
151 :param str namespace:
152 Introspection namespace of the foreign module (e.g. "cairo")
154 Optional symbol typename to ensure a converter exists.
155 :type symbol: str or None
160 .. code-block:: python
164 gi.require_foreign('cairo')
168 _gi
.require_foreign(namespace
, symbol
)
169 except Exception as e
:
170 raise ImportError(str(e
))
171 importlib
.import_module('gi.repository', namespace
)