Improved new console command

This commit is contained in:
Sven Heidemann 2021-03-10 11:28:04 +01:00
parent 064237904a
commit 9a149ec341
20 changed files with 466 additions and 109 deletions

View File

@ -31,9 +31,7 @@
"Main": "cpl_cli.main",
"EntryPoint": "cpl",
"IncludePackageData": "False",
"Included": [
"src/cpl_cli/templates/new/console/src/tests/"
],
"Included": [],
"Excluded": [
"*/__pycache__",
"*/logs",

View File

@ -92,7 +92,7 @@ class Logger(LoggerABC):
if self._level.value >= LoggingLevel.TRACE.value:
self._append_log(output)
# check if message can be shown in console
# check if message can be shown in console_old
if self._console.value >= LoggingLevel.TRACE.value:
Console.set_foreground_color(ForegroundColor.green)
Console.write_line(output)
@ -105,7 +105,7 @@ class Logger(LoggerABC):
if self._level.value >= LoggingLevel.DEBUG.value:
self._append_log(output)
# check if message can be shown in console
# check if message can be shown in console_old
if self._console.value >= LoggingLevel.DEBUG.value:
Console.set_foreground_color(ForegroundColor.green)
Console.write_line(output)
@ -118,7 +118,7 @@ class Logger(LoggerABC):
if self._level.value >= LoggingLevel.INFO.value:
self._append_log(output)
# check if message can be shown in console
# check if message can be shown in console_old
if self._console.value >= LoggingLevel.INFO.value:
Console.set_foreground_color(ForegroundColor.green)
Console.write_line(output)
@ -131,7 +131,7 @@ class Logger(LoggerABC):
if self._level.value >= LoggingLevel.WARN.value:
self._append_log(output)
# check if message can be shown in console
# check if message can be shown in console_old
if self._console.value >= LoggingLevel.WARN.value:
Console.set_foreground_color(ForegroundColor.yellow)
Console.write_line(output)
@ -150,7 +150,7 @@ class Logger(LoggerABC):
if self._level.value >= LoggingLevel.ERROR.value:
self._append_log(output)
# check if message can be shown in console
# check if message can be shown in console_old
if self._console.value >= LoggingLevel.ERROR.value:
Console.set_foreground_color(ForegroundColor.red)
Console.write_line(output)
@ -169,7 +169,7 @@ class Logger(LoggerABC):
if self._level.value >= LoggingLevel.FATAL.value:
self._append_log(output)
# check if message can be shown in console
# check if message can be shown in console_old
if self._console.value >= LoggingLevel.FATAL.value:
Console.set_foreground_color(ForegroundColor.red)
Console.write_line(output)
@ -186,7 +186,7 @@ class Logger(LoggerABC):
else:
output = self._get_string(name, LoggingLevel.ERROR, message)
# check if message can be shown in console
# check if message can be shown in console_old
if self._console.value >= LoggingLevel.FATAL.value:
Console.set_foreground_color(ForegroundColor.red)
Console.write_line(output)

View File

@ -1,7 +1,7 @@
import json
import os
import sys
from distutils.dir_util import copy_tree
from typing import Optional
from cpl.application.application_runtime_abc import ApplicationRuntimeABC
from cpl.configuration.configuration_abc import ConfigurationABC
@ -12,6 +12,13 @@ from cpl_cli.configuration.build_settings_name import BuildSettingsName
from cpl_cli.configuration.project_settings import ProjectSettings
from cpl_cli.configuration.project_settings_name import ProjectSettingsName
from cpl_cli.configuration.version_settings_name import VersionSettingsName
from cpl_cli.templates.new.console.license import LicenseTemplate
from cpl_cli.templates.new.console.readme_py import ReadmeTemplate
from cpl_cli.templates.new.console.src.application import ApplicationTemplate
from cpl_cli.templates.new.console.src.main import MainWithApplicationHostAndStartupTemplate, MainWithoutApplicationHostTemplate, MainWithApplicationHostTemplate
from cpl_cli.templates.new.console.src.startup import StartupTemplate
from cpl_cli.templates.new.console.src.tests.init import TestsInitTemplate
from cpl_cli.templates.template_file_abc import TemplateFileABC
class New(CommandABC):
@ -29,6 +36,9 @@ class New(CommandABC):
self._project_json = {}
self._command: str = ''
self._use_application_api: bool = False
self._use_startup: bool = False
self._use_service_providing: bool = False
def _create_project_settings(self, name: str):
self._project_dict = {
@ -76,12 +86,29 @@ class New(CommandABC):
BuildSettings.__name__: self._build_dict
}
def _create_project_dir(self):
def _get_project_path(self) -> Optional[str]:
project_path = os.path.join(self._runtime.working_directory, self._project.name)
if os.path.isdir(project_path) and len(os.listdir(project_path)) > 0:
Console.error('Project path is not empty\n')
exit()
return None
return project_path
def _get_project_informations(self):
result = Console.read('Do you want to use application host? (y/n) ')
if result.lower() == 'y':
self._use_application_api = True
if self._use_application_api:
result = Console.read('Do you want to use startup? (y/n) ')
if result.lower() == 'y':
self._use_startup = True
# result = Console.read('Do you want to use service providing? (y/n) ')
# if result.lower() == 'y':
# self._use_service_providing = True
def _build_project_dir(self, project_path: str):
if not os.path.isdir(project_path):
os.makedirs(project_path)
@ -89,19 +116,52 @@ class New(CommandABC):
project_json.write(json.dumps(self._project_json, indent=4))
project_json.close()
template_path = os.path.join(self._runtime.runtime_directory, f'templates/new/{self._command}')
if not os.path.isdir(template_path):
Console.error(template_path, '\n\nTemplate path not found\n')
exit()
templates: list[TemplateFileABC] = [
LicenseTemplate(),
ReadmeTemplate(),
TestsInitTemplate()
]
if self._use_application_api:
templates.append(ApplicationTemplate())
if self._use_startup:
templates.append(StartupTemplate())
templates.append(MainWithApplicationHostAndStartupTemplate())
else:
templates.append(MainWithApplicationHostTemplate())
else:
templates.append(MainWithoutApplicationHostTemplate())
copy_tree(template_path, project_path)
for template in templates:
Console.spinner(f'Creating {template.path}{template.name}', self._create_template, project_path, template)
@staticmethod
def _create_template(project_path: str, template: TemplateFileABC):
file_path = os.path.join(project_path, template.path, template.name)
file_rel_path = os.path.join(project_path, template.path)
if not os.path.isdir(file_rel_path):
os.makedirs(file_rel_path)
with open(file_path, 'w') as license_file:
license_file.write(template.value)
license_file.close()
def _console(self, args: list[str]):
name = self._config.get_configuration(self._command)
self._create_project_settings(name)
self._create_build_settings()
self._create_project_json()
self._create_project_dir()
Console.spinner('Creating project settings', self._create_project_settings, name)
Console.spinner('Creating build settings', self._create_build_settings)
path = self._get_project_path()
if path is None:
return
Console.spinner('Creating project file', self._create_project_json)
Console.write_line('Building project directory:')
self._get_project_informations()
try:
self._build_project_dir(path)
except Exception as e:
Console.error('Could not create project', str(e))
def run(self, args: list[str]):
self._command = args[0]

View File

@ -1,11 +1,12 @@
from cpl_cli.templates.template import Template
import textwrap
class Init:
@staticmethod
def get_init_py() -> str:
string = """# -*- coding: utf-8 -*-
string = textwrap.dedent("""\
# -*- coding: utf-8 -*-
\"\"\"
$Name $Description
@ -30,6 +31,6 @@ class Init:
VersionInfo = namedtuple('VersionInfo', 'major minor micro')
version_info = VersionInfo(major=$Major, minor=$Minor, micro=$Micro)
"""
""")
return Template.build_template_string(string)
return string

View File

@ -0,0 +1,25 @@
# -*- coding: utf-8 -*-
"""
sh_cpl sh-edraft Common Python library
~~~~~~~~~~~~~~~~~~~
sh-edraft Common Python library
:copyright: (c) 2020 - 2021 sh-edraft.de
:license: MIT, see LICENSE for more details.
"""
__title__ = 'cpl_cli.templates.new'
__author__ = 'Sven Heidemann'
__license__ = 'MIT'
__copyright__ = 'Copyright (c) 2020 - 2021 sh-edraft.de'
__version__ = '2021.4.1'
from collections import namedtuple
# imports:
VersionInfo = namedtuple('VersionInfo', 'major minor micro')
version_info = VersionInfo(major=2021, minor=4, micro=1)

View File

@ -0,0 +1,25 @@
# -*- coding: utf-8 -*-
"""
sh_cpl sh-edraft Common Python library
~~~~~~~~~~~~~~~~~~~
sh-edraft Common Python library
:copyright: (c) 2020 - 2021 sh-edraft.de
:license: MIT, see LICENSE for more details.
"""
__title__ = 'cpl_cli.templates.new.console'
__author__ = 'Sven Heidemann'
__license__ = 'MIT'
__copyright__ = 'Copyright (c) 2020 - 2021 sh-edraft.de'
__version__ = '2021.4.1'
from collections import namedtuple
# imports:
VersionInfo = namedtuple('VersionInfo', 'major minor micro')
version_info = VersionInfo(major=2021, minor=4, micro=1)

View File

@ -0,0 +1,23 @@
from cpl_cli.templates.template_file_abc import TemplateFileABC
class LicenseTemplate(TemplateFileABC):
def __init__(self):
TemplateFileABC.__init__(self)
self._name = 'LICENSE'
self._path = ''
self._value = """"""
@property
def name(self) -> str:
return self._name
@property
def path(self) -> str:
return self._path
@property
def value(self) -> str:
return self._value

View File

@ -0,0 +1,23 @@
from cpl_cli.templates.template_file_abc import TemplateFileABC
class ReadmeTemplate(TemplateFileABC):
def __init__(self):
TemplateFileABC.__init__(self)
self._name = 'README.md'
self._path = ''
self._value = """"""
@property
def name(self) -> str:
return self._name
@property
def path(self) -> str:
return self._path
@property
def value(self) -> str:
return self._value

View File

@ -0,0 +1,25 @@
# -*- coding: utf-8 -*-
"""
sh_cpl sh-edraft Common Python library
~~~~~~~~~~~~~~~~~~~
sh-edraft Common Python library
:copyright: (c) 2020 - 2021 sh-edraft.de
:license: MIT, see LICENSE for more details.
"""
__title__ = 'cpl_cli.templates.new.console.src'
__author__ = 'Sven Heidemann'
__license__ = 'MIT'
__copyright__ = 'Copyright (c) 2020 - 2021 sh-edraft.de'
__version__ = '2021.4.1'
from collections import namedtuple
# imports:
VersionInfo = namedtuple('VersionInfo', 'major minor micro')
version_info = VersionInfo(major=2021, minor=4, micro=1)

View File

@ -1,3 +1,16 @@
import textwrap
from cpl_cli.templates.template_file_abc import TemplateFileABC
class ApplicationTemplate(TemplateFileABC):
def __init__(self):
TemplateFileABC.__init__(self)
self._name = 'application.py'
self._path = 'src/'
self._value = textwrap.dedent("""\
from cpl.application.application_abc import ApplicationABC
from cpl.console.console import Console
@ -12,3 +25,16 @@ class Application(ApplicationABC):
def main(self):
Console.write_line('Hello World')
""")
@property
def name(self) -> str:
return self._name
@property
def path(self) -> str:
return self._path
@property
def value(self) -> str:
return self._value

View File

@ -1,3 +1,16 @@
import textwrap
from cpl_cli.templates.template_file_abc import TemplateFileABC
class MainWithApplicationHostAndStartupTemplate(TemplateFileABC):
def __init__(self):
TemplateFileABC.__init__(self)
self._name = 'main.py'
self._path = 'src/'
self._value = textwrap.dedent("""\
from startup import Startup
from application import Application
@ -11,3 +24,82 @@ def main():
if __name__ == '__main__':
main()
""")
@property
def name(self) -> str:
return self._name
@property
def path(self) -> str:
return self._path
@property
def value(self) -> str:
return self._value
class MainWithApplicationHostTemplate(TemplateFileABC):
def __init__(self):
TemplateFileABC.__init__(self)
self._name = 'main.py'
self._path = 'src/'
self._value = textwrap.dedent("""\
from application import Application
def main():
app = Application()
app.build()
app.run()
if __name__ == '__main__':
main()
""")
@property
def name(self) -> str:
return self._name
@property
def path(self) -> str:
return self._path
@property
def value(self) -> str:
return self._value
class MainWithoutApplicationHostTemplate(TemplateFileABC):
def __init__(self):
TemplateFileABC.__init__(self)
self._name = 'main.py'
self._path = 'src/'
self._value = textwrap.dedent("""\
from cpl.console.console import Console
def main():
Console.write_line('Hello World')
if __name__ == '__main__':
main()
""")
@property
def name(self) -> str:
return self._name
@property
def path(self) -> str:
return self._path
@property
def value(self) -> str:
return self._value

View File

@ -1,3 +1,16 @@
import textwrap
from cpl_cli.templates.template_file_abc import TemplateFileABC
class StartupTemplate(TemplateFileABC):
def __init__(self):
TemplateFileABC.__init__(self)
self._name = 'startup.py'
self._path = 'src/'
self._value = textwrap.dedent("""\
from typing import Optional
from cpl.application.application_host import ApplicationHost
@ -31,3 +44,16 @@ class Startup(StartupABC):
pass
return self._services
""")
@property
def name(self) -> str:
return self._name
@property
def path(self) -> str:
return self._path
@property
def value(self) -> str:
return self._value

View File

@ -11,7 +11,7 @@ sh-edraft Common Python library
"""
__title__ = 'cpl_cli.templates.new.console.src.tests'
__title__ = 'cpl_cli.templates.new.console_old.src.tests'
__author__ = 'Sven Heidemann'
__license__ = 'MIT'
__copyright__ = 'Copyright (c) 2020 - 2021 sh-edraft.de'

View File

@ -0,0 +1,27 @@
import textwrap
from cpl_cli.templates.template_file_abc import TemplateFileABC
class TestsInitTemplate(TemplateFileABC):
def __init__(self):
TemplateFileABC.__init__(self)
self._name = '__init__.py'
self._path = 'src/tests/'
self._value = textwrap.dedent("""\
# imports:
""")
@property
def name(self) -> str:
return self._name
@property
def path(self) -> str:
return self._path
@property
def value(self) -> str:
return self._value

View File

@ -1,11 +1,12 @@
from cpl_cli.templates.template import Template
import textwrap
class Setup:
@staticmethod
def get_setup_py() -> str:
string = """\"\"\"
string = textwrap.dedent("""\
\"\"\"
This file is generated by CPL CLI
\"\"\"
@ -26,6 +27,6 @@ class Setup:
entry_points=$EntryPoints,
package_data=$PackageData
)
"""
""")
return Template.build_template_string(string)
return string

View File

@ -1,14 +0,0 @@
class Template:
@staticmethod
def build_template_string(string: str) -> str:
return_value = ''
for i in range(0, len(string.splitlines())):
line = string.splitlines()[i]
if i == len(string.splitlines())-1:
return_value += f'{line.strip()}'
break
return_value += f'{line.strip()}\n'
return return_value

View File

@ -0,0 +1,19 @@
from abc import ABC, abstractmethod
class TemplateFileABC(ABC):
@abstractmethod
def __init__(self): pass
@property
@abstractmethod
def name(self) -> str: pass
@property
@abstractmethod
def path(self) -> str: pass
@property
@abstractmethod
def value(self) -> str: pass

View File

@ -25,7 +25,7 @@ class Application(ApplicationABC):
self._mailer.send_mail(mail)
def test_console(self):
self._logger.debug(__name__, 'Started console test')
self._logger.debug(__name__, 'Started console_old test')
Console.write_line('Hello World')
Console.write('\nName: ')
Console.write_line('Hello', Console.read_line())