From 69211cc9e8aee1987a4138e4a63140e058c7eb96 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Thu, 4 Mar 2021 19:06:16 +0100 Subject: [PATCH] Improved dependency injection --- src/cpl/configuration/configuration.py | 5 +- src/cpl/configuration/configuration_abc.py | 2 +- .../dependency_injection/service_provider.py | 8 +++- .../commands => cpl/version}/__init__.py | 0 src/cpl/version/version.py | 46 +++++++++++++++++++ src/cpl/version/version_enum.py | 8 ++++ src/tests/cpl.json | 12 ++++- 7 files changed, 74 insertions(+), 7 deletions(-) rename src/{cpl_cli/commands => cpl/version}/__init__.py (100%) create mode 100644 src/cpl/version/version.py create mode 100644 src/cpl/version/version_enum.py diff --git a/src/cpl/configuration/configuration.py b/src/cpl/configuration/configuration.py index 5a19599d..8ee1b859 100644 --- a/src/cpl/configuration/configuration.py +++ b/src/cpl/configuration/configuration.py @@ -134,7 +134,7 @@ class Configuration(ConfigurationABC): exit() - def add_json_file(self, name: str, optional: bool = None, output: bool = False): + def add_json_file(self, name: str, optional: bool = None, output: bool = True): if self._hosting_environment.content_root_path.endswith('/') and not name.startswith('/'): file_path = f'{self._hosting_environment.content_root_path}{name}' else: @@ -177,8 +177,7 @@ class Configuration(ConfigurationABC): def add_configuration(self, key_type: type, value: ConfigurationModelABC): self._config[key_type] = value - def get_configuration(self, search_type: Union[str, Type[ConfigurationModelABC]]) -> Union[ - str, Callable[ConfigurationModelABC]]: + def get_configuration(self, search_type: Union[str, Type[ConfigurationModelABC]]) -> Union[str, Callable[ConfigurationModelABC]]: if search_type not in self._config: raise Exception(f'Config model by type {search_type} not found') diff --git a/src/cpl/configuration/configuration_abc.py b/src/cpl/configuration/configuration_abc.py index b34e25ca..6a6a4b26 100644 --- a/src/cpl/configuration/configuration_abc.py +++ b/src/cpl/configuration/configuration_abc.py @@ -37,7 +37,7 @@ class ConfigurationABC(ABC): def add_console_arguments(self): pass @abstractmethod - def add_json_file(self, name: str, optional: bool = None, output: bool = False): pass + def add_json_file(self, name: str, optional: bool = None, output: bool = True): pass @abstractmethod def add_configuration(self, key_type: type, value: object): pass diff --git a/src/cpl/dependency_injection/service_provider.py b/src/cpl/dependency_injection/service_provider.py index a64d014d..6e7928a6 100644 --- a/src/cpl/dependency_injection/service_provider.py +++ b/src/cpl/dependency_injection/service_provider.py @@ -1,11 +1,9 @@ -from abc import ABC from collections import Callable from inspect import signature, Parameter from typing import Type, Optional, Union from cpl.application.application_runtime_abc import ApplicationRuntimeABC from cpl.configuration.configuration_model_abc import ConfigurationModelABC -from cpl.console.console import Console from cpl.database.context.database_context_abc import DatabaseContextABC from cpl.dependency_injection.service_abc import ServiceABC from cpl.dependency_injection.service_provider_base import ServiceProviderABC @@ -41,6 +39,9 @@ class ServiceProvider(ServiceProviderABC): elif issubclass(parameter.annotation, ConfigurationModelABC): params.append(self._app_runtime.configuration.get_configuration(parameter.annotation)) + elif issubclass(parameter.annotation, ServiceProviderABC): + params.append(self) + else: params.append(self.get_service(parameter.annotation)) @@ -75,6 +76,9 @@ class ServiceProvider(ServiceProviderABC): self._singleton_services[service_type] = self._create_instance(service) def get_service(self, instance_type: Type) -> Callable[ServiceABC]: + if issubclass(instance_type, ServiceProviderABC): + return self + for service in self._transient_services: if service == instance_type and isinstance(self._transient_services[service], type(instance_type)): return self._create_instance(self._transient_services[service]) diff --git a/src/cpl_cli/commands/__init__.py b/src/cpl/version/__init__.py similarity index 100% rename from src/cpl_cli/commands/__init__.py rename to src/cpl/version/__init__.py diff --git a/src/cpl/version/version.py b/src/cpl/version/version.py new file mode 100644 index 00000000..138fd6b4 --- /dev/null +++ b/src/cpl/version/version.py @@ -0,0 +1,46 @@ +from typing import Optional + +from cpl.configuration.configuration_model_abc import ConfigurationModelABC +from cpl.version.version_enum import VersionEnum + + +class Version(ConfigurationModelABC): + + def __init__( + self, + major: int = None, + minor: int = None, + micro: float = None + ): + ConfigurationModelABC.__init__(self) + + self._major: Optional[int] = major + self._minor: Optional[int] = minor + self._micro: Optional[int] = micro + + @property + def major(self) -> int: + return self._major + + @property + def minor(self) -> int: + return self._minor + + @property + def micro(self) -> float: + return self._micro + + def to_str(self) -> str: + return f'{self._major}.{self._minor}.{self._micro}' + + def from_dict(self, settings: dict): + self._major = int(settings[VersionEnum.Major.value]) + self._minor = int(settings[VersionEnum.Minor.value]) + self._micro = int(settings[VersionEnum.Micro.value]) + + def to_dict(self) -> dict: + return { + VersionEnum.Major.value: self._major, + VersionEnum.Minor.value: self._minor, + VersionEnum.Micro.value: self._micro + } diff --git a/src/cpl/version/version_enum.py b/src/cpl/version/version_enum.py new file mode 100644 index 00000000..8c294c1e --- /dev/null +++ b/src/cpl/version/version_enum.py @@ -0,0 +1,8 @@ +from enum import Enum + + +class VersionEnum(Enum): + + Major = 'Major' + Minor = 'Minor' + Micro = 'Micro' diff --git a/src/tests/cpl.json b/src/tests/cpl.json index 9e26dfee..a40f5e6c 100644 --- a/src/tests/cpl.json +++ b/src/tests/cpl.json @@ -1 +1,11 @@ -{} \ No newline at end of file +{ + "ProjectSettings": { + "Version": { + "major": "2021", + "minor": "04", + "micro": "01" + }, + "ExcludedFiles": [], + "DistPath": "../../dist" + } +} \ No newline at end of file