Skip to content

app

dev_tool.event.app

__all__ = ['AppEventManager', 'AppEventType', 'AppLifecycleHandler'] module-attribute

AppEventType

Bases: StrEnum

APP_STARTING = 'app_starting' class-attribute instance-attribute

APP_STARTED = 'app_started' class-attribute instance-attribute

APP_CLOSING = 'app_closing' class-attribute instance-attribute

APP_CLOSED = 'app_closed' class-attribute instance-attribute

COMMAND_STARTING = 'command_starting' class-attribute instance-attribute

COMMAND_COMPLETED = 'command_completed' class-attribute instance-attribute

COMMAND_SUCCESS = 'command_success' class-attribute instance-attribute

COMMAND_FAILURE = 'command_failure' class-attribute instance-attribute

AppLifecycleHandler

Bases: EventHandler

A handler class for application lifecycle events.

This class manages hooks for different application events and executes them when corresponding events are received.

The constructor for the AppLifecycleHandler class.

Initializes an empty hooks dictionary for storing event callbacks.

Source code in dev_tool/event/app/handler.py
def __init__(self) -> None:
    """
    The constructor for the AppLifecycleHandler class.

    Initializes an empty hooks dictionary for storing event callbacks.
    """

    self.hooks = {}

hooks = {} instance-attribute

handle

A method that handles incoming events by executing registered hooks.

Parameters:

  • event (Event) –

    The event object to handle.

Source code in dev_tool/event/app/handler.py
def handle(self, event: Event) -> None:
    """
    A method that handles incoming events by executing registered hooks.

    :param event: The event object to handle.
    """

    if event.type in self.hooks:
        for hook in self.hooks[event.type]:
            with contextlib.suppress(Exception):
                hook(event)

register_hook

A method that registers a hook function for a specific event type.

Parameters:

  • event_type (AppEventType) –

    The application event type to register the hook for.

  • function (Callable) –

    The callback function to execute when the event occurs.

Source code in dev_tool/event/app/handler.py
def register_hook(self, event_type: AppEventType, function: Callable) -> None:
    """
    A method that registers a hook function for a specific event type.

    :param event_type: The application event type to register the hook for.
    :param function: The callback function to execute when the event occurs.
    """

    if event_type not in self.hooks:
        self.hooks[event_type] = []

    self.hooks[event_type].append(function)

AppEventManager

A manager class for application lifecycle events.

This class handles the registration and emission of application events through an event bus system with lifecycle hooks.

The constructor for the AppEventManager class.

Parameters:

  • event_bus (EventBus) –

    The event bus instance for publishing events.

Source code in dev_tool/event/app/manager.py
def __init__(self, event_bus: EventBus) -> None:
    """
    The constructor for the AppEventManager class.

    :param event_bus: The event bus instance for publishing events.
    """

    self.event_bus = event_bus
    self.handler = AppLifecycleHandler()

    for event_type in cast('Iterable[AppEventType]', AppEventType):
        self.event_bus.subscribe(event_type, self.handler)

event_bus = event_bus instance-attribute

handler = AppLifecycleHandler() instance-attribute

emit_app_event

A method that emits an application lifecycle event.

Parameters:

  • event_type (AppEventType) –

    The type of application event to emit.

  • data (dict[str, Any] | None, default: None ) –

    Optional event data dictionary.

Source code in dev_tool/event/app/manager.py
def emit_app_event(self, event_type: AppEventType, data: dict[str, Any] | None = None) -> None:
    """
    A method that emits an application lifecycle event.

    :param event_type: The type of application event to emit.
    :param data: Optional event data dictionary.
    """

    self.event_bus.emit(event_type, data or {}, source='app')

register_hook

A method that registers a function to run when an application event occurs.

Parameters:

  • event_type (AppEventType) –

    The application event type to listen for.

  • function (Callable) –

    The callback function to execute when the event occurs.

Source code in dev_tool/event/app/manager.py
def register_hook(self, event_type: AppEventType, function: Callable) -> None:
    """
    A method that registers a function to run when an application event occurs.

    :param event_type: The application event type to listen for.
    :param function: The callback function to execute when the event occurs.
    """

    self.handler.register_hook(event_type, function)