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
26 from ._gi import Repository
27 from .module import DynamicModule
30 repository = Repository.get_default()
34 class DynamicImporter(object):
36 # Note: see PEP302 for the Importer Protocol implemented below.
38 def __init__(self, path):
41 def find_module(self, fullname, path=None):
42 if not fullname.startswith(self.path):
45 path, namespace = fullname.rsplit('.', 1)
49 if repository.enumerate_versions(namespace):
52 raise ImportError('cannot import name %s, '
53 'introspection typelib not found' % namespace)
55 def load_module(self, fullname):
56 if fullname in sys.modules:
57 return sys.modules[fullname]
59 path, namespace = fullname.rsplit('.', 1)
60 dynamic_module = DynamicModule(namespace)
61 modules[namespace] = dynamic_module
63 dynamic_module.__file__ = '<%s>' % fullname
64 dynamic_module.__loader__ = self
66 sys.modules[fullname] = dynamic_module
67 dynamic_module._load()