5.9. yapyutils.modules.loader

Utilities for the search of the location and the loading of modules based on the module name and the file system path.

5.9.1. Module

yapyutils.modules.loader provides module location and load as file.

5.9.2. Functions

5.9.2.1. get_modulelocation

yapyutils.modules.loader.get_modulelocation(mname, mbase=None, mpaths=None, **kargs)[source]

“A very basic function for the detection of the absolute path and the relative module search-path-name for a given path of a module. The values are the same as would be present in sys.modules. Supports source modules as input only.

The platformids is a low-level library within the software stack. The generic functions for the allocation of module sources and binaries are provided by sourceinfo [sourceinfo], which itself depends on the platformids. Thus sourceinfo could not be used in order to avoid circular dependencies. So implemented this function to keep platformids on lowest possible software-stack level only.

Parameters
  • mname

    The relative path of the module in dotted Python notation, without file suffix.

    mname := (
         <dotted-module-name-str>
       | <dotted-module-name-path-name-str>
    )
    

  • mbase

    Base for module search paths, filepath name with a trailing

    separator.

    default := os.path.normpath(os.path.curdir + os.sep + '..' ) + os.sep
    

    The base path is used within the post-processing of the eventually matched path, thus has to be appropriate for all items in mpaths.

    mpaths:

    List of module search paths relative to mbase:

    default := [
       '',
    ]
    

    resulting in:

    default := [
       mbase,
    ]
    
    kargs:
    permitrel:

    Permit the return of relative module names within mpath. If False absolute only, which is actually relative to an existing search path entry in sys.path.

    permitrel := (
        True,       # returns a relative module name if within subtree
        False       # returns in any case a module name relative to sys.path
    )
    

    Sets relavive base to the default:

    rbase = os.path.normpath(os.path.dirname(__file__) + os.sep + '..' + re.sub(r'[.]', os.sep, mname)) + os.sep
    

Returns

Returns in case of a match the resulting entry within sys.modules:

match -> (<relative-module-name>, <module-file-path-name>,)

The default when no match occured is to rely on the more versatile search mechanism of the import implementation of the concrete Python implementation for another final trial by the caller:

default -> (<mname>, None,)

Raises
  • PlatformIDsError – ‘mbase’ does not match ‘mpaths’

  • PlatformIDsPresentError – missing ‘mbase’

  • pass-through

5.9.2.2. load_module

yapyutils.modules.loader.load_module(importname, modfpath)[source]

Loads the specified module by it’s name and file system path. Provides a common interface for all supported platforms and Python implementations: CPyhton, IPython, IronPython, Jython, and PyPy. For the syntaxversions Python2.7 and Python3.

Parameters
  • importname – The import name of the module in dotted path notation. The importname is registered in sys.modules.

  • modfpath – The full file system path name. The modfpath is registered in sys.modules.

Returns

The module object on success, else None.

Raises
  • PlatformIDsError

  • pass-through

5.9.2.3. search_modulelocation

yapyutils.modules.loader.search_modulelocation(mname, mbases=None, mpaths=None, **kargs)[source]

Similar to get_modulelocation(), but searches multiple bases - mbases vs. base. Internally calls get_modulelocation for each base.

Parameters
  • mname – see get_modulelocation()

  • mbases

    A list of bases for the search of relative module paths.

    default := sys.path + os.path.curdir
    

  • mpaths – see get_modulelocation()

  • kargs

    permitrel:

    see get_modulelocation()

Returns

see get_modulelocation()

Raises

see get_modulelocation()

5.9.3. Exceptions

exception yapyutils.modules.loader.YapyUtilsLoaderError[source]

Module load error.