Files
SIA/sia/entry/write_entry.py

60 lines
1.6 KiB
Python

import xml.etree.ElementTree as ET
from . import Entry
from ..io_buffer import IOBuffer
class WriteEntry(Entry):
"""
Entry type for writing content to standard output.
"""
def __init__(
self,
id: str,
content: str,
io_buffer: IOBuffer,
):
"""
Initialize a new write entry.
Args:
id: Unique identifier for this entry
content: Text to write to stdout
io_buffer: Buffer to use for IO operations
"""
super().__init__(id)
self.content = content
self.written: bool = False
self._io_buffer = io_buffer
def update(self) -> None:
"""
Write the content to stdout if not already written.
Uses the provided IO buffer for the actual write operation.
"""
if not self.written:
self._io_buffer.write(self.content)
self.written = True
self.notify_change()
def generate_context(self) -> ET.Element:
"""
Generate an XML Element representing this write entry.
Returns:
ET.Element: XML element containing the entry's data
"""
element = ET.Element("write_stdout", {"id": self.id})
element.text = self.content
return element
def serialize(self):
"""
Collect all public attributes of the entry into a dictionary.
"""
return {
"type": "write",
"id": self._id,
"content": self.content,
}