1.1 --- a/classhook.py Sat Nov 13 23:16:42 2004 +0100
1.2 +++ b/classhook.py Sat Nov 13 23:17:20 2004 +0100
1.3 @@ -20,6 +20,12 @@
1.4
1.5 "A class providing support for searching directories for supported files."
1.6
1.7 + """
1.8 + def find_module(self, name, path=None):
1.9 + print "find_module", name, path
1.10 + return ihooks.ModuleLoader.find_module(self, name, path)
1.11 + """
1.12 +
1.13 def find_module_in_dir(self, name, dir):
1.14
1.15 """
1.16 @@ -39,9 +45,28 @@
1.17 path = os.path.join(dir, name)
1.18
1.19 print "Processing name", name, "in", dir, "producing", path
1.20 +
1.21 + if self._find_module_at_path(path):
1.22 + return (None, path, ("", "", PKG_DIRECTORY))
1.23 + else:
1.24 + return None
1.25 +
1.26 + def _find_module_at_path(self, path):
1.27 if os.path.isdir(path):
1.28 +
1.29 + # Look for classes in the directory.
1.30 +
1.31 if len(glob.glob(os.path.join(path, "*" + os.extsep + "class"))) != 0:
1.32 - return (None, path, ("", "", PKG_DIRECTORY))
1.33 + return 1
1.34 +
1.35 + # Otherwise permit importing where directories containing classes exist.
1.36 +
1.37 + for filename in os.listdir(path):
1.38 + pathname = os.path.join(path, filename)
1.39 + result = self._find_module_at_path(pathname)
1.40 + if result is not None:
1.41 + return result
1.42 +
1.43 return None
1.44
1.45 def load_module(self, name, stuff):
1.46 @@ -56,6 +81,7 @@
1.47 # Just go into the directory and find the class files.
1.48
1.49 file, filename, info = stuff
1.50 + print "*", file, filename, info
1.51
1.52 # Prepare a dictionary of globals.
1.53
1.54 @@ -76,11 +102,19 @@
1.55 cls = translator.process(global_names)
1.56 module.__dict__[cls.__name__] = cls
1.57
1.58 + module.__path__ = [filename]
1.59 return module
1.60
1.61 +"""
1.62 class ClassImporter(ihooks.ModuleImporter):
1.63
1.64 + def find_head_package(self, parent, name):
1.65 + print "find_head_package", parent, name
1.66 + return ihooks.ModuleImporter.find_head_package(self, parent, name)
1.67 +
1.68 def import_it(self, partname, fqname, parent, force_load=0):
1.69 + print "import_it", partname, fqname, parent, force_load
1.70 + print "modules", self.modules
1.71 try:
1.72 return parent.__dict__[partname]
1.73
1.74 @@ -88,8 +122,9 @@
1.75 return ihooks.ModuleImporter.import_it(
1.76 self, partname, fqname, parent, force_load
1.77 )
1.78 +"""
1.79
1.80 -importer = ClassImporter(loader=ClassLoader(hooks=ClassHooks()))
1.81 +importer = ihooks.ModuleImporter(loader=ClassLoader(hooks=ClassHooks()))
1.82 importer.install()
1.83
1.84 # vim: tabstop=4 expandtab shiftwidth=4