Files
SIA/sia/web_io_buffer.py
2024-11-01 16:27:56 +01:00

68 lines
2.0 KiB
Python

from .io_buffer import IOBuffer
class WebIOBuffer(IOBuffer):
"""
IOBuffer implementation for web interface communication.
This class manages input/output through memory buffers instead of system IO,
allowing for asynchronous communication through a web interface. It maintains
separate buffers for stdin and stdout to simulate bidirectional communication.
"""
def __init__(self):
"""Initialize empty input and output buffers."""
self._stdin_buffer = ""
self._stdout_buffer = ""
def read(self) -> str:
"""
Read and clear all available input from the stdin buffer.
Returns:
str: Content from the stdin buffer, or empty string if buffer is empty
"""
content = self._stdin_buffer
self._stdin_buffer = ""
return content
def write(self, content: str) -> None:
"""
Append content to the stdout buffer.
Args:
content: String content to append to the buffer
"""
if content:
self._stdout_buffer += content
def buffer_length(self) -> int:
"""
Get the current length of the stdin buffer.
Returns:
int: Number of characters in the stdin buffer
"""
return len(self._stdin_buffer)
def append_stdin(self, content: str) -> None:
"""
Append content to the stdin buffer.
Args:
content: String content to append to the stdin buffer
"""
if content:
self._stdin_buffer += content
def get_stdout(self) -> str:
"""
Get the current content of the stdout buffer.
Returns:
str: Current content of the stdout buffer
"""
return self._stdout_buffer
def clear_stdout(self) -> None:
"""Clear the stdout buffer."""
self._stdout_buffer = ""