2020-11-22 14:15:39 +01:00
|
|
|
from collections import Callable
|
|
|
|
from typing import Type
|
|
|
|
|
|
|
|
from termcolor import colored
|
|
|
|
|
2020-11-22 20:17:57 +01:00
|
|
|
from sh_edraft.service.base.service_provider_base import ServiceProviderBase
|
2020-11-22 14:15:39 +01:00
|
|
|
from sh_edraft.service.base.service_base import ServiceBase
|
|
|
|
from sh_edraft.service.model.provide_state import ProvideState
|
|
|
|
|
|
|
|
|
2020-11-22 20:17:57 +01:00
|
|
|
class ServiceProvider(ServiceProviderBase):
|
2020-11-22 14:15:39 +01:00
|
|
|
|
|
|
|
def __init__(self):
|
2020-11-22 20:17:57 +01:00
|
|
|
super().__init__()
|
2020-11-22 14:15:39 +01:00
|
|
|
|
2020-11-22 20:17:57 +01:00
|
|
|
def init(self, args: tuple): pass
|
|
|
|
|
|
|
|
def create(self): pass
|
2020-11-22 14:15:39 +01:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def _create_instance(service: type[ServiceBase], args: tuple) -> ServiceBase:
|
|
|
|
instance = service()
|
|
|
|
try:
|
|
|
|
instance.init(args)
|
|
|
|
return instance
|
|
|
|
except Exception as e:
|
|
|
|
print(colored(f'Argument error\n{e}', 'red'))
|
|
|
|
|
|
|
|
def add_transient(self, service: Type[ServiceBase], *args):
|
|
|
|
self._transient_services.append(ProvideState(service, args))
|
|
|
|
|
|
|
|
def add_scoped(self, service: Type[ServiceBase], *args):
|
|
|
|
self._transient_services.append(ProvideState(service, args))
|
|
|
|
|
|
|
|
def add_singleton(self, service: Type[ServiceBase], *args):
|
|
|
|
for known_service in self._singleton_services:
|
|
|
|
if type(known_service) == type(service):
|
|
|
|
raise Exception(f'Service from type {type(service)} already exists')
|
|
|
|
|
|
|
|
self._singleton_services.append(self._create_instance(service, args))
|
|
|
|
|
2020-11-22 20:17:57 +01:00
|
|
|
def get_service(self, instance_type: Type[ServiceBase]) -> Callable[ServiceBase]:
|
2020-11-22 14:15:39 +01:00
|
|
|
for state in self._transient_services:
|
2020-11-22 20:17:57 +01:00
|
|
|
if isinstance(state.service, type(instance_type)):
|
2020-11-22 14:15:39 +01:00
|
|
|
return self._create_instance(state.service, state.args)
|
|
|
|
|
|
|
|
for state in self._scoped_services:
|
2020-11-22 20:17:57 +01:00
|
|
|
if isinstance(state.service, type(instance_type)):
|
2020-11-22 14:15:39 +01:00
|
|
|
return self._create_instance(state.service, state.args)
|
|
|
|
|
|
|
|
for service in self._singleton_services:
|
2020-11-22 20:17:57 +01:00
|
|
|
if isinstance(service, instance_type):
|
2020-11-22 14:15:39 +01:00
|
|
|
return service
|
|
|
|
|
|
|
|
def remove_service(self, instance_type: type):
|
|
|
|
for state in self._transient_services:
|
2020-11-22 20:17:57 +01:00
|
|
|
if isinstance(state.service, type(instance_type)):
|
2020-11-22 14:15:39 +01:00
|
|
|
self._transient_services.remove(state)
|
|
|
|
return
|
|
|
|
|
|
|
|
for state in self._scoped_services:
|
2020-11-22 20:17:57 +01:00
|
|
|
if isinstance(state.service, type(instance_type)):
|
2020-11-22 14:15:39 +01:00
|
|
|
self._scoped_services.remove(state)
|
|
|
|
return
|
|
|
|
|
|
|
|
for service in self._singleton_services:
|
2020-11-22 20:17:57 +01:00
|
|
|
if isinstance(service, instance_type):
|
2020-11-22 14:15:39 +01:00
|
|
|
self._singleton_services.remove(service)
|
|
|
|
return
|