Importing utilities.

class mode.utils.imports.FactoryMapping(*args: Mapping, **kwargs: str)

Class plugin system.

This is an utility to maintain a mapping from name to fully qualified Python attribute path, and also supporting the use of these in URLs.


>>> # Specifying the type enables mypy to know that
>>> # this factory returns Driver subclasses.
>>> drivers: FactoryMapping[Type[Driver]]
>>> drivers = FactoryMapping({
...    'rabbitmq': 'my.drivers.rabbitmq:Driver',
...    'kafka': 'my.drivers.kafka:Driver',
...    'redis': 'my.drivers.redis:Driver',
... })
>>> drivers.by_url('rabbitmq://localhost:9090')
<class 'my.drivers.rabbitmq.Driver'>
>>> drivers.by_name('redis')
<class 'my.drivers.redis.Driver'>
aliases: MutableMapping[str, str] = None
namespaces: Set = None
iterate() → Iterator[_T]
by_url(url: Union[str, mode.utils.imports.URL]) → _T

Get class associated with URL (scheme is used as alias key).

by_name(name: Union[_T, str]) → _T
get_alias(name: str) → str
include_setuptools_namespace(namespace: str) → None
mode.utils.imports.symbol_by_name(name: Union[_T, str], aliases: Mapping[str, str] = None, imp: Any = None, package: str = None, sep: str = '.', default: _T = None, **kwargs: Any) → _T

Get symbol by qualified name.

The name should be the full dot-separated path to the class:



                        ^- class name

or using ‘:’ to separate module and symbol:


If aliases is provided, a dict containing short name/long name mappings, the name is looked up in the aliases first.


>>> symbol_by_name('mazecache.backends.redis:RedisBackend')
<class 'mazecache.backends.redis.RedisBackend'>
>>> symbol_by_name('default', {
...     'default': 'mazecache.backends.redis:RedisBackend'})
<class 'mazecache.backends.redis.RedisBackend'>

# Does not try to look up non-string names. >>> from mazecache.backends.redis import RedisBackend >>> symbol_by_name(RedisBackend) is RedisBackend True

mode.utils.imports.load_extension_classes(namespace: str) → Iterable[mode.utils.imports.EntrypointExtension]

Yield extension classes for setuptools entrypoint namespace.

If an entrypoint is defined in

    'faust.codecs': [
        'msgpack = faust_msgpack:msgpack',

Iterating over the ‘faust.codecs’ namespace will yield the actual attributes specified in the path (faust_msgpack:msgpack):

>>> from faust_msgpack import msgpack
>>> attrs = list(load_extension_classes('faust.codecs'))
assert msgpack in attrs
mode.utils.imports.load_extension_class_names(namespace: str) → Iterable[mode.utils.imports.RawEntrypointExtension]

Get setuptools entrypoint extension class names.

If the entrypoint is defined in as:

    'faust.codecs': [
        'msgpack = faust_msgpack:msgpack',

Iterating over the ‘faust.codecs’ namespace will yield the name:

>>> list(load_extension_class_names('faust.codecs'))
[('msgpack', 'faust_msgpack:msgpack')]
mode.utils.imports.cwd_in_path() → Generator

Context adding the current working directory to sys.path.

mode.utils.imports.import_from_cwd(module: str, *, imp: Callable = None, package: str = None) → module

Import module, temporarily including modules in the current directory.

Modules located in the current directory has precedence over modules located in sys.path.

mode.utils.imports.smart_import(path: str, imp: Any = None) → Any

Import module if module, otherwise same as symbol_by_name().