Files
SIA/sia/entry/read_entry.py

68 lines
1.7 KiB
Python

import xml.etree.ElementTree as ET
from . import Entry
from ..io_buffer import IOBuffer
class ReadEntry(Entry):
"""
Entry type for reading content from standard input.
"""
def __init__(
self,
id: str,
io_buffer: IOBuffer,
):
"""
Initialize a new read entry.
Args:
id: Unique identifier for this entry
io_buffer: Buffer to use for IO operations
"""
super().__init__(id)
self.content: str = ""
self.read = False
self._io_buffer = io_buffer
def update(self) -> None:
"""
Read from stdin if not already read.
Uses the provided IO buffer for the actual read operation.
"""
if not self.read:
self.content = self._io_buffer.read()
self.read = True
self.notify_change()
def reset(self) -> None:
"""
Reset the entry state to its initial state.
"""
self.read = False
self.content = ""
self.notify_change()
def generate_context(self) -> ET.Element:
"""
Generate an XML Element representing this read entry.
Returns:
ET.Element: XML element containing the entry's data
"""
element = ET.Element("read_stdin", {"id": self.id})
if self.read:
element.text = self.content
return element
def serialize(self):
"""
Collect all public attributes of the entry into a dictionary.
"""
return {
"type": "read_stdin",
"id": self.id,
"content": self.content,
"read": self.read,
}