1 #!/usr/bin/env python 2 3 import ihooks 4 import os, glob 5 from imp import PY_SOURCE, PKG_DIRECTORY, C_BUILTIN 6 import classfile, bytecode 7 import new 8 9 class ClassHooks(ihooks.Hooks): 10 11 "A filesystem hooks class providing information about supported files." 12 13 def get_suffixes(self): 14 15 "Return the recognised suffixes." 16 17 return ihooks.Hooks.get_suffixes(self) + [(os.extsep + "class", "r", PY_SOURCE)] 18 19 class ClassLoader(ihooks.ModuleLoader): 20 21 "A class providing support for searching directories for supported files." 22 23 """ 24 def find_module(self, name, path=None): 25 print "find_module", name, path 26 return ihooks.ModuleLoader.find_module(self, name, path) 27 """ 28 29 def find_module_in_dir(self, name, dir): 30 31 """ 32 Find the module with the given 'name' in the given directory 'dir'. 33 Since Java packages/modules are directories containing class files, 34 return the required information tuple only when the path constructed 35 from 'dir' and 'name' refers to a directory containing class files. 36 """ 37 38 dir = dir or "." 39 40 # Provide a special name for the current directory. 41 42 if name == "__this__": 43 path = dir 44 else: 45 path = os.path.join(dir, name) 46 47 print "Processing name", name, "in", dir, "producing", path 48 49 if self._find_module_at_path(path): 50 return (None, path, ("", "", PKG_DIRECTORY)) 51 else: 52 return None 53 54 def _find_module_at_path(self, path): 55 if os.path.isdir(path): 56 57 # Look for classes in the directory. 58 59 if len(glob.glob(os.path.join(path, "*" + os.extsep + "class"))) != 0: 60 return 1 61 62 # Otherwise permit importing where directories containing classes exist. 63 64 for filename in os.listdir(path): 65 pathname = os.path.join(path, filename) 66 result = self._find_module_at_path(pathname) 67 if result is not None: 68 return result 69 70 return None 71 72 def load_module(self, name, stuff): 73 74 """ 75 Load the module with the given 'name', whose 'stuff' which describes the 76 location of the module is a tuple of the form (file, filename, (suffix, 77 mode, data type)). Return a module object or raise an ImportError if a 78 problem occurred in the import operation. 79 """ 80 81 # Just go into the directory and find the class files. 82 83 file, filename, info = stuff 84 print "*", file, filename, info 85 86 # Prepare a dictionary of globals. 87 88 global_names = {} 89 global_names.update(__builtins__.__dict__) 90 module = self.hooks.add_module(name) 91 92 # Process each class file, producing a genuine Python class. 93 94 class_files = [] 95 for class_filename in glob.glob(os.path.join(filename, "*" + os.extsep + "class")): 96 print "Importing class", class_filename 97 f = open(class_filename, "rb") 98 s = f.read() 99 f.close() 100 class_file = classfile.ClassFile(s) 101 translator = bytecode.ClassTranslator(class_file) 102 cls = translator.process(global_names) 103 module.__dict__[cls.__name__] = cls 104 105 module.__path__ = [filename] 106 return module 107 108 """ 109 class ClassImporter(ihooks.ModuleImporter): 110 111 def find_head_package(self, parent, name): 112 print "find_head_package", parent, name 113 return ihooks.ModuleImporter.find_head_package(self, parent, name) 114 115 def import_it(self, partname, fqname, parent, force_load=0): 116 print "import_it", partname, fqname, parent, force_load 117 print "modules", self.modules 118 try: 119 return parent.__dict__[partname] 120 121 except (KeyError, AttributeError): 122 return ihooks.ModuleImporter.import_it( 123 self, partname, fqname, parent, force_load 124 ) 125 """ 126 127 importer = ihooks.ModuleImporter(loader=ClassLoader(hooks=ClassHooks())) 128 importer.install() 129 130 # vim: tabstop=4 expandtab shiftwidth=4