Added console module

This commit is contained in:
Sven Heidemann 2020-12-14 21:10:26 +01:00
parent bb5b1f5944
commit 096b50f9f1
14 changed files with 187 additions and 41 deletions

View File

@ -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:

View File

@ -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()

View File

@ -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()

View File

@ -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)

View File

@ -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()

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -0,0 +1,3 @@
# imports:
from .console import Console

View File

@ -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))

View File

@ -0,0 +1,4 @@
# imports:
from .background_color import BackgroundColor
from .foreground_color import ForegroundColor

View File

@ -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'

View File

@ -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'