Added first cli commands

This commit is contained in:
Sven Heidemann 2020-12-15 22:16:24 +01:00
parent d76bf45645
commit 8c531ebd11
15 changed files with 223 additions and 7 deletions

View File

@ -2,5 +2,8 @@ prefix: cpl
commands: commands:
new: new:
app app
base
class class
model configmodel
enum
service

View File

@ -19,7 +19,7 @@ setuptools.setup(
], ],
entry_points={ entry_points={
'console_scripts': [ 'console_scripts': [
'cpl = sh_edraft.cli.cpl_cli:CPLCli.main' 'cpl = sh_edraft.cli.cpl_cli.cli:main'
] ]
} }
) )

View File

@ -1,5 +0,0 @@
class CPLCli:
@staticmethod
def main():
print('Hello world')

View File

View File

@ -0,0 +1,33 @@
import sys
from sh_edraft.cli.cpl_cli.cli_commands import CLICommands
class CLI:
def __init__(self):
self._commands: dict = {}
def setup(self):
self._commands[CLICommands.new.__name__] = CLICommands.new
def main(self):
args = sys.argv[1:]
try:
cmd = self._commands[args[0]]
cmd(args[1:])
except KeyError:
CLICommands.unexpected_command(args[0])
except Exception as e:
print(e)
def main():
cli = CLI()
cli.setup()
cli.main()
if __name__ == '__main__':
main()

View File

@ -0,0 +1,87 @@
import os
class CLICommands:
@classmethod
def new(cls, args: list[str]):
if not os.path.isdir(f'./templates/{args[0]}'):
cls.unexpected_command(args[0])
sub_args = args[1:]
if len(sub_args) != 1:
cls.unexpected_argument(sub_args[1])
full_path = sub_args[0]
name = os.path.basename(full_path)
path = os.path.dirname(full_path)
if args[0] in ['base', 'class', 'configmodel', 'enum', 'service']:
if not os.path.isdir(path):
os.makedirs(path)
else:
if not os.path.isdir(full_path):
os.makedirs(full_path)
for r, d, f in os.walk(f'./templates/{args[0]}'):
for file in f:
template_content = ''
with open(f'{r}/{file}') as template:
template_content = template.read()
template.close()
file = file.replace('txt', 'py')
if args[0] in ['base', 'class', 'configmodel', 'enum', 'service']:
suffix = None
if args[0] == 'base':
suffix = 'base'
elif args[0] == 'configmodel':
suffix = 'settings'
elif args[0] == 'service':
suffix = 'service'
if suffix is not None:
file_path = f'{path}/{name}_{suffix}.py'
else:
file_path = f'{path}/{name}.py'
else:
file_path = f'{full_path}/{file}'
with open(file_path, 'w+') as pyfile:
if name[0].islower():
name = f'{name[0].upper()}{name[1:]}'
if args[0] == 'configmodel':
template_content = template_content.replace('$Name', f'{name}Settings')
pyfile.write(template_content)
elif args[0] == 'service':
template_content = template_content.replace('$Name', f'{name}Service')
template_content = template_content.replace('$Base', f'{name}Base')
pyfile.write(template_content)
else:
template_content = template_content.replace('$Name', name)
pyfile.write(template_content)
pyfile.close()
@staticmethod
def help():
print('Commands:')
@classmethod
def unexpected_command(cls, command: str):
print(f'Unexpected command {command}')
cls.help()
exit()
@classmethod
def unexpected_argument(cls, argument: str):
print(f'Unexpected argument {argument}')
cls.help()
exit()

View File

@ -0,0 +1 @@
# imports:

View File

@ -0,0 +1,8 @@
from program import Program
if __name__ == '__main__':
program = Program()
program.create_application_host()
program.create_configuration()
program.create_services()
program.main()

View File

@ -0,0 +1,43 @@
from typing import Optional
from sh_edraft.configuration.base import ConfigurationBase
from sh_edraft.hosting import ApplicationHost
from sh_edraft.hosting.base import ApplicationBase
from sh_edraft.logging import Logger
from sh_edraft.logging.base import LoggerBase
from sh_edraft.service.providing.base import ServiceProviderBase
class Program(ApplicationBase):
def __init__(self):
ApplicationBase.__init__(self)
self._app_host: Optional[ApplicationHost] = None
self._services: Optional[ServiceProviderBase] = None
self._configuration: Optional[ConfigurationBase] = None
self._logger: Optional[LoggerBase] = None
def create_application_host(self):
self._app_host = ApplicationHost()
self._configuration = self._app_host.configuration
self._services = self._app_host.services
def create_configuration(self):
self._configuration.add_environment_variables('PYTHON_')
self._configuration.add_environment_variables('CPL_')
self._configuration.add_argument_variables()
self._configuration.add_json_file(f'appsettings.json')
self._configuration.add_json_file(f'appsettings.{self._configuration.environment.environment_name}.json', optional=True)
self._configuration.add_json_file(f'appsettings.{self._configuration.environment.host_name}.json', optional=True)
def create_services(self):
# Add and create logger
self._services.add_singleton(LoggerBase, Logger)
self._logger = self._services.get_service(LoggerBase)
def main(self):
self._logger.header(f'{self._configuration.environment.application_name}:')
self._logger.debug(__name__, f'Host: {self._configuration.environment.host_name}')
self._logger.debug(__name__, f'Environment: {self._configuration.environment.environment_name}')
self._logger.debug(__name__, f'Customer: {self._configuration.environment.customer}')

View File

@ -0,0 +1,10 @@
from abc import ABC, abstractmethod
class $Name(ABC):
@abstractmethod
def __init__(self): pass
@abstractmethod
def create(self): pass

View File

@ -0,0 +1,4 @@
class $Name:
def __init__(self):
pass

View File

@ -0,0 +1,20 @@
import traceback
from sh_edraft.configuration.base import ConfigurationModelBase
from sh_edraft.console import Console
from sh_edraft.console.model import ForegroundColor
class $Name(ConfigurationModelBase):
def __init__(self):
ConfigurationModelBase.__init__(self)
def from_dict(self, settings: dict):
try:
pass
except Exception as e:
Console.set_foreground_color(ForegroundColor.red)
Console.write_line(f'[ ERROR ] [ {__name__} ]: Reading error in {self.__name__} settings')
Console.write_line(f'[ EXCEPTION ] [ {__name__} ]: {e} -> {traceback.format_exc()}')
Console.set_foreground_color(ForegroundColor.default)

View File

@ -0,0 +1,6 @@
from enum import Enum
class $Name(Enum):
pass

View File

@ -0,0 +1,6 @@
class $Name($Base):
def __init__(self):
TestBase.__init__(self)
self.create()