From 096b50f9f1a79fb010d0b878fec7f883ba5809df Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Mon, 14 Dec 2020 21:10:26 +0100 Subject: [PATCH] Added console module --- src/sh_edraft/configuration/configuration.py | 15 +++- .../connection/database_connection.py | 11 ++- .../database/context/database_context.py | 11 ++- .../database/model/database_settings.py | 9 ++- src/sh_edraft/logging/logger.py | 35 ++++++--- .../logging/model/logging_settings.py | 9 ++- .../model/publish_settings_model.py | 9 ++- .../time/model/time_format_settings.py | 7 +- src/sh_edraft/utils/console.py | 11 --- src/sh_edraft/utils/console/__init__.py | 3 + src/sh_edraft/utils/console/console.py | 76 +++++++++++++++++++ src/sh_edraft/utils/console/model/__init__.py | 4 + .../utils/console/model/background_color.py | 14 ++++ .../utils/console/model/foreground_color.py | 14 ++++ 14 files changed, 187 insertions(+), 41 deletions(-) delete mode 100644 src/sh_edraft/utils/console.py create mode 100644 src/sh_edraft/utils/console/__init__.py create mode 100644 src/sh_edraft/utils/console/console.py create mode 100644 src/sh_edraft/utils/console/model/__init__.py create mode 100644 src/sh_edraft/utils/console/model/background_color.py create mode 100644 src/sh_edraft/utils/console/model/foreground_color.py diff --git a/src/sh_edraft/configuration/configuration.py b/src/sh_edraft/configuration/configuration.py index f32148f0..6bc844d4 100644 --- a/src/sh_edraft/configuration/configuration.py +++ b/src/sh_edraft/configuration/configuration.py @@ -8,7 +8,8 @@ from sh_edraft.configuration.model.configuration_variable_name import Configurat from sh_edraft.environment.base.environment_base import EnvironmentBase from sh_edraft.environment.hosting_environment import HostingEnvironment from sh_edraft.environment.model.environment_name import EnvironmentName -from sh_edraft.utils.console import Console +from sh_edraft.utils.console.console import Console +from sh_edraft.utils.console.model.foreground_color import ForegroundColor class Configuration(ConfigurationBase): @@ -25,15 +26,21 @@ class Configuration(ConfigurationBase): @staticmethod def _print_info(name: str, message: str): - Console.write_line(f'[{name}] {message}', 'green') + Console.set_foreground_color(ForegroundColor.green) + Console.write_line(f'[{name}] {message}') + Console.set_foreground_color(ForegroundColor.default) @staticmethod def _print_warn(name: str, message: str): - Console.write_line(f'[{name}] {message}', 'yellow') + Console.set_foreground_color(ForegroundColor.yellow) + Console.write_line(f'[{name}] {message}') + Console.set_foreground_color(ForegroundColor.default) @staticmethod def _print_error(name: str, message: str): - Console.write_line(f'[{name}] {message}', 'red') + Console.set_foreground_color(ForegroundColor.red) + Console.write_line(f'[{name}] {message}') + Console.set_foreground_color(ForegroundColor.default) def _set_variable(self, name: str, value: str): if name == ConfigurationVariableName.environment.value: diff --git a/src/sh_edraft/database/connection/database_connection.py b/src/sh_edraft/database/connection/database_connection.py index 9c50ae79..5878f2c0 100644 --- a/src/sh_edraft/database/connection/database_connection.py +++ b/src/sh_edraft/database/connection/database_connection.py @@ -5,7 +5,8 @@ from sqlalchemy.orm import Session, sessionmaker from sh_edraft.database.connection.base.database_connection_base import DatabaseConnectionBase from sh_edraft.database.model.database_settings import DatabaseSettings -from sh_edraft.utils.console import Console +from sh_edraft.utils.console.console import Console +from sh_edraft.utils.console.model.foreground_color import ForegroundColor class DatabaseConnection(DatabaseConnectionBase): @@ -44,8 +45,12 @@ class DatabaseConnection(DatabaseConnectionBase): db_session = sessionmaker(bind=self._engine) self._session = db_session() - Console.write_line(f'[{__name__}] Connected to database', 'green') + Console.set_foreground_color(ForegroundColor.green) + Console.write_line(f'[{__name__}] Connected to database') + Console.set_foreground_color(ForegroundColor.default) except Exception as e: - Console.write_line(f'[{__name__}] Database connection failed -> {e}', 'red') + Console.set_foreground_color(ForegroundColor.red) + Console.write_line(f'[{__name__}] Database connection failed -> {e}') + Console.set_foreground_color(ForegroundColor.default) exit() diff --git a/src/sh_edraft/database/context/database_context.py b/src/sh_edraft/database/context/database_context.py index 07ba3719..0a3790ad 100644 --- a/src/sh_edraft/database/context/database_context.py +++ b/src/sh_edraft/database/context/database_context.py @@ -6,7 +6,8 @@ from sh_edraft.database.connection.base.database_connection_base import Database from sh_edraft.database.context.base.database_context_base import DatabaseContextBase from sh_edraft.database.model.dbmodel import DBModel from sh_edraft.database.model.database_settings import DatabaseSettings -from sh_edraft.utils.console import Console +from sh_edraft.utils.console.console import Console +from sh_edraft.utils.console.model.foreground_color import ForegroundColor class DatabaseContext(DatabaseContextBase): @@ -39,7 +40,11 @@ class DatabaseContext(DatabaseContextBase): DBModel.metadata.drop_all(self._db.engine, self._tables) DBModel.metadata.create_all(self._db.engine, self._tables, checkfirst=True) - Console.write_line(f'[{__name__}] Created tables', 'green') + Console.set_foreground_color(ForegroundColor.green) + Console.write_line(f'[{__name__}] Created tables') + Console.set_foreground_color(ForegroundColor.default) except Exception as e: - Console.write_line(f'[{__name__}] Creating tables failed -> {e}', 'red') + Console.set_foreground_color(ForegroundColor.red) + Console.write_line(f'[{__name__}] Creating tables failed -> {e}') + Console.set_foreground_color(ForegroundColor.default) exit() diff --git a/src/sh_edraft/database/model/database_settings.py b/src/sh_edraft/database/model/database_settings.py index a8df384d..da3dc590 100644 --- a/src/sh_edraft/database/model/database_settings.py +++ b/src/sh_edraft/database/model/database_settings.py @@ -3,7 +3,8 @@ from typing import Optional from sh_edraft.configuration.base.configuration_model_base import ConfigurationModelBase from sh_edraft.database.model.database_settings_name import DatabaseSettingsName -from sh_edraft.utils.console import Console +from sh_edraft.utils.console.console import Console +from sh_edraft.utils.console.model.foreground_color import ForegroundColor class DatabaseSettings(ConfigurationModelBase): @@ -71,5 +72,7 @@ class DatabaseSettings(ConfigurationModelBase): if DatabaseSettingsName.echo.value in settings: self._echo = bool(settings[DatabaseSettingsName.echo.value]) except Exception as e: - Console.write_line(f'[ ERROR ] [ {__name__} ]: Reading error in {self.__name__} settings', 'red') - Console.write_line(f'[ EXCEPTION ] [ {__name__} ]: {e} -> {traceback.format_exc()}', 'red') + Console.set_foreground_color(ForegroundColor.red) + Console.write_line(f'[ ERROR ] [ {__name__} ]: Reading error in {self.__name__} settings') + Console.write_line(f'[ EXCEPTION ] [ {__name__} ]: {e} -> {traceback.format_exc()}') + Console.set_foreground_color(ForegroundColor.default) diff --git a/src/sh_edraft/logging/logger.py b/src/sh_edraft/logging/logger.py index a043b21b..21e91c01 100644 --- a/src/sh_edraft/logging/logger.py +++ b/src/sh_edraft/logging/logger.py @@ -8,7 +8,8 @@ from sh_edraft.logging.base.logger_base import LoggerBase from sh_edraft.logging.model.logging_settings import LoggingSettings from sh_edraft.logging.model.logging_level import LoggingLevel from sh_edraft.time.model.time_format_settings import TimeFormatSettings -from sh_edraft.utils.console import Console +from sh_edraft.utils.console.console import Console +from sh_edraft.utils.console.model.foreground_color import ForegroundColor class Logger(LoggerBase): @@ -80,7 +81,9 @@ class Logger(LoggerBase): def header(self, string: str): # append log and print message self._append_log(string) - Console.write_line(string, 'white') + Console.set_foreground_color(ForegroundColor.default) + Console.write_line(string) + Console.set_foreground_color(ForegroundColor.default) def trace(self, name: str, message: str): output = self._get_string(name, LoggingLevel.TRACE, message) @@ -91,7 +94,9 @@ class Logger(LoggerBase): # check if message can be shown in console if self._console.value >= LoggingLevel.TRACE.value: - Console.write_line(output, 'green') + Console.set_foreground_color(ForegroundColor.green) + Console.write_line(output) + Console.set_foreground_color(ForegroundColor.default) def debug(self, name: str, message: str): output = self._get_string(name, LoggingLevel.DEBUG, message) @@ -102,7 +107,9 @@ class Logger(LoggerBase): # check if message can be shown in console if self._console.value >= LoggingLevel.DEBUG.value: - Console.write_line(output, 'green') + Console.set_foreground_color(ForegroundColor.green) + Console.write_line(output) + Console.set_foreground_color(ForegroundColor.default) def info(self, name: str, message: str): output = self._get_string(name, LoggingLevel.INFO, message) @@ -113,7 +120,9 @@ class Logger(LoggerBase): # check if message can be shown in console if self._console.value >= LoggingLevel.INFO.value: - Console.write_line(output, 'green') + Console.set_foreground_color(ForegroundColor.green) + Console.write_line(output) + Console.set_foreground_color(ForegroundColor.default) def warn(self, name: str, message: str): output = self._get_string(name, LoggingLevel.WARN, message) @@ -124,7 +133,9 @@ class Logger(LoggerBase): # check if message can be shown in console if self._console.value >= LoggingLevel.WARN.value: - Console.write_line(output, 'yellow') + Console.set_foreground_color(ForegroundColor.yellow) + Console.write_line(output) + Console.set_foreground_color(ForegroundColor.default) def error(self, name: str, message: str, ex: Exception = None): output = '' @@ -141,7 +152,9 @@ class Logger(LoggerBase): # check if message can be shown in console if self._console.value >= LoggingLevel.ERROR.value: - Console.write_line(output, 'red') + Console.set_foreground_color(ForegroundColor.red) + Console.write_line(output) + Console.set_foreground_color(ForegroundColor.default) def fatal(self, name: str, message: str, ex: Exception = None): output = '' @@ -158,7 +171,9 @@ class Logger(LoggerBase): # check if message can be shown in console if self._console.value >= LoggingLevel.FATAL.value: - Console.write_line(output, 'red') + Console.set_foreground_color(ForegroundColor.red) + Console.write_line(output) + Console.set_foreground_color(ForegroundColor.default) exit() @@ -173,6 +188,8 @@ class Logger(LoggerBase): # check if message can be shown in console if self._console.value >= LoggingLevel.FATAL.value: - Console.write_line(output, 'red') + Console.set_foreground_color(ForegroundColor.red) + Console.write_line(output) + Console.set_foreground_color(ForegroundColor.default) exit() diff --git a/src/sh_edraft/logging/model/logging_settings.py b/src/sh_edraft/logging/model/logging_settings.py index 47889dea..b884f944 100644 --- a/src/sh_edraft/logging/model/logging_settings.py +++ b/src/sh_edraft/logging/model/logging_settings.py @@ -3,8 +3,9 @@ from typing import Optional from sh_edraft.configuration.base.configuration_model_base import ConfigurationModelBase from sh_edraft.logging.model.logging_settings_name import LoggingSettingsName -from sh_edraft.utils.console import Console from sh_edraft.logging.model.logging_level import LoggingLevel +from sh_edraft.utils.console.console import Console +from sh_edraft.utils.console.model.foreground_color import ForegroundColor class LoggingSettings(ConfigurationModelBase): @@ -55,5 +56,7 @@ class LoggingSettings(ConfigurationModelBase): self._console = LoggingLevel[settings[LoggingSettingsName.console_level.value]] self._level = LoggingLevel[settings[LoggingSettingsName.file_level.value]] except Exception as e: - Console.write_line(f'[ ERROR ] [ {__name__} ]: Reading error in {self.__name__} settings', 'red') - Console.write_line(f'[ EXCEPTION ] [ {__name__} ]: {e} -> {traceback.format_exc()}', 'red') + Console.set_foreground_color(ForegroundColor.red) + Console.write_line(f'[ ERROR ] [ {__name__} ]: Reading error in {self.__name__} settings') + Console.write_line(f'[ EXCEPTION ] [ {__name__} ]: {e} -> {traceback.format_exc()}') + Console.set_foreground_color(ForegroundColor.default) diff --git a/src/sh_edraft/publishing/model/publish_settings_model.py b/src/sh_edraft/publishing/model/publish_settings_model.py index a743692a..3b9ed148 100644 --- a/src/sh_edraft/publishing/model/publish_settings_model.py +++ b/src/sh_edraft/publishing/model/publish_settings_model.py @@ -4,7 +4,8 @@ from typing import Optional from sh_edraft.configuration.base.configuration_model_base import ConfigurationModelBase from sh_edraft.publishing.model.template import Template from sh_edraft.publishing.model.publish_settings_name import PublishSettingsName -from sh_edraft.utils.console import Console +from sh_edraft.utils.console.console import Console +from sh_edraft.utils.console.model.foreground_color import ForegroundColor class PublishSettings(ConfigurationModelBase): @@ -78,6 +79,8 @@ class PublishSettings(ConfigurationModelBase): self._excluded_files = settings[PublishSettingsName.excluded_files.value] self._template_ending = settings[PublishSettingsName.template_ending.value] except Exception as e: + Console.set_foreground_color(ForegroundColor.red) Console.write_line( - f'[ ERROR ] [ {__name__} ]: Reading error in {PublishSettingsName.publish.value} settings', 'red') - Console.write_line(f'[ EXCEPTION ] [ {__name__} ]: {e} -> {traceback.format_exc()}', 'red') + f'[ ERROR ] [ {__name__} ]: Reading error in {PublishSettingsName.publish.value} settings') + Console.write_line(f'[ EXCEPTION ] [ {__name__} ]: {e} -> {traceback.format_exc()}') + Console.set_foreground_color(ForegroundColor.default) diff --git a/src/sh_edraft/time/model/time_format_settings.py b/src/sh_edraft/time/model/time_format_settings.py index 64508ef2..55366710 100644 --- a/src/sh_edraft/time/model/time_format_settings.py +++ b/src/sh_edraft/time/model/time_format_settings.py @@ -3,7 +3,8 @@ from typing import Optional from sh_edraft.configuration.base.configuration_model_base import ConfigurationModelBase from sh_edraft.time.model.time_format_settings_names import TimeFormatSettingsNames -from sh_edraft.utils.console import Console +from sh_edraft.utils.console.console import Console +from sh_edraft.utils.console.model.foreground_color import ForegroundColor class TimeFormatSettings(ConfigurationModelBase): @@ -56,5 +57,7 @@ class TimeFormatSettings(ConfigurationModelBase): self._date_time_format = settings[TimeFormatSettingsNames.date_time_format.value] self._date_time_log_format = settings[TimeFormatSettingsNames.date_time_log_format.value] except Exception as e: + Console.set_foreground_color(ForegroundColor.red) Console.write_line(f'[ ERROR ] [ {__name__} ]: Reading error in {self.__name__} settings') - Console.write_line(f'[ EXCEPTION ] [ {__name__} ]: {e} -> {traceback.format_exc()}', 'red') + Console.write_line(f'[ EXCEPTION ] [ {__name__} ]: {e} -> {traceback.format_exc()}') + Console.set_foreground_color(ForegroundColor.default) diff --git a/src/sh_edraft/utils/console.py b/src/sh_edraft/utils/console.py deleted file mode 100644 index 1d014a9c..00000000 --- a/src/sh_edraft/utils/console.py +++ /dev/null @@ -1,11 +0,0 @@ -from termcolor import colored - - -class Console: - - @staticmethod - def write_line(string: str, color: str = None): - if color is not None: - print(colored(string, color)) - else: - print(string) diff --git a/src/sh_edraft/utils/console/__init__.py b/src/sh_edraft/utils/console/__init__.py new file mode 100644 index 00000000..161a350e --- /dev/null +++ b/src/sh_edraft/utils/console/__init__.py @@ -0,0 +1,3 @@ +# imports: + +from .console import Console diff --git a/src/sh_edraft/utils/console/console.py b/src/sh_edraft/utils/console/console.py new file mode 100644 index 00000000..f4b13254 --- /dev/null +++ b/src/sh_edraft/utils/console/console.py @@ -0,0 +1,76 @@ +import os +import subprocess +from typing import Union +from termcolor import colored + +from sh_edraft.utils.console.model.background_color import BackgroundColor +from sh_edraft.utils.console.model.foreground_color import ForegroundColor + + +class Console: + _background_color: BackgroundColor = BackgroundColor.default + _foreground_color: ForegroundColor = ForegroundColor.default + + @property + def background_color(self) -> BackgroundColor: + return self._background_color + + @property + def foreground_color(self) -> ForegroundColor: + return self._foreground_color + + @classmethod + def set_background_color(cls, color: Union[BackgroundColor, str]): + if type(color) is str: + cls._background_color = BackgroundColor[color] + else: + cls._background_color = color + + @classmethod + def set_foreground_color(cls, color: Union[ForegroundColor, str]): + if type(color) is str: + cls._foreground_color = ForegroundColor[color] + else: + cls._foreground_color = color + + # useful methods + @staticmethod + def clear(): + os.system('cls' if os.name == 'nt' else 'clear') + + @staticmethod + def new(): + if os.name == 'nt': + os.system("start /wait cmd") + else: + p = subprocess.Popen(args=["gnome-terminal"]) + p.communicate() + + @staticmethod + def read(output: str = None) -> str: + user_input = input(output if output else '') + return user_input[0] + + @staticmethod + def read_line(output: str = None) -> str: + return input(output if output else '') + + @classmethod + def reset(cls): + cls._background_color = BackgroundColor.default + cls._foreground_color = ForegroundColor.default + + @classmethod + def write(cls, string: str): + if cls._foreground_color == ForegroundColor.default: + print(colored(string), end='') + else: + print(colored(string, cls._foreground_color.value), end='') + + @classmethod + def write_line(cls, *args): + string = ' '.join(map(str, args)) + if cls._foreground_color == ForegroundColor.default: + print(colored(string)) + else: + print(colored(string, cls._foreground_color.value)) diff --git a/src/sh_edraft/utils/console/model/__init__.py b/src/sh_edraft/utils/console/model/__init__.py new file mode 100644 index 00000000..c5051d39 --- /dev/null +++ b/src/sh_edraft/utils/console/model/__init__.py @@ -0,0 +1,4 @@ +# imports: + +from .background_color import BackgroundColor +from .foreground_color import ForegroundColor diff --git a/src/sh_edraft/utils/console/model/background_color.py b/src/sh_edraft/utils/console/model/background_color.py new file mode 100644 index 00000000..a50f49c8 --- /dev/null +++ b/src/sh_edraft/utils/console/model/background_color.py @@ -0,0 +1,14 @@ +from enum import Enum + + +class BackgroundColor(Enum): + + default = 'default' + grey = 'grey' + red = 'red' + green = 'green' + yellow = 'yellow' + blue = 'blue' + magenta = 'magenta' + cyan = 'cyan' + white = 'white' diff --git a/src/sh_edraft/utils/console/model/foreground_color.py b/src/sh_edraft/utils/console/model/foreground_color.py new file mode 100644 index 00000000..463d4ae7 --- /dev/null +++ b/src/sh_edraft/utils/console/model/foreground_color.py @@ -0,0 +1,14 @@ +from enum import Enum + + +class ForegroundColor(Enum): + + default = 'default' + grey = 'grey' + red = 'red' + green = 'green' + yellow = 'yellow' + blue = 'blue' + magenta = 'magenta' + cyan = 'cyan' + white = 'white'