javaclass

classhook.py

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