Compare commits
3 Commits
2025.09.25
...
2025.09.25
| Author | SHA1 | Date | |
|---|---|---|---|
| 0529269747 | |||
| e3e1703ff8 | |||
| cf4aa8291f |
@@ -25,7 +25,11 @@ jobs:
|
||||
git tag
|
||||
DATE=$(date +'%Y.%m.%d')
|
||||
TAG_COUNT=$(git tag -l "${DATE}.*" | wc -l)
|
||||
BUILD_NUMBER=$(($TAG_COUNT + 1))
|
||||
if [ "$TAG_COUNT" -eq 0 ]; then
|
||||
BUILD_NUMBER=0
|
||||
else
|
||||
BUILD_NUMBER=$(($TAG_COUNT + 1))
|
||||
fi
|
||||
|
||||
VERSION_SUFFIX=${{ inputs.version_suffix }}
|
||||
if [ -n "$VERSION_SUFFIX" ] && [ "$VERSION_SUFFIX" = "dev" ]; then
|
||||
|
||||
@@ -1,11 +1,14 @@
|
||||
from cpl import auth
|
||||
from cpl.application.abc.startup_abc import StartupABC
|
||||
from cpl.auth import permission
|
||||
from cpl.auth.auth_module import AuthModule
|
||||
from cpl.auth.permission.permission_module import PermissionsModule
|
||||
from cpl.core.configuration import Configuration
|
||||
from cpl.core.environment import Environment
|
||||
from cpl.core.log import Logger, LoggerABC
|
||||
from cpl.database import mysql
|
||||
from cpl.database.abc.data_access_object_abc import DataAccessObjectABC
|
||||
from cpl.database.mysql.mysql_module import MySQLModule
|
||||
from cpl.dependency import ServiceCollection
|
||||
from model.city_dao import CityDao
|
||||
from model.user_dao import UserDao
|
||||
@@ -21,9 +24,9 @@ class Startup(StartupABC):
|
||||
|
||||
@staticmethod
|
||||
async def configure_services(services: ServiceCollection):
|
||||
services.add_module(mysql)
|
||||
services.add_module(auth)
|
||||
services.add_module(permission)
|
||||
services.add_module(MySQLModule)
|
||||
services.add_module(AuthModule)
|
||||
services.add_module(PermissionsModule)
|
||||
|
||||
services.add_transient(DataAccessObjectABC, UserDao)
|
||||
services.add_transient(DataAccessObjectABC, CityDao)
|
||||
|
||||
@@ -1,11 +1,10 @@
|
||||
from cpl.application.abc import ApplicationABC
|
||||
from cpl.core.console.console import Console
|
||||
from cpl.dependency import ServiceProvider
|
||||
from di.static_test import StaticTest
|
||||
from di.test_abc import TestABC
|
||||
from di.test_service import TestService
|
||||
from di.di_tester_service import DITesterService
|
||||
from di.tester import Tester
|
||||
from test_abc import TestABC
|
||||
from test_service import TestService
|
||||
from di_tester_service import DITesterService
|
||||
from tester import Tester
|
||||
|
||||
|
||||
class Application(ApplicationABC):
|
||||
@@ -39,7 +38,8 @@ class Application(ApplicationABC):
|
||||
|
||||
Console.write_line("Global")
|
||||
self._part_of_scoped()
|
||||
StaticTest.test()
|
||||
#from static_test import StaticTest
|
||||
#StaticTest.test()
|
||||
|
||||
self._services.get_service(Tester)
|
||||
Console.write_line(self._services.get_services(TestABC))
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
from cpl.core.console.console import Console
|
||||
from di.test_service import TestService
|
||||
from test_service import TestService
|
||||
|
||||
|
||||
class DITesterService:
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
from cpl.application import ApplicationBuilder
|
||||
|
||||
from di.application import Application
|
||||
from di.startup import Startup
|
||||
from application import Application
|
||||
from startup import Startup
|
||||
|
||||
|
||||
def main():
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
from cpl.application.abc import StartupABC
|
||||
from cpl.dependency import ServiceProvider, ServiceCollection
|
||||
from di.di_tester_service import DITesterService
|
||||
from di.test1_service import Test1Service
|
||||
from di.test2_service import Test2Service
|
||||
from di.test_abc import TestABC
|
||||
from di.test_service import TestService
|
||||
from di.tester import Tester
|
||||
from di_tester_service import DITesterService
|
||||
from test1_service import Test1Service
|
||||
from test2_service import Test2Service
|
||||
from test_abc import TestABC
|
||||
from test_service import TestService
|
||||
from tester import Tester
|
||||
|
||||
|
||||
class Startup(StartupABC):
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
from cpl.dependency import ServiceProvider, ServiceProvider
|
||||
from cpl.dependency.inject import inject
|
||||
from di.test_service import TestService
|
||||
from test_service import TestService
|
||||
|
||||
|
||||
class StaticTest:
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import string
|
||||
from cpl.core.console.console import Console
|
||||
from cpl.core.utils.string import String
|
||||
from di.test_abc import TestABC
|
||||
from test_abc import TestABC
|
||||
|
||||
|
||||
class Test1Service(TestABC):
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import string
|
||||
from cpl.core.console.console import Console
|
||||
from cpl.core.utils.string import String
|
||||
from di.test_abc import TestABC
|
||||
from test_abc import TestABC
|
||||
|
||||
|
||||
class Test2Service(TestABC):
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
from cpl.core.console.console import Console
|
||||
from di.test_abc import TestABC
|
||||
from test_abc import TestABC
|
||||
|
||||
|
||||
class Tester:
|
||||
def __init__(self, t1: TestABC, t2: TestABC, t3: list[TestABC]):
|
||||
Console.write_line("Tester:")
|
||||
Console.write_line(t1, t2, t3)
|
||||
def __init__(self, t1: TestABC, t2: TestABC, t3: TestABC, t: list[TestABC]):
|
||||
Console.write_line("Tester:", t, t1, t2, t3)
|
||||
|
||||
@@ -48,9 +48,9 @@ def t_benchmark(data: list):
|
||||
|
||||
|
||||
def main():
|
||||
N = 10_000_000
|
||||
N = 1_000_000
|
||||
data = list(range(N))
|
||||
#t_benchmark(data)
|
||||
t_benchmark(data)
|
||||
|
||||
Console.write_line()
|
||||
_default()
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
from .error import APIError, AlreadyExists, EndpointNotImplemented, Forbidden, NotFound, Unauthorized
|
||||
from .logger import APILogger
|
||||
from .settings import ApiSettings
|
||||
|
||||
|
||||
@@ -1,2 +1,2 @@
|
||||
from .application_builder import ApplicationBuilder
|
||||
from .host import Host
|
||||
from .host import Host
|
||||
|
||||
@@ -83,6 +83,7 @@ class ApplicationBuilder(Generic[TApp]):
|
||||
for extension in self._app_extensions:
|
||||
Host.run(extension.run, self.service_provider)
|
||||
|
||||
use_root_provider(self._services.build())
|
||||
app = self._app(self.service_provider)
|
||||
self.validate_app_required_modules(app)
|
||||
return app
|
||||
|
||||
@@ -77,4 +77,4 @@ class Host:
|
||||
if asyncio.iscoroutinefunction(func):
|
||||
return cls.get_loop().run_until_complete(func(*args, **kwargs))
|
||||
|
||||
return func(*args, **kwargs)
|
||||
return func(*args, **kwargs)
|
||||
|
||||
@@ -17,5 +17,4 @@ def _with_permissions(self: _ApplicationABC, *permissions: Type[Enum]) -> _Appli
|
||||
return self
|
||||
|
||||
|
||||
|
||||
_ApplicationABC.extend(_ApplicationABC.with_permissions, _with_permissions)
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
from cpl.auth.auth_module import AuthModule
|
||||
from cpl.auth.permission.permission_seeder import PermissionSeeder
|
||||
from cpl.auth.permission.permissions import Permissions
|
||||
from cpl.auth.permission.permissions_registry import PermissionsRegistry
|
||||
from cpl.database.abc.data_seeder_abc import DataSeederABC
|
||||
from cpl.database.model.server_type import ServerType, ServerTypes
|
||||
from cpl.dependency.module import Module, TModule
|
||||
from cpl.dependency.service_collection import ServiceCollection
|
||||
|
||||
@@ -12,21 +12,7 @@ class PermissionsModule(Module):
|
||||
def dependencies() -> list[TModule]:
|
||||
from cpl.database.database_module import DatabaseModule
|
||||
|
||||
r = [DatabaseModule]
|
||||
|
||||
match ServerType.server_type:
|
||||
case ServerTypes.POSTGRES:
|
||||
from cpl.database.postgres.postgres_module import PostgresModule
|
||||
|
||||
r.append(PostgresModule)
|
||||
case ServerTypes.MYSQL:
|
||||
from cpl.database.mysql.mysql_module import MySQLModule
|
||||
|
||||
r.append(MySQLModule)
|
||||
case _:
|
||||
raise Exception(f"Unsupported database type: {ServerType.server_type}")
|
||||
|
||||
return r
|
||||
return [DatabaseModule, AuthModule]
|
||||
|
||||
@staticmethod
|
||||
def register(collection: ServiceCollection):
|
||||
|
||||
@@ -14,6 +14,7 @@ def dependency_error(src: str, package_name: str, e: ImportError = None) -> None
|
||||
|
||||
exit(1)
|
||||
|
||||
|
||||
def module_dependency_error(src: str, module: str, e: ImportError = None) -> None:
|
||||
Console.error(f"'{module}' is required to use feature: {src}. Please initialize it with `add_module({module})`.")
|
||||
tb = traceback.format_exc()
|
||||
@@ -23,4 +24,4 @@ def module_dependency_error(src: str, module: str, e: ImportError = None) -> Non
|
||||
elif e is not None:
|
||||
Console.write_line("->", str(e))
|
||||
|
||||
exit(1)
|
||||
exit(1)
|
||||
|
||||
@@ -2,7 +2,7 @@ import contextvars
|
||||
from contextlib import contextmanager
|
||||
|
||||
|
||||
_current_provider = contextvars.ContextVar("current_provider")
|
||||
_current_provider = contextvars.ContextVar("current_provider", default=None)
|
||||
|
||||
|
||||
def use_root_provider(provider: "ServiceProvider"):
|
||||
|
||||
@@ -1,2 +1,2 @@
|
||||
from .hosted_service import HostedService
|
||||
from .startup_task import StartupTask
|
||||
from .startup_task import StartupTask
|
||||
|
||||
@@ -6,4 +6,4 @@ class HostedService(ABC):
|
||||
async def start(self): ...
|
||||
|
||||
@abstractmethod
|
||||
async def stop(self): ...
|
||||
async def stop(self): ...
|
||||
|
||||
@@ -3,6 +3,7 @@ from typing import Type
|
||||
|
||||
TModule = Type["Module"]
|
||||
|
||||
|
||||
class Module(ABC):
|
||||
|
||||
@staticmethod
|
||||
|
||||
@@ -23,6 +23,11 @@ class ServiceProvider:
|
||||
type_args = list(typing.get_args(service_type))
|
||||
|
||||
for descriptor in self._service_descriptors:
|
||||
if typing.get_origin(service_type) is None and (
|
||||
descriptor.service_type == service_type or issubclass(descriptor.base_type, service_type)
|
||||
):
|
||||
return descriptor
|
||||
|
||||
descriptor_base_type = typing.get_origin(descriptor.base_type) or descriptor.base_type
|
||||
descriptor_type_args = list(typing.get_args(descriptor.base_type))
|
||||
|
||||
@@ -48,7 +53,7 @@ class ServiceProvider:
|
||||
if descriptor.implementation is not None:
|
||||
return descriptor.implementation
|
||||
|
||||
implementation = self._build_service(descriptor.service_type, origin_service_type=origin_service_type)
|
||||
implementation = self._build_service(descriptor, origin_service_type=origin_service_type)
|
||||
if descriptor.lifetime in (ServiceLifetimeEnum.singleton, ServiceLifetimeEnum.scoped):
|
||||
descriptor.implementation = implementation
|
||||
|
||||
@@ -62,9 +67,7 @@ class ServiceProvider:
|
||||
implementations.append(descriptor.implementation)
|
||||
continue
|
||||
|
||||
implementation = self._build_service(
|
||||
descriptor.service_type, *args, origin_service_type=service_type, **kwargs
|
||||
)
|
||||
implementation = self._build_service(descriptor, *args, origin_service_type=service_type, **kwargs)
|
||||
if descriptor.lifetime in (ServiceLifetimeEnum.singleton, ServiceLifetimeEnum.scoped):
|
||||
descriptor.implementation = implementation
|
||||
|
||||
@@ -78,7 +81,9 @@ class ServiceProvider:
|
||||
parameter = param[1]
|
||||
if parameter.name != "self" and parameter.annotation != Parameter.empty:
|
||||
if typing.get_origin(parameter.annotation) == list:
|
||||
params.append(self._get_services(typing.get_args(parameter.annotation)[0], origin_service_type))
|
||||
params.append(
|
||||
self._get_services(typing.get_args(parameter.annotation)[0], service_type=origin_service_type)
|
||||
)
|
||||
|
||||
elif parameter.annotation == Source:
|
||||
params.append(origin_service_type.__name__)
|
||||
@@ -101,18 +106,17 @@ class ServiceProvider:
|
||||
|
||||
return params
|
||||
|
||||
def _build_service(self, service_type: type, *args, origin_service_type: type = None, **kwargs) -> object:
|
||||
def _build_service(
|
||||
self, descriptor: ServiceDescriptor, *args, origin_service_type: type = None, **kwargs
|
||||
) -> object:
|
||||
if descriptor.implementation is not None:
|
||||
service_type = type(descriptor.implementation)
|
||||
else:
|
||||
service_type = descriptor.service_type
|
||||
|
||||
if origin_service_type is None:
|
||||
origin_service_type = service_type
|
||||
|
||||
for descriptor in self._service_descriptors:
|
||||
if descriptor.service_type == service_type or issubclass(descriptor.service_type, service_type):
|
||||
if descriptor.implementation is not None:
|
||||
service_type = type(descriptor.implementation)
|
||||
else:
|
||||
service_type = descriptor.service_type
|
||||
break
|
||||
|
||||
sig = signature(service_type.__init__)
|
||||
params = self._build_by_signature(sig, origin_service_type)
|
||||
return service_type(*params, *args, **kwargs)
|
||||
@@ -131,7 +135,11 @@ class ServiceProvider:
|
||||
yield scoped_provider
|
||||
|
||||
def get_hosted_services(self) -> list[Optional[T]]:
|
||||
hosted_services = [self.get_service(d.service_type) for d in self._service_descriptors if d.lifetime == ServiceLifetimeEnum.hosted]
|
||||
hosted_services = [
|
||||
self.get_service(d.service_type)
|
||||
for d in self._service_descriptors
|
||||
if d.lifetime == ServiceLifetimeEnum.hosted
|
||||
]
|
||||
return hosted_services
|
||||
|
||||
def get_service(self, service_type: Type[T], *args, **kwargs) -> Optional[T]:
|
||||
@@ -142,7 +150,7 @@ class ServiceProvider:
|
||||
if result.implementation is not None:
|
||||
return result.implementation
|
||||
|
||||
implementation = self._build_service(service_type, *args, **kwargs)
|
||||
implementation = self._build_service(result, *args, **kwargs)
|
||||
|
||||
if result.lifetime == ServiceLifetimeEnum.singleton:
|
||||
result.implementation = implementation
|
||||
|
||||
@@ -9,7 +9,6 @@ class MailModule(Module):
|
||||
def dependencies() -> list[TModule]:
|
||||
return []
|
||||
|
||||
|
||||
@staticmethod
|
||||
def register(collection: ServiceCollection):
|
||||
collection.add_singleton(EMailClientABC, EMailClient)
|
||||
collection.add_singleton(EMailClientABC, EMailClient)
|
||||
|
||||
Reference in New Issue
Block a user