Improved & added commands
This commit is contained in:
@@ -20,11 +20,17 @@ __version__ = '2022.6.3.dev2'
|
||||
from collections import namedtuple
|
||||
|
||||
# imports:
|
||||
from .argument_abc import ArgumentABC
|
||||
from .argument_builder import ArgumentBuilder
|
||||
from .argument_executable_abc import ArgumentExecutableABC
|
||||
from .argument_type_enum import ArgumentTypeEnum
|
||||
from .configuration import Configuration
|
||||
from .configuration_abc import ConfigurationABC
|
||||
from .configuration_model_abc import ConfigurationModelABC
|
||||
from .configuration_variable_name_enum import ConfigurationVariableNameEnum
|
||||
from .argument_abc import ArgumentABC
|
||||
from .executable_argument import ExecutableArgument
|
||||
from .flag_argument import FlagArgument
|
||||
from .variable_argument import VariableArgument
|
||||
|
||||
VersionInfo = namedtuple('VersionInfo', 'major minor micro')
|
||||
version_info = VersionInfo(major='2022', minor='6', micro='3.dev2')
|
||||
|
@@ -147,15 +147,6 @@ class Configuration(ConfigurationABC):
|
||||
self._print_error(__name__, f'Cannot load config file: {file}! -> {e}')
|
||||
return {}
|
||||
|
||||
def add_environment_variables(self, prefix: str):
|
||||
for variable in ConfigurationVariableNameEnum.to_list():
|
||||
var_name = f'{prefix}{variable}'
|
||||
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: ArgumentABC):
|
||||
self._argument_types.append(argument)
|
||||
|
||||
def _parse_arguments(self, call_stack: list[Callable], arg_list: list[str], args_types: list[ArgumentABC]):
|
||||
for i in range(0, len(arg_list)):
|
||||
arg_str = arg_list[i]
|
||||
@@ -169,7 +160,8 @@ class Configuration(ConfigurationABC):
|
||||
if arg_str.startswith(arg.token) \
|
||||
and arg_str_without_token == arg.name or arg_str_without_token in arg.aliases:
|
||||
call_stack.append(arg.run)
|
||||
self._parse_arguments(call_stack, arg_list[i:], arg.console_arguments)
|
||||
self._parse_arguments(call_stack, arg_list[i + 1:], arg.console_arguments)
|
||||
break
|
||||
|
||||
# variables
|
||||
elif isinstance(arg, VariableArgument):
|
||||
@@ -185,24 +177,28 @@ class Configuration(ConfigurationABC):
|
||||
value = arg_list[i + 1]
|
||||
self._set_variable(arg.name, value)
|
||||
self._parse_arguments(call_stack, arg_list[i + 1:], arg.console_arguments)
|
||||
break
|
||||
|
||||
# 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)
|
||||
break
|
||||
|
||||
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()], '='))
|
||||
# add left over values to args
|
||||
if arg_str not in self._additional_arguments:
|
||||
self._additional_arguments.append(arg_str)
|
||||
|
||||
arg_list = sys.argv[1:]
|
||||
call_stack = []
|
||||
self._parse_arguments(call_stack, arg_list, self._argument_types)
|
||||
def add_environment_variables(self, prefix: str):
|
||||
for variable in ConfigurationVariableNameEnum.to_list():
|
||||
var_name = f'{prefix}{variable}'
|
||||
if var_name in [key.upper() for key in os.environ.keys()]:
|
||||
self._set_variable(variable, os.environ[var_name])
|
||||
|
||||
for call in call_stack:
|
||||
call(self._additional_arguments)
|
||||
def add_console_argument(self, argument: ArgumentABC):
|
||||
self._argument_types.append(argument)
|
||||
|
||||
def add_json_file(self, name: str, optional: bool = None, output: bool = True, path: str = None):
|
||||
if os.path.isabs(name):
|
||||
@@ -246,6 +242,10 @@ class Configuration(ConfigurationABC):
|
||||
self._argument_types.append(argument)
|
||||
return argument
|
||||
|
||||
def for_each_argument(self, call: Callable):
|
||||
for arg in self._argument_types:
|
||||
call(arg)
|
||||
|
||||
def get_configuration(self, search_type: Union[str, Type[ConfigurationModelABC]]) -> \
|
||||
Optional[Union[str, ConfigurationModelABC]]:
|
||||
if type(search_type) is str:
|
||||
@@ -265,6 +265,18 @@ class Configuration(ConfigurationABC):
|
||||
if config_model == search_type:
|
||||
return self._config[config_model]
|
||||
|
||||
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()], '='))
|
||||
|
||||
arg_list = sys.argv[1:]
|
||||
call_stack = []
|
||||
self._parse_arguments(call_stack, arg_list, self._argument_types)
|
||||
|
||||
for call in call_stack:
|
||||
call(self._additional_arguments)
|
||||
|
||||
def resolve_runnable_argument_types(self, services: ServiceProviderABC):
|
||||
for arg in self._argument_types:
|
||||
if isinstance(arg, ExecutableArgument):
|
||||
|
@@ -53,17 +53,6 @@ class ConfigurationABC(ABC):
|
||||
"""
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def parse_console_arguments(self, error: bool = None):
|
||||
r"""Reads the console arguments
|
||||
|
||||
Parameter
|
||||
---------
|
||||
error: :class:`bool`
|
||||
Defines is invalid argument error will be shown or not
|
||||
"""
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def add_json_file(self, name: str, optional: bool = None, output: bool = True, path: str = None):
|
||||
r"""Reads and saves settings from given json file
|
||||
@@ -120,6 +109,17 @@ class ConfigurationABC(ABC):
|
||||
"""
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def for_each_argument(self, call: Callable):
|
||||
r"""Iterates through all arguments and calls the call function
|
||||
|
||||
Parameter
|
||||
---------
|
||||
call: :class:`Callable`
|
||||
Call for each argument
|
||||
"""
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def get_configuration(self, search_type: Union[str, Type[ConfigurationModelABC]]) -> Union[
|
||||
str, ConfigurationModelABC]:
|
||||
@@ -136,6 +136,17 @@ class ConfigurationABC(ABC):
|
||||
"""
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def parse_console_arguments(self, error: bool = None):
|
||||
r"""Reads the console arguments
|
||||
|
||||
Parameter
|
||||
---------
|
||||
error: :class:`bool`
|
||||
Defines is invalid argument error will be shown or not
|
||||
"""
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def resolve_runnable_argument_types(self, services: 'ServiceProviderABC'):
|
||||
r"""Gets all objects for given types of ConsoleArguments
|
||||
|
Reference in New Issue
Block a user