Files
SIA/sia/write_entry.py

54 lines
1.6 KiB
Python

from datetime import datetime
import xml.etree.ElementTree as ET
from .entry import Entry
from .util import escape_text_for_xml
class WriteEntry(Entry):
"""
Entry type for writing content to standard output.
Attributes:
content: Text content to write to stdout
io_buffer: Buffer to use for IO operations
_written: Whether the content has been written
"""
def __init__(self, content: str, io_buffer, id: str, timestamp: datetime):
"""
Initialize a new write entry.
Args:
content: Text to write to stdout
io_buffer: Buffer to use for IO operations
id: Unique identifier for this entry
timestamp: Creation timestamp for this entry
"""
super().__init__(id, timestamp)
self.content = content
self.io_buffer = io_buffer
self._written = False
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
def generate_context(self) -> ET.Element:
"""
Generate an XML Element representing this write entry.
Returns:
ET.Element: XML element containing the entry's data
"""
# Create root element
element = ET.Element("write_stdout", {"id": self.id})
# Add content as CDATA or escaped text
element.text = escape_text_for_xml(self.content)
return element