Moved test projects
This commit is contained in:
0
example/custom/di/src/di/__init__.py
Normal file
0
example/custom/di/src/di/__init__.py
Normal file
42
example/custom/di/src/di/application.py
Normal file
42
example/custom/di/src/di/application.py
Normal file
@@ -0,0 +1,42 @@
|
||||
from cpl.application.abc import ApplicationABC
|
||||
from cpl.core.console.console import Console
|
||||
from cpl.dependency import ServiceProviderABC
|
||||
from cpl.dependency.scope import Scope
|
||||
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
|
||||
|
||||
|
||||
class Application(ApplicationABC):
|
||||
def __init__(self, services: ServiceProviderABC):
|
||||
ApplicationABC.__init__(self, services)
|
||||
|
||||
def _part_of_scoped(self):
|
||||
ts: TestService = self._services.get_service(TestService)
|
||||
ts.run()
|
||||
|
||||
def configure(self): ...
|
||||
|
||||
def main(self):
|
||||
with self._services.create_scope() as scope:
|
||||
Console.write_line("Scope1")
|
||||
ts: TestService = scope.service_provider.get_service(TestService)
|
||||
ts.run()
|
||||
dit: DITesterService = scope.service_provider.get_service(DITesterService)
|
||||
dit.run()
|
||||
|
||||
with self._services.create_scope() as scope:
|
||||
Console.write_line("Scope2")
|
||||
ts: TestService = scope.service_provider.get_service(TestService)
|
||||
ts.run()
|
||||
dit: DITesterService = scope.service_provider.get_service(DITesterService)
|
||||
dit.run()
|
||||
|
||||
Console.write_line("Global")
|
||||
self._part_of_scoped()
|
||||
StaticTest.test()
|
||||
|
||||
self._services.get_service(Tester)
|
||||
Console.write_line(self._services.get_services(list[TestABC]))
|
||||
44
example/custom/di/src/di/di.json
Normal file
44
example/custom/di/src/di/di.json
Normal file
@@ -0,0 +1,44 @@
|
||||
{
|
||||
"Project": {
|
||||
"Name": "di",
|
||||
"Version": {
|
||||
"Major": "0",
|
||||
"Minor": "0",
|
||||
"Micro": "0"
|
||||
},
|
||||
"Author": "",
|
||||
"AuthorEmail": "",
|
||||
"Description": "",
|
||||
"LongDescription": "",
|
||||
"URL": "",
|
||||
"CopyrightDate": "",
|
||||
"CopyrightName": "",
|
||||
"LicenseName": "",
|
||||
"LicenseDescription": "",
|
||||
"Dependencies": [
|
||||
"cpl-core==2022.12.0"
|
||||
],
|
||||
"DevDependencies": [
|
||||
"cpl-cli==2022.12.0"
|
||||
],
|
||||
"PythonVersion": ">=3.9.2",
|
||||
"PythonPath": {},
|
||||
"Classifiers": []
|
||||
},
|
||||
"Build": {
|
||||
"ProjectType": "console",
|
||||
"SourcePath": "",
|
||||
"OutputPath": "../../dist",
|
||||
"Main": "di.main",
|
||||
"EntryPoint": "di",
|
||||
"IncludePackageData": false,
|
||||
"Included": [],
|
||||
"Excluded": [
|
||||
"*/__pycache__",
|
||||
"*/logs",
|
||||
"*/tests"
|
||||
],
|
||||
"PackageData": {},
|
||||
"ProjectReferences": []
|
||||
}
|
||||
}
|
||||
11
example/custom/di/src/di/di_tester_service.py
Normal file
11
example/custom/di/src/di/di_tester_service.py
Normal file
@@ -0,0 +1,11 @@
|
||||
from cpl.core.console.console import Console
|
||||
from di.test_service import TestService
|
||||
|
||||
|
||||
class DITesterService:
|
||||
def __init__(self, ts: TestService):
|
||||
self._ts = ts
|
||||
|
||||
def run(self):
|
||||
Console.write_line("DIT: ")
|
||||
self._ts.run()
|
||||
14
example/custom/di/src/di/main.py
Normal file
14
example/custom/di/src/di/main.py
Normal file
@@ -0,0 +1,14 @@
|
||||
from cpl.application import ApplicationBuilder
|
||||
|
||||
from di.application import Application
|
||||
from di.startup import Startup
|
||||
|
||||
|
||||
def main():
|
||||
app_builder = ApplicationBuilder(Application)
|
||||
app_builder.with_startup(Startup)
|
||||
app_builder.build().run()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
25
example/custom/di/src/di/startup.py
Normal file
25
example/custom/di/src/di/startup.py
Normal file
@@ -0,0 +1,25 @@
|
||||
from cpl.application.abc import StartupABC
|
||||
from cpl.dependency import ServiceProviderABC, 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
|
||||
|
||||
|
||||
class Startup(StartupABC):
|
||||
def __init__(self):
|
||||
StartupABC.__init__(self)
|
||||
|
||||
def configure_configuration(self): ...
|
||||
|
||||
def configure_services(self, services: ServiceCollection) -> ServiceProviderABC:
|
||||
services.add_scoped(TestService)
|
||||
services.add_scoped(DITesterService)
|
||||
|
||||
services.add_singleton(TestABC, Test1Service)
|
||||
services.add_singleton(TestABC, Test2Service)
|
||||
services.add_singleton(Tester)
|
||||
|
||||
return services.build()
|
||||
9
example/custom/di/src/di/static_test.py
Normal file
9
example/custom/di/src/di/static_test.py
Normal file
@@ -0,0 +1,9 @@
|
||||
from cpl.dependency import ServiceProvider, ServiceProviderABC
|
||||
from di.test_service import TestService
|
||||
|
||||
|
||||
class StaticTest:
|
||||
@staticmethod
|
||||
@ServiceProvider.inject
|
||||
def test(services: ServiceProviderABC, t1: TestService):
|
||||
t1.run()
|
||||
12
example/custom/di/src/di/test1_service.py
Normal file
12
example/custom/di/src/di/test1_service.py
Normal file
@@ -0,0 +1,12 @@
|
||||
import string
|
||||
from cpl.core.console.console import Console
|
||||
from cpl.core.utils.string import String
|
||||
from di.test_abc import TestABC
|
||||
|
||||
|
||||
class Test1Service(TestABC):
|
||||
def __init__(self):
|
||||
TestABC.__init__(self, String.random_string(string.ascii_lowercase, 8))
|
||||
|
||||
def run(self):
|
||||
Console.write_line(f"Im {self._name}")
|
||||
12
example/custom/di/src/di/test2_service.py
Normal file
12
example/custom/di/src/di/test2_service.py
Normal file
@@ -0,0 +1,12 @@
|
||||
import string
|
||||
from cpl.core.console.console import Console
|
||||
from cpl.core.utils.string import String
|
||||
from di.test_abc import TestABC
|
||||
|
||||
|
||||
class Test2Service(TestABC):
|
||||
def __init__(self):
|
||||
TestABC.__init__(self, String.random_string(string.ascii_lowercase, 8))
|
||||
|
||||
def run(self):
|
||||
Console.write_line(f"Im {self._name}")
|
||||
9
example/custom/di/src/di/test_abc.py
Normal file
9
example/custom/di/src/di/test_abc.py
Normal file
@@ -0,0 +1,9 @@
|
||||
from abc import ABC
|
||||
|
||||
|
||||
class TestABC(ABC):
|
||||
def __init__(self, name: str):
|
||||
self._name = name
|
||||
|
||||
def __repr__(self):
|
||||
return f"<{type(self).__name__} {self._name}>"
|
||||
12
example/custom/di/src/di/test_service.py
Normal file
12
example/custom/di/src/di/test_service.py
Normal file
@@ -0,0 +1,12 @@
|
||||
import string
|
||||
|
||||
from cpl.core.console.console import Console
|
||||
from cpl.core.utils.string import String
|
||||
|
||||
|
||||
class TestService:
|
||||
def __init__(self):
|
||||
self._name = String.random(8)
|
||||
|
||||
def run(self):
|
||||
Console.write_line(f"Im {self._name}")
|
||||
8
example/custom/di/src/di/tester.py
Normal file
8
example/custom/di/src/di/tester.py
Normal file
@@ -0,0 +1,8 @@
|
||||
from cpl.core.console.console import Console
|
||||
from di.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)
|
||||
0
example/custom/di/src/tests/__init__.py
Normal file
0
example/custom/di/src/tests/__init__.py
Normal file
Reference in New Issue
Block a user