asphalt.core.component

class asphalt.core.component.CLIApplicationComponent(components=None)

Specialized subclass of ContainerComponent for command line tools.

Command line tools and similar applications should use this as their root component and implement their main code in the run() method.

When all the subcomponents have been started, run() is started as a new task. When the task is finished, the application will exit using the return value as its exit code.

If run() raises an exception, a stack trace is printed and the exit code will be set to 1. If the returned exit code is out of range or of the wrong data type, it is set to 1 and a warning is emitted.

abstract async run(ctx)

Run the business logic of the command line tool.

Do not call this method yourself.

Return type:

int | None

Returns:

the application’s exit code (0-127; None = 0)

async start(ctx)

Create child components that have been configured but not yet created and then calls their start() methods in separate tasks and waits until they have completed.

Return type:

None

class asphalt.core.component.Component

This is the base class for all Asphalt components.

abstract async start(ctx)

Perform any necessary tasks to start the services provided by this component.

In this method, components typically use the context to:

It is advisable for Components to first add all the resources they can to the context before requesting any from it. This will speed up the dependency resolution and prevent deadlocks.

Parameters:

ctx (Context) – the containing context for this component

Return type:

None

class asphalt.core.component.ContainerComponent(components=None)

A component that can contain other components.

Parameters:

components – dictionary of component alias ⭢ component configuration dictionary

Variables:
  • child_components (Dict[str, Component]) – dictionary of component alias ⭢ Component instance (of child components added with add_component())

  • component_configs (Dict[str, Optional[Dict[str, Any]]]) – dictionary of component alias ⭢ externally provided component configuration

add_component(alias, type=None, **config)

Add a child component.

This will instantiate a component class, as specified by the type argument.

If the second argument is omitted, the value of alias is used as its value.

The locally given configuration can be overridden by component configuration parameters supplied to the constructor (via the components argument).

When configuration values are provided both as keyword arguments to this method and component configuration through the components constructor argument, the configurations are merged together using merge_config() in a way that the configuration values from the components argument override the keyword arguments to this method.

Parameters:
  • alias (str) – a name for the component instance, unique within this container

  • type (str | type | None) – entry point name or Component subclass or a module:varname reference to one

  • config – keyword arguments passed to the component’s constructor

Return type:

None

async start(ctx)

Create child components that have been configured but not yet created and then calls their start() methods in separate tasks and waits until they have completed.

Return type:

None