From 5835a3cb7b564b513ee7dbc1e68c975db17b1d62 Mon Sep 17 00:00:00 2001 From: Jim Madge Date: Tue, 8 Oct 2024 14:54:41 +0100 Subject: [PATCH 1/4] Add strip ansi method --- data_safe_haven/logging/plain_file_handler.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/data_safe_haven/logging/plain_file_handler.py b/data_safe_haven/logging/plain_file_handler.py index c41d0e5ffc..fe8252e6cb 100644 --- a/data_safe_haven/logging/plain_file_handler.py +++ b/data_safe_haven/logging/plain_file_handler.py @@ -16,16 +16,25 @@ def __init__(self, *args: Any, **kwargs: Any): super().__init__(*args, **kwargs) @staticmethod - def strip_formatting(input_string: str) -> str: + def strip_rich_formatting(input_string: str) -> str: """Strip console markup formatting from a string""" text = Text.from_markup(input_string) text.spans = [] return str(text) + @staticmethod + def strip_ansi_escapes(input_string: str) -> str: + """Strip ANSI escape sequences from a string""" + text = Text.from_ansi(input_string) + text.spans = [] + return str(text) + def emit(self, record: logging.LogRecord) -> None: """Emit a record without formatting""" if isinstance(record.msg, Text): # Convert rich.text.Text objects to strings record.msg = str(record.msg) - record.msg = self.strip_formatting(record.msg) + record.msg = self.strip_ansi_escapes(self.strip_rich_formatting( + record.msg + )) super().emit(record) From fdb6da31468159895149be6328a9f9aa3470a421 Mon Sep 17 00:00:00 2001 From: Jim Madge Date: Tue, 8 Oct 2024 15:00:01 +0100 Subject: [PATCH 2/4] Add test --- data_safe_haven/logging/plain_file_handler.py | 4 +--- tests/logging/test_plain_file_handler.py | 9 +++++++-- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/data_safe_haven/logging/plain_file_handler.py b/data_safe_haven/logging/plain_file_handler.py index fe8252e6cb..f41690b043 100644 --- a/data_safe_haven/logging/plain_file_handler.py +++ b/data_safe_haven/logging/plain_file_handler.py @@ -34,7 +34,5 @@ def emit(self, record: logging.LogRecord) -> None: if isinstance(record.msg, Text): # Convert rich.text.Text objects to strings record.msg = str(record.msg) - record.msg = self.strip_ansi_escapes(self.strip_rich_formatting( - record.msg - )) + record.msg = self.strip_ansi_escapes(self.strip_rich_formatting(record.msg)) super().emit(record) diff --git a/tests/logging/test_plain_file_handler.py b/tests/logging/test_plain_file_handler.py index a2bf60ad81..feee190266 100644 --- a/tests/logging/test_plain_file_handler.py +++ b/tests/logging/test_plain_file_handler.py @@ -2,5 +2,10 @@ class TestPlainFileHandler: - def test_strip_formatting(self): - assert PlainFileHandler.strip_formatting("[green]hello[/]") == "hello" + def test_strip_rich_formatting(self): + assert PlainFileHandler.strip_rich_formatting("[green]Hello[/]") == "Hello" + + def test_strip_ansi_escapes(self): + assert ( + PlainFileHandler.strip_ansi_escapes("\033[31;1;4mHello\033[0m") == "Hello" + ) From 1f3e751a04c3223b069f52fea7dc670c2634d79e Mon Sep 17 00:00:00 2001 From: Jim Madge Date: Tue, 8 Oct 2024 15:16:10 +0100 Subject: [PATCH 3/4] Add test for alternative escape char formats --- tests/logging/test_plain_file_handler.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/tests/logging/test_plain_file_handler.py b/tests/logging/test_plain_file_handler.py index feee190266..90f7c6ca70 100644 --- a/tests/logging/test_plain_file_handler.py +++ b/tests/logging/test_plain_file_handler.py @@ -1,3 +1,5 @@ +import pytest + from data_safe_haven.logging.plain_file_handler import PlainFileHandler @@ -5,7 +7,9 @@ class TestPlainFileHandler: def test_strip_rich_formatting(self): assert PlainFileHandler.strip_rich_formatting("[green]Hello[/]") == "Hello" - def test_strip_ansi_escapes(self): + @pytest.mark.parametrize("escape", ["\033", "\x1B", "\u001b", "\x1B"]) + def test_strip_ansi_escapes(self, escape): assert ( - PlainFileHandler.strip_ansi_escapes("\033[31;1;4mHello\033[0m") == "Hello" + PlainFileHandler.strip_ansi_escapes(f"{escape}[31;1;4mHello{escape}[0m") + == "Hello" ) From 42d46c1a310f15e538544dd7beab1e896d2a2fac Mon Sep 17 00:00:00 2001 From: Jim Madge Date: Tue, 8 Oct 2024 15:17:18 +0100 Subject: [PATCH 4/4] Correct indentation --- data_safe_haven/logging/plain_file_handler.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data_safe_haven/logging/plain_file_handler.py b/data_safe_haven/logging/plain_file_handler.py index f41690b043..ffce4a551c 100644 --- a/data_safe_haven/logging/plain_file_handler.py +++ b/data_safe_haven/logging/plain_file_handler.py @@ -34,5 +34,5 @@ def emit(self, record: logging.LogRecord) -> None: if isinstance(record.msg, Text): # Convert rich.text.Text objects to strings record.msg = str(record.msg) - record.msg = self.strip_ansi_escapes(self.strip_rich_formatting(record.msg)) + record.msg = self.strip_ansi_escapes(self.strip_rich_formatting(record.msg)) super().emit(record)