Branch and push for 2.0
[profile/ivi/pygobject2.git] / pygtk.py
1 # -*- Mode: Python; py-indent-offset: 4 -*-
2 # pygtk - Python bindings for the GTK+ widget set.
3 # Copyright (C) 1998-2002  James Henstridge
4 #
5 #   pygtk.py: pygtk version selection code.
6 #
7 # This library is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU Lesser General Public License as published by
9 # the Free Software Foundation; either version 2.1 of the License, or
10 # (at your option) any later version.
11 #
12 # This library is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 # GNU Lesser General Public License for more details.
16 #
17 # You should have received a copy of the GNU Lesser General Public License
18 # along with this library; if not, write to the Free Software
19 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
20 # USA
21
22 import fnmatch
23 import glob
24 import os
25 import os.path
26 import sys
27
28 __all__ = ['require']
29
30 _our_dir = os.path.dirname(os.path.abspath(os.path.normpath(__file__)))
31 _pygtk_2_0_dir = os.path.normpath('%s/gtk-2.0' % _our_dir)
32
33 _pygtk_dir_pat = 'gtk-[0-9].[0-9]'
34
35 _pygtk_required_version = None
36
37 def _get_available_versions():
38     versions = {}
39     for dir in sys.path:
40         if not dir: 
41             dir = os.getcwd()
42             
43         if not os.path.isdir(dir):
44             continue
45         
46         # if the dir is a pygtk dir, skip it
47         if fnmatch.fnmatchcase(os.path.basename(dir), _pygtk_dir_pat):
48             continue  
49         
50         for filename in glob.glob(os.path.join(dir, _pygtk_dir_pat)):
51             pathname = os.path.join(dir, filename)
52
53             # skip non directories
54             if not os.path.isdir(pathname):
55                 continue
56             
57             # skip empty directories
58             if not os.listdir(pathname):
59                 continue
60             
61             if not versions.has_key(filename[-3:]):
62                 versions[filename[-3:]] = pathname
63     return versions
64
65 def require20():
66     if _pygtk_2_0_dir not in sys.path:
67         sys.path.insert(0, _pygtk_2_0_dir)
68
69 def require(version):
70     if version == '2.0':
71         return require20()
72     
73     global _pygtk_required_version
74
75     if _pygtk_required_version != None:
76         assert _pygtk_required_version == version, \
77                "a different version of gtk was already required"
78         return
79
80     assert not sys.modules.has_key('gtk'), \
81            "pygtk.require() must be called before importing gtk"
82
83     versions = _get_available_versions()
84     assert versions.has_key(version), \
85            "required version '%s' not found on system" % version
86
87     # remove any pygtk dirs first ...
88     for dir in sys.path:
89         if fnmatch.fnmatchcase(os.path.basename(dir), _pygtk_dir_pat):
90             sys.path.remove(dir)
91
92     # prepend the pygtk path ...
93     sys.path.insert(0, versions[version])
94     
95     _pygtk_required_version = version