Robot Framework
robot.utils.importer.Importer Class Reference

Utility that can import modules and classes based on names and paths. More...

Public Member Functions

def __init__ (self, type=None, logger=None)
 :param type: Type of the thing being imported. More...
 
def import_class_or_module (self, name_or_path, instantiate_with_args=None, return_source=False)
 Imports Python class or module based on the given name or path. More...
 
def import_class_or_module_by_path (self, path, instantiate_with_args=None)
 Import a Python module or class using a file system path. More...
 
def import_module (self, name_or_path)
 Imports Python module based on the given name or path. More...
 

Private Member Functions

def _get_arg_spec (self, imported)
 
def _handle_return_values (self, imported, source, return_source=False)
 
def _import (self, name, get_class=True)
 
def _instantiate_class (self, imported, args)
 
def _instantiate_if_needed (self, imported, args)
 
def _log_import_succeeded (self, item, name, source)
 
def _raise_import_failed (self, name, error)
 
def _sanitize_source (self, source)
 

Private Attributes

 _by_path_importer
 
 _importers
 
 _logger
 
 _type
 

Detailed Description

Utility that can import modules and classes based on names and paths.

Imported classes can optionally be instantiated automatically.

Definition at line 33 of file importer.py.

Constructor & Destructor Documentation

◆ __init__()

def robot.utils.importer.Importer.__init__ (   self,
  type = None,
  logger = None 
)

:param type: Type of the thing being imported.

Used in error and log messages. :param logger: Logger to be notified about successful imports and other events. Currently only needs the info method, but other level specific methods may be needed in the future. If not given, logging is disabled.

Definition at line 43 of file importer.py.

Member Function Documentation

◆ _get_arg_spec()

def robot.utils.importer.Importer._get_arg_spec (   self,
  imported 
)
private

Definition at line 195 of file importer.py.

◆ _handle_return_values()

def robot.utils.importer.Importer._handle_return_values (   self,
  imported,
  source,
  return_source = False 
)
private

Definition at line 124 of file importer.py.

◆ _import()

def robot.utils.importer.Importer._import (   self,
  name,
  get_class = True 
)
private

Definition at line 119 of file importer.py.

◆ _instantiate_class()

def robot.utils.importer.Importer._instantiate_class (   self,
  imported,
  args 
)
private

Definition at line 184 of file importer.py.

◆ _instantiate_if_needed()

def robot.utils.importer.Importer._instantiate_if_needed (   self,
  imported,
  args 
)
private

Definition at line 175 of file importer.py.

◆ _log_import_succeeded()

def robot.utils.importer.Importer._log_import_succeeded (   self,
  item,
  name,
  source 
)
private

Definition at line 164 of file importer.py.

◆ _raise_import_failed()

def robot.utils.importer.Importer._raise_import_failed (   self,
  name,
  error 
)
private

Definition at line 171 of file importer.py.

◆ _sanitize_source()

def robot.utils.importer.Importer._sanitize_source (   self,
  source 
)
private

Definition at line 131 of file importer.py.

◆ import_class_or_module()

def robot.utils.importer.Importer.import_class_or_module (   self,
  name_or_path,
  instantiate_with_args = None,
  return_source = False 
)

Imports Python class or module based on the given name or path.

    :param name_or_path:
        Name or path of the module or class to import.
    :param instantiate_with_args:
        When arguments are given, imported classes are automatically initialized
        using them.
    :param return_source:
        When true, returns a tuple containing the imported module or class
        and a path to it. By default, returns only the imported module or class.

    The class or module to import can be specified either as a name, in which
    case it must be in the module search path, or as a path to the file or
    directory implementing the module. See :meth:`import_class_or_module_by_path`
    for more information about importing classes and modules by path.

    Classes can be imported from the module search path using name like
    ``modulename.ClassName``. If the class name and module name are same, using
    just ``CommonName`` is enough. When importing a class by a path, the class
    name and the module name must match.

    Optional arguments to use when creating an instance are given as a list.
    Starting from Robot Framework 4.0, both positional and named arguments are
    supported (e.g. ``['positional', 'name=value']``) and arguments are converted
    automatically based on type hints and default values.

    If arguments needed when creating an instance are initially embedded into
    the name or path like ``Example:arg1:arg2``, separate
    :func:`~robot.utils.text.split_args_from_name_or_path` function can be
    used to split them before calling this method.

    Use :meth:`import_module` if only a module needs to be imported.

Definition at line 84 of file importer.py.

◆ import_class_or_module_by_path()

def robot.utils.importer.Importer.import_class_or_module_by_path (   self,
  path,
  instantiate_with_args = None 
)

Import a Python module or class using a file system path.

    :param path:
        Path to the module or class to import.
    :param instantiate_with_args:
        When arguments are given, imported classes are automatically initialized
        using them.

    When importing a Python file, the path must end with :file:`.py` and the
    actual file must also exist.

    Use :meth:`import_class_or_module` to support importing also using name,
    not only path. See the documentation of that function for more information
    about creating instances automatically.

Definition at line 156 of file importer.py.

◆ import_module()

def robot.utils.importer.Importer.import_module (   self,
  name_or_path 
)

Imports Python module based on the given name or path.

    :param name_or_path:
        Name or path of the module to import.

    The module to import can be specified either as a name, in which
    case it must be in the module search path, or as a path to the file or
    directory implementing the module. See :meth:`import_class_or_module_by_path`
    for more information about importing modules by path.

    Use :meth:`import_class_or_module` if it is desired to get a class
    from the imported module automatically.

    New in Robot Framework 6.0.

Definition at line 110 of file importer.py.

Member Data Documentation

◆ _by_path_importer

robot.utils.importer.Importer._by_path_importer
private

Definition at line 49 of file importer.py.

◆ _importers

robot.utils.importer.Importer._importers
private

Definition at line 46 of file importer.py.

◆ _logger

robot.utils.importer.Importer._logger
private

Definition at line 45 of file importer.py.

◆ _type

robot.utils.importer.Importer._type
private

Definition at line 44 of file importer.py.


The documentation for this class was generated from the following file: