javaclass

Change of classhook.py

39:e64b08b9681f
classhook.py
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/classhook.py	Sat Nov 13 19:19:46 2004 +0100
     1.3 @@ -0,0 +1,95 @@
     1.4 +#!/usr/bin/env python
     1.5 +
     1.6 +import ihooks
     1.7 +import os, glob
     1.8 +from imp import PY_SOURCE, PKG_DIRECTORY, C_BUILTIN
     1.9 +import classfile, bytecode
    1.10 +import new
    1.11 +
    1.12 +class ClassHooks(ihooks.Hooks):
    1.13 +
    1.14 +    "A filesystem hooks class providing information about supported files."
    1.15 +
    1.16 +    def get_suffixes(self):
    1.17 +
    1.18 +        "Return the recognised suffixes."
    1.19 +
    1.20 +        return ihooks.Hooks.get_suffixes(self) + [(os.extsep + "class", "r", PY_SOURCE)]
    1.21 +
    1.22 +class ClassLoader(ihooks.ModuleLoader):
    1.23 +
    1.24 +    "A class providing support for searching directories for supported files."
    1.25 +
    1.26 +    def find_module_in_dir(self, name, dir):
    1.27 +
    1.28 +        """
    1.29 +        Find the module with the given 'name' in the given directory 'dir'.
    1.30 +        Since Java packages/modules are directories containing class files,
    1.31 +        return the required information tuple only when the path constructed
    1.32 +        from 'dir' and 'name' refers to a directory containing class files.
    1.33 +        """
    1.34 +
    1.35 +        dir = dir or ""
    1.36 +
    1.37 +        # Provide a special name for the current directory.
    1.38 +
    1.39 +        if name == "__this__":
    1.40 +            path = dir
    1.41 +        else:
    1.42 +            path = os.path.join(dir, name)
    1.43 +
    1.44 +        print "*", name, dir, path
    1.45 +        if os.path.isdir(path):
    1.46 +            if len(glob.glob(os.path.join(path, "*" + os.extsep + "class"))) != 0:
    1.47 +                return (None, path, ("", "", PKG_DIRECTORY))
    1.48 +        return None
    1.49 +
    1.50 +    def load_module(self, name, stuff):
    1.51 +
    1.52 +        """
    1.53 +        Load the module with the given 'name', whose 'stuff' which describes the
    1.54 +        location of the module is a tuple of the form (file, filename, (suffix,
    1.55 +        mode, data type)). Return a module object or raise an ImportError if a
    1.56 +        problem occurred in the import operation.
    1.57 +        """
    1.58 +
    1.59 +        # Just go into the directory and find the class files.
    1.60 +
    1.61 +        file, filename, info = stuff
    1.62 +
    1.63 +        # Prepare a dictionary of globals.
    1.64 +
    1.65 +        global_names = {}
    1.66 +        global_names.update(__builtins__.__dict__)
    1.67 +        module = new.module(name)
    1.68 +
    1.69 +        # Process each class file, producing a genuine Python class.
    1.70 +
    1.71 +        class_files = []
    1.72 +        for class_filename in glob.glob(os.path.join(filename, "*" + os.extsep + "class")):
    1.73 +            print "*", class_filename
    1.74 +            f = open(class_filename, "rb")
    1.75 +            s = f.read()
    1.76 +            f.close()
    1.77 +            class_file = classfile.ClassFile(s)
    1.78 +            translator = bytecode.ClassTranslator(class_file)
    1.79 +            cls = translator.process(global_names)
    1.80 +            module.__dict__[cls.__name__] = cls
    1.81 +
    1.82 +        return module
    1.83 +
    1.84 +class ClassImporter(ihooks.ModuleImporter):
    1.85 +
    1.86 +    def import_it(self, partname, fqname, parent, force_load=0):
    1.87 +        try:
    1.88 +            return parent.__dict__[partname]
    1.89 +
    1.90 +        except (KeyError, AttributeError):
    1.91 +            return ihooks.ModuleImporter.import_it(
    1.92 +                self, partname, fqname, parent, force_load
    1.93 +                )
    1.94 +
    1.95 +importer = ClassImporter(loader=ClassLoader(hooks=ClassHooks()))
    1.96 +importer.install()
    1.97 +
    1.98 +# vim: tabstop=4 expandtab shiftwidth=4