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 # importer.py: dynamic importer for introspected libraries.
8 # This library is free software; you can redistribute it and/or
9 # modify it under the terms of the GNU Lesser General Public
10 # License as published by the Free Software Foundation; either
11 # version 2.1 of the License, or (at your option) any later version.
13 # This library is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 # Lesser General Public License for more details.
18 # You should have received a copy of the GNU Lesser General Public
19 # License along with this library; if not, write to the Free Software
20 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
23 from __future__ import absolute_import
29 from ._gi import Repository, RepositoryError
30 from .module import DynamicModule, DynamicGObjectModule, ModuleProxy
33 repository = Repository.get_default()
37 class DynamicImporter(object):
39 # Note: see PEP302 for the Importer Protocol implemented below.
41 def __init__(self, path):
44 def find_module(self, fullname, path=None):
45 if not fullname.startswith(self.path):
48 path, namespace = fullname.rsplit('.', 1)
52 repository.require(namespace)
53 except RepositoryError, e:
58 def load_module(self, fullname):
59 if fullname in sys.modules:
60 return sys.modules[fullname]
62 path, namespace = fullname.rsplit('.', 1)
64 # Workaround for GObject
65 if namespace == 'GObject':
66 sys.modules[fullname] = DynamicGObjectModule()
67 return sys.modules[fullname]
69 dynamic_module = DynamicModule(namespace)
70 modules[namespace] = dynamic_module
72 overrides_modules = __import__('gi.overrides', fromlist=[namespace])
73 overrides_module = getattr(overrides_modules, namespace, None)
75 if overrides_module is not None:
76 module = ModuleProxy(fullname, namespace, dynamic_module, overrides_module)
78 module = dynamic_module
80 module.__file__ = '<%s>' % fullname
81 module.__loader__ = self
83 sys.modules[fullname] = module