def _test_dir(): "Debug/test function to create DirectoryImporters from sys.path." imputil.ImportManager().install() path = sys.path[:] path.reverse() for d in path: sys.path.insert(0, DirectoryImporter(d)) sys.path.insert(0, imputil.BuiltinImporter())
#
# Configure. # ========== importer = NetworkImporter() if 'NETIMP_CACHEDIR' in os.environ: importer.cachedir = os.environ['NETIMP_CACHEDIR'] if 'NETIMP_PATH' in os.environ: importer.path = list(os.environ['NETIMP_PATH'].split()) importer.verbose = 'NETIMP_VERBOSE' in os.environ # Install. # ======== imputil.ImportManager().install() sys.path.append(importer) # Test. # ===== def test(): # module: foo.py import foo assert foo.made_it == 'BLAM!!!!!!!!!!' try: # most likely to fail # extension: _foo.so import _foo
def _test_revamp(): "Debug/test function for the revamped import system." imputil.ImportManager().install() sys.path.insert(0, PathImporter()) sys.path.insert(0, imputil.BuiltinImporter())
return (retvals[0], self.callback(retvals[1]), retvals[2]) return retvals def get_code(self, parent, modname, fqname): if parent: # we are looking for a module inside of a specific package return self.rewrite(_fs_import(parent.__pkgdir__, modname, fqname)) # scan sys.path, looking for the requested module for dir in self.path: if isinstance(dir, str): result = _fs_import(dir, modname, fqname) if result: return self.rewrite(result) # not found return None import_manager = imputil.ImportManager() def install(callback): "Install callback as a code-rewriting function for each imported module." import_manager.install() sys.path.insert(0, PathImporter(sys.path, callback)) def uninstall(): import_manager.uninstall()