Skip to content

container

dev_tool.container

__all__ = ['CommandGroupRegistry', 'DependencyContainer', 'ServiceRegistry'] module-attribute

CommandGroupRegistry

A registry class for command group registration.

This class provides methods for registering all command groups with the dependency injection container.

register_command_groups staticmethod

A method that registers all command groups with the container.

Parameters:

Source code in dev_tool/container/command_registry.py
@staticmethod
def register_command_groups(container: DependencyContainer) -> None:
    """
    A method that registers all command groups with the container.

    :param container: The dependency injection container to register with.
    """

    container.register(HomeCommandGroup, singleton=True)
    container.register(DatabaseCommandGroup, singleton=True)
    container.register(DjangoCommandGroup, singleton=True)
    container.register(PythonCommandGroup, singleton=True)
    # container.register(AICommandGroup, singleton=True)
    container.register(OtherCommandGroup, singleton=True)
    container.register(TaskCommandGroup, singleton=True)

DependencyContainer

A dependency injection container for managing service instances.

This class provides methods for registering services, factories, and instances, and resolving dependencies automatically using constructor injection.

The constructor for the DependencyContainer class.

Initializes empty collections for storing factories, instances, services, and type mappings.

Source code in dev_tool/container/container.py
def __init__(self) -> None:
    """
    The constructor for the DependencyContainer class.

    Initializes empty collections for storing factories, instances, services, and type mappings.
    """

    self._factories = {}
    self._instances = {}
    self._services = {}
    self._typemap = {}

register

A method that registers a service type with the container.

Parameters:

  • interface (type[T]) –

    The interface or base class.

  • implementation (type[T] | None, default: None ) –

    The implementation class, defaults to interface.

  • singleton (bool, default: True ) –

    Whether to create a singleton instance.

Source code in dev_tool/container/container.py
def register(self, interface: type[T], implementation: type[T] | None = None, singleton: bool = True) -> None:
    """
    A method that registers a service type with the container.

    :param interface: The interface or base class.
    :param implementation: The implementation class, defaults to interface.
    :param singleton: Whether to create a singleton instance.
    """

    service = implementation if implementation is not None else interface

    self._services[interface] = {
        'type': service,
        'singleton': singleton
    }

    name = getattr(
        interface,
        '__name__',
        str(interface)
    )

    self._typemap[name] = interface

register_factory

A method that registers a factory function with the container.

Parameters:

  • interface (type[T]) –

    The interface the factory creates.

  • factory (Callable[[], T]) –

    The factory function.

  • singleton (bool, default: True ) –

    Whether to cache the factory result.

Source code in dev_tool/container/container.py
def register_factory(self, interface: type[T], factory: Callable[[], T], singleton: bool = True) -> None:
    """
    A method that registers a factory function with the container.

    :param interface: The interface the factory creates.
    :param factory: The factory function.
    :param singleton: Whether to cache the factory result.
    """

    self._factories[interface] = {
        'factory': factory,
        'singleton': singleton
    }

    name = getattr(
        interface,
        '__name__',
        str(interface)
    )

    self._typemap[name] = interface

register_instance

A method that registers a pre-created instance with the container.

Parameters:

  • interface (type[T]) –

    The interface the instance implements.

  • instance (T) –

    The instance to register.

Source code in dev_tool/container/container.py
def register_instance(self, interface: type[T], instance: T) -> None:
    """
    A method that registers a pre-created instance with the container.

    :param interface: The interface the instance implements.
    :param instance: The instance to register.
    """

    self._instances[interface] = instance
    self._typemap[interface.__class__.__name__] = interface

resolve

A method that resolves a service instance from the container.

Parameters:

  • service (type[T]) –

    The service type to resolve.

Returns:

  • T

    An instance of the requested service.

Raises:

  • ValueError

    If the service is not registered.

Source code in dev_tool/container/container.py
def resolve(self, service: type[T]) -> T:
    """
    A method that resolves a service instance from the container.

    :param service: The service type to resolve.
    :return: An instance of the requested service.
    :raises ValueError: If the service is not registered.
    """

    if service in self._instances:
        return self._instances[service]

    if service in self._factories:
        information = self._factories[service]
        instance = information['factory']()

        if information['singleton']:
            self._instances[service] = instance

        return instance

    if service not in self._services:
        name = getattr(
            service,
            '__name__',
            str(service)
        )

        message = f'Service {name} is not registered'
        raise ValueError(message)

    information = self._services[service]
    implementation = information['type']

    if information['singleton'] and service in self._instances:
        return self._instances[service]

    instance = self._create_instance(implementation)

    if information['singleton']:
        self._instances[service] = instance

    return instance

ServiceRegistry

A registry class for service registration.

This class provides methods for creating and registering all services with the dependency injection container.

register_all_services staticmethod

A method that registers all services with the dependency container.

Parameters:

Source code in dev_tool/container/service_registry.py
@staticmethod
def register_all_services(container: DependencyContainer) -> None:
    """
    A method that registers all services with the dependency container.

    :param container: The dependency injection container to register with.
    """

    container.register_factory(
        RequestService,
        lambda: RequestService(),
        singleton=True
    )

    container.register_factory(
        SQLiteDatabase,
        lambda: SQLiteDatabase(),
        singleton=True
    )

    container.register(SchedulerService, singleton=True)

    container.register_factory(
        PostgresService,
        lambda: PostgresService(),
        singleton=True
    )

    container.register_factory(
        PythonService,
        lambda: PythonService(),
        singleton=True
    )

    container.register_factory(
        ScriptingService,
        lambda: ScriptingService(),
        singleton=True
    )

    container.register(UpdateService, singleton=True)

    container.register_factory(
        DjangoService,
        lambda: DjangoService(),
        singleton=True
    )

    container.register_factory(
        ProfilingService,
        lambda: ProfilingService(),
        singleton=True
    )

    # container.register_factory(
    #     AIService,
    #     ServiceRegistry._create_ai_service,
    #     singleton=True
    # )

    container.register_factory(
        DockerService,
        ServiceRegistry._create_docker_service,
        singleton=True
    )

    container.register_factory(
        CoverageService, ServiceRegistry._create_coverage_service, singleton=False
    )

    container.register_factory(
        UnitTestService, ServiceRegistry._create_unittest_service, singleton=False
    )

    container.register(APIClientService, singleton=True)
    container.register(PortalService, singleton=True)
    container.register(NgrokService, singleton=True)

    ServiceRegistry._setup_application_hook(container)