Added logger and improved service provider
This commit is contained in:
@@ -21,7 +21,7 @@ from collections import namedtuple
|
||||
|
||||
# imports:
|
||||
from sh_edraft.service.base.service_base import ServiceBase
|
||||
from sh_edraft.service.base.provider_base import ProviderBase
|
||||
from sh_edraft.service.base.service_provider_base import ServiceProviderBase
|
||||
|
||||
VersionInfo = namedtuple('VersionInfo', 'major minor micro')
|
||||
version_info = VersionInfo(major=2020, minor=12, micro=0.1)
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
from abc import ABC, abstractmethod
|
||||
from abc import abstractmethod
|
||||
from collections import Callable
|
||||
from typing import Type
|
||||
|
||||
@@ -6,10 +6,11 @@ from sh_edraft.service.base.service_base import ServiceBase
|
||||
from sh_edraft.service.model.provide_state import ProvideState
|
||||
|
||||
|
||||
class ProviderBase(ABC):
|
||||
class ServiceProviderBase(ServiceBase):
|
||||
|
||||
@abstractmethod
|
||||
def __init__(self):
|
||||
ServiceBase.__init__(self)
|
||||
self._transient_services: list[ProvideState] = []
|
||||
self._scoped_services: list[ProvideState] = []
|
||||
self._singleton_services: list[ServiceBase] = []
|
||||
@@ -24,7 +25,7 @@ class ProviderBase(ABC):
|
||||
def add_singleton(self, service: Type[ServiceBase], *args): pass
|
||||
|
||||
@abstractmethod
|
||||
def get_service(self, instance_type: type) -> Callable[ServiceBase]: pass
|
||||
def get_service(self, instance_type: Type[ServiceBase]) -> Callable[ServiceBase]: pass
|
||||
|
||||
@abstractmethod
|
||||
def remove_service(self, instance_type: type): pass
|
||||
@@ -3,18 +3,19 @@ from typing import Type
|
||||
|
||||
from termcolor import colored
|
||||
|
||||
from sh_edraft.service.base.provider_base import ProviderBase
|
||||
from sh_edraft.service.base.service_provider_base import ServiceProviderBase
|
||||
from sh_edraft.service.base.service_base import ServiceBase
|
||||
from sh_edraft.service.model.provide_state import ProvideState
|
||||
|
||||
|
||||
class ServiceProvider(ProviderBase):
|
||||
class ServiceProvider(ServiceProviderBase):
|
||||
|
||||
def __init__(self):
|
||||
ProviderBase.__init__(self)
|
||||
super().__init__()
|
||||
|
||||
def create(self):
|
||||
pass
|
||||
def init(self, args: tuple): pass
|
||||
|
||||
def create(self): pass
|
||||
|
||||
@staticmethod
|
||||
def _create_instance(service: type[ServiceBase], args: tuple) -> ServiceBase:
|
||||
@@ -38,31 +39,31 @@ class ServiceProvider(ProviderBase):
|
||||
|
||||
self._singleton_services.append(self._create_instance(service, args))
|
||||
|
||||
def get_service(self, instance_type: type) -> Callable[ServiceBase]:
|
||||
def get_service(self, instance_type: Type[ServiceBase]) -> Callable[ServiceBase]:
|
||||
for state in self._transient_services:
|
||||
if state.service == instance_type:
|
||||
if isinstance(state.service, type(instance_type)):
|
||||
return self._create_instance(state.service, state.args)
|
||||
|
||||
for state in self._scoped_services:
|
||||
if type(state.service) == instance_type:
|
||||
if isinstance(state.service, type(instance_type)):
|
||||
return self._create_instance(state.service, state.args)
|
||||
|
||||
for service in self._singleton_services:
|
||||
if type(service) == instance_type:
|
||||
if isinstance(service, instance_type):
|
||||
return service
|
||||
|
||||
def remove_service(self, instance_type: type):
|
||||
for state in self._transient_services:
|
||||
if state.service == instance_type:
|
||||
if isinstance(state.service, type(instance_type)):
|
||||
self._transient_services.remove(state)
|
||||
return
|
||||
|
||||
for state in self._scoped_services:
|
||||
if type(state.service) == instance_type:
|
||||
if isinstance(state.service, type(instance_type)):
|
||||
self._scoped_services.remove(state)
|
||||
return
|
||||
|
||||
for service in self._singleton_services:
|
||||
if type(service) == instance_type:
|
||||
if isinstance(service, instance_type):
|
||||
self._singleton_services.remove(service)
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user