[CALLBACK RETURN] Callback returns may not always be formated the same.
[gnome.gobject-introspection] / giscanner / libtoolimporter.py
1 # -*- Mode: Python -*-
2 # GObject-Introspection - a framework for introspecting GObject libraries
3 # Copyright (C) 2008  Johan Dahlin
4 #
5 # This library is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU Lesser General Public
7 # License as published by the Free Software Foundation; either
8 # version 2 of the License, or (at your option) any later version.
9 #
10 # This library is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 # Lesser General Public License for more details.
14 #
15 # You should have received a copy of the GNU Lesser General Public
16 # License along with this library; if not, write to the
17 # Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 # Boston, MA 02111-1307, USA.
19 #
20
21 import imp
22 import os
23 import sys
24
25 from .utils import extract_libtool
26
27
28 class LibtoolImporter(object):
29
30     def __init__(self, name, path):
31         self.name = name
32         self.path = path
33
34     @classmethod
35     def find_module(cls, name, packagepath=None):
36         modparts = name.split('.')
37         filename = modparts.pop() + '.la'
38
39         # Given some.package.module 'path' is where subpackages of some.package
40         # should be looked for. See if we can find a ".libs/module.la" relative
41         # to those directories and failing that look for file
42         # "some/package/.libs/module.la" relative to sys.path
43         module = os.path.join(*modparts)
44
45         for path in sys.path:
46             full = os.path.join(path, module, '.libs', filename)
47             if os.path.exists(full):
48                 return cls(name, full)
49
50     def load_module(self, name):
51         realpath = extract_libtool(self.path)
52         mod = imp.load_module(name, open(realpath), realpath,
53                               ('.so', 'rb', 3))
54         mod.__loader__ = self
55         return mod
56
57     @classmethod
58     def __enter__(cls):
59         sys.meta_path.append(cls)
60
61     @classmethod
62     def __exit__(cls, type, value, traceback):
63         sys.meta_path.remove(cls)