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