Added first cli commands
This commit is contained in:
parent
d76bf45645
commit
8c531ebd11
@ -2,5 +2,8 @@ prefix: cpl
|
|||||||
commands:
|
commands:
|
||||||
new:
|
new:
|
||||||
app
|
app
|
||||||
|
base
|
||||||
class
|
class
|
||||||
model
|
configmodel
|
||||||
|
enum
|
||||||
|
service
|
@ -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'
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
@ -1,5 +0,0 @@
|
|||||||
class CPLCli:
|
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
def main():
|
|
||||||
print('Hello world')
|
|
0
src/sh_edraft/cli/cpl_cli/__init__.py
Normal file
0
src/sh_edraft/cli/cpl_cli/__init__.py
Normal file
33
src/sh_edraft/cli/cpl_cli/cli.py
Normal file
33
src/sh_edraft/cli/cpl_cli/cli.py
Normal 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()
|
87
src/sh_edraft/cli/cpl_cli/cli_commands.py
Normal file
87
src/sh_edraft/cli/cpl_cli/cli_commands.py
Normal 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()
|
1
src/sh_edraft/cli/cpl_cli/templates/app/__init__.txt
Normal file
1
src/sh_edraft/cli/cpl_cli/templates/app/__init__.txt
Normal file
@ -0,0 +1 @@
|
|||||||
|
# imports:
|
8
src/sh_edraft/cli/cpl_cli/templates/app/main.txt
Normal file
8
src/sh_edraft/cli/cpl_cli/templates/app/main.txt
Normal 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()
|
43
src/sh_edraft/cli/cpl_cli/templates/app/program.txt
Normal file
43
src/sh_edraft/cli/cpl_cli/templates/app/program.txt
Normal 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}')
|
10
src/sh_edraft/cli/cpl_cli/templates/base/base.txt
Normal file
10
src/sh_edraft/cli/cpl_cli/templates/base/base.txt
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
from abc import ABC, abstractmethod
|
||||||
|
|
||||||
|
|
||||||
|
class $Name(ABC):
|
||||||
|
|
||||||
|
@abstractmethod
|
||||||
|
def __init__(self): pass
|
||||||
|
|
||||||
|
@abstractmethod
|
||||||
|
def create(self): pass
|
4
src/sh_edraft/cli/cpl_cli/templates/class/class.txt
Normal file
4
src/sh_edraft/cli/cpl_cli/templates/class/class.txt
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
class $Name:
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
pass
|
20
src/sh_edraft/cli/cpl_cli/templates/configmodel/model.txt
Normal file
20
src/sh_edraft/cli/cpl_cli/templates/configmodel/model.txt
Normal 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)
|
6
src/sh_edraft/cli/cpl_cli/templates/enum/enum.txt
Normal file
6
src/sh_edraft/cli/cpl_cli/templates/enum/enum.txt
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
from enum import Enum
|
||||||
|
|
||||||
|
|
||||||
|
class $Name(Enum):
|
||||||
|
|
||||||
|
pass
|
6
src/sh_edraft/cli/cpl_cli/templates/service/service.txt
Normal file
6
src/sh_edraft/cli/cpl_cli/templates/service/service.txt
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
class $Name($Base):
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
TestBase.__init__(self)
|
||||||
|
|
||||||
|
self.create()
|
Loading…
Reference in New Issue
Block a user