Compare commits

...

3 Commits

Author SHA1 Message Date
Clemens Schwaighofer
67f1a6688d Update Colors to have static colors that we can reset to 2025-07-09 15:22:31 +09:00
Clemens Schwaighofer
efb7968e93 time zone general names: upper case for constant 2025-07-09 15:09:12 +09:00
Clemens Schwaighofer
fe7c7db004 Change all __class__ to self for class global var 2025-07-09 15:06:27 +09:00
4 changed files with 112 additions and 41 deletions

View File

@@ -43,7 +43,6 @@ class CustomConsoleFormatter(logging.Formatter):
"ERROR": Colors.red, "ERROR": Colors.red,
"CRITICAL": Colors.magenta, "CRITICAL": Colors.magenta,
"EXCEPTION": Colors.magenta_bold, "EXCEPTION": Colors.magenta_bold,
"RESET": Colors.reset, # Reset
} }
def format(self, record: logging.LogRecord) -> str: def format(self, record: logging.LogRecord) -> str:
@@ -57,7 +56,7 @@ class CustomConsoleFormatter(logging.Formatter):
str -- _description_ str -- _description_
""" """
# Add color to levelname for console output # Add color to levelname for console output
reset = self.COLORS["RESET"] reset = Colors.reset
color = self.COLORS.get(record.levelname, reset) color = self.COLORS.get(record.levelname, reset)
# only highlight level for basic # only highlight level for basic
if record.levelname in ['DEBUG', 'INFO']: if record.levelname in ['DEBUG', 'INFO']:
@@ -154,17 +153,17 @@ class Log:
) -> LogSettings: ) -> LogSettings:
# skip with defaul it not set # skip with defaul it not set
if log_settings is None: if log_settings is None:
return __class__.DEFAULT_LOG_SETTINGS return self.DEFAULT_LOG_SETTINGS
# check entries # check entries
default_log_settings = __class__.DEFAULT_LOG_SETTINGS default_log_settings = self.DEFAULT_LOG_SETTINGS
# check log levels # check log levels
for __log_entry in ['log_level_console', 'log_level_file']: for __log_entry in ['log_level_console', 'log_level_file']:
if log_settings.get(__log_entry) is None: if log_settings.get(__log_entry) is None:
continue continue
# if not valid reset to default, if not in default set to WARNING # if not valid reset to default, if not in default set to WARNING
if not self.validate_log_level(_log_level := log_settings.get(__log_entry, '')): if not self.validate_log_level(_log_level := log_settings.get(__log_entry, '')):
_log_level = __class__.DEFAULT_LOG_SETTINGS.get( _log_level = self.DEFAULT_LOG_SETTINGS.get(
__log_entry, __class__.DEFAULT_LOG_LEVEL __log_entry, self.DEFAULT_LOG_LEVEL
) )
default_log_settings[__log_entry] = str(_log_level) default_log_settings[__log_entry] = str(_log_level)
# check bool # check bool
@@ -177,10 +176,10 @@ class Log:
if log_settings.get(__log_entry) is None: if log_settings.get(__log_entry) is None:
continue continue
if not isinstance(__setting := log_settings.get(__log_entry, ''), bool): if not isinstance(__setting := log_settings.get(__log_entry, ''), bool):
__setting = __class__.DEFAULT_LOG_SETTINGS.get(__log_entry, True) __setting = self.DEFAULT_LOG_SETTINGS.get(__log_entry, True)
default_log_settings[__log_entry] = __setting default_log_settings[__log_entry] = __setting
# check log queue # check log queue
__setting = log_settings.get('log_queue', __class__.DEFAULT_LOG_SETTINGS['log_queue']) __setting = log_settings.get('log_queue', self.DEFAULT_LOG_SETTINGS['log_queue'])
if __setting is not None: if __setting is not None:
__setting = cast('Queue[str]', __setting) __setting = cast('Queue[str]', __setting)
default_log_settings['log_queue'] = __setting default_log_settings['log_queue'] = __setting
@@ -308,7 +307,7 @@ class Log:
Keyword Arguments: Keyword Arguments:
info {str} -- _description_ (default: {"BREAK"}) info {str} -- _description_ (default: {"BREAK"})
""" """
self.logger.info("[%s] %s>", info, __class__.SPACER_CHAR * __class__.SPACER_LENGTH) self.logger.info("[%s] %s>", info, self.SPACER_CHAR * self.SPACER_LENGTH)
def exception(self, msg: object, *args: object, extra: Mapping[str, object] | None = None) -> None: def exception(self, msg: object, *args: object, extra: Mapping[str, object] | None = None) -> None:
""" """

View File

@@ -10,40 +10,73 @@ class Colors:
""" """
ANSI colors defined ANSI colors defined
""" """
# General sets # General sets, these should not be accessd
bold = '\033[1m' __BOLD = '\033[1m'
underline = '\033[4m' __UNDERLINE = '\033[4m'
end = '\033[0m' __END = '\033[0m'
reset = '\033[0m' __RESET = '\033[0m'
# Define ANSI color codes as class attributes # Define ANSI color codes as class attributes
black = "\033[30m" __BLACK = "\033[30m"
red = "\033[31m" __RED = "\033[31m"
green = "\033[32m" __GREEN = "\033[32m"
yellow = "\033[33m" __YELLOW = "\033[33m"
blue = "\033[34m" __BLUE = "\033[34m"
magenta = "\033[35m" __MAGENTA = "\033[35m"
cyan = "\033[36m" __CYAN = "\033[36m"
white = "\033[37m" __WHITE = "\033[37m"
# Define bold/bright versions of the colors # Define bold/bright versions of the colors
black_bold = "\033[1;30m" __BLACK_BOLD = "\033[1;30m"
red_bold = "\033[1;31m" __RED_BOLD = "\033[1;31m"
green_bold = "\033[1;32m" __GREEN_BOLD = "\033[1;32m"
yellow_bold = "\033[1;33m" __YELLOW_BOLD = "\033[1;33m"
blue_bold = "\033[1;34m" __BLUE_BOLD = "\033[1;34m"
magenta_bold = "\033[1;35m" __MAGENTA_BOLD = "\033[1;35m"
cyan_bold = "\033[1;36m" __CYAN_BOLD = "\033[1;36m"
white_bold = "\033[1;37m" __WHITE_BOLD = "\033[1;37m"
# BRIGHT, alternative # BRIGHT, alternative
black_bright = '\033[90m' __BLACK_BRIGHT = '\033[90m'
red_bright = '\033[91m' __RED_BRIGHT = '\033[91m'
green_bright = '\033[92m' __GREEN_BRIGHT = '\033[92m'
yellow_bright = '\033[93m' __YELLOW_BRIGHT = '\033[93m'
blue_bright = '\033[94m' __BLUE_BRIGHT = '\033[94m'
magenta_bright = '\033[95m' __MAGENTA_BRIGHT = '\033[95m'
cyan_bright = '\033[96m' __CYAN_BRIGHT = '\033[96m'
white_bright = '\033[97m' __WHITE_BRIGHT = '\033[97m'
# set access vars
bold = __BOLD
underline = __UNDERLINE
end = __END
reset = __RESET
# normal
black = __BLACK
red = __RED
green = __GREEN
yellow = __YELLOW
blue = __BLUE
magenta = __MAGENTA
cyan = __CYAN
white = __WHITE
# bold
black_bold = __BLACK_BOLD
red_bold = __RED_BOLD
green_bold = __GREEN_BOLD
yellow_bold = __YELLOW_BOLD
blue_bold = __BLUE_BOLD
magenta_bold = __MAGENTA_BOLD
cyan_bold = __CYAN_BOLD
white_bold = __WHITE_BOLD
# bright
black_bright = __BLACK_BRIGHT
red_bright = __RED_BRIGHT
green_bright = __GREEN_BRIGHT
yellow_bright = __YELLOW_BRIGHT
blue_bright = __BLUE_BRIGHT
magenta_bright = __MAGENTA_BRIGHT
cyan_bright = __CYAN_BRIGHT
white_bright = __WHITE_BRIGHT
@staticmethod @staticmethod
def disable(): def disable():
@@ -81,4 +114,43 @@ class Colors:
Colors.magenta_bright = '' Colors.magenta_bright = ''
Colors.cyan_bright = '' Colors.cyan_bright = ''
Colors.white_bright = '' Colors.white_bright = ''
@staticmethod
def reset_colors():
"""
reset colors to the original ones
"""
# set access vars
Colors.bold = Colors.__BOLD
Colors.underline = Colors.__UNDERLINE
Colors.end = Colors.__END
Colors.reset = Colors.__RESET
# normal
Colors.black = Colors.__BLACK
Colors.red = Colors.__RED
Colors.green = Colors.__GREEN
Colors.yellow = Colors.__YELLOW
Colors.blue = Colors.__BLUE
Colors.magenta = Colors.__MAGENTA
Colors.cyan = Colors.__CYAN
Colors.white = Colors.__WHITE
# bold
Colors.black_bold = Colors.__BLACK_BOLD
Colors.red_bold = Colors.__RED_BOLD
Colors.green_bold = Colors.__GREEN_BOLD
Colors.yellow_bold = Colors.__YELLOW_BOLD
Colors.blue_bold = Colors.__BLUE_BOLD
Colors.magenta_bold = Colors.__MAGENTA_BOLD
Colors.cyan_bold = Colors.__CYAN_BOLD
Colors.white_bold = Colors.__WHITE_BOLD
# bright
Colors.black_bright = Colors.__BLACK_BRIGHT
Colors.red_bright = Colors.__RED_BRIGHT
Colors.green_bright = Colors.__GREEN_BRIGHT
Colors.yellow_bright = Colors.__YELLOW_BRIGHT
Colors.blue_bright = Colors.__BLUE_BRIGHT
Colors.magenta_bright = Colors.__MAGENTA_BRIGHT
Colors.cyan_bright = Colors.__CYAN_BRIGHT
Colors.white_bright = Colors.__WHITE_BRIGHT
# __END__ # __END__

View File

@@ -11,11 +11,11 @@ class TimestampStrings:
set default time stamps set default time stamps
""" """
time_zone: str = 'Asia/Tokyo' TIME_ZONE: str = 'Asia/Tokyo'
def __init__(self, time_zone: str | None = None): def __init__(self, time_zone: str | None = None):
self.timestamp_now = datetime.now() self.timestamp_now = datetime.now()
self.time_zone = time_zone if time_zone is not None else __class__.time_zone self.time_zone = time_zone if time_zone is not None else self.TIME_ZONE
try: try:
self.timestamp_now_tz = datetime.now(ZoneInfo(self.time_zone)) self.timestamp_now_tz = datetime.now(ZoneInfo(self.time_zone))
except ZoneInfoNotFoundError as e: except ZoneInfoNotFoundError as e:

View File

@@ -82,7 +82,7 @@ class TestTimestampStrings:
def test_class_default_timezone(self): def test_class_default_timezone(self):
"""Test that class default timezone is correctly set""" """Test that class default timezone is correctly set"""
assert TimestampStrings.time_zone == 'Asia/Tokyo' assert TimestampStrings.TIME_ZONE == 'Asia/Tokyo'
def test_none_timezone_uses_default(self): def test_none_timezone_uses_default(self):
"""Test that passing None for timezone uses class default""" """Test that passing None for timezone uses class default"""