Improved configuration
This commit is contained in:
parent
ed0b761502
commit
b32f846fcf
@ -23,8 +23,18 @@
|
||||
</component>
|
||||
<component name="ChangeListManager">
|
||||
<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$/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>
|
||||
<option name="SHOW_DIALOG" value="false" />
|
||||
<option name="HIGHLIGHT_CONFLICTS" value="true" />
|
||||
@ -74,7 +84,7 @@
|
||||
<component name="PropertiesComponent">
|
||||
<property name="RunOnceActivity.OpenProjectViewOnStart" 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" />
|
||||
</component>
|
||||
<component name="ReaderModeSettings">
|
||||
@ -82,11 +92,11 @@
|
||||
</component>
|
||||
<component name="RecentsManager">
|
||||
<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/application" />
|
||||
<recent name="$PROJECT_DIR$/src/cpl/common/application" />
|
||||
<recent name="$PROJECT_DIR$/src/cpl" />
|
||||
<recent name="$PROJECT_DIR$/src/tests" />
|
||||
</key>
|
||||
<key name="MoveFile.RECENT_KEYS">
|
||||
<recent name="$PROJECT_DIR$/src/cpl" />
|
||||
@ -96,7 +106,7 @@
|
||||
<recent name="$PROJECT_DIR$/src/cpl/common/application" />
|
||||
</key>
|
||||
</component>
|
||||
<component name="RunManager" selected="Python.main">
|
||||
<component name="RunManager" selected="Python.main cli">
|
||||
<configuration name="cli build" type="PythonConfigurationType" factoryName="Python">
|
||||
<module name="sh_common_py_lib" />
|
||||
<option name="INTERPRETER_OPTIONS" value="" />
|
||||
@ -370,6 +380,27 @@
|
||||
<option name="INPUT_FILE" value="" />
|
||||
<method v="2" />
|
||||
</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">
|
||||
<module name="sh_common_py_lib" />
|
||||
<option name="INTERPRETER_OPTIONS" value="" />
|
||||
@ -444,10 +475,12 @@
|
||||
<item itemvalue="Python.cli version" />
|
||||
<item itemvalue="Python.cli" />
|
||||
<item itemvalue="Python.main" />
|
||||
<item itemvalue="Python.main cli" />
|
||||
<item itemvalue="Python tests.Unittests" />
|
||||
</list>
|
||||
<recent_temporary>
|
||||
<list>
|
||||
<item itemvalue="Python.main cli" />
|
||||
<item itemvalue="Python tests.Unittests" />
|
||||
<item itemvalue="Python tests.Unittests" />
|
||||
</list>
|
||||
|
@ -1,29 +1,29 @@
|
||||
aiohttp==3.6.3
|
||||
async-timeout==3.0.1
|
||||
attrs==20.3.0
|
||||
certifi==2020.11.8
|
||||
chardet==3.0.4
|
||||
click==7.1.2
|
||||
dateutils==0.6.12
|
||||
discord==1.0.1
|
||||
discord.py==1.5.1
|
||||
Flask==1.1.2
|
||||
idna==2.10
|
||||
itsdangerous==1.1.0
|
||||
Jinja2==2.11.2
|
||||
keyboard==0.13.5
|
||||
MarkupSafe==1.1.1
|
||||
multidict==4.7.6
|
||||
mysql-connector==2.2.9
|
||||
overloading==0.5.0
|
||||
python-dateutil==2.8.1
|
||||
pytz==2020.4
|
||||
six==1.15.0
|
||||
SQLAlchemy==1.3.20
|
||||
termcolor==1.1.0
|
||||
urllib3==1.26.2
|
||||
Werkzeug==1.0.1
|
||||
yarl==1.5.1
|
||||
setuptools~=49.2.1
|
||||
pyfiglet~=0.8.post1
|
||||
tabulate~=0.8.7
|
||||
aiohttp
|
||||
async-timeout
|
||||
attrs
|
||||
certifi
|
||||
chardet
|
||||
click
|
||||
dateutils
|
||||
discord
|
||||
discord.py
|
||||
Flask
|
||||
idna
|
||||
itsdangerous
|
||||
Jinja2
|
||||
keyboard
|
||||
MarkupSafe
|
||||
multidict
|
||||
mysql-connector
|
||||
overloading
|
||||
python-dateutil
|
||||
pytz
|
||||
six
|
||||
SQLAlchemy
|
||||
termcolor
|
||||
urllib3
|
||||
Werkzeug
|
||||
yarl
|
||||
setuptools
|
||||
pyfiglet
|
||||
tabulate
|
@ -20,13 +20,17 @@ class ApplicationABC(ABC):
|
||||
self._startup = startup()
|
||||
|
||||
def build(self):
|
||||
if self._startup is None:
|
||||
print('Startup is empty')
|
||||
exit()
|
||||
if self._startup is not None:
|
||||
self._app_host = self._startup.create_application_host()
|
||||
self._configuration = self._startup.create_configuration()
|
||||
self._services = self._startup.create_services()
|
||||
|
||||
self._app_host = self._startup.create_application_host()
|
||||
self._configuration = self._startup.create_configuration()
|
||||
self._services = self._startup.create_services()
|
||||
def run(self):
|
||||
self.configure()
|
||||
self.main()
|
||||
|
||||
@abstractmethod
|
||||
def run(self): pass
|
||||
def configure(self): pass
|
||||
|
||||
@abstractmethod
|
||||
def main(self): pass
|
||||
|
@ -1,10 +1,13 @@
|
||||
import json
|
||||
import os
|
||||
import sys
|
||||
from collections import Callable
|
||||
from typing import Union, Type
|
||||
|
||||
from cpl.configuration.configuration_abc import ConfigurationABC
|
||||
from cpl.configuration.configuration_model_abc import ConfigurationModelABC
|
||||
from cpl.configuration.configuration_variable_name import ConfigurationVariableName
|
||||
from cpl.configuration.console_argument import ConsoleArgument
|
||||
from cpl.console.console import Console
|
||||
from cpl.console.foreground_color import ForegroundColor
|
||||
from cpl.environment.hosting_environment import HostingEnvironment
|
||||
@ -18,12 +21,19 @@ class Configuration(ConfigurationABC):
|
||||
ConfigurationABC.__init__(self)
|
||||
|
||||
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
|
||||
def environment(self) -> EnvironmentABC:
|
||||
return self._hosting_environment
|
||||
|
||||
@property
|
||||
def additional_arguments(self) -> list[str]:
|
||||
return self._additional_arguments
|
||||
|
||||
@staticmethod
|
||||
def _print_info(name: str, message: str):
|
||||
Console.set_foreground_color(ForegroundColor.green)
|
||||
@ -52,22 +62,52 @@ class Configuration(ConfigurationABC):
|
||||
elif name == ConfigurationVariableName.customer.value:
|
||||
self._hosting_environment.customer = value
|
||||
|
||||
else:
|
||||
self._config[name] = value
|
||||
|
||||
def add_environment_variables(self, prefix: str):
|
||||
for variable in ConfigurationVariableName.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_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:]:
|
||||
try:
|
||||
argument = arg.split('--')[1].split('=')[0].upper()
|
||||
value = arg.split('=')[1]
|
||||
is_done = False
|
||||
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():
|
||||
raise Exception(f'Invalid argument name: {argument}')
|
||||
if argument_type.value_token == '':
|
||||
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:
|
||||
self._print_error(__name__, f'Invalid argument: {arg} -> {e}')
|
||||
exit()
|
||||
@ -109,7 +149,7 @@ class Configuration(ConfigurationABC):
|
||||
def add_configuration(self, key_type: type, value: ConfigurationModelABC):
|
||||
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:
|
||||
raise Exception(f'Config model by type {search_type} not found')
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
from abc import abstractmethod, ABC
|
||||
from collections import Callable
|
||||
from typing import Type
|
||||
from typing import Type, Union
|
||||
|
||||
from cpl.configuration.configuration_model_abc import ConfigurationModelABC
|
||||
from cpl.environment.environment_abc import EnvironmentABC
|
||||
@ -15,11 +15,18 @@ class ConfigurationABC(ABC):
|
||||
@abstractmethod
|
||||
def environment(self) -> EnvironmentABC: pass
|
||||
|
||||
@property
|
||||
@abstractmethod
|
||||
def additional_arguments(self) -> list[str]: pass
|
||||
|
||||
@abstractmethod
|
||||
def add_environment_variables(self, prefix: str): pass
|
||||
|
||||
@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
|
||||
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
|
||||
|
||||
@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
|
||||
|
23
src/cpl/configuration/console_argument.py
Normal file
23
src/cpl/configuration/console_argument.py
Normal 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
|
@ -33,13 +33,14 @@ class Application(ApplicationABC):
|
||||
Console.write_at(5, 5, 'at 5, 5')
|
||||
Console.write_at(10, 10, 'at 10, 10')
|
||||
|
||||
def run(self):
|
||||
def configure(self):
|
||||
self._logger = self._services.get_service(LoggerABC)
|
||||
self._mailer = self._services.get_service(EMailClientABC)
|
||||
|
||||
def main(self):
|
||||
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'Environment: {self._configuration.environment.environment_name}')
|
||||
self._logger.debug(__name__, f'Customer: {self._configuration.environment.customer}')
|
||||
self.test_send_mail()
|
||||
# self.test_send_mail()
|
||||
# self.test_console()
|
||||
|
@ -1,5 +1,5 @@
|
||||
from tests.Application import Application
|
||||
from tests.Startup import Startup
|
||||
from tests.startup import Startup
|
||||
|
||||
if __name__ == '__main__':
|
||||
app = Application()
|
||||
|
@ -32,7 +32,7 @@ class Startup(StartupABC):
|
||||
def create_configuration(self) -> ConfigurationABC:
|
||||
self._configuration.add_environment_variables('PYTHON_')
|
||||
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.{self._configuration.environment.environment_name}.json')
|
||||
self._configuration.add_json_file(f'appsettings.{self._configuration.environment.host_name}.json', optional=True)
|
Loading…
Reference in New Issue
Block a user