From 368d81cb3f62ade8055cee4c630ec6d93a16ccd8 Mon Sep 17 00:00:00 2001 From: Jan Lindemann Date: Thu, 23 Feb 2023 14:47:52 +0100 Subject: [PATCH] os.Connection.read(): Support ReadOnlyPrintable Add flag ReadOnlyPrintable and support it in read(). This omits all characters not in string.printable from read()'s result. This comes in handy if systemd spits out bouncing start-job-running asterisks which find their way back to the reader but close the shell on him. Signed-off-by: Jan Lindemann --- src/python/devtest/os/Connection.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/python/devtest/os/Connection.py b/src/python/devtest/os/Connection.py index 260e2eb..129325d 100644 --- a/src/python/devtest/os/Connection.py +++ b/src/python/devtest/os/Connection.py @@ -4,6 +4,7 @@ from abc import ABC, abstractmethod from enum import Enum, IntFlag import asyncio import types +import string from jwutils.log import * from .TestPhases import TestPhases @@ -21,6 +22,7 @@ class Connection(ABC): # export FailOnTimeout = 0x01 ReadStripNewline = 0x02 ReadDecodeToString = 0x04 + ReadOnlyPrintable = 0x08 class Info: @@ -124,10 +126,12 @@ class Connection(ABC): # export r = r.decode('utf-8', errors='replace') except Exception as e: slog(WARNING, 'Failed to decode string, returning undecoded ({}): "{}"'.format(e, r)) - return str(r) + r = str(r) if flags & self.Flags.ReadStripNewline: if len(r) and r[-1] == '\n': r = r[0:-1] + if flags & self.Flags.ReadOnlyPrintable: + r = ''.join(filter(lambda x: x in string.printable, r)) return r async def readline(self, timeout=None):