From 7d38dab9983aece79925df9a60f75ec06ba74634 Mon Sep 17 00:00:00 2001 From: EKNr1 Date: Mon, 13 Jan 2025 18:00:54 +0100 Subject: [PATCH] Improved log and log test. --- .gitignore | 3 ++- tests/text/test_log.py | 15 +++++++++++- wobbl_tools/text/log.py | 53 +++++++++++++++++++++++++++-------------- 3 files changed, 51 insertions(+), 20 deletions(-) diff --git a/.gitignore b/.gitignore index 71733a2..ffa5293 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ __pycache__ build -wobbl_tools.egg-info \ No newline at end of file +wobbl_tools.egg-info +test_log.txt \ No newline at end of file diff --git a/tests/text/test_log.py b/tests/text/test_log.py index b52510e..da5bada 100644 --- a/tests/text/test_log.py +++ b/tests/text/test_log.py @@ -1,17 +1,30 @@ #!/usr/bin/python3 +import pytest from wobbl_tools.text.log import Log print("\n==== wobbl_tools.text.log =====") test_log = Log("test_log.txt") +test_log.clear() test_log.write("Testing the log module...") test_log.write("This should be fine...", "ok") test_log.write("Something happened...", "warning") test_log.write("Oh shit!", "error") - print("Logfile contents:") print(test_log.read()) +expected_content = \ + "[info]: Testing the log module...\n" \ + "[OK]: This should be fine...\n" \ + "[WARNING]: Something happened...\n" \ + "[ERROR]: Oh shit!\n" + +assert test_log.read() == expected_content + +without_file = Log() + +with pytest.raises(FileNotFoundError, match="You didn't specify a log path at the log instance definition."): + without_file.read() diff --git a/wobbl_tools/text/log.py b/wobbl_tools/text/log.py index e14adc4..3ed68ed 100644 --- a/wobbl_tools/text/log.py +++ b/wobbl_tools/text/log.py @@ -2,14 +2,14 @@ from .format import format_string -msg_types_ncl = { # message types without colors +_msg_types_ncl = { # message types without colors "info": "[info]: ", "ok": "[OK]: ", "warning": "[WARNING]: ", "error": "[ERROR]: " } -msg_types = { +_msg_types = { # message types with colors "info": "[info]: ", "ok": format_string("[§dark_green§boldOK§rs]: "), "warning": format_string("§yellow§bold[WARNING]: ", auto_rs=False), @@ -18,29 +18,41 @@ msg_types = { class Log: - def __init__(self, log_path: str = None, no_console: bool = False, no_colors: bool = False): + def __init__( + self, log_path: str = None, + console_out: bool = True, + console_colored: bool = True, + file_colored: bool = True + ): self.log_path = log_path - self.no_console = no_console - self.no_colors = no_colors - self.log = "" + self.console_out = console_out + self.console_colored = console_colored + self.file_colored = file_colored def write(self, msg: str, msg_type: str = "info", prefix: str = ""): - if self.no_colors: - msg = prefix + msg_types_ncl[msg_type] + msg + logfile_msg = self.format_msg(msg, msg_type, self.file_colored, prefix) + "\n" - else: - msg = prefix + msg_types[msg_type] + msg - msg += format_string("§rs") + if self.console_out: + console_msg = self.format_msg(msg, msg_type, self.console_colored, prefix) - if not self.no_console: - print(msg) + print(console_msg) if self.log_path is not None: - log_file = open(self.log_path, "a") - log_file.write(msg + "\n") - log_file.close() + logfile = open(self.log_path, "a") + logfile.write(logfile_msg) + logfile.close() - self.log += msg + "\n" + def format_msg(self, msg, msg_type: str="info", colored: bool=True, prefix: str=""): + if colored: + formatted_msg = _msg_types[msg_type] + msg + formatted_msg += format_string("§rs") + + else: + formatted_msg = _msg_types_ncl[msg_type] + msg + + formatted_msg = prefix + formatted_msg + + return formatted_msg def read(self): if self.log_path is not None: @@ -50,4 +62,9 @@ class Log: return log_content else: - return self.log \ No newline at end of file + raise FileNotFoundError("You didn't specify a log path at the log instance definition.") + + def clear(self): + logfile = open(self.log_path, "w") + logfile.write("") + logfile.close()