From b5663861724d54d9f039ded3a3a8d038d9d34a74 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Wed, 24 Jan 2024 13:14:59 -1000 Subject: [PATCH 1/2] Revert "Fix overly greedy _strip_rich_formatting (#703)" This reverts commit ae6a31463ef5840276e4d4e7ce8d3a63060a9e03. --- kasa/cli.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/kasa/cli.py b/kasa/cli.py index 86aea4367..5f726be05 100755 --- a/kasa/cli.py +++ b/kasa/cli.py @@ -37,9 +37,6 @@ try: from rich import print as _do_echo except ImportError: - # Remove 7-bit C1 ANSI sequences - # https://stackoverflow.com/questions/14693701/how-can-i-remove-the-ansi-escape-sequences-from-a-string-in-python - ansi_escape = re.compile(r"\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])") def _strip_rich_formatting(echo_func): """Strip rich formatting from messages.""" @@ -47,7 +44,7 @@ def _strip_rich_formatting(echo_func): @wraps(echo_func) def wrapper(message=None, *args, **kwargs): if message is not None: - message = ansi_escape.sub("", message) + message = re.sub(r"\[/?.+?]", "", message) echo_func(message, *args, **kwargs) return wrapper From 81757905a9a2398ab50d47b0363170de9cb16ad0 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Wed, 24 Jan 2024 13:18:24 -1000 Subject: [PATCH 2/2] Improve rich formatter stripper reverts and replaces #703 --- kasa/cli.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/kasa/cli.py b/kasa/cli.py index 5f726be05..42b13b9bb 100755 --- a/kasa/cli.py +++ b/kasa/cli.py @@ -37,6 +37,11 @@ try: from rich import print as _do_echo except ImportError: + # Strip out rich formatting if rich is not installed + # but only lower case tags to avoid stripping out + # raw data from the device that is printed from + # the device state. + rich_formatting = re.compile(r"\[/?[a-z]+]") def _strip_rich_formatting(echo_func): """Strip rich formatting from messages.""" @@ -44,7 +49,7 @@ def _strip_rich_formatting(echo_func): @wraps(echo_func) def wrapper(message=None, *args, **kwargs): if message is not None: - message = re.sub(r"\[/?.+?]", "", message) + message = rich_formatting.sub("", message) echo_func(message, *args, **kwargs) return wrapper