Approve context

This commit is contained in:
2024-11-02 19:17:27 +01:00
parent 96d14fc46d
commit 2a15708145
9 changed files with 296 additions and 149 deletions

View File

@@ -1,38 +1,38 @@
from threading import Lock
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.
Thread-safe WebIOBuffer that maintains the synchronous IOBuffer interface.
Uses threading primitives instead of asyncio for synchronization.
"""
def __init__(self):
"""Initialize empty input and output buffers."""
self._stdin_buffer = ""
self._stdout_buffer = ""
self._lock = Lock()
def append_stdin(self, content: str) -> None:
"""Thread-safe append to stdin buffer."""
if not content:
return
with self._lock:
self._stdin_buffer += content
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
"""Thread-safe read from stdin buffer."""
with self._lock:
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:
"""Thread-safe write to stdout buffer."""
if not content:
return
with self._lock:
self._stdout_buffer += content
def buffer_length(self) -> int:
@@ -55,14 +55,16 @@ class WebIOBuffer(IOBuffer):
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
"""Thread-safe get stdout content."""
with self._lock:
return self._stdout_buffer
def clear_stdout(self) -> None:
"""Clear the stdout buffer."""
self._stdout_buffer = ""
"""Thread-safe clear stdout buffer."""
with self._lock:
self._stdout_buffer = ""
def buffer_length(self) -> int:
"""Thread-safe get stdin buffer length."""
with self._lock:
return len(self._stdin_buffer)