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