include.base.import_hook module

class include.base.import_hook.BaseIncludeLoader(module_prefix)

Bases: object

Import hook to load Python modules from an arbitrary location

Parameters:module_prefix (str) – prefix for modules to import

Base class for import hooks to non-standard code sources. Implements the general structure for encoded sources: a module source translates to an artificial module path of the form <module_prefix>.<encoded_name>. The module_prefix identifies the code source type (and import hook) while the encoded_name contains all required information to retrieve the code.

For example, a module_prefix of include.type.files could identify a source file type, and an encoded_name of SLASHtmpSLASHfooDOTpy point to the path /tmp/foo.py. The resulting module would appear as include.type.files.SLASHtmpSLASHfooDOTpy.

Note that module_prefix must point to a valid package, not a module. It will be actually imported by the regular import machinery, and can be used to bootstrap hooks.

The encoded_name is a free form field. The base class provides means to escape invalid and reserved symbols (/ and .), but subclasses are free to use them if it is suitable for them. Hooks should use encoded_name to store a URI (or similar) to retrieve source code. As per Python rules, including a dot (.) in the encoded_name requires the hook to import each portion separately.

find_module(fullname, path=None)

Find the appropriate loader for module name

Parameters:
  • fullname (str) – __name__ of the module to import
  • path (str or None) – __path__ of the parent package already imported
load_module(name)

Load and return a module

Always returns the corresponding module. If the module is already loaded, the existing module is returned.

module2uri(module_name)

Convert an encoded module name to an unencoded source uri

module_prefix
uri2module(uri)

Convert an unencoded source uri to an encoded module name