Added ServiceProvider
This commit is contained in:
parent
e921338fe6
commit
bcfe6f2de4
@ -4,9 +4,7 @@ from abc import ABC, abstractmethod
|
||||
class ConfigurationModelBase(ABC):
|
||||
|
||||
@abstractmethod
|
||||
def from_dict(self, settings: dict):
|
||||
pass
|
||||
def from_dict(self, settings: dict): pass
|
||||
|
||||
@abstractmethod
|
||||
def to_dict(self) -> dict:
|
||||
pass
|
||||
def to_dict(self) -> dict: pass
|
||||
|
@ -1,30 +1,18 @@
|
||||
from abc import ABC, abstractmethod
|
||||
|
||||
from sh_edraft.publish.model.template import Template
|
||||
|
||||
|
||||
class PublisherBase(ABC):
|
||||
|
||||
@abstractmethod
|
||||
def __init__(self, source_path: str, dist_path: str, settings: list[Template]):
|
||||
self._source_path = source_path
|
||||
self._dist_path = dist_path
|
||||
self._settings = settings
|
||||
def __init__(self): pass
|
||||
|
||||
@property
|
||||
@abstractmethod
|
||||
def source_path(self) -> str:
|
||||
pass
|
||||
def source_path(self) -> str: pass
|
||||
|
||||
@property
|
||||
@abstractmethod
|
||||
def dist_path(self):
|
||||
pass
|
||||
def dist_path(self) -> str: pass
|
||||
|
||||
@abstractmethod
|
||||
def create(self):
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def publish(self):
|
||||
pass
|
||||
def publish(self) -> str: pass
|
||||
|
@ -1,15 +1,23 @@
|
||||
import os
|
||||
import shutil
|
||||
from string import Template as stringTemplate
|
||||
from typing import Optional
|
||||
|
||||
from sh_edraft.publish.base.publisher_base import PublisherBase
|
||||
from sh_edraft.publish.model.template import Template
|
||||
from sh_edraft.service.base import ServiceBase
|
||||
|
||||
|
||||
class Publisher(PublisherBase):
|
||||
class Publisher(ServiceBase, PublisherBase):
|
||||
|
||||
def __init__(self, source_path: str, dist_path: str, settings: list[Template]):
|
||||
super().__init__(source_path, dist_path, settings)
|
||||
def __init__(self):
|
||||
ServiceBase.__init__(self)
|
||||
PublisherBase.__init__(self)
|
||||
|
||||
self._logger: Optional[None] = None
|
||||
self._source_path: Optional[str] = None
|
||||
self._dist_path: Optional[str] = None
|
||||
self._settings: Optional[list[Template]] = None
|
||||
|
||||
self._included_files: list[str] = []
|
||||
self._excluded_files: list[str] = []
|
||||
@ -24,7 +32,8 @@ class Publisher(PublisherBase):
|
||||
def dist_path(self):
|
||||
return self._dist_path
|
||||
|
||||
def _get_template_output(self, t: Template, name: str, imports: str) -> str:
|
||||
@staticmethod
|
||||
def _get_template_output(t: Template, name: str, imports: str) -> str:
|
||||
try:
|
||||
if t.file_content == '':
|
||||
raise Exception(f'Template is empty: {t.template_path}')
|
||||
@ -84,7 +93,8 @@ class Publisher(PublisherBase):
|
||||
print(e)
|
||||
# todo: log error
|
||||
|
||||
def _get_template_name_from_dirs(self, file: str) -> str:
|
||||
@staticmethod
|
||||
def _get_template_name_from_dirs(file: str) -> str:
|
||||
dirs = os.path.dirname(file).split('/')
|
||||
for d in dirs:
|
||||
if d.__contains__('.'):
|
||||
@ -194,6 +204,12 @@ class Publisher(PublisherBase):
|
||||
def exclude(self, path: str):
|
||||
self._excluded_files.append(path)
|
||||
|
||||
def init(self, args: tuple):
|
||||
self._logger = args[0]
|
||||
self._source_path = args[1]
|
||||
self._dist_path = args[2]
|
||||
self._settings = args[3]
|
||||
|
||||
def create(self):
|
||||
if not self._dist_path.endswith('/'):
|
||||
self._dist_path += '/'
|
||||
|
@ -19,7 +19,8 @@ __version__ = '2020.12.0.1'
|
||||
|
||||
from collections import namedtuple
|
||||
|
||||
|
||||
# imports:
|
||||
from sh_edraft.service.service_provider import ServiceProvider
|
||||
|
||||
VersionInfo = namedtuple('VersionInfo', 'major minor micro')
|
||||
version_info = VersionInfo(major=2020, minor=12, micro=0.1)
|
||||
|
27
src/sh_edraft/service/base/__init__.py
Normal file
27
src/sh_edraft/service/base/__init__.py
Normal file
@ -0,0 +1,27 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
"""
|
||||
sh_edraft.service.base
|
||||
~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
|
||||
|
||||
:copyright: (c) 2020 sh-edraft.de
|
||||
:license: MIT, see LICENSE for more details.
|
||||
|
||||
"""
|
||||
|
||||
__title__ = 'sh_edraft.service.base'
|
||||
__author__ = 'Sven Heidemann'
|
||||
__license__ = 'MIT'
|
||||
__copyright__ = 'Copyright (c) 2020 sh-edraft.de'
|
||||
__version__ = '2020.12.0.1'
|
||||
|
||||
from collections import namedtuple
|
||||
|
||||
# imports:
|
||||
from sh_edraft.service.base.service_base import ServiceBase
|
||||
from sh_edraft.service.base.provider_base import ProviderBase
|
||||
|
||||
VersionInfo = namedtuple('VersionInfo', 'major minor micro')
|
||||
version_info = VersionInfo(major=2020, minor=12, micro=0.1)
|
30
src/sh_edraft/service/base/provider_base.py
Normal file
30
src/sh_edraft/service/base/provider_base.py
Normal file
@ -0,0 +1,30 @@
|
||||
from abc import ABC, abstractmethod
|
||||
from collections import Callable
|
||||
from typing import Type
|
||||
|
||||
from sh_edraft.service.base.service_base import ServiceBase
|
||||
from sh_edraft.service.model.provide_state import ProvideState
|
||||
|
||||
|
||||
class ProviderBase(ABC):
|
||||
|
||||
@abstractmethod
|
||||
def __init__(self):
|
||||
self._transient_services: list[ProvideState] = []
|
||||
self._scoped_services: list[ProvideState] = []
|
||||
self._singleton_services: list[ServiceBase] = []
|
||||
|
||||
@abstractmethod
|
||||
def add_transient(self, service: Type[ServiceBase], *args): pass
|
||||
|
||||
@abstractmethod
|
||||
def add_scoped(self, service: Type[ServiceBase], *args): pass
|
||||
|
||||
@abstractmethod
|
||||
def add_singleton(self, service: Type[ServiceBase], *args): pass
|
||||
|
||||
@abstractmethod
|
||||
def get_service(self, instance_type: type) -> Callable[ServiceBase]: pass
|
||||
|
||||
@abstractmethod
|
||||
def remove_service(self, instance_type: type): pass
|
13
src/sh_edraft/service/base/service_base.py
Normal file
13
src/sh_edraft/service/base/service_base.py
Normal file
@ -0,0 +1,13 @@
|
||||
from abc import ABC, abstractmethod
|
||||
|
||||
|
||||
class ServiceBase(ABC):
|
||||
|
||||
@abstractmethod
|
||||
def __init__(self): pass
|
||||
|
||||
@abstractmethod
|
||||
def init(self, args: tuple): pass
|
||||
|
||||
@abstractmethod
|
||||
def create(self): pass
|
25
src/sh_edraft/service/model/__init__.py
Normal file
25
src/sh_edraft/service/model/__init__.py
Normal file
@ -0,0 +1,25 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
"""
|
||||
sh_edraft.service.model
|
||||
~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
|
||||
|
||||
:copyright: (c) 2020 sh-edraft.de
|
||||
:license: MIT, see LICENSE for more details.
|
||||
|
||||
"""
|
||||
|
||||
__title__ = 'sh_edraft.service.model'
|
||||
__author__ = 'Sven Heidemann'
|
||||
__license__ = 'MIT'
|
||||
__copyright__ = 'Copyright (c) 2020 sh-edraft.de'
|
||||
__version__ = '2020.12.0.1'
|
||||
|
||||
from collections import namedtuple
|
||||
|
||||
# imports:
|
||||
|
||||
VersionInfo = namedtuple('VersionInfo', 'major minor micro')
|
||||
version_info = VersionInfo(major=2020, minor=12, micro=0.1)
|
18
src/sh_edraft/service/model/provide_state.py
Normal file
18
src/sh_edraft/service/model/provide_state.py
Normal file
@ -0,0 +1,18 @@
|
||||
from typing import Type
|
||||
|
||||
from sh_edraft.service.base import ServiceBase
|
||||
|
||||
|
||||
class ProvideState:
|
||||
|
||||
def __init__(self, service: Type[ServiceBase] = None, args: tuple = None):
|
||||
self._service: Type[ServiceBase] = service
|
||||
self._args: tuple = args
|
||||
|
||||
@property
|
||||
def service(self):
|
||||
return self._service
|
||||
|
||||
@property
|
||||
def args(self) -> tuple:
|
||||
return self._args
|
68
src/sh_edraft/service/service_provider.py
Normal file
68
src/sh_edraft/service/service_provider.py
Normal file
@ -0,0 +1,68 @@
|
||||
from collections import Callable
|
||||
from typing import Type
|
||||
|
||||
from termcolor import colored
|
||||
|
||||
from sh_edraft.service.base.provider_base import ProviderBase
|
||||
from sh_edraft.service.base.service_base import ServiceBase
|
||||
from sh_edraft.service.model.provide_state import ProvideState
|
||||
|
||||
|
||||
class ServiceProvider(ProviderBase):
|
||||
|
||||
def __init__(self):
|
||||
ProviderBase.__init__(self)
|
||||
|
||||
def create(self):
|
||||
pass
|
||||
|
||||
@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))
|
||||
|
||||
def get_service(self, instance_type: type) -> Callable[ServiceBase]:
|
||||
for state in self._transient_services:
|
||||
if state.service == instance_type:
|
||||
return self._create_instance(state.service, state.args)
|
||||
|
||||
for state in self._scoped_services:
|
||||
if type(state.service) == instance_type:
|
||||
return self._create_instance(state.service, state.args)
|
||||
|
||||
for service in self._singleton_services:
|
||||
if type(service) == instance_type:
|
||||
return service
|
||||
|
||||
def remove_service(self, instance_type: type):
|
||||
for state in self._transient_services:
|
||||
if state.service == instance_type:
|
||||
self._transient_services.remove(state)
|
||||
return
|
||||
|
||||
for state in self._scoped_services:
|
||||
if type(state.service) == instance_type:
|
||||
self._scoped_services.remove(state)
|
||||
return
|
||||
|
||||
for service in self._singleton_services:
|
||||
if type(service) == instance_type:
|
||||
self._singleton_services.remove(service)
|
||||
return
|
Loading…
Reference in New Issue
Block a user