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
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 if not repository.enumerate_versions(namespace):
53 logging.error('Could not find any typelib for %s', namespace)
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 dynamic_module.__file__ = '<%s>' % fullname
73 dynamic_module.__loader__ = self
75 sys.modules[fullname] = dynamic_module
76 dynamic_module._load()