Added cpl new
All checks were successful
Test before pr merge / test-lint (pull_request) Successful in 7s

This commit is contained in:
2025-10-18 17:48:49 +02:00
parent 8ebac05dd8
commit 76b44ca517
32 changed files with 427 additions and 130 deletions

View File

@@ -14,7 +14,7 @@ TApp = TypeVar("TApp", bound=ApplicationABC)
class ApplicationBuilder(Generic[TApp]):
def __init__(self, app: Type[ApplicationABC]):
def __init__(self, app: Type[TApp]):
assert app is not None, "app must not be None"
assert issubclass(app, ApplicationABC), "app must be an subclass of ApplicationABC or its subclass"

View File

@@ -1,7 +1,9 @@
import asyncio
from typing import Callable
from cpl.dependency import get_provider
from cpl.core.property import classproperty
from cpl.dependency.context import get_provider, use_root_provider
from cpl.dependency.service_collection import ServiceCollection
from cpl.dependency.hosted.startup_task import StartupTask
@@ -9,6 +11,24 @@ class Host:
_loop: asyncio.AbstractEventLoop | None = None
_tasks: dict = {}
_service_collection: ServiceCollection | None = None
@classproperty
def services(cls) -> ServiceCollection:
if cls._service_collection is None:
cls._service_collection = ServiceCollection()
return cls._service_collection
@classmethod
def get_provider(cls):
provider = get_provider()
if provider is None:
provider = cls.services.build()
use_root_provider(provider)
return provider
@classmethod
def get_loop(cls) -> asyncio.AbstractEventLoop:
if cls._loop is None:
@@ -18,7 +38,7 @@ class Host:
@classmethod
def run_start_tasks(cls):
provider = get_provider()
provider = cls.get_provider()
tasks = provider.get_services(StartupTask)
loop = cls.get_loop()
@@ -30,7 +50,7 @@ class Host:
@classmethod
def run_hosted_services(cls):
provider = get_provider()
provider = cls.get_provider()
services = provider.get_hosted_services()
loop = cls.get_loop()
@@ -49,6 +69,10 @@ class Host:
cls._tasks.clear()
@classmethod
async def wait_for_all(cls):
await asyncio.gather(*cls._tasks.values())
@classmethod
def run_app(cls, func: Callable, *args, **kwargs):
cls.run_start_tasks()