2020-11-26 18:58:08 +01:00
|
|
|
from typing import Optional
|
|
|
|
|
2020-11-26 19:17:05 +01:00
|
|
|
from sh_edraft.configuration.base import ConfigurationBase
|
2020-12-15 16:53:15 +01:00
|
|
|
from sh_edraft.console import Console
|
2020-12-11 21:48:46 +01:00
|
|
|
from sh_edraft.database.context import DatabaseContext
|
2020-12-10 18:11:05 +01:00
|
|
|
from sh_edraft.database.model import DatabaseSettings
|
2020-11-26 17:57:51 +01:00
|
|
|
from sh_edraft.hosting import ApplicationHost
|
2020-11-26 18:58:08 +01:00
|
|
|
from sh_edraft.hosting.base import ApplicationBase
|
2020-11-26 19:17:05 +01:00
|
|
|
from sh_edraft.logging import Logger
|
|
|
|
from sh_edraft.logging.base import LoggerBase
|
2020-12-10 18:11:05 +01:00
|
|
|
from sh_edraft.service.providing.base import ServiceProviderBase
|
2020-12-15 16:53:15 +01:00
|
|
|
from sh_edraft.utils import CredentialManager
|
2020-12-11 21:48:46 +01:00
|
|
|
|
|
|
|
from tests_dev.db.user_repo import UserRepo
|
|
|
|
from tests_dev.db.user_repo_base import UserRepoBase
|
2020-11-26 17:57:51 +01:00
|
|
|
|
|
|
|
|
|
|
|
class Program(ApplicationBase):
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
ApplicationBase.__init__(self)
|
|
|
|
|
2020-11-26 18:58:08 +01:00
|
|
|
self._app_host: Optional[ApplicationHost] = None
|
2020-11-26 19:17:05 +01:00
|
|
|
self._services: Optional[ServiceProviderBase] = None
|
|
|
|
self._configuration: Optional[ConfigurationBase] = None
|
2020-11-29 17:29:16 +01:00
|
|
|
self._logger: Optional[LoggerBase] = None
|
2020-11-26 19:17:05 +01:00
|
|
|
|
2020-11-26 18:58:08 +01:00
|
|
|
def create_application_host(self):
|
2020-11-28 15:13:54 +01:00
|
|
|
self._app_host = ApplicationHost()
|
2020-11-26 19:17:05 +01:00
|
|
|
self._configuration = self._app_host.configuration
|
2020-11-29 17:29:16 +01:00
|
|
|
self._services = self._app_host.services
|
2020-11-26 19:17:05 +01:00
|
|
|
|
2020-11-26 17:57:51 +01:00
|
|
|
def create_configuration(self):
|
2020-11-28 15:13:54 +01:00
|
|
|
self._configuration.add_environment_variables('PYTHON_')
|
|
|
|
self._configuration.add_environment_variables('CPL_')
|
|
|
|
self._configuration.add_argument_variables()
|
|
|
|
self._configuration.add_json_file(f'appsettings.json')
|
|
|
|
self._configuration.add_json_file(f'appsettings.{self._configuration.environment.environment_name}.json')
|
2020-12-06 21:14:57 +01:00
|
|
|
self._configuration.add_json_file(f'appsettings.{self._configuration.environment.host_name}.json', optional=True)
|
2020-11-26 17:57:51 +01:00
|
|
|
|
|
|
|
def create_services(self):
|
2020-12-10 18:11:05 +01:00
|
|
|
# Create and connect to database
|
|
|
|
db_settings: DatabaseSettings = self._configuration.get_configuration(DatabaseSettings)
|
2020-12-11 21:48:46 +01:00
|
|
|
self._services.add_db_context(DatabaseContext)
|
|
|
|
db: DatabaseContext = self._services.get_db_context()
|
2020-12-11 19:42:09 +01:00
|
|
|
db.connect(CredentialManager.build_string(db_settings.connection_string, db_settings.credentials))
|
2020-12-10 18:11:05 +01:00
|
|
|
|
2020-12-11 21:48:46 +01:00
|
|
|
self._services.add_scoped(UserRepoBase, UserRepo)
|
|
|
|
|
2020-12-10 18:11:05 +01:00
|
|
|
# Add and create logger
|
2020-11-26 19:17:05 +01:00
|
|
|
self._services.add_singleton(LoggerBase, Logger)
|
2020-12-06 21:14:57 +01:00
|
|
|
self._logger = self._services.get_service(LoggerBase)
|
2020-11-26 17:57:51 +01:00
|
|
|
|
|
|
|
def main(self):
|
2020-11-29 17:29:16 +01:00
|
|
|
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}')
|
2020-12-11 21:48:46 +01:00
|
|
|
self._services.get_service(UserRepoBase).add_test_user()
|
2020-12-15 14:03:31 +01:00
|
|
|
|
|
|
|
Console.clear()
|
|
|
|
Console.write_line('Hello', 'World')
|
|
|
|
# name = Console.read_line('Name: ')
|
|
|
|
# Console.write_line('Hello', name)
|
|
|
|
Console.set_foreground_color('red')
|
|
|
|
Console.set_background_color('green')
|
|
|
|
Console.set_cursor_position(5, 5)
|
|
|
|
Console.write_line('Error')
|
2020-12-15 16:53:15 +01:00
|
|
|
Console.write_line_at(10, 5, 'Error')
|
|
|
|
Console.write_at(15, 5, 'Error')
|
2020-12-15 14:03:31 +01:00
|
|
|
Console.reset_cursor_position()
|
|
|
|
Console.set_foreground_color('green')
|
|
|
|
Console.set_background_color('default')
|
2020-12-15 16:53:15 +01:00
|
|
|
Console.write_line('Test')
|
|
|
|
Console.write('1')
|
|
|
|
# Console.write('x: ')
|
|
|
|
# Console.read_line('Test> ')
|
2020-12-15 14:03:31 +01:00
|
|
|
Console.write_line(Console.foreground_color)
|