Added comments to configuration

This commit is contained in:
Sven Heidemann 2021-03-14 16:46:21 +01:00
parent 84680c0c44
commit 3f56247aa7
4 changed files with 118 additions and 16 deletions

View File

@ -18,6 +18,9 @@ from cpl.environment.environment_name_enum import EnvironmentNameEnum
class Configuration(ConfigurationABC): class Configuration(ConfigurationABC):
def __init__(self): def __init__(self):
"""
Representation of configuration
"""
ConfigurationABC.__init__(self) ConfigurationABC.__init__(self)
self._hosting_environment = ApplicationEnvironment() self._hosting_environment = ApplicationEnvironment()
@ -28,7 +31,6 @@ class Configuration(ConfigurationABC):
self._argument_error_function: Optional[Callable] = None self._argument_error_function: Optional[Callable] = None
self._is_multiple_args_allowed = False
self._handled_args = [] self._handled_args = []
@property @property
@ -47,28 +49,49 @@ 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):
"""
Prints an info message
:param name:
:param message:
:return:
"""
Console.set_foreground_color(ForegroundColorEnum.green) Console.set_foreground_color(ForegroundColorEnum.green)
Console.write_line(f'[{name}] {message}') Console.write_line(f'[{name}] {message}')
Console.set_foreground_color(ForegroundColorEnum.default) Console.set_foreground_color(ForegroundColorEnum.default)
@staticmethod @staticmethod
def _print_warn(name: str, message: str): def _print_warn(name: str, message: str):
"""
Prints a warning
:param name:
:param message:
:return:
"""
Console.set_foreground_color(ForegroundColorEnum.yellow) Console.set_foreground_color(ForegroundColorEnum.yellow)
Console.write_line(f'[{name}] {message}') Console.write_line(f'[{name}] {message}')
Console.set_foreground_color(ForegroundColorEnum.default) Console.set_foreground_color(ForegroundColorEnum.default)
@staticmethod @staticmethod
def _print_error(name: str, message: str): def _print_error(name: str, message: str):
"""
Prints an error
:param name:
:param message:
:return:
"""
Console.set_foreground_color(ForegroundColorEnum.red) Console.set_foreground_color(ForegroundColorEnum.red)
Console.write_line(f'[{name}] {message}') Console.write_line(f'[{name}] {message}')
Console.set_foreground_color(ForegroundColorEnum.default) Console.set_foreground_color(ForegroundColorEnum.default)
def _set_variable(self, name: str, value: str): def _set_variable(self, name: str, value: str):
"""
Sets variable to given value
:param name:
:param value:
:return:
"""
if name == ConfigurationVariableNameEnum.environment.value: if name == ConfigurationVariableNameEnum.environment.value:
self._hosting_environment.environment_name = EnvironmentNameEnum(value) self._hosting_environment.environment_name = EnvironmentNameEnum(value)
@ -83,6 +106,13 @@ class Configuration(ConfigurationABC):
def _validate_argument_child(self, argument: str, argument_type: ConsoleArgument, def _validate_argument_child(self, argument: str, argument_type: ConsoleArgument,
next_arguments: Optional[list[str]]) -> bool: next_arguments: Optional[list[str]]) -> bool:
"""
Validates the child arguments of argument
:param argument:
:param argument_type:
:param next_arguments:
:return:
"""
if argument_type.console_arguments is not None and len(argument_type.console_arguments) > 0: if argument_type.console_arguments is not None and len(argument_type.console_arguments) > 0:
found = False found = False
for child_argument_type in argument_type.console_arguments: for child_argument_type in argument_type.console_arguments:
@ -99,6 +129,13 @@ class Configuration(ConfigurationABC):
def _validate_argument_by_argument_type(self, argument: str, argument_type: ConsoleArgument, def _validate_argument_by_argument_type(self, argument: str, argument_type: ConsoleArgument,
next_arguments: list[str] = None) -> bool: next_arguments: list[str] = None) -> bool:
"""
Validate argument by argument type
:param argument:
:param argument_type:
:param next_arguments:
:return:
"""
argument_name = '' argument_name = ''
value = '' value = ''
result = False result = False
@ -274,6 +311,12 @@ class Configuration(ConfigurationABC):
self.add_configuration(sub, configuration) self.add_configuration(sub, configuration)
def _load_json_file(self, file: str, output: bool) -> dict: def _load_json_file(self, file: str, output: bool) -> dict:
"""
Reads the json file
:param file:
:param output:
:return:
"""
try: try:
# open config file, create if not exists # open config file, create if not exists
with open(file, encoding='utf-8') as cfg: with open(file, encoding='utf-8') as cfg:

View File

@ -10,7 +10,11 @@ from cpl.environment.environment_abc import ApplicationEnvironmentABC
class ConfigurationABC(ABC): class ConfigurationABC(ABC):
@abstractmethod @abstractmethod
def __init__(self): pass def __init__(self):
"""
ABC of configuration
"""
pass
@property @property
@abstractmethod @abstractmethod
@ -29,22 +33,58 @@ class ConfigurationABC(ABC):
def argument_error_function(self, argument_error_function: Callable): pass def argument_error_function(self, argument_error_function: Callable): pass
@abstractmethod @abstractmethod
def allow_multiple_args(self): pass def add_environment_variables(self, prefix: str):
"""
Reads the environment variables
:param prefix:
:return:
"""
pass
@abstractmethod @abstractmethod
def add_environment_variables(self, prefix: str): pass def add_console_argument(self, argument: ConsoleArgument):
"""
Adds console argument to known console arguments
:param argument:
:return:
"""
pass
@abstractmethod @abstractmethod
def add_console_argument(self, argument: ConsoleArgument): pass def add_console_arguments(self):
"""
Reads the console arguments
:return:
"""
pass
@abstractmethod @abstractmethod
def add_console_arguments(self): pass def add_json_file(self, name: str, optional: bool = None, output: bool = True):
"""
Reads and saves settings from given json file
:param name:
:param optional:
:param output:
:return:
"""
pass
@abstractmethod @abstractmethod
def add_json_file(self, name: str, optional: bool = None, output: bool = True): pass def add_configuration(self, key_type: type, value: object):
"""
Add configuration object
:param key_type:
:param value:
:return:
"""
pass
@abstractmethod @abstractmethod
def add_configuration(self, key_type: type, value: object): pass def get_configuration(self, search_type: Union[str, Type[ConfigurationModelABC]]) -> Union[
str, Callable[ConfigurationModelABC]]:
@abstractmethod """
def get_configuration(self, search_type: Union[str, Type[ConfigurationModelABC]]) -> Union[str, Callable[ConfigurationModelABC]]: pass Returns value in configuration by given type
:param search_type:
:return:
"""
pass

View File

@ -4,7 +4,17 @@ from abc import ABC, abstractmethod
class ConfigurationModelABC(ABC): class ConfigurationModelABC(ABC):
@abstractmethod @abstractmethod
def __init__(self): pass def __init__(self):
"""
ABC for settings representation
"""
pass
@abstractmethod @abstractmethod
def from_dict(self, settings: dict): pass def from_dict(self, settings: dict):
"""
Converts attributes to dict
:param settings:
:return:
"""
pass

View File

@ -8,6 +8,15 @@ class ConsoleArgument:
is_value_token_optional: bool = None, is_value_token_optional: bool = None,
console_arguments: list['ConsoleArgument'] = None console_arguments: list['ConsoleArgument'] = None
): ):
"""
Representation of an console argument
:param token:
:param name:
:param aliases:
:param value_token:
:param is_value_token_optional:
:param console_arguments:
"""
self._token = token self._token = token
self._name = name self._name = name
self._aliases = aliases self._aliases = aliases