Improved argument parsing

This commit is contained in:
Sven Heidemann 2022-05-18 22:26:11 +02:00
parent 50201346e2
commit d87ce72f07
52 changed files with 344 additions and 395 deletions

View File

@ -15,7 +15,7 @@ __title__ = 'cpl_cli'
__author__ = 'Sven Heidemann'
__license__ = 'MIT'
__copyright__ = 'Copyright (c) 2020 - 2022 sh-edraft.de'
__version__ = '2022.6.1'
__version__ = '2022.6.3.dev2'
from collections import namedtuple
@ -29,4 +29,4 @@ from .main import main
from .startup import Startup
VersionInfo = namedtuple('VersionInfo', 'major minor micro')
version_info = VersionInfo(major='2022', minor='6', micro='1')
version_info = VersionInfo(major='2022', minor='6', micro='3.dev2')

View File

@ -15,11 +15,11 @@ __title__ = 'cpl_cli._templates'
__author__ = 'Sven Heidemann'
__license__ = 'MIT'
__copyright__ = 'Copyright (c) 2020 - 2022 sh-edraft.de'
__version__ = '2022.6.1'
__version__ = '2022.6.3.dev2'
from collections import namedtuple
# imports:
VersionInfo = namedtuple('VersionInfo', 'major minor micro')
version_info = VersionInfo(major='2022', minor='6', micro='1')
version_info = VersionInfo(major='2022', minor='6', micro='3.dev2')

View File

@ -15,11 +15,11 @@ __title__ = 'cpl_cli._templates.build'
__author__ = 'Sven Heidemann'
__license__ = 'MIT'
__copyright__ = 'Copyright (c) 2020 - 2022 sh-edraft.de'
__version__ = '2022.6.1'
__version__ = '2022.6.3.dev2'
from collections import namedtuple
# imports:
VersionInfo = namedtuple('VersionInfo', 'major minor micro')
version_info = VersionInfo(major='2022', minor='6', micro='1')
version_info = VersionInfo(major='2022', minor='6', micro='3.dev2')

View File

@ -15,11 +15,11 @@ __title__ = 'cpl_cli._templates.generate'
__author__ = 'Sven Heidemann'
__license__ = 'MIT'
__copyright__ = 'Copyright (c) 2020 - 2022 sh-edraft.de'
__version__ = '2022.6.1'
__version__ = '2022.6.3.dev2'
from collections import namedtuple
# imports:
VersionInfo = namedtuple('VersionInfo', 'major minor micro')
version_info = VersionInfo(major='2022', minor='6', micro='1')
version_info = VersionInfo(major='2022', minor='6', micro='3.dev2')

View File

@ -15,11 +15,11 @@ __title__ = 'cpl_cli._templates.new'
__author__ = 'Sven Heidemann'
__license__ = 'MIT'
__copyright__ = 'Copyright (c) 2020 - 2022 sh-edraft.de'
__version__ = '2022.6.1'
__version__ = '2022.6.3.dev2'
from collections import namedtuple
# imports:
VersionInfo = namedtuple('VersionInfo', 'major minor micro')
version_info = VersionInfo(major='2022', minor='6', micro='1')
version_info = VersionInfo(major='2022', minor='6', micro='3.dev2')

View File

@ -15,11 +15,11 @@ __title__ = 'cpl_cli._templates.new.console'
__author__ = 'Sven Heidemann'
__license__ = 'MIT'
__copyright__ = 'Copyright (c) 2020 - 2022 sh-edraft.de'
__version__ = '2022.6.1'
__version__ = '2022.6.3.dev2'
from collections import namedtuple
# imports:
VersionInfo = namedtuple('VersionInfo', 'major minor micro')
version_info = VersionInfo(major='2022', minor='6', micro='1')
version_info = VersionInfo(major='2022', minor='6', micro='3.dev2')

View File

@ -15,11 +15,11 @@ __title__ = 'cpl_cli._templates.new.console.source'
__author__ = 'Sven Heidemann'
__license__ = 'MIT'
__copyright__ = 'Copyright (c) 2020 - 2022 sh-edraft.de'
__version__ = '2022.6.1'
__version__ = '2022.6.3.dev2'
from collections import namedtuple
# imports:
VersionInfo = namedtuple('VersionInfo', 'major minor micro')
version_info = VersionInfo(major='2022', minor='6', micro='1')
version_info = VersionInfo(major='2022', minor='6', micro='3.dev2')

View File

@ -15,11 +15,11 @@ __title__ = 'cpl_cli._templates.new.console.source.name'
__author__ = 'Sven Heidemann'
__license__ = 'MIT'
__copyright__ = 'Copyright (c) 2020 - 2022 sh-edraft.de'
__version__ = '2022.6.1'
__version__ = '2022.6.3.dev2'
from collections import namedtuple
# imports:
VersionInfo = namedtuple('VersionInfo', 'major minor micro')
version_info = VersionInfo(major='2022', minor='6', micro='1')
version_info = VersionInfo(major='2022', minor='6', micro='3.dev2')

View File

@ -15,11 +15,11 @@ __title__ = 'cpl_cli._templates.new.console.source.tests'
__author__ = 'Sven Heidemann'
__license__ = 'MIT'
__copyright__ = 'Copyright (c) 2020 - 2022 sh-edraft.de'
__version__ = '2022.6.1'
__version__ = '2022.6.3.dev2'
from collections import namedtuple
# imports:
VersionInfo = namedtuple('VersionInfo', 'major minor micro')
version_info = VersionInfo(major='2022', minor='6', micro='1')
version_info = VersionInfo(major='2022', minor='6', micro='3.dev2')

View File

@ -15,11 +15,11 @@ __title__ = 'cpl_cli._templates.new.library'
__author__ = 'Sven Heidemann'
__license__ = 'MIT'
__copyright__ = 'Copyright (c) 2020 - 2022 sh-edraft.de'
__version__ = '2022.6.1'
__version__ = '2022.6.3.dev2'
from collections import namedtuple
# imports:
VersionInfo = namedtuple('VersionInfo', 'major minor micro')
version_info = VersionInfo(major='2022', minor='6', micro='1')
version_info = VersionInfo(major='2022', minor='6', micro='3.dev2')

View File

@ -15,11 +15,11 @@ __title__ = 'cpl_cli._templates.new.library.source'
__author__ = 'Sven Heidemann'
__license__ = 'MIT'
__copyright__ = 'Copyright (c) 2020 - 2022 sh-edraft.de'
__version__ = '2022.6.1'
__version__ = '2022.6.3.dev2'
from collections import namedtuple
# imports:
VersionInfo = namedtuple('VersionInfo', 'major minor micro')
version_info = VersionInfo(major='2022', minor='6', micro='1')
version_info = VersionInfo(major='2022', minor='6', micro='3.dev2')

View File

@ -15,11 +15,11 @@ __title__ = 'cpl_cli._templates.new.library.source.name'
__author__ = 'Sven Heidemann'
__license__ = 'MIT'
__copyright__ = 'Copyright (c) 2020 - 2022 sh-edraft.de'
__version__ = '2022.6.1'
__version__ = '2022.6.3.dev2'
from collections import namedtuple
# imports:
VersionInfo = namedtuple('VersionInfo', 'major minor micro')
version_info = VersionInfo(major='2022', minor='6', micro='1')
version_info = VersionInfo(major='2022', minor='6', micro='3.dev2')

View File

@ -15,11 +15,11 @@ __title__ = 'cpl_cli._templates.new.library.source.tests'
__author__ = 'Sven Heidemann'
__license__ = 'MIT'
__copyright__ = 'Copyright (c) 2020 - 2022 sh-edraft.de'
__version__ = '2022.6.1'
__version__ = '2022.6.3.dev2'
from collections import namedtuple
# imports:
VersionInfo = namedtuple('VersionInfo', 'major minor micro')
version_info = VersionInfo(major='2022', minor='6', micro='1')
version_info = VersionInfo(major='2022', minor='6', micro='3.dev2')

View File

@ -15,11 +15,11 @@ __title__ = 'cpl_cli._templates.publish'
__author__ = 'Sven Heidemann'
__license__ = 'MIT'
__copyright__ = 'Copyright (c) 2020 - 2022 sh-edraft.de'
__version__ = '2022.6.1'
__version__ = '2022.6.3.dev2'
from collections import namedtuple
# imports:
VersionInfo = namedtuple('VersionInfo', 'major minor micro')
version_info = VersionInfo(major='2022', minor='6', micro='1')
version_info = VersionInfo(major='2022', minor='6', micro='3.dev2')

View File

@ -3,7 +3,6 @@ import sys
from typing import Optional
from cpl_core.application.application_abc import ApplicationABC
from cpl_core.configuration.console_argument import ConsoleArgument
from cpl_core.configuration.configuration_abc import ConfigurationABC
from cpl_core.console.console import Console
from cpl_core.dependency_injection import ServiceProviderABC

View File

@ -15,7 +15,7 @@ __title__ = 'cpl_cli.command'
__author__ = 'Sven Heidemann'
__license__ = 'MIT'
__copyright__ = 'Copyright (c) 2020 - 2022 sh-edraft.de'
__version__ = '2022.6.1'
__version__ = '2022.6.3.dev2'
from collections import namedtuple
@ -28,4 +28,4 @@ from .publish_service import PublishService
from .version_service import VersionService
VersionInfo = namedtuple('VersionInfo', 'major minor micro')
version_info = VersionInfo(major='2022', minor='6', micro='1')
version_info = VersionInfo(major='2022', minor='6', micro='3.dev2')

View File

@ -15,7 +15,7 @@ __title__ = 'cpl_cli.configuration'
__author__ = 'Sven Heidemann'
__license__ = 'MIT'
__copyright__ = 'Copyright (c) 2020 - 2022 sh-edraft.de'
__version__ = '2022.6.1'
__version__ = '2022.6.3.dev2'
from collections import namedtuple
@ -30,4 +30,4 @@ from .workspace_settings import WorkspaceSettings
from .workspace_settings_name_enum import WorkspaceSettingsNameEnum
VersionInfo = namedtuple('VersionInfo', 'major minor micro')
version_info = VersionInfo(major='2022', minor='6', micro='1')
version_info = VersionInfo(major='2022', minor='6', micro='3.dev2')

View File

@ -4,7 +4,7 @@
"Version": {
"Major": "2022",
"Minor": "6",
"Micro": "1"
"Micro": "3.dev2"
},
"Author": "Sven Heidemann",
"AuthorEmail": "sven.heidemann@sh-edraft.de",
@ -16,7 +16,7 @@
"LicenseName": "MIT",
"LicenseDescription": "MIT, see LICENSE for more details.",
"Dependencies": [
"cpl-core>=2022.6.2"
"cpl-core>=2022.6.3.dev2"
],
"PythonVersion": ">=3.10",
"PythonPath": {},

View File

@ -15,11 +15,11 @@ __title__ = 'cpl_cli.live_server'
__author__ = 'Sven Heidemann'
__license__ = 'MIT'
__copyright__ = 'Copyright (c) 2020 - 2022 sh-edraft.de'
__version__ = '2022.6.1'
__version__ = '2022.6.3.dev2'
from collections import namedtuple
# imports:
VersionInfo = namedtuple('VersionInfo', 'major minor micro')
version_info = VersionInfo(major='2022', minor='6', micro='1')
version_info = VersionInfo(major='2022', minor='6', micro='3.dev2')

View File

@ -15,7 +15,7 @@ __title__ = 'cpl_cli.publish'
__author__ = 'Sven Heidemann'
__license__ = 'MIT'
__copyright__ = 'Copyright (c) 2020 - 2022 sh-edraft.de'
__version__ = '2022.6.1'
__version__ = '2022.6.3.dev2'
from collections import namedtuple
@ -24,4 +24,4 @@ from .publisher_abc import PublisherABC
from .publisher_service import PublisherService
VersionInfo = namedtuple('VersionInfo', 'major minor micro')
version_info = VersionInfo(major='2022', minor='6', micro='1')
version_info = VersionInfo(major='2022', minor='6', micro='3.dev2')

View File

@ -15,11 +15,11 @@ __title__ = 'cpl_cli.source_creator'
__author__ = 'Sven Heidemann'
__license__ = 'MIT'
__copyright__ = 'Copyright (c) 2020 - 2022 sh-edraft.de'
__version__ = '2022.6.1'
__version__ = '2022.6.3.dev2'
from collections import namedtuple
# imports:
VersionInfo = namedtuple('VersionInfo', 'major minor micro')
version_info = VersionInfo(major='2022', minor='6', micro='1')
version_info = VersionInfo(major='2022', minor='6', micro='3.dev2')

View File

@ -2,7 +2,7 @@ import os
from typing import Optional
from cpl_core.application.startup_abc import StartupABC
from cpl_core.configuration.console_argument import ConsoleArgument
from cpl_core.configuration.argument_abc import ConsoleArgument
from cpl_core.configuration.configuration_abc import ConfigurationABC
from cpl_core.dependency_injection.service_collection_abc import ServiceCollectionABC
from cpl_core.dependency_injection.service_provider_abc import ServiceProviderABC
@ -80,7 +80,7 @@ class Startup(StartupABC):
configuration.add_console_argument(
ConsoleArgument('', script, [], ' ', is_value_token_optional=True))
configuration.add_console_arguments(error=False)
configuration.parse_console_arguments(error=False)
return configuration

View File

@ -15,11 +15,11 @@ __title__ = 'cpl_core'
__author__ = 'Sven Heidemann'
__license__ = 'MIT'
__copyright__ = 'Copyright (c) 2020 - 2022 sh-edraft.de'
__version__ = '2022.6.2'
__version__ = '2022.6.3.dev2'
from collections import namedtuple
# imports:
VersionInfo = namedtuple('VersionInfo', 'major minor micro')
version_info = VersionInfo(major='2022', minor='6', micro='2')
version_info = VersionInfo(major='2022', minor='6', micro='3.dev2')

View File

@ -15,7 +15,7 @@ __title__ = 'cpl_core.application'
__author__ = 'Sven Heidemann'
__license__ = 'MIT'
__copyright__ = 'Copyright (c) 2020 - 2022 sh-edraft.de'
__version__ = '2022.6.2'
__version__ = '2022.6.3.dev2'
from collections import namedtuple
@ -27,4 +27,4 @@ from .startup_abc import StartupABC
from .startup_extension_abc import StartupExtensionABC
VersionInfo = namedtuple('VersionInfo', 'major minor micro')
version_info = VersionInfo(major='2022', minor='6', micro='2')
version_info = VersionInfo(major='2022', minor='6', micro='3.dev2')

View File

@ -55,6 +55,7 @@ class ApplicationBuilder(ApplicationBuilderABC):
config = self._configuration
services = self._services.build_service_provider()
config.resolve_runnable_argument_types(services)
config.parse_console_arguments(error=False)
for ex in self._app_extensions:
extension = ex()

View File

@ -15,7 +15,7 @@ __title__ = 'cpl_core.configuration'
__author__ = 'Sven Heidemann'
__license__ = 'MIT'
__copyright__ = 'Copyright (c) 2020 - 2022 sh-edraft.de'
__version__ = '2022.6.2'
__version__ = '2022.6.3.dev2'
from collections import namedtuple
@ -24,7 +24,7 @@ from .configuration import Configuration
from .configuration_abc import ConfigurationABC
from .configuration_model_abc import ConfigurationModelABC
from .configuration_variable_name_enum import ConfigurationVariableNameEnum
from .console_argument import ConsoleArgument
from .argument_abc import ArgumentABC
VersionInfo = namedtuple('VersionInfo', 'major minor micro')
version_info = VersionInfo(major='2022', minor='6', micro='2')
version_info = VersionInfo(major='2022', minor='6', micro='3.dev2')

View File

@ -0,0 +1,60 @@
from abc import ABC, abstractmethod
from cpl_core.configuration.argument_type_enum import ArgumentTypeEnum
class ArgumentABC(ABC):
@abstractmethod
def __init__(self,
token: str,
name: str,
aliases: list[str],
console_arguments: list['ArgumentABC'] = None
):
r"""Representation of an console argument
Parameter
---------
token: :class:`str`
name: :class:`str`
aliases: list[:class:`str`]
console_arguments: List[:class:`cpl_core.configuration.console_argument.ConsoleArgument`]
"""
self._token = token
self._name = name
self._aliases = aliases
self._console_arguments = console_arguments if console_arguments is not None else []
@property
def token(self) -> str:
return self._token
@property
def name(self) -> str:
return self._name
@property
def aliases(self) -> list[str]:
return self._aliases
@property
def console_arguments(self) -> list['ArgumentABC']:
return self._console_arguments
def add_console_argument(self, arg_type: ArgumentTypeEnum, *args, **kwargs) -> 'ArgumentABC':
r"""Creates and adds a console argument to known console arguments
Parameter
---------
arg_type: :class:`str`
Specifies the specific type of the argument
Returns
------
self :class:`cpl_core.configuration.console_argument.ConsoleArgument` not created argument!
"""
from cpl_core.configuration.argument_builder import ArgumentBuilder
argument = ArgumentBuilder.build_argument(arg_type, *args, *kwargs)
self._console_arguments.append(argument)
return self

View File

@ -0,0 +1,30 @@
from typing import Union
from cpl_core.configuration.argument_type_enum import ArgumentTypeEnum
from cpl_core.configuration.executable_argument import ExecutableArgument
from cpl_core.configuration.flag_argument import FlagArgument
from cpl_core.configuration.variable_argument import VariableArgument
from cpl_core.console import Console
class ArgumentBuilder:
@staticmethod
def build_argument(arg_type: ArgumentTypeEnum, *args, **kwargs) -> Union[
ExecutableArgument, FlagArgument, VariableArgument]:
argument = None
try:
match arg_type:
case ArgumentTypeEnum.Flag:
argument = FlagArgument(*args, **kwargs)
case ArgumentTypeEnum.Executable:
argument = ExecutableArgument(*args, **kwargs)
case ArgumentTypeEnum.Variable:
argument = VariableArgument(*args, **kwargs)
case _:
Console.error('Invalid argument type')
Console.close()
except TypeError as e:
Console.error(str(e))
Console.close()
return argument

View File

@ -1,7 +1,7 @@
from abc import ABC, abstractmethod
class RunnableArgumentABC(ABC):
class ArgumentExecutableABC(ABC):
@abstractmethod
def __init__(self): pass

View File

@ -0,0 +1,8 @@
from enum import Enum
class ArgumentTypeEnum(Enum):
Flag = 0
Executable = 1
Variable = 3

View File

@ -4,11 +4,15 @@ import sys
from collections.abc import Callable
from typing import Union, Type, Optional
from cpl_core.configuration.argument_abc import ArgumentABC
from cpl_core.configuration.argument_builder import ArgumentBuilder
from cpl_core.configuration.argument_type_enum import ArgumentTypeEnum
from cpl_core.configuration.configuration_abc import ConfigurationABC
from cpl_core.configuration.configuration_model_abc import ConfigurationModelABC
from cpl_core.configuration.configuration_variable_name_enum import ConfigurationVariableNameEnum
from cpl_core.configuration.console_argument import ConsoleArgument
from cpl_core.configuration.runnable_argument_abc import RunnableArgumentABC
from cpl_core.configuration.executable_argument import ExecutableArgument
from cpl_core.configuration.flag_argument import FlagArgument
from cpl_core.configuration.variable_argument import VariableArgument
from cpl_core.console.console import Console
from cpl_core.console.foreground_color_enum import ForegroundColorEnum
from cpl_core.dependency_injection import ServiceProviderABC
@ -26,7 +30,7 @@ class Configuration(ConfigurationABC):
self._application_environment = ApplicationEnvironment()
self._config: dict[Union[type, str], Union[ConfigurationModelABC, str]] = {}
self._argument_types: list[ConsoleArgument] = []
self._argument_types: list[ArgumentABC] = []
self._additional_arguments: list[str] = []
self._argument_error_function: Optional[Callable] = None
@ -116,155 +120,6 @@ class Configuration(ConfigurationABC):
else:
self._config[name] = value
def _validate_argument_by_argument_type(self, argument: str, argument_type: ConsoleArgument,
next_arguments: list[str] = None) -> bool:
r"""Validate argument by argument type
Parameter
---------
argument: :class:`str`
Command as string
argument_type: :class:`cpl_core.configuration.console_argument.ConsoleArgument`
Command type as ConsoleArgument
next_arguments: list[:class:`str`]
Following arguments of argument
Returns
-------
Object of :class:`bool`
Raises
------
Exception: An error occurred getting an argument for a command
"""
argument_name = ''
value = ''
result = False
if argument_type.value_token != '' and argument_type.value_token in argument:
# ?new=value
found = False
for alias in argument_type.aliases:
if alias in argument:
found = True
if argument_type.name not in argument_name and not found:
return False
if argument_type.is_value_token_optional is not None and argument_type.is_value_token_optional:
if argument_type.name not in self._additional_arguments:
self._additional_arguments.append(argument_type.name)
result = True
if argument_type.token != '' and argument.startswith(argument_type.token):
# --new=value
if len(argument.split(argument_type.token)[1].split(argument_type.value_token)) == 0:
raise Exception(f'Expected argument for command: {argument}')
argument_name = argument.split(argument_type.token)[1].split(argument_type.value_token)[0]
else:
# new=value
argument_name = argument.split(argument_type.value_token)[1]
if argument_name == '':
raise Exception(f'Expected argument for command: {argument_type.name}')
result = True
if argument_type.is_value_token_optional is True:
is_valid = False
name_list = argument.split(argument_type.token)
if len(name_list) > 1:
value_list = name_list[1].split(argument_type.value_token)
if len(value_list) > 1:
is_valid = True
value = argument.split(argument_type.token)[1].split(argument_type.value_token)[1]
if not is_valid:
if argument_type.name not in self._additional_arguments:
self._additional_arguments.append(argument_type.name)
result = True
else:
value = argument.split(argument_type.token)[1].split(argument_type.value_token)[1]
if argument_name != argument_type.name and argument_name not in argument_type.aliases:
return False
self._set_variable(argument_type.name, value)
result = True
elif argument_type.value_token == ' ':
# ?new value
found = False
for alias in argument_type.aliases:
if alias == argument or f' {alias} ' == argument:
found = True
if (argument_type.token != '' and f'{argument_type.token}{argument_type.name}' != argument
or argument_type.name not in argument) and not found:
return False
if (next_arguments is None or len(next_arguments) == 0) and \
argument_type.is_value_token_optional is not True:
raise Exception(f'Expected argument for command: {argument_type.name}')
if (next_arguments is None or len(next_arguments) == 0) and argument_type.is_value_token_optional is True:
value = ''
else:
value = next_arguments[0]
# next_arguments.remove(value)
self._handled_args.append(value)
if argument_type.token != '' and argument.startswith(argument_type.token):
# --new value
argument_name = argument.split(argument_type.token)[1]
else:
# new value
argument_name = argument
if argument_name != argument_type.name and argument_name not in argument_type.aliases:
return False
if value == '':
if argument_type.name not in self._additional_arguments:
self._additional_arguments.append(argument_type.name)
else:
self._set_variable(argument_type.name, value)
result = True
elif argument_type.name == argument or argument in argument_type.aliases:
# new
self._additional_arguments.append(argument_type.name)
result = True
if result:
self._handled_args.append(argument)
if next_arguments is not None and len(next_arguments) > 0:
next_args = []
if len(next_arguments) > 1:
next_args = next_arguments[1:]
if argument_type.console_arguments is not None and len(argument_type.console_arguments) > 0:
found_child = False
for child_argument_type in argument_type.console_arguments:
found_child = self._validate_argument_by_argument_type(
next_arguments[0],
child_argument_type,
next_args
)
if found_child and child_argument_type.name not in self._additional_arguments:
self._additional_arguments.append(child_argument_type.name)
if found_child:
break
if not found_child:
result = self._validate_argument_by_argument_type(next_arguments[0], argument_type, next_args)
return result
def _load_json_file(self, file: str, output: bool) -> dict:
r"""Reads the json file
@ -298,51 +153,65 @@ class Configuration(ConfigurationABC):
if var_name in [key.upper() for key in os.environ.keys()]:
self._set_variable(variable, os.environ[var_name])
def add_console_argument(self, argument: ConsoleArgument):
def add_console_argument(self, argument: ArgumentABC):
self._argument_types.append(argument)
def add_console_arguments(self, error: bool = None):
for arg_name in ConfigurationVariableNameEnum.to_list():
self.add_console_argument(ConsoleArgument('--', str(arg_name).upper(), [str(arg_name).lower()], '='))
arg_list = sys.argv[1:]
def _parse_arguments(self, call_stack: list[Callable], arg_list: list[str], args_types: list[ArgumentABC]):
for i in range(0, len(arg_list)):
argument = arg_list[i]
next_arguments = []
error_message = ''
arg_str = arg_list[i]
Console.write_line('ARG_STR', arg_str)
for arg in args_types:
Console.write_line('ARG', arg.name, type(arg), len(arg.console_arguments))
arg_str_without_token = arg_str
if arg.token != "" and arg.token in arg_str:
arg_str_without_token = arg_str.split(arg.token)[1]
if argument in self._handled_args:
break
# executable
if isinstance(arg, ExecutableArgument):
if arg_str.startswith(arg.token) \
and arg_str_without_token == arg.name or arg_str_without_token in arg.aliases:
Console.write_line('EXEC', arg.name)
call_stack.append(arg.run)
self._parse_arguments(call_stack, arg_list[i:], arg.console_arguments)
if i + 1 < len(arg_list):
next_arguments = arg_list[i + 1:]
# variables
elif isinstance(arg, VariableArgument):
arg_str_without_value = arg_str_without_token
if arg.value_token in arg_str_without_value:
arg_str_without_value = arg_str_without_token.split(arg.value_token)[0]
found = False
for argument_type in self._argument_types:
try:
found = self._validate_argument_by_argument_type(argument, argument_type, next_arguments)
if found:
break
except Exception as e:
error_message = e
if arg_str.startswith(arg.token) \
and arg_str_without_value == arg.name or arg_str_without_value in arg.aliases:
if arg.value_token != ' ':
value = arg_str_without_token.split(arg.value_token)[1]
else:
value = arg_list[i + 1]
self._set_variable(arg.name, value)
self._parse_arguments(call_stack, arg_list[i + 1:], arg.console_arguments)
# flags
elif isinstance(arg, FlagArgument):
if arg_str.startswith(arg.token) \
and arg_str_without_token == arg.name or arg_str_without_token in arg.aliases:
self._additional_arguments.append(arg.name)
self._parse_arguments(call_stack, arg_list[i + 1:], arg.console_arguments)
if not found and error_message == '' and error is not False:
error_message = f'Invalid argument: {argument}'
Console.write_line()
if error_message != '':
if self._argument_error_function is not None:
self._argument_error_function(error_message)
else:
self._print_error(__name__, error_message)
def parse_console_arguments(self, error: bool = None):
# sets environment variables as possible arguments as: --VAR=VALUE
for arg_name in ConfigurationVariableNameEnum.to_list():
self.add_console_argument(VariableArgument('--', str(arg_name).upper(), [str(arg_name).lower()], '='))
sys.exit()
Console.error('Parsing arguments:')
arg_list = sys.argv[1:]
call_stack = []
self._parse_arguments(call_stack, arg_list, self._argument_types)
add_args = []
for next_arg in next_arguments:
if next_arg not in self._handled_args and next_arg not in self._additional_arguments:
add_args.append(next_arg)
Console.error('Parsing finished')
self._set_variable(f'{argument}AdditionalArguments', add_args)
for call in call_stack:
Console.write_line(call)
call(self._additional_arguments)
def add_json_file(self, name: str, optional: bool = None, output: bool = True, path: str = None):
if os.path.isabs(name):
@ -380,11 +249,10 @@ class Configuration(ConfigurationABC):
def add_configuration(self, key_type: Union[str, type], value: ConfigurationModelABC):
self._config[key_type] = value
def create_console_argument(self, token: str, name: str, aliases: list[str], value_token: str,
is_value_token_optional: bool = None,
runnable: Type[RunnableArgumentABC] = None) -> ConsoleArgument:
argument = ConsoleArgument(token, name, aliases, value_token, is_value_token_optional, runnable)
self.add_console_argument(argument)
def create_console_argument(self, arg_type: ArgumentTypeEnum, token: str, name: str, aliases: list[str],
*args, **kwargs) -> ArgumentABC:
argument = ArgumentBuilder.build_argument(arg_type, token, name, aliases, *args, *kwargs)
self._argument_types.append(argument)
return argument
def get_configuration(self, search_type: Union[str, Type[ConfigurationModelABC]]) -> \
@ -408,4 +276,5 @@ class Configuration(ConfigurationABC):
def resolve_runnable_argument_types(self, services: ServiceProviderABC):
for arg in self._argument_types:
arg.set_runnable(services.get_service(arg.runnable_type))
if isinstance(arg, ExecutableArgument):
arg.set_executable(services.get_service(arg.executable_type))

View File

@ -2,9 +2,9 @@ from abc import abstractmethod, ABC
from collections.abc import Callable
from typing import Type, Union, Optional
from cpl_core.configuration.console_argument import ConsoleArgument
from cpl_core.configuration.configuration_model_abc import ConfigurationModelABC
from cpl_core.configuration.runnable_argument_abc import RunnableArgumentABC
from cpl_core.configuration.argument_abc import ArgumentABC
from cpl_core.configuration.argument_type_enum import ArgumentTypeEnum
from cpl_core.environment.application_environment_abc import ApplicationEnvironmentABC
@ -43,18 +43,18 @@ class ConfigurationABC(ABC):
pass
@abstractmethod
def add_console_argument(self, argument: ConsoleArgument):
def add_console_argument(self, argument: ArgumentABC):
r"""Adds console argument to known console arguments
Parameter
---------
argument: :class:`cpl_core.configuration.console_argument.ConsoleArgument`
argument: :class:`cpl_core.configuration.console_argument.ConsoleArgumentABC`
Specifies the console argument
"""
pass
@abstractmethod
def add_console_arguments(self, error: bool = None):
def parse_console_arguments(self, error: bool = None):
r"""Reads the console arguments
Parameter
@ -95,9 +95,8 @@ class ConfigurationABC(ABC):
pass
@abstractmethod
def create_console_argument(self, token: str, name: str, aliases: list[str], value_token: str,
is_value_token_optional: bool = None,
runnable: Type[RunnableArgumentABC] = None) -> ConsoleArgument:
def create_console_argument(self, arg_type: ArgumentTypeEnum, token: str, name: str, aliases: list[str],
*args, **kwargs) -> ArgumentABC:
r"""Creates and adds a console argument to known console arguments
Parameter
@ -112,12 +111,12 @@ class ConfigurationABC(ABC):
Specifies were the value begins
is_value_token_optional :class:`bool`
Specifies if values are optional
runnable: :class:`cpl_core.configuration.console_argument.ConsoleArgument`
runnable: :class:`cpl_core.configuration.console_argument.ConsoleArgumentABC`
Specifies class to run when called if value is not None
Returns
------
Object of :class:`cpl_core.configuration.console_argument.ConsoleArgument`
Object of :class:`cpl_core.configuration.console_argument.ConsoleArgumentABC`
"""
pass

View File

@ -1,99 +0,0 @@
from typing import Type, Optional
from cpl_core.configuration.runnable_argument_abc import RunnableArgumentABC
class ConsoleArgument:
def __init__(self,
token: str,
name: str,
aliases: list[str],
value_token: str,
is_value_token_optional: bool = None,
runnable: Type[RunnableArgumentABC] = None,
console_arguments: list['ConsoleArgument'] = None
):
r"""Representation of an console argument
Parameter
---------
token: :class:`str`
name: :class:`str`
aliases: list[:class:`str`]
value_token: :class:`str`
is_value_token_optional: :class:`bool`
runnable: :class:`cpl_core.configuration.console_argument.ConsoleArgument`
console_arguments: List[:class:`cpl_core.configuration.console_argument.ConsoleArgument`]
"""
self._token = token
self._name = name
self._aliases = aliases
self._value_token = value_token
self._is_value_token_optional = is_value_token_optional
self._console_arguments = console_arguments if console_arguments is not None else []
self._runnable_type = runnable
self._runnable: Optional[RunnableArgumentABC] = None
@property
def token(self) -> str:
return self._token
@property
def name(self) -> str:
return self._name
@property
def aliases(self) -> list[str]:
return self._aliases
@property
def value_token(self) -> str:
return self._value_token
@property
def is_value_token_optional(self) -> bool:
return self._is_value_token_optional
@property
def console_arguments(self) -> list['ConsoleArgument']:
return self._console_arguments
@property
def runnable_type(self) -> Type[RunnableArgumentABC]:
return self._runnable_type
def set_runnable(self, runnable: RunnableArgumentABC):
self._runnable = runnable
def add_console_argument(self, token: str, name: str, aliases: list[str], value_token: str,
is_value_token_optional: bool = None) -> 'ConsoleArgument':
r"""Creates and adds a console argument to known console arguments
Parameter
---------
token: :class:`str`
Specifies optional beginning of argument
name :class:`str`
Specifies name of argument
aliases list[:class:`str`]
Specifies possible aliases of name
value_token :class:`str`
Specifies were the value begins
is_value_token_optional :class:`bool`
Specifies if values are optional
Returns
------
self :class:`cpl_core.configuration.console_argument.ConsoleArgument` not created argument!
"""
argument = ConsoleArgument(token, name, aliases, value_token, is_value_token_optional)
self._console_arguments.append(argument)
return self
def run(self, args: list[str]):
r"""Executes runnable if exists
"""
if self._runnable is None:
return
self._runnable.run(args)

View File

@ -0,0 +1,34 @@
from typing import Type, Optional
from cpl_core.configuration.argument_executable_abc import ArgumentExecutableABC
from cpl_core.configuration.argument_abc import ArgumentABC
class ExecutableArgument(ArgumentABC):
def __init__(self,
token: str,
name: str,
aliases: list[str],
executable: Type[ArgumentExecutableABC],
console_arguments: list['ArgumentABC'] = None
):
self._executable_type = executable
self._executable: Optional[ArgumentExecutableABC] = None
ArgumentABC.__init__(self, token, name, aliases, console_arguments)
@property
def executable_type(self) -> type:
return self._executable_type
def set_executable(self, executable: ArgumentExecutableABC):
self._executable = executable
def run(self, args: list[str]):
r"""Executes runnable if exists
"""
if self._executable is None:
return
self._executable.run(args)

View File

@ -0,0 +1,13 @@
from cpl_core.configuration.argument_abc import ArgumentABC
class FlagArgument(ArgumentABC):
def __init__(self,
token: str,
name: str,
aliases: list[str],
console_arguments: list['ArgumentABC'] = None
):
ArgumentABC.__init__(self, token, name, aliases, console_arguments)

View File

@ -0,0 +1,27 @@
from cpl_core.configuration.argument_abc import ArgumentABC
class VariableArgument(ArgumentABC):
def __init__(self,
token: str,
name: str,
aliases: list[str],
value_token: str,
console_arguments: list['ArgumentABC'] = None
):
self._value_token = value_token
self._value: str = ''
ArgumentABC.__init__(self, token, name, aliases, console_arguments)
@property
def value_token(self) -> str:
return self._value_token
@property
def value(self) -> str:
return self._value
def set_value(self, value: str):
self._value = value

View File

@ -15,7 +15,7 @@ __title__ = 'cpl_core.console'
__author__ = 'Sven Heidemann'
__license__ = 'MIT'
__copyright__ = 'Copyright (c) 2020 - 2022 sh-edraft.de'
__version__ = '2022.6.2'
__version__ = '2022.6.3.dev2'
from collections import namedtuple
@ -27,4 +27,4 @@ from .foreground_color_enum import ForegroundColorEnum
from .spinner_thread import SpinnerThread
VersionInfo = namedtuple('VersionInfo', 'major minor micro')
version_info = VersionInfo(major='2022', minor='6', micro='2')
version_info = VersionInfo(major='2022', minor='6', micro='3.dev2')

View File

@ -4,7 +4,7 @@
"Version": {
"Major": "2022",
"Minor": "6",
"Micro": "2"
"Micro": "3.dev1"
},
"Author": "Sven Heidemann",
"AuthorEmail": "sven.heidemann@sh-edraft.de",

View File

@ -15,7 +15,7 @@ __title__ = 'cpl_core.database'
__author__ = 'Sven Heidemann'
__license__ = 'MIT'
__copyright__ = 'Copyright (c) 2020 - 2022 sh-edraft.de'
__version__ = '2022.6.2'
__version__ = '2022.6.3.dev2'
from collections import namedtuple
@ -25,4 +25,4 @@ from .database_settings import DatabaseSettings
from .table_abc import TableABC
VersionInfo = namedtuple('VersionInfo', 'major minor micro')
version_info = VersionInfo(major='2022', minor='6', micro='2')
version_info = VersionInfo(major='2022', minor='6', micro='3.dev2')

View File

@ -15,7 +15,7 @@ __title__ = 'cpl_core.database.connection'
__author__ = 'Sven Heidemann'
__license__ = 'MIT'
__copyright__ = 'Copyright (c) 2020 - 2022 sh-edraft.de'
__version__ = '2022.6.2'
__version__ = '2022.6.3.dev2'
from collections import namedtuple
@ -24,4 +24,4 @@ from .database_connection import DatabaseConnection
from .database_connection_abc import DatabaseConnectionABC
VersionInfo = namedtuple('VersionInfo', 'major minor micro')
version_info = VersionInfo(major='2022', minor='6', micro='2')
version_info = VersionInfo(major='2022', minor='6', micro='3.dev2')

View File

@ -15,7 +15,7 @@ __title__ = 'cpl_core.database.context'
__author__ = 'Sven Heidemann'
__license__ = 'MIT'
__copyright__ = 'Copyright (c) 2020 - 2022 sh-edraft.de'
__version__ = '2022.6.2'
__version__ = '2022.6.3.dev2'
from collections import namedtuple
@ -24,4 +24,4 @@ from .database_context import DatabaseContext
from .database_context_abc import DatabaseContextABC
VersionInfo = namedtuple('VersionInfo', 'major minor micro')
version_info = VersionInfo(major='2022', minor='6', micro='2')
version_info = VersionInfo(major='2022', minor='6', micro='3.dev2')

View File

@ -15,7 +15,7 @@ __title__ = 'cpl_core.dependency_injection'
__author__ = 'Sven Heidemann'
__license__ = 'MIT'
__copyright__ = 'Copyright (c) 2020 - 2022 sh-edraft.de'
__version__ = '2022.6.2'
__version__ = '2022.6.3.dev2'
from collections import namedtuple
@ -28,4 +28,4 @@ from .service_provider import ServiceProvider
from .service_provider_abc import ServiceProviderABC
VersionInfo = namedtuple('VersionInfo', 'major minor micro')
version_info = VersionInfo(major='2022', minor='6', micro='2')
version_info = VersionInfo(major='2022', minor='6', micro='3.dev2')

View File

@ -34,7 +34,7 @@ class ServiceProvider(ServiceProviderABC):
self._database_context = db_context
self._scope: Optional[ScopeABC] = None
def _find_service(self, service_type: type) -> ServiceDescriptor:
def _find_service(self, service_type: type) -> Optional[ServiceDescriptor]:
for descriptor in self._service_descriptors:
if descriptor.service_type == service_type or issubclass(descriptor.service_type, service_type):
return descriptor

View File

@ -15,7 +15,7 @@ __title__ = 'cpl_core.environment'
__author__ = 'Sven Heidemann'
__license__ = 'MIT'
__copyright__ = 'Copyright (c) 2020 - 2022 sh-edraft.de'
__version__ = '2022.6.2'
__version__ = '2022.6.3.dev2'
from collections import namedtuple
@ -25,4 +25,4 @@ from .environment_name_enum import EnvironmentNameEnum
from .application_environment import ApplicationEnvironment
VersionInfo = namedtuple('VersionInfo', 'major minor micro')
version_info = VersionInfo(major='2022', minor='6', micro='2')
version_info = VersionInfo(major='2022', minor='6', micro='3.dev2')

View File

@ -15,7 +15,7 @@ __title__ = 'cpl_core.logging'
__author__ = 'Sven Heidemann'
__license__ = 'MIT'
__copyright__ = 'Copyright (c) 2020 - 2022 sh-edraft.de'
__version__ = '2022.6.2'
__version__ = '2022.6.3.dev2'
from collections import namedtuple
@ -27,4 +27,4 @@ from .logging_settings import LoggingSettings
from .logging_settings_name_enum import LoggingSettingsNameEnum
VersionInfo = namedtuple('VersionInfo', 'major minor micro')
version_info = VersionInfo(major='2022', minor='6', micro='2')
version_info = VersionInfo(major='2022', minor='6', micro='3.dev2')

View File

@ -15,7 +15,7 @@ __title__ = 'cpl_core.mailing'
__author__ = 'Sven Heidemann'
__license__ = 'MIT'
__copyright__ = 'Copyright (c) 2020 - 2022 sh-edraft.de'
__version__ = '2022.6.2'
__version__ = '2022.6.3.dev2'
from collections import namedtuple
@ -27,4 +27,4 @@ from .email_client_settings import EMailClientSettings
from .email_client_settings_name_enum import EMailClientSettingsNameEnum
VersionInfo = namedtuple('VersionInfo', 'major minor micro')
version_info = VersionInfo(major='2022', minor='6', micro='2')
version_info = VersionInfo(major='2022', minor='6', micro='3.dev2')

View File

@ -15,7 +15,7 @@ __title__ = 'cpl_core.time'
__author__ = 'Sven Heidemann'
__license__ = 'MIT'
__copyright__ = 'Copyright (c) 2020 - 2022 sh-edraft.de'
__version__ = '2022.6.2'
__version__ = '2022.6.3.dev2'
from collections import namedtuple
@ -24,4 +24,4 @@ from .time_format_settings import TimeFormatSettings
from .time_format_settings_names_enum import TimeFormatSettingsNamesEnum
VersionInfo = namedtuple('VersionInfo', 'major minor micro')
version_info = VersionInfo(major='2022', minor='6', micro='2')
version_info = VersionInfo(major='2022', minor='6', micro='3.dev2')

View File

@ -15,7 +15,7 @@ __title__ = 'cpl_core.utils'
__author__ = 'Sven Heidemann'
__license__ = 'MIT'
__copyright__ = 'Copyright (c) 2020 - 2022 sh-edraft.de'
__version__ = '2022.6.2'
__version__ = '2022.6.3.dev2'
from collections import namedtuple
@ -25,4 +25,4 @@ from .string import String
from .pip import Pip
VersionInfo = namedtuple('VersionInfo', 'major minor micro')
version_info = VersionInfo(major='2022', minor='6', micro='2')
version_info = VersionInfo(major='2022', minor='6', micro='3.dev2')

View File

@ -21,7 +21,7 @@ class Startup(StartupABC):
def configure_configuration(self, configuration: ConfigurationABC, environment: ApplicationEnvironmentABC) -> ConfigurationABC:
configuration.add_environment_variables('PYTHON_')
configuration.add_environment_variables('CPL_')
configuration.add_console_arguments()
configuration.parse_console_arguments()
configuration.add_json_file(f'appsettings.json')
configuration.add_json_file(f'appsettings.{configuration.environment.environment_name}.json')
configuration.add_json_file(f'appsettings.{configuration.environment.host_name}.json', optional=True)

View File

@ -1,11 +1,18 @@
from cpl_core.configuration.runnable_argument_abc import RunnableArgumentABC
from cpl_core.configuration import ConfigurationABC
from cpl_core.configuration.argument_executable_abc import ArgumentExecutableABC
from cpl_core.console import Console
from cpl_core.environment import ApplicationEnvironmentABC
class GenerateArgument(RunnableArgumentABC):
class GenerateArgument(ArgumentExecutableABC):
def __init__(self):
RunnableArgumentABC.__init__(self)
def __init__(self, config: ConfigurationABC, env: ApplicationEnvironmentABC):
ArgumentExecutableABC.__init__(self)
self._config = config
self._env = env
def run(self, args: list[str]):
Console.write_line('Generate:', args)
Console.error('Generate:')
for c in self._config._config:
Console.write_line(c, self._config.get_configuration(c))
Console.write_line(args, self._env.environment_name)

View File

@ -1,11 +1,11 @@
from cpl_core.configuration.runnable_argument_abc import RunnableArgumentABC
from cpl_core.configuration.argument_executable_abc import ArgumentExecutableABC
from cpl_core.console import Console
class InstallArgument(RunnableArgumentABC):
class InstallArgument(ArgumentExecutableABC):
def __init__(self):
RunnableArgumentABC.__init__(self)
ArgumentExecutableABC.__init__(self)
def run(self, args: list[str]):
Console.write_line('Install:', args)

View File

@ -1,5 +1,6 @@
from cpl_core.application import StartupExtensionABC
from cpl_core.configuration import ConfigurationABC
from cpl_core.configuration.argument_type_enum import ArgumentTypeEnum
from cpl_core.dependency_injection import ServiceCollectionABC
from cpl_core.environment import ApplicationEnvironmentABC
from arguments.generate_argument import GenerateArgument
@ -12,18 +13,18 @@ class ParameterStartup(StartupExtensionABC):
StartupExtensionABC.__init__(self)
def configure_configuration(self, config: ConfigurationABC, env: ApplicationEnvironmentABC):
config.create_console_argument('', 'generate', ['g', 'G'], '', runnable=GenerateArgument) \
.add_console_argument('', 'abc', ['a', 'A'], ' ') \
.add_console_argument('', 'class', ['c', 'C'], ' ') \
.add_console_argument('', 'enum', ['e', 'E'], ' ') \
.add_console_argument('', 'service', ['s', 'S'], ' ') \
.add_console_argument('', 'settings', ['st', 'ST'], ' ') \
.add_console_argument('', 'thread', ['t', 'T'], ' ') \
.add_console_argument('-', 'o', ['o', 'O'], '=')
config.create_console_argument('', 'install', ['i', 'I'], ' ', is_value_token_optional=True,
runnable=InstallArgument) \
.add_console_argument('--', 'virtual', ['v', 'V'], '') \
.add_console_argument('--', 'simulate', ['s', 'S'], '')
config.create_console_argument(ArgumentTypeEnum.Executable, '', 'generate', ['g', 'G'], GenerateArgument) \
.add_console_argument(ArgumentTypeEnum.Variable, '', 'abc', ['a', 'A'], ' ') \
.add_console_argument(ArgumentTypeEnum.Variable, '', 'class', ['c', 'C'], ' ') \
.add_console_argument(ArgumentTypeEnum.Variable, '', 'enum', ['e', 'E'], ' ') \
.add_console_argument(ArgumentTypeEnum.Variable, '', 'service', ['s', 'S'], ' ') \
.add_console_argument(ArgumentTypeEnum.Variable, '', 'settings', ['st', 'ST'], ' ') \
.add_console_argument(ArgumentTypeEnum.Variable, '', 'thread', ['t', 'T'], ' ') \
.add_console_argument(ArgumentTypeEnum.Variable, '-', 'o', ['o', 'O'], '=') \
.add_console_argument(ArgumentTypeEnum.Flag, '--', 'virtual', ['v', 'V'])
config.create_console_argument(ArgumentTypeEnum.Executable, '', 'install', ['i', 'I'], InstallArgument) \
.add_console_argument(ArgumentTypeEnum.Flag, '--', 'virtual', ['v', 'V']) \
.add_console_argument(ArgumentTypeEnum.Flag, '--', 'simulate', ['s', 'S'])
def configure_services(self, services: ServiceCollectionABC, env: ApplicationEnvironmentABC):
services \