Added logic to generate new projects with async

This commit is contained in:
Sven Heidemann 2021-11-14 12:32:11 +01:00
parent 4b829cc484
commit 589ef4fd4c
6 changed files with 271 additions and 122 deletions

View File

@ -5,29 +5,51 @@ from cpl_cli._templates.template_file_abc import TemplateFileABC
class ApplicationTemplate(TemplateFileABC): class ApplicationTemplate(TemplateFileABC):
def __init__(self, name: str, path: str): def __init__(self, name: str, path: str, use_async: bool):
TemplateFileABC.__init__(self) TemplateFileABC.__init__(self)
self._name = 'application.py' self._name = 'application.py'
self._path = path self._path = path
self._value = textwrap.dedent("""\ self._use_async = use_async
from cpl_core.application import ApplicationABC
from cpl_core.configuration import ConfigurationABC if self._use_async:
from cpl_core.console import Console self._value = textwrap.dedent("""\
from cpl_core.dependency_injection import ServiceProviderABC from cpl_core.application import ApplicationABC
from cpl_core.configuration import ConfigurationABC
from cpl_core.console import Console
from cpl_core.dependency_injection import ServiceProviderABC
class Application(ApplicationABC): class Application(ApplicationABC):
def __init__(self, config: ConfigurationABC, services: ServiceProviderABC): def __init__(self, config: ConfigurationABC, services: ServiceProviderABC):
ApplicationABC.__init__(self, config, services) ApplicationABC.__init__(self, config, services)
def configure(self): async def configure(self):
pass pass
def main(self): async def main(self):
Console.write_line('Hello World') Console.write_line('Hello World')
""") """)
else:
self._value = textwrap.dedent("""\
from cpl_core.application import ApplicationABC
from cpl_core.configuration import ConfigurationABC
from cpl_core.console import Console
from cpl_core.dependency_injection import ServiceProviderABC
class Application(ApplicationABC):
def __init__(self, config: ConfigurationABC, services: ServiceProviderABC):
ApplicationABC.__init__(self, config, services)
def configure(self):
pass
def main(self):
Console.write_line('Hello World')
""")
@property @property
def name(self) -> str: def name(self) -> str:

View File

@ -6,31 +6,52 @@ from cpl_cli._templates.template_file_abc import TemplateFileABC
class MainWithApplicationHostAndStartupTemplate(TemplateFileABC): class MainWithApplicationHostAndStartupTemplate(TemplateFileABC):
def __init__(self, name: str, path: str): def __init__(self, name: str, path: str, use_async: bool):
TemplateFileABC.__init__(self) TemplateFileABC.__init__(self)
name = String.convert_to_snake_case(name) name = String.convert_to_snake_case(name)
self._name = 'main.py' self._name = 'main.py'
self._path = path self._path = path
self._use_async = use_async
import_pkg = f'{name}.' import_pkg = f'{name}.'
self._value = textwrap.dedent(f"""\ if use_async:
from cpl_core.application import ApplicationBuilder self._value = textwrap.dedent(f"""\
from cpl_core.application import ApplicationBuilder
from {import_pkg}application import Application from {import_pkg}application import Application
from {import_pkg}startup import Startup from {import_pkg}startup import Startup
def main(): async def main():
app_builder = ApplicationBuilder(Application) app_builder = ApplicationBuilder(Application)
app_builder.use_startup(Startup) app_builder.use_startup(Startup)
app_builder.build().run() app: Application = await app_builder.build_async()
await app.run()
if __name__ == '__main__': if __name__ == '__main__':
main() ml = asyncio.get_event_loop()
""") ml.run_until_complete(main())
""")
else:
self._value = textwrap.dedent(f"""\
from cpl_core.application import ApplicationBuilder
from {import_pkg}application import Application
from {import_pkg}startup import Startup
def main():
app_builder = ApplicationBuilder(Application)
app_builder.use_startup(Startup)
app_builder.build().run()
if __name__ == '__main__':
main()
""")
@property @property
def name(self) -> str: def name(self) -> str:
@ -47,29 +68,48 @@ class MainWithApplicationHostAndStartupTemplate(TemplateFileABC):
class MainWithApplicationBaseTemplate(TemplateFileABC): class MainWithApplicationBaseTemplate(TemplateFileABC):
def __init__(self, name: str, path: str): def __init__(self, name: str, path: str, use_async: bool):
TemplateFileABC.__init__(self) TemplateFileABC.__init__(self)
name = String.convert_to_snake_case(name) name = String.convert_to_snake_case(name)
self._name = 'main.py' self._name = 'main.py'
self._path = path self._path = path
self._use_async = use_async
import_pkg = f'{name}.' import_pkg = f'{name}.'
self._value = textwrap.dedent(f"""\ if use_async:
from cpl_core.application import ApplicationBuilder self._value = textwrap.dedent(f"""\
from cpl_core.application import ApplicationBuilder
from {import_pkg}application import Application from {import_pkg}application import Application
def main(): async def main():
app_builder = ApplicationBuilder(Application) app_builder = ApplicationBuilder(Application)
app_builder.build().run() app: Application = await app_builder.build_async()
await app.run()
if __name__ == '__main__': if __name__ == '__main__':
main() ml = asyncio.get_event_loop()
""") ml.run_until_complete(main())
""")
else:
self._value = textwrap.dedent(f"""\
from cpl_core.application import ApplicationBuilder
from {import_pkg}application import Application
def main():
app_builder = ApplicationBuilder(Application)
app_builder.build().run()
if __name__ == '__main__':
main()
""")
@property @property
def name(self) -> str: def name(self) -> str:
@ -86,26 +126,41 @@ class MainWithApplicationBaseTemplate(TemplateFileABC):
class MainWithoutApplicationBaseTemplate(TemplateFileABC): class MainWithoutApplicationBaseTemplate(TemplateFileABC):
def __init__(self, name: str, path: str): def __init__(self, name: str, path: str, use_async: bool):
TemplateFileABC.__init__(self) TemplateFileABC.__init__(self)
name = String.convert_to_snake_case(name) name = String.convert_to_snake_case(name)
self._name = 'main.py' self._name = 'main.py'
self._path = path self._path = path
self._use_async = use_async
import_pkg = f'{name}.' import_pkg = f'{name}.'
self._value = textwrap.dedent("""\ if use_async:
from cpl_core.console import Console self._value = textwrap.dedent("""\
from cpl_core.console import Console
def main(): async def main():
Console.write_line('Hello World') Console.write_line('Hello World')
if __name__ == '__main__': if __name__ == '__main__':
main() ml = asyncio.get_event_loop()
""") ml.run_until_complete(main())
""")
else:
self._value = textwrap.dedent("""\
from cpl_core.console import Console
def main():
Console.write_line('Hello World')
if __name__ == '__main__':
main()
""")
@property @property
def name(self) -> str: def name(self) -> str:
@ -122,40 +177,69 @@ class MainWithoutApplicationBaseTemplate(TemplateFileABC):
class MainWithDependencyInjection(TemplateFileABC): class MainWithDependencyInjection(TemplateFileABC):
def __init__(self, name: str, path: str): def __init__(self, name: str, path: str, use_async: bool):
TemplateFileABC.__init__(self) TemplateFileABC.__init__(self)
name = String.convert_to_snake_case(name) name = String.convert_to_snake_case(name)
self._name = 'main.py' self._name = 'main.py'
self._path = path self._path = path
self._use_async = use_async
import_pkg = f'{name}.' import_pkg = f'{name}.'
self._value = textwrap.dedent("""\ if use_async:
from cpl_core.configuration import Configuration, ConfigurationABC self._value = textwrap.dedent("""\
from cpl_core.console import Console from cpl_core.configuration import Configuration, ConfigurationABC
from cpl_core.dependency_injection import ServiceCollection, ServiceProviderABC from cpl_core.console import Console
from cpl_core.dependency_injection import ServiceCollection, ServiceProviderABC
def configure_configuration() -> ConfigurationABC: async def configure_configuration() -> ConfigurationABC:
config = Configuration() config = Configuration()
return config return config
def configure_services(config: ConfigurationABC) -> ServiceProviderABC: async def configure_services(config: ConfigurationABC) -> ServiceProviderABC:
services = ServiceCollection(config) services = ServiceCollection(config)
return services.build_service_provider() return services.build_service_provider()
def main(): async def main():
config = configure_configuration() await config = configure_configuration()
provider = configure_services(config) await provider = configure_services(config)
Console.write_line('Hello World') Console.write_line('Hello World')
if __name__ == '__main__': if __name__ == '__main__':
main() ml = asyncio.get_event_loop()
""") ml.run_until_complete(main())
""")
else:
self._value = textwrap.dedent("""\
from cpl_core.configuration import Configuration, ConfigurationABC
from cpl_core.console import Console
from cpl_core.dependency_injection import ServiceCollection, ServiceProviderABC
def configure_configuration() -> ConfigurationABC:
config = Configuration()
return config
def configure_services(config: ConfigurationABC) -> ServiceProviderABC:
services = ServiceCollection(config)
return services.build_service_provider()
def main():
config = configure_configuration()
provider = configure_services(config)
Console.write_line('Hello World')
if __name__ == '__main__':
main()
""")
@property @property
def name(self) -> str: def name(self) -> str:

View File

@ -5,29 +5,51 @@ from cpl_cli._templates.template_file_abc import TemplateFileABC
class StartupTemplate(TemplateFileABC): class StartupTemplate(TemplateFileABC):
def __init__(self, name: str, path: str): def __init__(self, name: str, path: str, use_async: bool):
TemplateFileABC.__init__(self) TemplateFileABC.__init__(self)
self._name = 'startup.py' self._name = 'startup.py'
self._path = path self._path = path
self._value = textwrap.dedent("""\ self._use_async = use_async
from cpl_core.application import StartupABC
from cpl_core.configuration import ConfigurationABC if self._use_async:
from cpl_core.dependency_injection import ServiceProviderABC, ServiceCollectionABC self._value = textwrap.dedent("""\
from cpl_core.environment import ApplicationEnvironment from cpl_core.application import StartupABC
from cpl_core.configuration import ConfigurationABC
from cpl_core.dependency_injection import ServiceProviderABC, ServiceCollectionABC
from cpl_core.environment import ApplicationEnvironment
class Startup(StartupABC): class Startup(StartupABC):
def __init__(self): def __init__(self):
StartupABC.__init__(self) StartupABC.__init__(self)
def configure_configuration(self, configuration: ConfigurationABC, environment: ApplicationEnvironment) -> ConfigurationABC: async def configure_configuration(self, configuration: ConfigurationABC, environment: ApplicationEnvironment) -> ConfigurationABC:
return configuration return configuration
def configure_services(self, services: ServiceCollectionABC, environment: ApplicationEnvironment) -> ServiceProviderABC: async def configure_services(self, services: ServiceCollectionABC, environment: ApplicationEnvironment) -> ServiceProviderABC:
return services.build_service_provider() return services.build_service_provider()
""") """)
else:
self._value = textwrap.dedent("""\
from cpl_core.application import StartupABC
from cpl_core.configuration import ConfigurationABC
from cpl_core.dependency_injection import ServiceProviderABC, ServiceCollectionABC
from cpl_core.environment import ApplicationEnvironment
class Startup(StartupABC):
def __init__(self):
StartupABC.__init__(self)
def configure_configuration(self, configuration: ConfigurationABC, environment: ApplicationEnvironment) -> ConfigurationABC:
return configuration
def configure_services(self, services: ServiceCollectionABC, environment: ApplicationEnvironment) -> ServiceProviderABC:
return services.build_service_provider()
""")
@property @property
def name(self) -> str: def name(self) -> str:

View File

@ -46,6 +46,7 @@ class NewService(CommandABC):
self._use_application_api: bool = False self._use_application_api: bool = False
self._use_startup: bool = False self._use_startup: bool = False
self._use_service_providing: bool = False self._use_service_providing: bool = False
self._use_async: bool = False
@property @property
def help_message(self) -> str: def help_message(self) -> str:
@ -178,6 +179,10 @@ class NewService(CommandABC):
if result.lower() == 'y': if result.lower() == 'y':
self._use_service_providing = True self._use_service_providing = True
result = Console.read('Do you want to use async? (y/n) ')
if result.lower == 'y':
self._use_async = True
Console.set_foreground_color(ForegroundColorEnum.default) Console.set_foreground_color(ForegroundColorEnum.default)
def _console(self, args: list[str]): def _console(self, args: list[str]):
@ -202,6 +207,7 @@ class NewService(CommandABC):
self._use_application_api, self._use_application_api,
self._use_startup, self._use_startup,
self._use_service_providing, self._use_service_providing,
self._use_async,
self._project.name, self._project.name,
self._project_json, self._project_json,
self._workspace self._workspace
@ -231,6 +237,7 @@ class NewService(CommandABC):
self._use_application_api, self._use_application_api,
self._use_startup, self._use_startup,
self._use_service_providing, self._use_service_providing,
self._use_async,
self._project.name, self._project.name,
self._project_json, self._project_json,
self._workspace self._workspace

View File

@ -59,13 +59,14 @@ class ConsoleBuilder:
@classmethod @classmethod
def build(cls, project_path: str, use_application_api: bool, use_startup: bool, use_service_providing: bool, def build(cls, project_path: str, use_application_api: bool, use_startup: bool, use_service_providing: bool,
project_name: str, project_settings: dict, workspace: Optional[WorkspaceSettings]): use_async: bool, project_name: str, project_settings: dict, workspace: Optional[WorkspaceSettings]):
""" """
Builds the console project files Builds the console project files
:param project_path: :param project_path:
:param use_application_api: :param use_application_api:
:param use_startup: :param use_startup:
:param use_service_providing: :param use_service_providing:
:param use_async:
:param project_name: :param project_name:
:param project_settings: :param project_settings:
:param workspace: :param workspace:
@ -79,7 +80,8 @@ class ConsoleBuilder:
ReadmeTemplate(), ReadmeTemplate(),
TestsInitTemplate(), TestsInitTemplate(),
AppsettingsTemplate(), AppsettingsTemplate(),
MainInitTemplate(project_name, os.path.join('src/', project_name_snake)) MainInitTemplate(project_name, os.path.join(
'src/', project_name_snake))
] ]
else: else:
project_path = os.path.join( project_path = os.path.join(
@ -103,18 +105,22 @@ class ConsoleBuilder:
src_rel_path = os.path.join('src/', src_name) src_rel_path = os.path.join('src/', src_name)
if use_application_api: if use_application_api:
templates.append(ApplicationTemplate(src_name, src_rel_path)) templates.append(ApplicationTemplate(src_name, src_rel_path, use_async))
if use_startup: if use_startup:
templates.append(StartupTemplate(src_name, src_rel_path)) templates.append(StartupTemplate(src_name, src_rel_path, use_async))
templates.append(MainWithApplicationHostAndStartupTemplate(src_name, src_rel_path)) templates.append(MainWithApplicationHostAndStartupTemplate(
src_name, src_rel_path, use_async))
else: else:
templates.append(MainWithApplicationBaseTemplate(src_name, src_rel_path)) templates.append(MainWithApplicationBaseTemplate(
src_name, src_rel_path, use_async))
else: else:
if use_service_providing: if use_service_providing:
templates.append(MainWithDependencyInjection(src_name, src_rel_path)) templates.append(MainWithDependencyInjection(
src_name, src_rel_path, use_async))
else: else:
templates.append(MainWithoutApplicationBaseTemplate(src_name, src_rel_path)) templates.append(MainWithoutApplicationBaseTemplate(
src_name, src_rel_path, use_async))
proj_name = project_name proj_name = project_name
if workspace is not None: if workspace is not None:
@ -132,7 +138,8 @@ class ConsoleBuilder:
else: else:
workspace.projects[project_name] = f'src/{project_file_path}' workspace.projects[project_name] = f'src/{project_file_path}'
cls._create_workspace('cpl-workspace.json', workspace.default_project, workspace.projects) cls._create_workspace('cpl-workspace.json',
workspace.default_project, workspace.projects)
Console.spinner( Console.spinner(
f'Creating {project_file_path}', f'Creating {project_file_path}',

View File

@ -59,7 +59,7 @@ class LibraryBuilder:
@classmethod @classmethod
def build(cls, project_path: str, use_application_api: bool, use_startup: bool, def build(cls, project_path: str, use_application_api: bool, use_startup: bool,
use_service_providing: bool, project_name: str, project_settings: dict, use_async: bool, use_service_providing: bool, project_name: str, project_settings: dict,
workspace: Optional[WorkspaceSettings]): workspace: Optional[WorkspaceSettings]):
""" """
Builds the library project files Builds the library project files
@ -67,6 +67,7 @@ class LibraryBuilder:
:param use_application_api: :param use_application_api:
:param use_startup: :param use_startup:
:param use_service_providing: :param use_service_providing:
:param use_async:
:param project_name: :param project_name:
:param project_settings: :param project_settings:
:param workspace: :param workspace:
@ -79,7 +80,8 @@ class LibraryBuilder:
LicenseTemplate(), LicenseTemplate(),
ReadmeTemplate(), ReadmeTemplate(),
TestsInitTemplate(), TestsInitTemplate(),
NameInitTemplate(project_name, os.path.join('src/', project_name_snake)), NameInitTemplate(project_name, os.path.join(
'src/', project_name_snake)),
AppsettingsTemplate() AppsettingsTemplate()
] ]
else: else:
@ -108,14 +110,18 @@ class LibraryBuilder:
if use_startup: if use_startup:
templates.append(StartupTemplate(src_name, src_rel_path)) templates.append(StartupTemplate(src_name, src_rel_path))
templates.append(MainWithApplicationHostAndStartupTemplate(src_name, src_rel_path)) templates.append(MainWithApplicationHostAndStartupTemplate(
src_name, src_rel_path))
else: else:
templates.append(MainWithApplicationBaseTemplate(src_name, src_rel_path)) templates.append(MainWithApplicationBaseTemplate(
src_name, src_rel_path))
else: else:
if use_service_providing: if use_service_providing:
templates.append(MainWithDependencyInjection(src_name, src_rel_path)) templates.append(MainWithDependencyInjection(
src_name, src_rel_path))
else: else:
templates.append(MainWithoutApplicationBaseTemplate(src_name, src_rel_path)) templates.append(MainWithoutApplicationBaseTemplate(
src_name, src_rel_path))
proj_name = project_name proj_name = project_name
if workspace is not None: if workspace is not None:
@ -133,7 +139,8 @@ class LibraryBuilder:
else: else:
workspace.projects[project_name] = f'src/{project_file_path}' workspace.projects[project_name] = f'src/{project_file_path}'
cls._create_workspace('cpl-workspace.json', workspace.default_project, workspace.projects) cls._create_workspace('cpl-workspace.json',
workspace.default_project, workspace.projects)
Console.spinner( Console.spinner(
f'Creating {project_file_path}', f'Creating {project_file_path}',