Package pyamf :: Package util :: Module imports
[hide private]
[frames] | no frames]

Module imports

source code

Tools for doing dynamic imports

This module has been borrowed from the Importing package.


See Also:
http://pypi.python.org/pypi/Importing, http://peak.telecommunity.com/DevCenter/Importing

Original author: Phillip J. Eby

Since: 0.3.0

Classes [hide private]
  SubModuleLoadHook
  AlreadyRead
  LazyModule
Functions [hide private]
 
find_module(subname, path=None) source code
 
_loadModule(module) source code
 
joinPath(modname, relativePath)
Adjust a module name by a '/'-separated, relative or absolute path
source code
 
lazyModule(modname, relativePath=None)
Return module 'modname', but with its contents loaded "on demand"
source code
 
_isLazy(module)
Checks to see if the supplied module is lazy
source code
 
_loadAndRunHooks(module)
Load an unactivated "lazy" module object
source code
 
getModuleHooks(moduleName)
Get list of hooks for 'moduleName'; error if module already loaded
source code
 
_setModuleHook(moduleName, hook) source code
 
whenImported(moduleName, hook)
Call 'hook(module)' when module named 'moduleName' is first used
source code
Variables [hide private]
  postLoadHooks = {'decimal': None, 'django': None, 'django.db':...
  loadedModules = [<module 'decimal' from '/Library/Frameworks/P...

Imports: sys, os, ModuleType


Function Details [hide private]

lazyModule(modname, relativePath=None)

source code 

Return module 'modname', but with its contents loaded "on demand"

This function returns 'sys.modules[modname]', if present. Otherwise it creates a 'LazyModule' object for the specified module, caches it in 'sys.modules', and returns it.

'LazyModule' is a subclass of the standard Python module type, that remains empty until an attempt is made to access one of its attributes. At that moment, the module is loaded into memory, and any hooks that were defined via 'whenImported()' are invoked.

Note that calling 'lazyModule' with the name of a non-existent or unimportable module will delay the 'ImportError' until the moment access is attempted. The 'ImportError' will occur every time an attribute access is attempted, until the problem is corrected.

This function also takes an optional second parameter, 'relativePath', which will be interpreted as a '/'-separated path string relative to 'modname'. If a 'relativePath' is supplied, the module found by traversing the path will be loaded instead of 'modname'. In the path, '.' refers to the current module, and '..' to the current module's parent. For example:

   fooBaz = lazyModule('foo.bar','../baz')

will return the module 'foo.baz'. The main use of the 'relativePath' feature is to allow relative imports in modules that are intended for use with module inheritance. Where an absolute import would be carried over as-is into the inheriting module, an import relative to '__name__' will be relative to the inheriting module, e.g.:

   something = lazyModule(__name__,'../path/to/something')

The above code will have different results in each module that inherits it.

(Note: 'relativePath' can also be an absolute path (starting with '/'); this is mainly useful for module '__bases__' lists.)

whenImported(moduleName, hook)

source code 

Call 'hook(module)' when module named 'moduleName' is first used

'hook' must accept one argument: the module object named by 'moduleName', which must be a fully qualified (i.e. absolute) module name. The hook should not raise any exceptions, or it may prevent later hooks from running.

If the module has already been imported normally, 'hook(module)' is called immediately, and the module object is returned from this function. If the module has not been imported, or has only been imported lazily, then the hook is called when the module is first used, and a lazy import of the module is returned from this function. If the module was imported lazily and used before calling this function, the hook is called immediately, and the loaded module is returned from this function.

Note that using this function implies a possible lazy import of the specified module, and lazy importing means that any 'ImportError' will be deferred until the module is used.


Variables Details [hide private]

postLoadHooks

Value:
{}

loadedModules

Value:
[<module 'decimal' from '/Library/Frameworks/Python.framework/Versions\
/2.5/lib/python2.5/decimal.pyc'>,
 <module 'sqlalchemy' from '/Library/Frameworks/Python.framework/Versi\
ons/2.5/lib/python2.5/site-packages/SQLAlchemy-0.5.2-py2.5.egg/sqlalch\
emy/__init__.pyc'>,
 <module 'sets' from '/Library/Frameworks/Python.framework/Versions/2.\
5/lib/python2.5/sets.pyc'>,
 <module 'django' from '/Library/Frameworks/Python.framework/Versions/\
...