mode.utils.imports — mode-ng 0.4.0 documentation

# `mode.utils.imports`

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.

  Example

      >>> # 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]
  by_name(name: Union[_T, str]) → _T
  by_url(url: Union[str, URL]) → _T

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

  property data: MutableMapping
  get_alias(name: str) → str
  include_setuptools_namespace(namespace: str) → None
  iterate() → Iterator[_T]
  namespaces: set

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: Optional[Callable] = None, package: Optional[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.load_extension_class_names(namespace: str) → Iterable[RawEntrypointExtension]

  Get setuptools entrypoint extension class names.

  If the entrypoint is defined in `setup.py` as:

      entry_points={
          '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.load_extension_classes(namespace: str) → Iterable[EntrypointExtension]

  Yield extension classes for setuptools entrypoint namespace.

  If an entrypoint is defined in `setup.py`:

      entry_points={
          '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.smart_import(path: str, imp: Optional[Any] = None) → Any

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

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

  Get symbol by qualified name.

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

      modulename.ClassName

  Example:

      mazecache.backends.redis.RedisBackend
                              ^- class name

  or using ‘:’ to separate module and symbol:

      mazecache.backends.redis:RedisBackend

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

  Examples

      >>> 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