2021.4.1 #11
@ -28,6 +28,8 @@ class Configuration(ConfigurationABC):
|
|||||||
|
|
||||||
self._argument_error_function: Optional[Callable] = None
|
self._argument_error_function: Optional[Callable] = None
|
||||||
|
|
||||||
|
self._is_multiple_args_allowed = False
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def environment(self) -> EnvironmentABC:
|
def environment(self) -> EnvironmentABC:
|
||||||
return self._hosting_environment
|
return self._hosting_environment
|
||||||
@ -44,6 +46,9 @@ class Configuration(ConfigurationABC):
|
|||||||
def argument_error_function(self, argument_error_function: Callable):
|
def argument_error_function(self, argument_error_function: Callable):
|
||||||
self._argument_error_function = argument_error_function
|
self._argument_error_function = argument_error_function
|
||||||
|
|
||||||
|
def allow_multiple_args(self):
|
||||||
|
self._is_multiple_args_allowed = True
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _print_info(name: str, message: str):
|
def _print_info(name: str, message: str):
|
||||||
Console.set_foreground_color(ForegroundColor.green)
|
Console.set_foreground_color(ForegroundColor.green)
|
||||||
@ -81,14 +86,16 @@ class Configuration(ConfigurationABC):
|
|||||||
if var_name in [key.upper() for key in os.environ.keys()]:
|
if var_name in [key.upper() for key in os.environ.keys()]:
|
||||||
self._set_variable(variable, os.environ[var_name])
|
self._set_variable(variable, os.environ[var_name])
|
||||||
|
|
||||||
def add_console_argument(self, token: str, name: str, aliases: list[str], value_token: str):
|
def add_console_argument(self, argument: ConsoleArgument):
|
||||||
self._argument_types.append(ConsoleArgument(token, name, aliases, value_token))
|
self._argument_types.append(argument)
|
||||||
|
|
||||||
def add_console_arguments(self):
|
def add_console_arguments(self):
|
||||||
for arg_name in ConfigurationVariableName.to_list():
|
for arg_name in ConfigurationVariableName.to_list():
|
||||||
self.add_console_argument('--', arg_name, [], '')
|
self.add_console_argument(ConsoleArgument('--', arg_name, [], ''))
|
||||||
|
|
||||||
for arg in sys.argv[1:]:
|
old_is_arg = None
|
||||||
|
arg_list = sys.argv[1:]
|
||||||
|
for arg in arg_list:
|
||||||
try:
|
try:
|
||||||
is_done = False
|
is_done = False
|
||||||
for argument_type in self._argument_types:
|
for argument_type in self._argument_types:
|
||||||
@ -98,21 +105,82 @@ class Configuration(ConfigurationABC):
|
|||||||
|
|
||||||
if argument_type.value_token == '':
|
if argument_type.value_token == '':
|
||||||
if name == argument_type.name or name in argument_type.aliases:
|
if name == argument_type.name or name in argument_type.aliases:
|
||||||
|
if not self._is_multiple_args_allowed:
|
||||||
|
if old_is_arg is None:
|
||||||
self._additional_arguments.append(argument_type.name)
|
self._additional_arguments.append(argument_type.name)
|
||||||
is_done = True
|
is_done = True
|
||||||
|
old_is_arg = argument_type
|
||||||
|
break
|
||||||
|
break
|
||||||
|
|
||||||
|
self._additional_arguments.append(argument_type.name)
|
||||||
|
is_done = True
|
||||||
|
old_is_arg = argument_type
|
||||||
break
|
break
|
||||||
|
|
||||||
if argument_type.value_token != '' and arg.__contains__(argument_type.value_token):
|
if argument_type.value_token != '' and arg.__contains__(argument_type.value_token):
|
||||||
name = name.split(argument_type.value_token)[0]
|
name = name.split(argument_type.value_token)[0]
|
||||||
if name == argument_type.name or name in argument_type.aliases:
|
if name == argument_type.name or name in argument_type.aliases:
|
||||||
|
if not self._is_multiple_args_allowed:
|
||||||
|
if old_is_arg is None:
|
||||||
value = arg.split(argument_type.value_token)[1]
|
value = arg.split(argument_type.value_token)[1]
|
||||||
self._set_variable(argument_type.name, value)
|
self._set_variable(argument_type.name, value)
|
||||||
is_done = True
|
is_done = True
|
||||||
|
old_is_arg = argument_type
|
||||||
|
break
|
||||||
break
|
break
|
||||||
|
|
||||||
elif argument_type.value_token == '' and arg == argument_type.name or arg in argument_type.aliases:
|
value = arg.split(argument_type.value_token)[1]
|
||||||
|
self._set_variable(argument_type.name, value)
|
||||||
is_done = True
|
is_done = True
|
||||||
|
old_is_arg = argument_type
|
||||||
|
break
|
||||||
|
|
||||||
|
elif arg == argument_type.name or arg in argument_type.aliases:
|
||||||
|
if not self._is_multiple_args_allowed:
|
||||||
|
if old_is_arg is None:
|
||||||
self._additional_arguments.append(argument_type.name)
|
self._additional_arguments.append(argument_type.name)
|
||||||
|
is_done = True
|
||||||
|
old_is_arg = argument_type
|
||||||
|
break
|
||||||
|
break
|
||||||
|
|
||||||
|
self._additional_arguments.append(argument_type.name)
|
||||||
|
is_done = True
|
||||||
|
old_is_arg = argument_type
|
||||||
|
break
|
||||||
|
|
||||||
|
elif old_is_arg is not None and old_is_arg.arg_types is not None:
|
||||||
|
for arg_type in old_is_arg.arg_types:
|
||||||
|
if is_done:
|
||||||
|
break
|
||||||
|
|
||||||
|
if arg_type.name == arg:
|
||||||
|
if arg_type.value_token != '' and arg_type.value_token in arg:
|
||||||
|
value = arg.split(arg_type.value_token)[1]
|
||||||
|
self._set_variable(arg_type.name, value)
|
||||||
|
self._additional_arguments.append(arg_type.name)
|
||||||
|
is_done = True
|
||||||
|
old_is_arg = arg_type
|
||||||
|
break
|
||||||
|
|
||||||
|
elif arg_type.value_token == ' ':
|
||||||
|
for i in range(0, len(arg_list)):
|
||||||
|
if arg_list[i] == arg and i+1 < len(arg_list):
|
||||||
|
value = arg_list[i+1]
|
||||||
|
self._set_variable(arg_type.name, value)
|
||||||
|
self._additional_arguments.append(arg_type.name)
|
||||||
|
arg_list.remove(value)
|
||||||
|
is_done = True
|
||||||
|
old_is_arg = arg_type
|
||||||
|
break
|
||||||
|
|
||||||
|
if is_done:
|
||||||
|
break
|
||||||
|
|
||||||
|
self._additional_arguments.append(arg_type.name)
|
||||||
|
is_done = True
|
||||||
|
old_is_arg = arg_type
|
||||||
break
|
break
|
||||||
|
|
||||||
if not is_done:
|
if not is_done:
|
||||||
@ -177,7 +245,8 @@ class Configuration(ConfigurationABC):
|
|||||||
def add_configuration(self, key_type: type, value: ConfigurationModelABC):
|
def add_configuration(self, key_type: type, value: ConfigurationModelABC):
|
||||||
self._config[key_type] = value
|
self._config[key_type] = value
|
||||||
|
|
||||||
def get_configuration(self, search_type: Union[str, Type[ConfigurationModelABC]]) -> Union[str, Callable[ConfigurationModelABC]]:
|
def get_configuration(self, search_type: Union[str, Type[ConfigurationModelABC]]) -> Union[
|
||||||
|
str, Callable[ConfigurationModelABC]]:
|
||||||
if search_type not in self._config:
|
if search_type not in self._config:
|
||||||
raise Exception(f'Config model by type {search_type} not found')
|
raise Exception(f'Config model by type {search_type} not found')
|
||||||
|
|
||||||
|
@ -2,6 +2,7 @@ from abc import abstractmethod, ABC
|
|||||||
from collections import Callable
|
from collections import Callable
|
||||||
from typing import Type, Union, Optional
|
from typing import Type, Union, Optional
|
||||||
|
|
||||||
|
from cpl.configuration.console_argument import ConsoleArgument
|
||||||
from cpl.configuration.configuration_model_abc import ConfigurationModelABC
|
from cpl.configuration.configuration_model_abc import ConfigurationModelABC
|
||||||
from cpl.environment.environment_abc import EnvironmentABC
|
from cpl.environment.environment_abc import EnvironmentABC
|
||||||
|
|
||||||
@ -27,11 +28,14 @@ class ConfigurationABC(ABC):
|
|||||||
@abstractmethod
|
@abstractmethod
|
||||||
def argument_error_function(self, argument_error_function: Callable): pass
|
def argument_error_function(self, argument_error_function: Callable): pass
|
||||||
|
|
||||||
|
@abstractmethod
|
||||||
|
def allow_multiple_args(self): pass
|
||||||
|
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
def add_environment_variables(self, prefix: str): pass
|
def add_environment_variables(self, prefix: str): pass
|
||||||
|
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
def add_console_argument(self, token: str, name: str, aliases: list[str], value_token: str): pass
|
def add_console_argument(self, argument: ConsoleArgument): pass
|
||||||
|
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
def add_console_arguments(self): pass
|
def add_console_arguments(self): pass
|
||||||
|
@ -1,23 +1,28 @@
|
|||||||
class ConsoleArgument:
|
class ConsoleArgument:
|
||||||
|
|
||||||
def __init__(self, token: str, name: str, aliases: list[str], value_token: str):
|
def __init__(self, token: str, name: str, aliases: list[str], value_token: str, arg_types: list['ConsoleArgument'] = None):
|
||||||
self._token = token
|
self._token = token
|
||||||
self._name = name
|
self._name = name
|
||||||
self._aliases = aliases
|
self._aliases = aliases
|
||||||
self._value_token = value_token
|
self._value_token = value_token
|
||||||
|
self._arg_types = arg_types
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def token(self):
|
def token(self) -> str:
|
||||||
return self._token
|
return self._token
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def name(self):
|
def name(self) -> str:
|
||||||
return self._name
|
return self._name
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def aliases(self):
|
def aliases(self) -> list[str]:
|
||||||
return self._aliases
|
return self._aliases
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def value_token(self):
|
def value_token(self) -> str:
|
||||||
return self._value_token
|
return self._value_token
|
||||||
|
|
||||||
|
@property
|
||||||
|
def arg_types(self) -> list['ConsoleArgument']:
|
||||||
|
return self._arg_types
|
||||||
|
@ -3,6 +3,7 @@ from inspect import signature, Parameter
|
|||||||
from typing import Type, Optional, Union
|
from typing import Type, Optional, Union
|
||||||
|
|
||||||
from cpl.application.application_runtime_abc import ApplicationRuntimeABC
|
from cpl.application.application_runtime_abc import ApplicationRuntimeABC
|
||||||
|
from cpl.configuration.configuration_abc import ConfigurationABC
|
||||||
from cpl.configuration.configuration_model_abc import ConfigurationModelABC
|
from cpl.configuration.configuration_model_abc import ConfigurationModelABC
|
||||||
from cpl.database.context.database_context_abc import DatabaseContextABC
|
from cpl.database.context.database_context_abc import DatabaseContextABC
|
||||||
from cpl.dependency_injection.service_abc import ServiceABC
|
from cpl.dependency_injection.service_abc import ServiceABC
|
||||||
@ -39,6 +40,9 @@ class ServiceProvider(ServiceProviderABC):
|
|||||||
elif issubclass(parameter.annotation, ConfigurationModelABC):
|
elif issubclass(parameter.annotation, ConfigurationModelABC):
|
||||||
params.append(self._app_runtime.configuration.get_configuration(parameter.annotation))
|
params.append(self._app_runtime.configuration.get_configuration(parameter.annotation))
|
||||||
|
|
||||||
|
elif issubclass(parameter.annotation, ConfigurationABC):
|
||||||
|
params.append(self._app_runtime.configuration)
|
||||||
|
|
||||||
elif issubclass(parameter.annotation, ServiceProviderABC):
|
elif issubclass(parameter.annotation, ServiceProviderABC):
|
||||||
params.append(self)
|
params.append(self)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user