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@39 | 23 | def find_module_in_dir(self, name, dir): |
paul@39 | 24 | |
paul@39 | 25 | """ |
paul@39 | 26 | Find the module with the given 'name' in the given directory 'dir'. |
paul@39 | 27 | Since Java packages/modules are directories containing class files, |
paul@39 | 28 | return the required information tuple only when the path constructed |
paul@39 | 29 | from 'dir' and 'name' refers to a directory containing class files. |
paul@39 | 30 | """ |
paul@39 | 31 | |
paul@41 | 32 | dir = dir or "." |
paul@39 | 33 | |
paul@39 | 34 | # Provide a special name for the current directory. |
paul@39 | 35 | |
paul@39 | 36 | if name == "__this__": |
paul@39 | 37 | path = dir |
paul@39 | 38 | else: |
paul@39 | 39 | path = os.path.join(dir, name) |
paul@39 | 40 | |
paul@41 | 41 | print "Processing name", name, "in", dir, "producing", path |
paul@39 | 42 | if os.path.isdir(path): |
paul@39 | 43 | if len(glob.glob(os.path.join(path, "*" + os.extsep + "class"))) != 0: |
paul@39 | 44 | return (None, path, ("", "", PKG_DIRECTORY)) |
paul@39 | 45 | return None |
paul@39 | 46 | |
paul@39 | 47 | def load_module(self, name, stuff): |
paul@39 | 48 | |
paul@39 | 49 | """ |
paul@39 | 50 | Load the module with the given 'name', whose 'stuff' which describes the |
paul@39 | 51 | location of the module is a tuple of the form (file, filename, (suffix, |
paul@39 | 52 | mode, data type)). Return a module object or raise an ImportError if a |
paul@39 | 53 | problem occurred in the import operation. |
paul@39 | 54 | """ |
paul@39 | 55 | |
paul@39 | 56 | # Just go into the directory and find the class files. |
paul@39 | 57 | |
paul@39 | 58 | file, filename, info = stuff |
paul@39 | 59 | |
paul@39 | 60 | # Prepare a dictionary of globals. |
paul@39 | 61 | |
paul@39 | 62 | global_names = {} |
paul@39 | 63 | global_names.update(__builtins__.__dict__) |
paul@41 | 64 | module = self.hooks.add_module(name) |
paul@39 | 65 | |
paul@39 | 66 | # Process each class file, producing a genuine Python class. |
paul@39 | 67 | |
paul@39 | 68 | class_files = [] |
paul@39 | 69 | for class_filename in glob.glob(os.path.join(filename, "*" + os.extsep + "class")): |
paul@41 | 70 | print "Importing class", class_filename |
paul@39 | 71 | f = open(class_filename, "rb") |
paul@39 | 72 | s = f.read() |
paul@39 | 73 | f.close() |
paul@39 | 74 | class_file = classfile.ClassFile(s) |
paul@39 | 75 | translator = bytecode.ClassTranslator(class_file) |
paul@39 | 76 | cls = translator.process(global_names) |
paul@39 | 77 | module.__dict__[cls.__name__] = cls |
paul@39 | 78 | |
paul@39 | 79 | return module |
paul@39 | 80 | |
paul@39 | 81 | class ClassImporter(ihooks.ModuleImporter): |
paul@39 | 82 | |
paul@39 | 83 | def import_it(self, partname, fqname, parent, force_load=0): |
paul@39 | 84 | try: |
paul@39 | 85 | return parent.__dict__[partname] |
paul@39 | 86 | |
paul@39 | 87 | except (KeyError, AttributeError): |
paul@39 | 88 | return ihooks.ModuleImporter.import_it( |
paul@39 | 89 | self, partname, fqname, parent, force_load |
paul@39 | 90 | ) |
paul@39 | 91 | |
paul@39 | 92 | importer = ClassImporter(loader=ClassLoader(hooks=ClassHooks())) |
paul@39 | 93 | importer.install() |
paul@39 | 94 | |
paul@39 | 95 | # vim: tabstop=4 expandtab shiftwidth=4 |