Improved configuration

This commit is contained in:
Sven Heidemann 2021-03-03 18:37:39 +01:00
parent ed0b761502
commit b32f846fcf
9 changed files with 164 additions and 56 deletions

View File

@ -23,8 +23,18 @@
</component> </component>
<component name="ChangeListManager"> <component name="ChangeListManager">
<list default="true" id="7e2256bc-a6b8-4880-83a6-8b0e3372d0a4" name="Default Changelist" comment="Refactored code"> <list default="true" id="7e2256bc-a6b8-4880-83a6-8b0e3372d0a4" name="Default Changelist" comment="Refactored code">
<change beforePath="$PROJECT_DIR$/.gitignore" beforeDir="false" afterPath="$PROJECT_DIR$/.gitignore" afterDir="false" /> <change afterPath="$PROJECT_DIR$/src/cpl/configuration/console_argument.py" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/cpl_cli/cli.py" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/cpl_cli/main.py" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/cpl_cli/startup.py" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" /> <change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/requirements.txt" beforeDir="false" afterPath="$PROJECT_DIR$/requirements.txt" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/cpl/application/application_abc.py" beforeDir="false" afterPath="$PROJECT_DIR$/src/cpl/application/application_abc.py" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/cpl/configuration/configuration.py" beforeDir="false" afterPath="$PROJECT_DIR$/src/cpl/configuration/configuration.py" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/cpl/configuration/configuration_abc.py" beforeDir="false" afterPath="$PROJECT_DIR$/src/cpl/configuration/configuration_abc.py" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/tests/Application.py" beforeDir="false" afterPath="$PROJECT_DIR$/src/tests/Application.py" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/tests/Startup.py" beforeDir="false" afterPath="$PROJECT_DIR$/src/tests/startup.py" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/tests/main.py" beforeDir="false" afterPath="$PROJECT_DIR$/src/tests/main.py" afterDir="false" />
</list> </list>
<option name="SHOW_DIALOG" value="false" /> <option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" /> <option name="HIGHLIGHT_CONFLICTS" value="true" />
@ -74,7 +84,7 @@
<component name="PropertiesComponent"> <component name="PropertiesComponent">
<property name="RunOnceActivity.OpenProjectViewOnStart" value="true" /> <property name="RunOnceActivity.OpenProjectViewOnStart" value="true" />
<property name="RunOnceActivity.ShowReadmeOnStart" value="true" /> <property name="RunOnceActivity.ShowReadmeOnStart" value="true" />
<property name="last_opened_file_path" value="$PROJECT_DIR$/src/cpl/dependency_injection" /> <property name="last_opened_file_path" value="$PROJECT_DIR$/src/cpl_cli" />
<property name="settings.editor.selected.configurable" value="com.jetbrains.python.configuration.PyActiveSdkModuleConfigurable" /> <property name="settings.editor.selected.configurable" value="com.jetbrains.python.configuration.PyActiveSdkModuleConfigurable" />
</component> </component>
<component name="ReaderModeSettings"> <component name="ReaderModeSettings">
@ -82,11 +92,11 @@
</component> </component>
<component name="RecentsManager"> <component name="RecentsManager">
<key name="CopyFile.RECENT_KEYS"> <key name="CopyFile.RECENT_KEYS">
<recent name="$PROJECT_DIR$/src/cpl_cli" />
<recent name="$PROJECT_DIR$/src_old" />
<recent name="$PROJECT_DIR$/src/cpl/dependency_injection" /> <recent name="$PROJECT_DIR$/src/cpl/dependency_injection" />
<recent name="$PROJECT_DIR$/src/cpl/application" /> <recent name="$PROJECT_DIR$/src/cpl/application" />
<recent name="$PROJECT_DIR$/src/cpl/common/application" /> <recent name="$PROJECT_DIR$/src/cpl/common/application" />
<recent name="$PROJECT_DIR$/src/cpl" />
<recent name="$PROJECT_DIR$/src/tests" />
</key> </key>
<key name="MoveFile.RECENT_KEYS"> <key name="MoveFile.RECENT_KEYS">
<recent name="$PROJECT_DIR$/src/cpl" /> <recent name="$PROJECT_DIR$/src/cpl" />
@ -96,7 +106,7 @@
<recent name="$PROJECT_DIR$/src/cpl/common/application" /> <recent name="$PROJECT_DIR$/src/cpl/common/application" />
</key> </key>
</component> </component>
<component name="RunManager" selected="Python.main"> <component name="RunManager" selected="Python.main cli">
<configuration name="cli build" type="PythonConfigurationType" factoryName="Python"> <configuration name="cli build" type="PythonConfigurationType" factoryName="Python">
<module name="sh_common_py_lib" /> <module name="sh_common_py_lib" />
<option name="INTERPRETER_OPTIONS" value="" /> <option name="INTERPRETER_OPTIONS" value="" />
@ -370,6 +380,27 @@
<option name="INPUT_FILE" value="" /> <option name="INPUT_FILE" value="" />
<method v="2" /> <method v="2" />
</configuration> </configuration>
<configuration name="main cli" type="PythonConfigurationType" factoryName="Python" temporary="true">
<module name="sh_common_py_lib" />
<option name="INTERPRETER_OPTIONS" value="" />
<option name="PARENT_ENVS" value="true" />
<envs>
<env name="PYTHONUNBUFFERED" value="1" />
</envs>
<option name="SDK_HOME" value="" />
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$/src/cpl_cli" />
<option name="IS_MODULE_SDK" value="true" />
<option name="ADD_CONTENT_ROOTS" value="true" />
<option name="ADD_SOURCE_ROOTS" value="true" />
<option name="SCRIPT_NAME" value="$PROJECT_DIR$/src/cpl_cli/main.py" />
<option name="PARAMETERS" value="v -test -v:test" />
<option name="SHOW_COMMAND_LINE" value="false" />
<option name="EMULATE_TERMINAL" value="false" />
<option name="MODULE_MODE" value="false" />
<option name="REDIRECT_INPUT" value="false" />
<option name="INPUT_FILE" value="" />
<method v="2" />
</configuration>
<configuration name="main" type="PythonConfigurationType" factoryName="Python" nameIsGenerated="true"> <configuration name="main" type="PythonConfigurationType" factoryName="Python" nameIsGenerated="true">
<module name="sh_common_py_lib" /> <module name="sh_common_py_lib" />
<option name="INTERPRETER_OPTIONS" value="" /> <option name="INTERPRETER_OPTIONS" value="" />
@ -444,10 +475,12 @@
<item itemvalue="Python.cli version" /> <item itemvalue="Python.cli version" />
<item itemvalue="Python.cli" /> <item itemvalue="Python.cli" />
<item itemvalue="Python.main" /> <item itemvalue="Python.main" />
<item itemvalue="Python.main cli" />
<item itemvalue="Python tests.Unittests" /> <item itemvalue="Python tests.Unittests" />
</list> </list>
<recent_temporary> <recent_temporary>
<list> <list>
<item itemvalue="Python.main cli" />
<item itemvalue="Python tests.Unittests" /> <item itemvalue="Python tests.Unittests" />
<item itemvalue="Python tests.Unittests" /> <item itemvalue="Python tests.Unittests" />
</list> </list>

View File

@ -1,29 +1,29 @@
aiohttp==3.6.3 aiohttp
async-timeout==3.0.1 async-timeout
attrs==20.3.0 attrs
certifi==2020.11.8 certifi
chardet==3.0.4 chardet
click==7.1.2 click
dateutils==0.6.12 dateutils
discord==1.0.1 discord
discord.py==1.5.1 discord.py
Flask==1.1.2 Flask
idna==2.10 idna
itsdangerous==1.1.0 itsdangerous
Jinja2==2.11.2 Jinja2
keyboard==0.13.5 keyboard
MarkupSafe==1.1.1 MarkupSafe
multidict==4.7.6 multidict
mysql-connector==2.2.9 mysql-connector
overloading==0.5.0 overloading
python-dateutil==2.8.1 python-dateutil
pytz==2020.4 pytz
six==1.15.0 six
SQLAlchemy==1.3.20 SQLAlchemy
termcolor==1.1.0 termcolor
urllib3==1.26.2 urllib3
Werkzeug==1.0.1 Werkzeug
yarl==1.5.1 yarl
setuptools~=49.2.1 setuptools
pyfiglet~=0.8.post1 pyfiglet
tabulate~=0.8.7 tabulate

View File

@ -20,13 +20,17 @@ class ApplicationABC(ABC):
self._startup = startup() self._startup = startup()
def build(self): def build(self):
if self._startup is None: if self._startup is not None:
print('Startup is empty') self._app_host = self._startup.create_application_host()
exit() self._configuration = self._startup.create_configuration()
self._services = self._startup.create_services()
self._app_host = self._startup.create_application_host() def run(self):
self._configuration = self._startup.create_configuration() self.configure()
self._services = self._startup.create_services() self.main()
@abstractmethod @abstractmethod
def run(self): pass def configure(self): pass
@abstractmethod
def main(self): pass

View File

@ -1,10 +1,13 @@
import json import json
import os import os
import sys import sys
from collections import Callable
from typing import Union, Type
from cpl.configuration.configuration_abc import ConfigurationABC 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.configuration.configuration_variable_name import ConfigurationVariableName from cpl.configuration.configuration_variable_name import ConfigurationVariableName
from cpl.configuration.console_argument import ConsoleArgument
from cpl.console.console import Console from cpl.console.console import Console
from cpl.console.foreground_color import ForegroundColor from cpl.console.foreground_color import ForegroundColor
from cpl.environment.hosting_environment import HostingEnvironment from cpl.environment.hosting_environment import HostingEnvironment
@ -18,12 +21,19 @@ class Configuration(ConfigurationABC):
ConfigurationABC.__init__(self) ConfigurationABC.__init__(self)
self._hosting_environment = HostingEnvironment() self._hosting_environment = HostingEnvironment()
self._config: dict[type, ConfigurationModelABC] = {} self._config: dict[Union[type, str], Union[ConfigurationModelABC, str]] = {}
self._argument_types: list[ConsoleArgument] = []
self._additional_arguments: list[str] = []
@property @property
def environment(self) -> EnvironmentABC: def environment(self) -> EnvironmentABC:
return self._hosting_environment return self._hosting_environment
@property
def additional_arguments(self) -> list[str]:
return self._additional_arguments
@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)
@ -52,22 +62,52 @@ class Configuration(ConfigurationABC):
elif name == ConfigurationVariableName.customer.value: elif name == ConfigurationVariableName.customer.value:
self._hosting_environment.customer = value self._hosting_environment.customer = value
else:
self._config[name] = value
def add_environment_variables(self, prefix: str): def add_environment_variables(self, prefix: str):
for variable in ConfigurationVariableName.to_list(): for variable in ConfigurationVariableName.to_list():
var_name = f'{prefix}{variable}' var_name = f'{prefix}{variable}'
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_argument_variables(self): def add_console_argument(self, token: str, name: str, aliases: list[str], value_token: str):
self._argument_types.append(ConsoleArgument(token, name, aliases, value_token))
def add_console_arguments(self):
for arg_name in ConfigurationVariableName.to_list():
self.add_console_argument('--', arg_name, [], '')
for arg in sys.argv[1:]: for arg in sys.argv[1:]:
try: try:
argument = arg.split('--')[1].split('=')[0].upper() is_done = False
value = arg.split('=')[1] for argument_type in self._argument_types:
# check prefix
if argument_type.token != '' and arg.startswith(argument_type.token):
name = arg.split(argument_type.token)[1]
if argument not in ConfigurationVariableName.to_list(): if argument_type.value_token == '':
raise Exception(f'Invalid argument name: {argument}') if name == argument_type.name or name in argument_type.aliases:
self._additional_arguments.append(argument_type.name)
is_done = True
break
self._set_variable(argument, value) if argument_type.value_token != '' and arg.__contains__(argument_type.value_token):
name = name.split(argument_type.value_token)[0]
if name == argument_type.name or name in argument_type.aliases:
value = arg.split(argument_type.value_token)[1]
self._set_variable(argument_type.name, value)
is_done = True
break
elif argument_type.value_token == '' and arg == argument_type.name or arg in argument_type.aliases:
is_done = True
self._additional_arguments.append(argument_type.name)
break
if not is_done:
self._print_error(__name__, f'Invalid argument: {arg}')
exit()
except Exception as e: except Exception as e:
self._print_error(__name__, f'Invalid argument: {arg} -> {e}') self._print_error(__name__, f'Invalid argument: {arg} -> {e}')
exit() exit()
@ -109,7 +149,7 @@ 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: type) -> 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')

View File

@ -1,6 +1,6 @@
from abc import abstractmethod, ABC from abc import abstractmethod, ABC
from collections import Callable from collections import Callable
from typing import Type from typing import Type, Union
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
@ -15,11 +15,18 @@ class ConfigurationABC(ABC):
@abstractmethod @abstractmethod
def environment(self) -> EnvironmentABC: pass def environment(self) -> EnvironmentABC: pass
@property
@abstractmethod
def additional_arguments(self) -> list[str]: pass
@abstractmethod @abstractmethod
def add_environment_variables(self, prefix: str): pass def add_environment_variables(self, prefix: str): pass
@abstractmethod @abstractmethod
def add_argument_variables(self): pass def add_console_argument(self, token: str, name: str, aliases: list[str], value_token: str): pass
@abstractmethod
def add_console_arguments(self): pass
@abstractmethod @abstractmethod
def add_json_file(self, name: str, optional: bool = None): pass def add_json_file(self, name: str, optional: bool = None): pass
@ -28,4 +35,4 @@ class ConfigurationABC(ABC):
def add_configuration(self, key_type: type, value: object): pass def add_configuration(self, key_type: type, value: object): pass
@abstractmethod @abstractmethod
def get_configuration(self, search_type: Type[ConfigurationModelABC]) -> Callable[ConfigurationModelABC]: pass def get_configuration(self, search_type: Union[str, Type[ConfigurationModelABC]]) -> Union[str, Callable[ConfigurationModelABC]]: pass

View File

@ -0,0 +1,23 @@
class ConsoleArgument:
def __init__(self, token: str, name: str, aliases: list[str], value_token: str):
self._token = token
self._name = name
self._aliases = aliases
self._value_token = value_token
@property
def token(self):
return self._token
@property
def name(self):
return self._name
@property
def aliases(self):
return self._aliases
@property
def value_token(self):
return self._value_token

View File

@ -33,13 +33,14 @@ class Application(ApplicationABC):
Console.write_at(5, 5, 'at 5, 5') Console.write_at(5, 5, 'at 5, 5')
Console.write_at(10, 10, 'at 10, 10') Console.write_at(10, 10, 'at 10, 10')
def run(self): def configure(self):
self._logger = self._services.get_service(LoggerABC) self._logger = self._services.get_service(LoggerABC)
self._mailer = self._services.get_service(EMailClientABC) self._mailer = self._services.get_service(EMailClientABC)
def main(self):
self._logger.header(f'{self._configuration.environment.application_name}:') self._logger.header(f'{self._configuration.environment.application_name}:')
self._logger.debug(__name__, f'Host: {self._configuration.environment.host_name}') self._logger.debug(__name__, f'Host: {self._configuration.environment.host_name}')
self._logger.debug(__name__, f'Environment: {self._configuration.environment.environment_name}') self._logger.debug(__name__, f'Environment: {self._configuration.environment.environment_name}')
self._logger.debug(__name__, f'Customer: {self._configuration.environment.customer}') self._logger.debug(__name__, f'Customer: {self._configuration.environment.customer}')
self.test_send_mail() # self.test_send_mail()
# self.test_console() # self.test_console()

View File

@ -1,5 +1,5 @@
from tests.Application import Application from tests.Application import Application
from tests.Startup import Startup from tests.startup import Startup
if __name__ == '__main__': if __name__ == '__main__':
app = Application() app = Application()

View File

@ -32,7 +32,7 @@ class Startup(StartupABC):
def create_configuration(self) -> ConfigurationABC: def create_configuration(self) -> ConfigurationABC:
self._configuration.add_environment_variables('PYTHON_') self._configuration.add_environment_variables('PYTHON_')
self._configuration.add_environment_variables('CPL_') self._configuration.add_environment_variables('CPL_')
self._configuration.add_argument_variables() self._configuration.add_console_arguments()
self._configuration.add_json_file(f'appsettings.json') self._configuration.add_json_file(f'appsettings.json')
self._configuration.add_json_file(f'appsettings.{self._configuration.environment.environment_name}.json') self._configuration.add_json_file(f'appsettings.{self._configuration.environment.environment_name}.json')
self._configuration.add_json_file(f'appsettings.{self._configuration.environment.host_name}.json', optional=True) self._configuration.add_json_file(f'appsettings.{self._configuration.environment.host_name}.json', optional=True)