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
27 from ._gi import Repository, RepositoryError
28 from .module import DynamicModule, DynamicGObjectModule, DynamicGLibModule
31 repository = Repository.get_default()
35 class DynamicImporter(object):
37 # Note: see PEP302 for the Importer Protocol implemented below.
39 def __init__(self, path):
42 def find_module(self, fullname, path=None):
43 if not fullname.startswith(self.path):
46 path, namespace = fullname.rsplit('.', 1)
50 if not repository.enumerate_versions(namespace):
51 logging.error('Could not find any typelib for %s', namespace)
56 def load_module(self, fullname):
57 if fullname in sys.modules:
58 return sys.modules[fullname]
60 path, namespace = fullname.rsplit('.', 1)
62 # Special case GObject and GLib
63 if namespace == 'GObject':
64 dynamic_module = DynamicGObjectModule()
65 elif namespace == "GLib":
66 dynamic_module = DynamicGLibModule()
68 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()