From 3f56247aa7f81b62fbed045089cc9b5c454b3ece Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Sun, 14 Mar 2021 16:46:21 +0100 Subject: [PATCH] Added comments to configuration --- src/cpl/configuration/configuration.py | 51 ++++++++++++++-- src/cpl/configuration/configuration_abc.py | 60 +++++++++++++++---- .../configuration/configuration_model_abc.py | 14 ++++- src/cpl/configuration/console_argument.py | 9 +++ 4 files changed, 118 insertions(+), 16 deletions(-) diff --git a/src/cpl/configuration/configuration.py b/src/cpl/configuration/configuration.py index 942e3584..3d31e6f0 100644 --- a/src/cpl/configuration/configuration.py +++ b/src/cpl/configuration/configuration.py @@ -18,6 +18,9 @@ from cpl.environment.environment_name_enum import EnvironmentNameEnum class Configuration(ConfigurationABC): def __init__(self): + """ + Representation of configuration + """ ConfigurationABC.__init__(self) self._hosting_environment = ApplicationEnvironment() @@ -28,7 +31,6 @@ class Configuration(ConfigurationABC): self._argument_error_function: Optional[Callable] = None - self._is_multiple_args_allowed = False self._handled_args = [] @property @@ -47,28 +49,49 @@ class Configuration(ConfigurationABC): def argument_error_function(self, argument_error_function: Callable): self._argument_error_function = argument_error_function - def allow_multiple_args(self): - self._is_multiple_args_allowed = True - @staticmethod def _print_info(name: str, message: str): + """ + Prints an info message + :param name: + :param message: + :return: + """ Console.set_foreground_color(ForegroundColorEnum.green) Console.write_line(f'[{name}] {message}') Console.set_foreground_color(ForegroundColorEnum.default) @staticmethod def _print_warn(name: str, message: str): + """ + Prints a warning + :param name: + :param message: + :return: + """ Console.set_foreground_color(ForegroundColorEnum.yellow) Console.write_line(f'[{name}] {message}') Console.set_foreground_color(ForegroundColorEnum.default) @staticmethod def _print_error(name: str, message: str): + """ + Prints an error + :param name: + :param message: + :return: + """ Console.set_foreground_color(ForegroundColorEnum.red) Console.write_line(f'[{name}] {message}') Console.set_foreground_color(ForegroundColorEnum.default) def _set_variable(self, name: str, value: str): + """ + Sets variable to given value + :param name: + :param value: + :return: + """ if name == ConfigurationVariableNameEnum.environment.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, 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: found = False 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, next_arguments: list[str] = None) -> bool: + """ + Validate argument by argument type + :param argument: + :param argument_type: + :param next_arguments: + :return: + """ argument_name = '' value = '' result = False @@ -274,6 +311,12 @@ class Configuration(ConfigurationABC): self.add_configuration(sub, configuration) def _load_json_file(self, file: str, output: bool) -> dict: + """ + Reads the json file + :param file: + :param output: + :return: + """ try: # open config file, create if not exists with open(file, encoding='utf-8') as cfg: diff --git a/src/cpl/configuration/configuration_abc.py b/src/cpl/configuration/configuration_abc.py index 60f43e8a..ffe032e5 100644 --- a/src/cpl/configuration/configuration_abc.py +++ b/src/cpl/configuration/configuration_abc.py @@ -10,7 +10,11 @@ from cpl.environment.environment_abc import ApplicationEnvironmentABC class ConfigurationABC(ABC): @abstractmethod - def __init__(self): pass + def __init__(self): + """ + ABC of configuration + """ + pass @property @abstractmethod @@ -29,22 +33,58 @@ class ConfigurationABC(ABC): def argument_error_function(self, argument_error_function: Callable): pass @abstractmethod - def allow_multiple_args(self): pass + def add_environment_variables(self, prefix: str): + """ + Reads the environment variables + :param prefix: + :return: + """ + pass @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 - def add_console_argument(self, argument: ConsoleArgument): pass + def add_console_arguments(self): + """ + Reads the console arguments + :return: + """ + pass @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 - 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 - def add_configuration(self, key_type: type, value: object): pass - - @abstractmethod - def get_configuration(self, search_type: Union[str, Type[ConfigurationModelABC]]) -> Union[str, Callable[ConfigurationModelABC]]: pass + def get_configuration(self, search_type: Union[str, Type[ConfigurationModelABC]]) -> Union[ + str, Callable[ConfigurationModelABC]]: + """ + Returns value in configuration by given type + :param search_type: + :return: + """ + pass diff --git a/src/cpl/configuration/configuration_model_abc.py b/src/cpl/configuration/configuration_model_abc.py index b84cbeef..bcc6ab37 100644 --- a/src/cpl/configuration/configuration_model_abc.py +++ b/src/cpl/configuration/configuration_model_abc.py @@ -4,7 +4,17 @@ from abc import ABC, abstractmethod class ConfigurationModelABC(ABC): @abstractmethod - def __init__(self): pass + def __init__(self): + """ + ABC for settings representation + """ + pass @abstractmethod - def from_dict(self, settings: dict): pass + def from_dict(self, settings: dict): + """ + Converts attributes to dict + :param settings: + :return: + """ + pass diff --git a/src/cpl/configuration/console_argument.py b/src/cpl/configuration/console_argument.py index 6232d9dd..bc8a4e13 100644 --- a/src/cpl/configuration/console_argument.py +++ b/src/cpl/configuration/console_argument.py @@ -8,6 +8,15 @@ class ConsoleArgument: is_value_token_optional: bool = 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._name = name self._aliases = aliases