Cheetah.ImportManager module — Cheetah3 - The Python-Powered Template Engine

# Cheetah.ImportManager module

Provides an emulator/replacement for Python’s standard import system.

@@TR: Be warned that Import Hooks are in the deepest, darkest corner of Python’s jungle. If you need to start hacking with this, be prepared to get lost for a while. Also note, this module predates the newstyle import hooks in Python 2.3 http://www.python.org/peps/pep-0302.html.

This is a hacked/documented version of Gordon McMillan’s iu.py. I have:

> - made it a little less terse

> - added docstrings and explanatations

> - standardized the variable naming scheme

> - reorganized the code layout to enhance readability

class Cheetah.ImportManager.BuiltinImportDirector

  Bases: `ImportDirector`

  Directs imports of builtin modules

  getmod(nm)

class Cheetah.ImportManager.DirOwner(path)

  Bases: `Owner`

  getmod(nm, getsuffixes=<function get_suffixes>, loadco=<built-in function loads>, newmod=<class 'module'>)

class Cheetah.ImportManager.FrozenImportDirector

  Bases: `ImportDirector`

  Directs imports of frozen modules

  getmod(nm)

class Cheetah.ImportManager.ImportDirector(path)

  Bases: `Owner`

  ImportDirectors live on the metapath There’s one for builtins, one for frozen modules, and one for sys.path Windows gets one for modules gotten from the Registry Mac would have them for PY_RESOURCE modules etc. A generalization of Owner - their concept of ‘turf’ is broader

class Cheetah.ImportManager.ImportManager

  Bases: `object`

  doimport(nm, parentnm, fqname)
  importHook(name, globals=None, locals=None, fromlist=None, level=-1)

    NOTE: Currently importHook will accept the keyword-argument “level” but it will NOT use it. Details about the “level” keyword argument can be found here: https://docs.python.org/2/library/functions.html#__import__

  install()
  reloadHook(mod)
  setThreaded()

class Cheetah.ImportManager.Owner(path)

  Bases: `object`

  An Owner does imports from a particular piece of turf That is, there’s an Owner for each thing on sys.path There are owners for directories and .pyz files. There could be owners for zip files, or even URLs. A shadowpath (a dictionary mapping the names in sys.path to their owners) is used so that sys.path (or a package’s __path__) is still a bunch of strings.

  getmod(nm)

class Cheetah.ImportManager.PathImportDirector(pathlist=None, importers=None, ownertypes=None)

  Bases: `ImportDirector`

  Directs imports of modules stored on the filesystem.

  getmod(nm)

class Cheetah.ImportManager.RegistryImportDirector

  Bases: `ImportDirector`

  Directs imports of modules stored in the Windows Registry

  getmod(nm)

Cheetah.ImportManager.getDescr(fnm)

Cheetah.ImportManager.getPathExt(fnm)

Cheetah.ImportManager.nameSplit(s)

Cheetah.ImportManager.packageName(s)

Cheetah.ImportManager.pathIsDir(pathname)

  Local replacement for os.path.isdir().