Added docs for cpl.configuration
This commit is contained in:
@@ -19,7 +19,7 @@ class ApplicationBuilderABC(ABC):
|
||||
Parameter
|
||||
---------
|
||||
startup: Type[:class:`cpl.application.startup_abc.StartupABC`]
|
||||
Type of :class:`cpl.application.startup_abc.StartupABC`
|
||||
Startup class to use
|
||||
"""
|
||||
pass
|
||||
|
||||
|
@@ -18,9 +18,7 @@ from cpl.environment.environment_name_enum import EnvironmentNameEnum
|
||||
class Configuration(ConfigurationABC):
|
||||
|
||||
def __init__(self):
|
||||
"""
|
||||
Representation of configuration
|
||||
"""
|
||||
r"""Representation of configuration"""
|
||||
ConfigurationABC.__init__(self)
|
||||
|
||||
self._application_environment = ApplicationEnvironment()
|
||||
@@ -51,11 +49,12 @@ class Configuration(ConfigurationABC):
|
||||
|
||||
@staticmethod
|
||||
def _print_info(name: str, message: str):
|
||||
"""
|
||||
Prints an info message
|
||||
:param name:
|
||||
:param message:
|
||||
:return:
|
||||
r"""Prints an info message
|
||||
|
||||
Parameter
|
||||
---------
|
||||
name: :class:`str`
|
||||
message: :class:`str`
|
||||
"""
|
||||
Console.set_foreground_color(ForegroundColorEnum.green)
|
||||
Console.write_line(f'[{name}] {message}')
|
||||
@@ -63,11 +62,12 @@ class Configuration(ConfigurationABC):
|
||||
|
||||
@staticmethod
|
||||
def _print_warn(name: str, message: str):
|
||||
"""
|
||||
Prints a warning
|
||||
:param name:
|
||||
:param message:
|
||||
:return:
|
||||
r"""Prints a warning
|
||||
|
||||
Parameter
|
||||
---------
|
||||
name: :class:`str`
|
||||
message: :class:`str`
|
||||
"""
|
||||
Console.set_foreground_color(ForegroundColorEnum.yellow)
|
||||
Console.write_line(f'[{name}] {message}')
|
||||
@@ -75,22 +75,24 @@ class Configuration(ConfigurationABC):
|
||||
|
||||
@staticmethod
|
||||
def _print_error(name: str, message: str):
|
||||
"""
|
||||
Prints an error
|
||||
:param name:
|
||||
:param message:
|
||||
:return:
|
||||
r"""Prints an error
|
||||
|
||||
Parameter
|
||||
---------
|
||||
name: :class:`str`
|
||||
message: :class:`str`
|
||||
"""
|
||||
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: any):
|
||||
"""
|
||||
Sets variable to given value
|
||||
:param name:
|
||||
:param value:
|
||||
:return:
|
||||
r"""Sets variable to given value
|
||||
|
||||
Parameter
|
||||
---------
|
||||
name: :class:`str`
|
||||
value: :class:`any`
|
||||
"""
|
||||
if name == ConfigurationVariableNameEnum.environment.value:
|
||||
self._application_environment.environment_name = EnvironmentNameEnum(value)
|
||||
@@ -106,12 +108,21 @@ 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:
|
||||
r"""Validate argument by argument type
|
||||
|
||||
Parameter
|
||||
---------
|
||||
argument: :class:`str`
|
||||
argument_type: :class:`cpl.configuration.console_argument.ConsoleArgument`
|
||||
next_arguments: list[:class:`str`]
|
||||
|
||||
Returns
|
||||
-------
|
||||
Object of :class:`bool`
|
||||
|
||||
Raises
|
||||
------
|
||||
Exception: An error occurred getting an argument for a command
|
||||
"""
|
||||
argument_name = ''
|
||||
value = ''
|
||||
@@ -240,6 +251,33 @@ class Configuration(ConfigurationABC):
|
||||
|
||||
return result
|
||||
|
||||
def _load_json_file(self, file: str, output: bool) -> dict:
|
||||
r"""Reads the json file
|
||||
|
||||
Parameter
|
||||
---------
|
||||
file: :class:`str`
|
||||
Name of the file
|
||||
output: :class:`bool`
|
||||
Specifies whether an output should take place
|
||||
|
||||
Returns
|
||||
-------
|
||||
Object of :class:`dict`
|
||||
"""
|
||||
try:
|
||||
# open config file, create if not exists
|
||||
with open(file, encoding='utf-8') as cfg:
|
||||
# load json
|
||||
json_cfg = json.load(cfg)
|
||||
if output:
|
||||
self._print_info(__name__, f'Loaded config file: {file}')
|
||||
|
||||
return json_cfg
|
||||
except Exception as e:
|
||||
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}'
|
||||
@@ -325,31 +363,11 @@ class Configuration(ConfigurationABC):
|
||||
configuration.from_dict(value)
|
||||
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:
|
||||
# load json
|
||||
json_cfg = json.load(cfg)
|
||||
if output:
|
||||
self._print_info(__name__, f'Loaded config file: {file}')
|
||||
|
||||
return json_cfg
|
||||
except Exception as e:
|
||||
self._print_error(__name__, f'Cannot load config file: {file}! -> {e}')
|
||||
return {}
|
||||
|
||||
def add_configuration(self, key_type: Union[str, type], value: ConfigurationModelABC):
|
||||
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 type(search_type) is str:
|
||||
if search_type == ConfigurationVariableNameEnum.environment.value:
|
||||
return self._application_environment.environment_name
|
||||
|
@@ -11,9 +11,7 @@ class ConfigurationABC(ABC):
|
||||
|
||||
@abstractmethod
|
||||
def __init__(self):
|
||||
"""
|
||||
ABC of configuration
|
||||
"""
|
||||
r"""ABC for the :class:`cpl.configuration.configuration.Configuration`"""
|
||||
pass
|
||||
|
||||
@property
|
||||
@@ -34,58 +32,73 @@ class ConfigurationABC(ABC):
|
||||
|
||||
@abstractmethod
|
||||
def add_environment_variables(self, prefix: str):
|
||||
"""
|
||||
Reads the environment variables
|
||||
:param prefix:
|
||||
:return:
|
||||
r"""Reads the environment variables
|
||||
|
||||
Parameter
|
||||
---------
|
||||
prefix: :class:`str`
|
||||
"""
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def add_console_argument(self, argument: ConsoleArgument):
|
||||
"""
|
||||
Adds console argument to known console arguments
|
||||
:param argument:
|
||||
:return:
|
||||
r"""Adds console argument to known console arguments
|
||||
|
||||
Parameter
|
||||
---------
|
||||
argument: :class:`cpl.configuration.console_argument.ConsoleArgument`
|
||||
"""
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def add_console_arguments(self, error: bool = None):
|
||||
"""
|
||||
Reads the console arguments
|
||||
:param error: defines is invalid argument error will be shown or not
|
||||
:return:
|
||||
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):
|
||||
"""
|
||||
Reads and saves settings from given json file
|
||||
:param name:
|
||||
:param optional:
|
||||
:param output:
|
||||
:param path:
|
||||
:return:
|
||||
r"""Reads and saves settings from given json file
|
||||
|
||||
Parameter
|
||||
---------
|
||||
name: :class:`str`
|
||||
Name of the file
|
||||
optional: :class:`str`
|
||||
Specifies whether an error should occur if the file was not found
|
||||
output: :class:`bool`
|
||||
Specifies whether an output should take place
|
||||
path: :class:`str`
|
||||
Path in which the file should be stored
|
||||
"""
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def add_configuration(self, key_type: Union[str, type], value: object):
|
||||
"""
|
||||
Add configuration object
|
||||
:param key_type:
|
||||
:param value:
|
||||
:return:
|
||||
def add_configuration(self, key_type: Union[str, type], value: ConfigurationModelABC):
|
||||
r"""Add configuration object
|
||||
|
||||
Parameter
|
||||
---------
|
||||
key_type: Union[:class:`str`, :class:`type`]
|
||||
value: :class:`cpl.configuration.configuration_model_abc.ConfigurationModelABC`
|
||||
"""
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
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:
|
||||
r"""Returns value from configuration by given type
|
||||
|
||||
Parameter
|
||||
---------
|
||||
search_type: Union[:class:`str`, Type[:class:`cpl.configuration.configuration_model_abc.ConfigurationModelABC`]]
|
||||
|
||||
Returns
|
||||
-------
|
||||
Object of Union[:class:`str`, Callable[:class:`cpl.configuration.configuration_model_abc.ConfigurationModelABC`]]
|
||||
"""
|
||||
pass
|
||||
|
@@ -5,16 +5,15 @@ class ConfigurationModelABC(ABC):
|
||||
|
||||
@abstractmethod
|
||||
def __init__(self):
|
||||
"""
|
||||
ABC for settings representation
|
||||
"""
|
||||
r"""ABC for settings representation"""
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def from_dict(self, settings: dict):
|
||||
"""
|
||||
Converts attributes to dict
|
||||
:param settings:
|
||||
:return:
|
||||
r"""Converts attributes to dict
|
||||
|
||||
Parameter
|
||||
---------
|
||||
settings: :class:`dict`
|
||||
"""
|
||||
pass
|
||||
|
@@ -8,14 +8,16 @@ 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:
|
||||
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`
|
||||
console_arguments: List[:class:`cpl.configuration.console_argument.ConsoleArgument`]
|
||||
"""
|
||||
self._token = token
|
||||
self._name = name
|
||||
|
Reference in New Issue
Block a user