[WIP] Improved cpl new templating #139
This commit is contained in:
42
src/cpl_cli/.cpl/project_console.py
Normal file
42
src/cpl_cli/.cpl/project_console.py
Normal file
@@ -0,0 +1,42 @@
|
||||
import os
|
||||
|
||||
from cpl_cli.abc.project_type_abc import ProjectTypeABC
|
||||
from cpl_cli.configuration import WorkspaceSettings
|
||||
from cpl_core.utils import String
|
||||
|
||||
|
||||
class Console(ProjectTypeABC):
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
base_path: str,
|
||||
project_name: str,
|
||||
workspace: WorkspaceSettings,
|
||||
use_application_api: bool,
|
||||
use_startup: bool,
|
||||
use_service_providing: bool,
|
||||
use_async: bool,
|
||||
):
|
||||
from project_file_license import ProjectFileLicense
|
||||
from project_file_readme import ProjectFileReadme
|
||||
from schematic_init import Init
|
||||
from project_file_code_application import ProjectFileApplication
|
||||
from project_file_code_main import ProjectFileMain
|
||||
from project_file_code_startup import ProjectFileStartup
|
||||
|
||||
ProjectTypeABC.__init__(self, base_path, project_name, workspace, use_application_api, use_startup, use_service_providing, use_async)
|
||||
|
||||
project_path = f'{base_path}{String.convert_to_snake_case(project_name.split("/")[-1])}/'
|
||||
|
||||
self.add_template(ProjectFileLicense(''))
|
||||
self.add_template(ProjectFileReadme(''))
|
||||
self.add_template(Init('', 'init', f'{base_path}tests/'))
|
||||
self.add_template(Init('', 'init', project_path))
|
||||
|
||||
if use_application_api:
|
||||
self.add_template(ProjectFileApplication(project_path, use_application_api, use_startup, use_service_providing, use_async))
|
||||
|
||||
if use_startup:
|
||||
self.add_template(ProjectFileStartup(project_path, use_application_api, use_startup, use_service_providing, use_async))
|
||||
|
||||
self.add_template(ProjectFileMain(project_path, use_application_api, use_startup, use_service_providing, use_async))
|
||||
29
src/cpl_cli/.cpl/project_file_appsettings.py
Normal file
29
src/cpl_cli/.cpl/project_file_appsettings.py
Normal file
@@ -0,0 +1,29 @@
|
||||
import textwrap
|
||||
|
||||
from cpl_cli.abc.file_template_abc import FileTemplateABC
|
||||
|
||||
|
||||
class ProjectFileAppsettings(FileTemplateABC):
|
||||
|
||||
def __init__(self, path: str):
|
||||
code = textwrap.dedent("""\
|
||||
{
|
||||
"TimeFormatSettings": {
|
||||
"DateFormat": "%Y-%m-%d",
|
||||
"TimeFormat": "%H:%M:%S",
|
||||
"DateTimeFormat": "%Y-%m-%d %H:%M:%S.%f",
|
||||
"DateTimeLogFormat": "%Y-%m-%d_%H-%M-%S"
|
||||
},
|
||||
|
||||
"LoggingSettings": {
|
||||
"Path": "logs/",
|
||||
"Filename": "log_$start_time.log",
|
||||
"ConsoleLogLevel": "ERROR",
|
||||
"FileLogLevel": "WARN"
|
||||
}
|
||||
}
|
||||
""")
|
||||
FileTemplateABC.__init__(self, 'appsettings.json', path, code)
|
||||
|
||||
def get_code(self) -> str:
|
||||
return self._code
|
||||
49
src/cpl_cli/.cpl/project_file_code_application.py
Normal file
49
src/cpl_cli/.cpl/project_file_code_application.py
Normal file
@@ -0,0 +1,49 @@
|
||||
from cpl_cli.abc.code_file_template_abc import CodeFileTemplateABC
|
||||
|
||||
|
||||
class ProjectFileApplication(CodeFileTemplateABC):
|
||||
|
||||
def __init__(self, path: str, use_application_api: bool, use_startup: bool, use_service_providing: bool, use_async: bool):
|
||||
CodeFileTemplateABC.__init__(self, 'application', path, '', use_application_api, use_startup, use_service_providing, use_async)
|
||||
|
||||
def get_code(self) -> str:
|
||||
import textwrap
|
||||
|
||||
if self._use_async:
|
||||
return 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)
|
||||
|
||||
async def configure(self):
|
||||
pass
|
||||
|
||||
async def main(self):
|
||||
Console.write_line('Hello World')
|
||||
""")
|
||||
|
||||
return 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')
|
||||
""")
|
||||
93
src/cpl_cli/.cpl/project_file_code_main.py
Normal file
93
src/cpl_cli/.cpl/project_file_code_main.py
Normal file
@@ -0,0 +1,93 @@
|
||||
from cpl_cli.abc.code_file_template_abc import CodeFileTemplateABC
|
||||
|
||||
|
||||
class ProjectFileMain(CodeFileTemplateABC):
|
||||
|
||||
def __init__(self, path: str, use_application_api: bool, use_startup: bool, use_service_providing: bool, use_async: bool):
|
||||
CodeFileTemplateABC.__init__(self, 'main', path, '', use_application_api, use_startup, use_service_providing, use_async)
|
||||
|
||||
import textwrap
|
||||
|
||||
import_pkg = f'{self._name}.'
|
||||
|
||||
self._main_with_application_host_and_startup = textwrap.dedent(f"""\
|
||||
{"import asyncio" if self._use_async else ''}
|
||||
|
||||
from cpl_core.application import ApplicationBuilder
|
||||
|
||||
from {import_pkg}application import Application
|
||||
from {import_pkg}startup import Startup
|
||||
|
||||
|
||||
{self._async()}def main():
|
||||
app_builder = ApplicationBuilder(Application)
|
||||
app_builder.use_startup(Startup)
|
||||
{"app: Application = await app_builder.build_async()" if self._use_async else ""}
|
||||
{"await app.run_async()" if self._use_async else "app_builder.build().run()"}
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
{"asyncio.run(main())" if self._use_async else "main()"}
|
||||
""")
|
||||
self._main_with_application_base = textwrap.dedent(f"""\
|
||||
{"import asyncio" if self._use_async else ''}
|
||||
|
||||
from cpl_core.application import ApplicationBuilder
|
||||
|
||||
from {import_pkg}application import Application
|
||||
|
||||
|
||||
{self._async()}def main():
|
||||
app_builder = ApplicationBuilder(Application)
|
||||
{"app: Application = await app_builder.build_async()" if self._use_async else ""}
|
||||
{"await app.run_async()" if self._use_async else "app_builder.build().run()"}
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
{"asyncio.run(main())" if self._use_async else "main()"}
|
||||
""")
|
||||
|
||||
self._main_with_dependency_injection = textwrap.dedent(f"""\
|
||||
{"import asyncio" if self._use_async else ''}
|
||||
|
||||
from cpl_core.application import ApplicationBuilder
|
||||
|
||||
from {import_pkg}application import Application
|
||||
|
||||
|
||||
{self._async()}def configure_configuration() -> ConfigurationABC:
|
||||
config = Configuration()
|
||||
return config
|
||||
|
||||
|
||||
{self._async()}def configure_services(config: ConfigurationABC) -> ServiceProviderABC:
|
||||
services = ServiceCollection(config)
|
||||
return services.build_service_provider()
|
||||
|
||||
|
||||
{self._async()}def main():
|
||||
config = {self._async()}configure_configuration()
|
||||
provider = {self._async()}configure_services(config)
|
||||
Console.write_line('Hello World')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
{"asyncio.run(main())" if self._use_async else "main()"}
|
||||
""")
|
||||
|
||||
def _async(self) -> str:
|
||||
if self._use_async:
|
||||
return 'async '
|
||||
return ''
|
||||
|
||||
def get_code(self) -> str:
|
||||
if self._use_application_api and self._use_startup:
|
||||
return self._main_with_application_host_and_startup
|
||||
|
||||
if self._use_application_api:
|
||||
return self._main_with_application_base
|
||||
|
||||
if self._use_service_providing:
|
||||
return self._main_with_dependency_injection
|
||||
|
||||
return self._main_with_application_base
|
||||
29
src/cpl_cli/.cpl/project_file_code_startup.py
Normal file
29
src/cpl_cli/.cpl/project_file_code_startup.py
Normal file
@@ -0,0 +1,29 @@
|
||||
from cpl_cli.abc.code_file_template_abc import CodeFileTemplateABC
|
||||
|
||||
|
||||
class ProjectFileStartup(CodeFileTemplateABC):
|
||||
|
||||
def __init__(self, path: str, use_application_api: bool, use_startup: bool, use_service_providing: bool, use_async: bool):
|
||||
CodeFileTemplateABC.__init__(self, 'startup', path, '', use_application_api, use_startup, use_service_providing, use_async)
|
||||
|
||||
def get_code(self) -> str:
|
||||
import textwrap
|
||||
|
||||
return 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()
|
||||
""")
|
||||
11
src/cpl_cli/.cpl/project_file_license.py
Normal file
11
src/cpl_cli/.cpl/project_file_license.py
Normal file
@@ -0,0 +1,11 @@
|
||||
from cpl_cli.abc.file_template_abc import FileTemplateABC
|
||||
|
||||
|
||||
class ProjectFileLicense(FileTemplateABC):
|
||||
|
||||
def __init__(self, path: str):
|
||||
FileTemplateABC.__init__(self, '', path, '')
|
||||
self._name = 'LICENSE'
|
||||
|
||||
def get_code(self) -> str:
|
||||
return self._code
|
||||
11
src/cpl_cli/.cpl/project_file_readme.py
Normal file
11
src/cpl_cli/.cpl/project_file_readme.py
Normal file
@@ -0,0 +1,11 @@
|
||||
from cpl_cli.abc.file_template_abc import FileTemplateABC
|
||||
|
||||
|
||||
class ProjectFileReadme(FileTemplateABC):
|
||||
|
||||
def __init__(self, path: str):
|
||||
FileTemplateABC.__init__(self, '', path, '')
|
||||
self._name = 'README.md'
|
||||
|
||||
def get_code(self) -> str:
|
||||
return self._code
|
||||
Reference in New Issue
Block a user