Imported Upstream version 3.3.1
[platform/upstream/pygobject2.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 from ._gi import _API, Repository
24
25 # Force loading the GObject typelib so we have available the wrappers for
26 # base classes such as GInitiallyUnowned
27 import gi._gobject
28 gi  # pyflakes
29
30 _API = _API  # pyflakes
31
32 import os
33
34 _versions = {}
35 _overridesdir = os.path.join(os.path.dirname(__file__), 'overrides')
36
37
38 def require_version(namespace, version):
39     repository = Repository.get_default()
40
41     if namespace in repository.get_loaded_namespaces():
42         loaded_version = repository.get_version(namespace)
43         if loaded_version != version:
44             raise ValueError('Namespace %s is already loaded with version %s' % \
45                              (namespace, loaded_version))
46
47     if namespace in _versions and _versions[namespace] != version:
48         raise ValueError('Namespace %s already requires version %s' % \
49                          (namespace, _versions[namespace]))
50
51     available_versions = repository.enumerate_versions(namespace)
52     if not available_versions:
53         raise ValueError('Namespace %s not available' % namespace)
54
55     if version not in available_versions:
56         raise ValueError('Namespace %s not available for version %s' % \
57                          (namespace, version))
58
59     _versions[namespace] = version
60
61
62 def get_required_version(namespace):
63     return _versions.get(namespace, None)