[Python-checkins] r56970 - sandbox/trunk/import_in_py/_importlib.py
brett.cannon
python-checkins at python.org
Mon Aug 13 02:30:14 CEST 2007
Author: brett.cannon
Date: Mon Aug 13 02:30:14 2007
New Revision: 56970
Modified:
sandbox/trunk/import_in_py/_importlib.py
Log:
An untested, initial pass at making file system importers more modular.
Modified: sandbox/trunk/import_in_py/_importlib.py
==============================================================================
--- sandbox/trunk/import_in_py/_importlib.py (original)
+++ sandbox/trunk/import_in_py/_importlib.py Mon Aug 13 02:30:14 2007
@@ -239,6 +239,80 @@
raise ImportError("can only handle directories")
+class FileImporter(object):
+
+ """Base class for file importers.
+
+ Subclasses are expected to define the following attributes:
+
+ * _file_types
+ Sequence of imp file types (e.g., imp.C_EXTENSION).
+ * _possible_package
+ True if importer should check for packages.
+ * _loader
+ A callable that takes the found file path to the requested module and
+ returns a loader for the module found at that path.
+
+ """
+
+ def __init__(self, path_entry):
+ """Initialize an importer for the passed-in sys.path entry (which is
+ assumed to have already been verified as an existing directory).
+
+ Can be used as an entry on sys.path_hook.
+
+ """
+ self._path_entry = path_entry
+ # XXX Worth pre-calculating in a metaclass?
+ self._suffixes = [suffix[0] for suffix in imp.get_suffixes()
+ if suffix[2] in self._file_types]
+
+ def find_module(self, fullname, path=None):
+ tail_module = name.rsplit('.', 1)[-1]
+ # XXX Do we need to check for packages first with all possible file
+ # extensions and then check for files?
+ for ext in self._suffixes:
+ if self._possible_package:
+ package_directory = _path_join(self._path_entry, tail_module)
+ init_filename = '__init__' + ext
+ package_init = _path_join(package_directory, init_filename)
+ if (_path_isfile(package_init) and
+ _case_ok(self._path_entry, tail_module) and
+ _case_ok(package_directory, init_filename)):
+ return self._loader(package_init)
+ file_name = tail_module + ext
+ file_path = _path_join(self._path_entry, file_name)
+ if (_path_isfile(file_path) and
+ _case_ok(self._path_entry, file_name)):
+ return self._loader(file_path)
+ else:
+ # Raise a warning if it matches a directory w/o an __init__ file.
+ if (_path_isdir(package_directory) and
+ _case_ok(self._path_entry, tail_module)):
+ warnings.warn("Not importing directory %s: missing __init__"
+ % package_directory,
+ ImportWarning)
+ return None
+
+
+class ExtensionFileImporter(FileImporter):
+
+ """Importer for extension files."""
+
+ _file_types = [imp.C_EXTENSION]
+ _possible_package = False
+ _loader = ExtensionFileLoader
+
+
+class PyFileImporter(FileImporter):
+
+ """Importer for source/bytecode files."""
+
+ _file_types = [imp.PY_SOURCE, imp.PY_COMPILED]
+ _possible_package = True
+ _loader = PyFileLoader
+
+
class FileSystemImporter(object):
"""Importer for the filesystem using the passed-in handlers."""
More information about the Python-checkins
mailing list