authnzerver.modtools module

This contains functions to dynamically import modules and get Python objects.

authnzerver.modtools.module_from_string(module, force_reload=False)[source]

This imports the module specified.

Used to dynamically import Python modules.

Parameters:
  • module (str) –

    This is either:

    • a Python module import path, e.g. ‘concurrent.futures’ or
    • a path to a Python file, e.g. ‘~/authnzerver/authnzerver/main.py’
  • force_reload (bool) – If True, will reload a previous imported module even if it’s been previously imported. This is useful to pick up changes in Python module files used as program config files.
Returns:

This returns a Python module if it’s able to successfully import it.

Return type:

Python module

Notes

Hypens are not allowed in module filenames.

authnzerver.modtools.object_from_string(objectpath, force_reload=False)[source]

This returns a Python object pointed to by the given string.

An object can be any valid Python object. One of the main uses for this function is to dynamically load Python functions from a module given its file path on disk or a fully qualified module string.

The string should be in one of the forms below:

  • fully qualified module name and object name, e.g.:

    'authnzerver.confvars.CONF' -> gets the CONF dict
    'sqlalchemy.dialects.postgresql.JSONB' -> gets the JSONB class
    'scipy.ndimage.convolve' -> gets the convolve() function
    
  • path to a module on disk and the object name separated by ‘::’, e.g.:

    '~/authnzerver/authnzerver/actions/user.py::create_new_user'
    '~/authzerver/authnzerver/authdb.py::Users'
    

    (This is similar to the format used by pytest.)