Commit 348e90a8 authored by Matias Guijarro's avatar Matias Guijarro
Browse files

fix issue #2849: finished work with the prompt toolkit output wrapper, make elog_add working again

parent 1194bb75
......@@ -219,9 +219,8 @@ class Info:
class WrappedStdout:
def __init__(self, ptpython_output, current_output):
self._ptpython_output = ptpython_output
self._current_output = current_output
def __init__(self, output_buffer):
self._buffer = output_buffer
self._orig_stdout = sys.stdout
# context manager
......@@ -231,8 +230,6 @@ class WrappedStdout:
return self
def __exit__(self, *args, **kwargs):
self._ptpython_output._output.append("".join(self._current_output))
self._current_output.clear()
sys.stdout = self._orig_stdout
# delegated members
......@@ -258,18 +255,16 @@ class WrappedStdout:
def write(self, data):
# wait for stdout to be ready to receive output
if True: # if gevent.select.select([],[self.fileno()], []):
self._current_output.append(data)
self._buffer.append(data)
self._orig_stdout.write(data)
# in the next class, inheritance from DummyOutput is just needed
# to make ptpython happy (as there are some asserts checking instance type)
class PromptToolkitOutputWrapper(DummyOutput):
SIZE = 20
def __init__(self, output):
self.__wrapped_output = output
self._current_output = []
self._output_buffer = []
self._output = deque(maxlen=20)
def __getattr__(self, attr):
......@@ -279,17 +274,21 @@ class PromptToolkitOutputWrapper(DummyOutput):
@property
def capture_stdout(self):
return WrappedStdout(self, self._current_output)
return WrappedStdout(self._output_buffer)
def acknowledge_output(self):
self._output.append("".join(self._output_buffer))
self._output_buffer.clear()
def __getitem__(self, item_no):
if item_no >= 0:
if item_no > 0:
# item_no starts at 1 to match "Out" number in ptpython
item_no -= 1
# if item_no is specified negative => no decrement of number of course
# if item_no is specified negative or 0 => no decrement of number
return self._output[item_no]
def write(self, data):
self._current_output.append(data)
self._output_buffer.append(data)
self.__wrapped_output.write(data)
def fileno(self):
......@@ -358,6 +357,13 @@ class BlissRepl(NoThreadPythonRepl, metaclass=Singleton):
##
# NB: next methods are overloaded
##
async def eval_async(self, text):
with self.app.output.capture_stdout:
result = await super().eval_async(text)
if result is None:
self.app.output.acknowledge_output()
return result
def show_result(self, result):
try:
if hasattr(result, "__info__"):
......@@ -369,6 +375,8 @@ class BlissRepl(NoThreadPythonRepl, metaclass=Singleton):
sys.excepthook(*sys.exc_info())
else:
return super().show_result(result)
finally:
self.app.output.acknowledge_output()
def _handle_keyboard_interrupt(self, e: KeyboardInterrupt) -> None:
sys.excepthook(*sys.exc_info())
......
......@@ -1505,11 +1505,11 @@ def elog_add(index=-1):
"""
Send to the logbook given cell output and the print that was
performed during the elaboration.
Only a fixed size of output are kept in memory (normally last 100).
Only a fixed size of output are kept in memory (normally last 20).
Args:
index (int): Index of the cell to be sent to logbook, can
be positive reflectiong the prompt index
be positive reflecting the prompt index
or negative.
Default is -1 (previous cell)
......@@ -1522,11 +1522,16 @@ def elog_add(index=-1):
unit = None
mode = MEAN (1)
BLISS [3]: elog_add() # sends last otput from diode
BLISS [3]: elog_add() # sends last output from diode
"""
from bliss.shell.cli.repl import CaptureOutput
from bliss.shell.cli.repl import BlissRepl
logtools.elogbook.comment(CaptureOutput()[index])
try:
comment = BlissRepl().app.output[index]
except IndexError:
logtools.user_warning(f"Cell output [{index}] does not exist")
else:
logtools.elogbook.comment(comment)
@logtools.elogbook.disable_command_logging
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment