Injected add_translation function from translation package

This commit is contained in:
Sven Heidemann 2022-07-10 17:17:45 +02:00
parent e4f843829f
commit 6995b25518
5 changed files with 60 additions and 15 deletions

View File

@ -27,6 +27,7 @@ class InitTemplate:
from collections import namedtuple
$Imports
VersionInfo = namedtuple('VersionInfo', 'major minor micro')

View File

@ -227,11 +227,19 @@ class PublisherService(PublisherABC):
imports = '# imports:'
else:
is_started = False
build_ignore = False
for line in module_file_lines:
if line.__contains__('# imports'):
is_started = True
if ((line.__contains__('from') or line.__contains__('import')) and is_started) or line.startswith('__cli_startup_extension__'):
if line.__contains__('# build-ignore'):
build_ignore = True
if line.__contains__('# build-ignore-end') and is_started:
module_py_lines.append('# build-ignore-end'.replace('\n', ''))
build_ignore = False
if ((line.__contains__('from') or line.__contains__('import')) and is_started) or line.startswith('__cli_startup_extension__') or build_ignore:
module_py_lines.append(line.replace('\n', ''))
if len(module_py_lines) > 0:

View File

@ -1,7 +1,6 @@
from typing import Union, Type, Callable, Optional
from cpl_core.configuration.configuration_abc import ConfigurationABC
from cpl_core.console import Console
from cpl_core.database.context.database_context_abc import DatabaseContextABC
from cpl_core.database.database_settings import DatabaseSettings
from cpl_core.dependency_injection.service_collection_abc import ServiceCollectionABC
@ -59,17 +58,6 @@ class ServiceCollection(ServiceCollectionABC):
for pipe in PipeABC.__subclasses__():
self.add_transient(PipeABC, pipe)
def add_translation(self):
try:
from cpl_translation.translation_service_abc import TranslationServiceABC
from cpl_translation.translation_service import TranslationService
from cpl_translation.translate_pipe import TranslatePipe
from cpl_translation.translation_settings import TranslationSettings
self.add_singleton(TranslationServiceABC, TranslationService)
self.add_transient(PipeABC, TranslatePipe)
except ImportError as e:
Console.error('cpl-translation is not installed', str(e))
def add_singleton(self, service_type: Union[type, object], service: Union[type, object] = None):
self._add_descriptor_by_lifetime(service_type, ServiceLifetimeEnum.singleton, service)
return self

View File

@ -35,9 +35,9 @@ class ServiceCollectionABC(ABC):
r"""Adds the CPL internal pipes as transient"""
pass
@abstractmethod
def add_translation(self):
r"""Adds the CPL translation"""
raise NotImplementedError('You should install and use the cpl-translation package')
pass
@abstractmethod

View File

@ -1 +1,49 @@
# -*- coding: utf-8 -*-
"""
cpl-translation sh-edraft Common Python library Translation
~~~~~~~~~~~~~~~~~~~
sh-edraft Common Python library Python Translation
:copyright: (c) 2022 sh-edraft.de
:license: MIT, see LICENSE for more details.
"""
__title__ = 'cpl_translation'
__author__ = 'Sven Heidemann'
__license__ = 'MIT'
__copyright__ = 'Copyright (c) 2022 sh-edraft.de'
__version__ = '2022.8.1.dev7'
from collections import namedtuple
# imports:
# build-ignore
def add_translation(self):
from cpl_core.console import Console
from cpl_core.pipes import PipeABC
from cpl_translation.translate_pipe import TranslatePipe
from cpl_translation.translation_service import TranslationService
from cpl_translation.translation_service_abc import TranslationServiceABC
try:
self.add_singleton(TranslationServiceABC, TranslationService)
self.add_transient(PipeABC, TranslatePipe)
except ImportError as e:
Console.error('cpl-translation is not installed', str(e))
def init():
from cpl_core.dependency_injection import ServiceCollection
ServiceCollection.add_translation = add_translation
init()
# build-ignore-end
VersionInfo = namedtuple('VersionInfo', 'major minor micro')
version_info = VersionInfo(major='2022', minor='8', micro='1.dev7')