From e3fb13de397c2f31884d1acb729da63a1bba9f87 Mon Sep 17 00:00:00 2001 From: Philippe Teuwen Date: Wed, 7 Aug 2024 10:29:40 +0200 Subject: [PATCH] Move output_grabber --- .gitignore | 5 +- .../testembedded_grab.py | 2 +- .../example_py/output_grabber.py | 78 ------------------- .../experimental_lib/example_py/test_grab.py | 2 +- .../output_grabber.py | 13 ++-- client/pyscripts/pm3.py | 4 +- 6 files changed, 16 insertions(+), 88 deletions(-) delete mode 100755 client/experimental_lib/example_py/output_grabber.py rename client/{experimental_client_with_swig => pyscripts}/output_grabber.py (90%) mode change 100755 => 100644 diff --git a/.gitignore b/.gitignore index 174c9579b..37583d538 100644 --- a/.gitignore +++ b/.gitignore @@ -119,4 +119,7 @@ fpga_version_info.c # local codeql _codeql* -/codeql \ No newline at end of file +/codeql + +# Pyton venvs +venv/ diff --git a/client/experimental_client_with_swig/testembedded_grab.py b/client/experimental_client_with_swig/testembedded_grab.py index caec75769..fda910fb1 100755 --- a/client/experimental_client_with_swig/testembedded_grab.py +++ b/client/experimental_client_with_swig/testembedded_grab.py @@ -8,6 +8,6 @@ p=pm3.pm3() print("Device:", p.name) with out: p.console("hw status") -for line in out.capturedtext.split('\n'): +for line in out.captured_output.split('\n'): if "Unique ID" in line: print(line) diff --git a/client/experimental_lib/example_py/output_grabber.py b/client/experimental_lib/example_py/output_grabber.py deleted file mode 100755 index ca9828149..000000000 --- a/client/experimental_lib/example_py/output_grabber.py +++ /dev/null @@ -1,78 +0,0 @@ -import os -import sys -import threading -import time - -# From https://stackoverflow.com/a/29834357 -class OutputGrabber(object): - """ - Class used to grab standard output or another stream. - """ - escape_char = "\b" - - def __init__(self, stream=None, threaded=False): - self.origstream = stream - self.threaded = threaded - if self.origstream is None: - self.origstream = sys.stdout - self.origstreamfd = self.origstream.fileno() - self.capturedtext = "" - - def __enter__(self): - self.start() - return self - - def __exit__(self, type, value, traceback): - self.stop() - - def start(self): - """ - Start capturing the stream data. - """ - self.capturedtext = "" - # Create a pipe so the stream can be captured: - self.pipe_out, self.pipe_in = os.pipe() - # Save a copy of the stream: - self.streamfd = os.dup(self.origstreamfd) - # Replace the original stream with our write pipe: - os.dup2(self.pipe_in, self.origstreamfd) - if self.threaded: - # Start thread that will read the stream: - self.workerThread = threading.Thread(target=self.readOutput) - self.workerThread.start() - # Make sure that the thread is running and os.read() has executed: - time.sleep(0.01) - - def stop(self): - """ - Stop capturing the stream data and save the text in `capturedtext`. - """ - # Print the escape character to make the readOutput method stop: - self.origstream.write(self.escape_char) - # Flush the stream to make sure all our data goes in before - # the escape character: - self.origstream.flush() - if self.threaded: - # wait until the thread finishes so we are sure that - # we have until the last character: - self.workerThread.join() - else: - self.readOutput() - # Close the pipe: - os.close(self.pipe_in) - os.close(self.pipe_out) - # Restore the original stream: - os.dup2(self.streamfd, self.origstreamfd) - # Close the duplicate stream: - os.close(self.streamfd) - - def readOutput(self): - """ - Read the stream data (one byte at a time) - and save the text in `capturedtext`. - """ - while True: - char = os.read(self.pipe_out,1).decode(self.origstream.encoding, errors='replace') - if not char or self.escape_char in char: - break - self.capturedtext += char diff --git a/client/experimental_lib/example_py/test_grab.py b/client/experimental_lib/example_py/test_grab.py index f80b190ee..f13773afe 100755 --- a/client/experimental_lib/example_py/test_grab.py +++ b/client/experimental_lib/example_py/test_grab.py @@ -8,6 +8,6 @@ p=pm3.pm3("/dev/ttyACM0") print("Device:", p.name) with out: p.console("hw status") -for line in out.capturedtext.split('\n'): +for line in out.captured_output.split('\n'): if "Unique ID" in line: print(line) diff --git a/client/experimental_client_with_swig/output_grabber.py b/client/pyscripts/output_grabber.py old mode 100755 new mode 100644 similarity index 90% rename from client/experimental_client_with_swig/output_grabber.py rename to client/pyscripts/output_grabber.py index ca9828149..3ea17cf3f --- a/client/experimental_client_with_swig/output_grabber.py +++ b/client/pyscripts/output_grabber.py @@ -16,7 +16,7 @@ class OutputGrabber(object): if self.origstream is None: self.origstream = sys.stdout self.origstreamfd = self.origstream.fileno() - self.capturedtext = "" + self.captured_output = "" def __enter__(self): self.start() @@ -29,7 +29,7 @@ class OutputGrabber(object): """ Start capturing the stream data. """ - self.capturedtext = "" + self.captured_output = "" # Create a pipe so the stream can be captured: self.pipe_out, self.pipe_in = os.pipe() # Save a copy of the stream: @@ -45,7 +45,7 @@ class OutputGrabber(object): def stop(self): """ - Stop capturing the stream data and save the text in `capturedtext`. + Stop capturing the stream data and save the text in `captured_output`. """ # Print the escape character to make the readOutput method stop: self.origstream.write(self.escape_char) @@ -69,10 +69,13 @@ class OutputGrabber(object): def readOutput(self): """ Read the stream data (one byte at a time) - and save the text in `capturedtext`. + and save the text in `captured_output`. """ while True: char = os.read(self.pipe_out,1).decode(self.origstream.encoding, errors='replace') if not char or self.escape_char in char: break - self.capturedtext += char + self.captured_output += char + +if __name__ == "__main__": + print("This is a library, don't use it as a script") diff --git a/client/pyscripts/pm3.py b/client/pyscripts/pm3.py index 521d59426..7f5a91445 100644 --- a/client/pyscripts/pm3.py +++ b/client/pyscripts/pm3.py @@ -76,5 +76,5 @@ class pm3(object): # Register pm3 in _pm3: _pm3.pm3_swigregister(pm3) - - +if __name__ == "__main__": + print("This is a library, don't use it as a script")