aea.helpers.base

Miscellaneous helpers.

yaml_load

yaml_load(stream: TextIO) -> Dict[str, str]

Load a yaml from a file pointer in an ordered way.

Arguments:

  • stream: the file pointer

Returns:

the yaml

yaml_dump

yaml_dump(data, stream: TextIO) -> None

Dump data to a yaml file in an ordered way.

Arguments:

  • data: the data to be dumped
  • stream: the file pointer

locate

locate(path: str) -> Any

Locate an object by name or dotted path, importing as necessary.

load_aea_package

load_aea_package(configuration: ComponentConfiguration) -> None

Load the AEA package.

It adds all the init.py modules into sys.modules.

Arguments:

  • configuration: the configuration object.

Returns:

None

load_module

load_module(dotted_path: str, filepath: Path) -> types.ModuleType

Load a module.

Arguments:

  • dotted_path: the dotted path of the package/module.
  • filepath: the file to the package/module.

Returns:

None

Raises:

  • ValueError: if the filepath provided is not a module.
  • Exception: if the execution of the module raises exception.

load_env_file

load_env_file(env_file: str)

Load the content of the environment file into the process environment.

Arguments:

  • env_file: path to the env file.

Returns:

None.

sigint_crossplatform

sigint_crossplatform(process: subprocess.Popen) -> None

Send a SIGINT, cross-platform.

The reason is because the subprocess module doesn't have an API to send a SIGINT-like signal both on Posix and Windows with a single method.

However, a subprocess.Popen class has the method 'send_signal' that gives more flexibility in this terms.

Arguments:

  • process: the process to send the signal to.

Returns:

None

RegexConstrainedString Objects

class RegexConstrainedString(UserString)

A string that is constrained by a regex.

The default behaviour is to match anything. Subclass this class and change the 'REGEX' class attribute to implement a different behaviour.

__init__

 | __init__(seq)

Initialize a regex constrained string.

cd

@contextlib.contextmanager
cd(path)

Change working directory temporarily.

get_logger_method

get_logger_method(fn: Callable, logger_method: Union[str, Callable]) -> Callable

Get logger method for function.

Get logger in fn definion module or creates logger is module.name. Or return logger_method if it's callable.

Arguments:

  • fn: function to get logger for.
  • logger_method: logger name or callable.

Returns:

callable to write log with

try_decorator

try_decorator(error_message: str, default_return=None, logger_method="error")

Run function, log and return default value on exception.

Does not support async or coroutines!

Arguments:

  • error_message: message template with one {} for exception
  • default_return: value to return on exception, by default None
  • logger_method: name of the logger method or callable to print logs

MaxRetriesError Objects

class MaxRetriesError(Exception)

Exception for retry decorator.

retry_decorator

retry_decorator(number_of_retries: int, error_message: str, delay: float = 0, logger_method="error")

Run function with several attempts.

Does not support async or coroutines!

Arguments:

  • number_of_retries: amount of attempts
  • error_message: message template with one {} for exception
  • delay: num of seconds to sleep between retries. default 0
  • logger_method: name of the logger method or callable to print logs

exception_log_and_reraise

@contextlib.contextmanager
exception_log_and_reraise(log_method: Callable, message: str)

Run code in context to log and re raise exception.

Arguments:

  • log_method: function to print log
  • message: message template to add error text.