Imported Upstream version 3.7.3
[platform/upstream/python-gobject.git] / gi / __init__.py
1 # -*- Mode: Python; py-indent-offset: 4 -*-
2 # vim: tabstop=4 shiftwidth=4 expandtab
3 #
4 # Copyright (C) 2005-2009 Johan Dahlin <johan@gnome.org>
5 #
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.
10 #
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.
15 #
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
19 # USA
20
21 from __future__ import absolute_import
22
23 # support overrides in different directories than our gi module
24 from pkgutil import extend_path
25 __path__ = extend_path(__path__, __name__)
26
27 from ._gi import _API, Repository
28
29 # Force loading the GObject typelib so we have available the wrappers for
30 # base classes such as GInitiallyUnowned
31 import gi._gobject
32 gi  # pyflakes
33
34 _API = _API  # pyflakes
35
36 import os
37
38 _versions = {}
39 _overridesdir = os.path.join(os.path.dirname(__file__), 'overrides')
40
41 version_info = gi._gobject.pygobject_version[:]
42 __version__ = "{0}.{1}.{2}".format(*version_info)
43
44
45 def check_version(version):
46     if isinstance(version, str):
47         version_list = tuple(map(int, version.split(".")))
48     else:
49         version_list = version
50
51     if version_list > version_info:
52         raise ValueError((
53             "pygobject's version %s required, and available version "
54             "%s is not recent enough") % (version, __version__)
55         )
56
57
58 def require_version(namespace, version):
59     repository = Repository.get_default()
60
61     if namespace in repository.get_loaded_namespaces():
62         loaded_version = repository.get_version(namespace)
63         if loaded_version != version:
64             raise ValueError('Namespace %s is already loaded with version %s' %
65                              (namespace, loaded_version))
66
67     if namespace in _versions and _versions[namespace] != version:
68         raise ValueError('Namespace %s already requires version %s' %
69                          (namespace, _versions[namespace]))
70
71     available_versions = repository.enumerate_versions(namespace)
72     if not available_versions:
73         raise ValueError('Namespace %s not available' % namespace)
74
75     if version not in available_versions:
76         raise ValueError('Namespace %s not available for version %s' %
77                          (namespace, version))
78
79     _versions[namespace] = version
80
81
82 def get_required_version(namespace):
83     return _versions.get(namespace, None)
84
85
86 # note, DeprecationWarning would be more suitable as a base, but this
87 # unhelpfully isn't shown by default and thus useless
88 class PyGIDeprecationWarning(RuntimeWarning):
89     pass